Skip to content
This repository was archived by the owner on Oct 5, 2018. It is now read-only.

Commit a1d23b8

Browse files
committed
Rewrite configuration
1 parent cd21397 commit a1d23b8

File tree

23 files changed

+259
-628
lines changed

23 files changed

+259
-628
lines changed

bep/src/main/kotlin/net/syncthing/java/bep/BlockExchangeConnectionHandler.kt

+14-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import net.syncthing.java.client.protocol.rp.RelayClient
2121
import net.syncthing.java.core.beans.DeviceAddress
2222
import net.syncthing.java.core.beans.DeviceId
2323
import net.syncthing.java.core.beans.FolderInfo
24-
import net.syncthing.java.core.configuration.ConfigurationService
24+
import net.syncthing.java.core.configuration.Configuration
2525
import net.syncthing.java.core.security.KeystoreHandler
2626
import net.syncthing.java.core.utils.NetworkUtils
2727
import net.syncthing.java.core.utils.submitLogging
@@ -43,7 +43,7 @@ import java.util.concurrent.Future
4343
import java.util.concurrent.TimeUnit
4444
import javax.net.ssl.SSLSocket
4545

46-
class BlockExchangeConnectionHandler(private val configuration: ConfigurationService, val address: DeviceAddress,
46+
class BlockExchangeConnectionHandler(private val configuration: Configuration, val address: DeviceAddress,
4747
private val indexHandler: IndexHandler,
4848
private val onDeviceAddressActiveListener: (DeviceId) -> Unit,
4949
private val onNewFolderSharedListener: (FolderInfo) -> Unit,
@@ -64,10 +64,8 @@ class BlockExchangeConnectionHandler(private val configuration: ConfigurationSer
6464
private val blockPuller = BlockPuller(configuration, this)
6565
private val blockPusher = BlockPusher(configuration, this, indexHandler)
6666
private val onRequestMessageReceivedListeners = mutableSetOf<(Request) -> Unit>()
67-
var isClosed = false
68-
private set
69-
var isConnected = false
70-
private set
67+
private var isClosed = false
68+
private var isConnected = false
7169

7270
fun deviceId(): DeviceId = address.deviceId()
7371

@@ -90,7 +88,7 @@ class BlockExchangeConnectionHandler(private val configuration: ConfigurationSer
9088
assert(socket == null && !isConnected, {"already connected!"})
9189
logger.info("connecting to {}", address.address)
9290

93-
val keystoreHandler = KeystoreHandler.Loader().loadAndStore(configuration)
91+
val keystoreHandler = KeystoreHandler.Loader().loadKeystore(configuration)
9492

9593
socket = when (address.getType()) {
9694
DeviceAddress.AddressType.TCP -> {
@@ -111,9 +109,9 @@ class BlockExchangeConnectionHandler(private val configuration: ConfigurationSer
111109
outputStream = DataOutputStream(socket!!.getOutputStream())
112110

113111
sendHelloMessage(BlockExchangeProtos.Hello.newBuilder()
114-
.setClientName(configuration.getClientName())
112+
.setClientName(configuration.clientName)
115113
.setClientVersion(configuration.clientVersion)
116-
.setDeviceName(configuration.deviceName)
114+
.setDeviceName(configuration.localDeviceName)
117115
.build().toByteArray())
118116
markActivityOnSocket()
119117

@@ -127,12 +125,12 @@ class BlockExchangeConnectionHandler(private val configuration: ConfigurationSer
127125

128126
run {
129127
val clusterConfigBuilder = ClusterConfig.newBuilder()
130-
for (folder in configuration.getFolderNames()) {
128+
for (folder in configuration.folderNames) {
131129
val folderBuilder = Folder.newBuilder().setId(folder)
132130
run {
133131
//our device
134132
val deviceBuilder = Device.newBuilder()
135-
.setId(ByteString.copyFrom(configuration.deviceId!!.toHashData()))
133+
.setId(ByteString.copyFrom(configuration.localDeviceId.toHashData()))
136134
.setIndexId(indexHandler.sequencer().indexId())
137135
.setMaxSequence(indexHandler.sequencer().currentSequence())
138136
folderBuilder.addDevices(deviceBuilder)
@@ -173,7 +171,7 @@ class BlockExchangeConnectionHandler(private val configuration: ConfigurationSer
173171
throw IOException("unable to retrieve cluster config from peer!")
174172
}
175173
}
176-
for (folder in configuration.getFolderNames()) {
174+
for (folder in configuration.folderNames) {
177175
if (hasFolder(folder)) {
178176
sendIndexMessage(folder)
179177
}
@@ -407,16 +405,16 @@ class BlockExchangeConnectionHandler(private val configuration: ConfigurationSer
407405
DeviceId.fromHashData(input.id!!.toByteArray())
408406
}
409407
val otherDevice = devicesById[address.deviceId()]
410-
val ourDevice = devicesById[configuration.deviceId]
408+
val ourDevice = devicesById[configuration.localDeviceId]
411409
if (otherDevice != null) {
412410
folderInfo.isAnnounced = true
413411
}
414412
if (ourDevice != null) {
415413
folderInfo.isShared = true
416414
logger.info("folder shared from device = {} folder = {}", address.deviceId, folderInfo)
417-
if (!configuration.getFolderNames().contains(folderInfo.folder)) {
415+
if (!configuration.folderNames.contains(folderInfo.folder)) {
418416
val fi = FolderInfo(folderInfo.folder, folderInfo.label)
419-
configuration.Editor().addFolders(setOf(fi))
417+
configuration.folders = configuration.folders + fi
420418
onNewFolderSharedListener(fi)
421419
logger.info("new folder shared = {}", folderInfo)
422420
}
@@ -425,7 +423,7 @@ class BlockExchangeConnectionHandler(private val configuration: ConfigurationSer
425423
}
426424
clusterConfigInfo!!.putFolderInfo(folderInfo)
427425
}
428-
configuration.Editor().persistLater()
426+
configuration.persistLater()
429427
indexHandler.handleClusterConfigMessageProcessedEvent(clusterConfig)
430428
onDeviceAddressActive()
431429
synchronized(clusterConfigWaitingLock) {

bep/src/main/kotlin/net/syncthing/java/bep/BlockPuller.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import net.syncthing.java.bep.BlockExchangeProtos.ErrorCode
1818
import net.syncthing.java.bep.BlockExchangeProtos.Request
1919
import net.syncthing.java.core.beans.FileBlocks
2020
import net.syncthing.java.core.cache.BlockCache
21-
import net.syncthing.java.core.configuration.ConfigurationService
21+
import net.syncthing.java.core.configuration.Configuration
2222
import net.syncthing.java.core.utils.NetworkUtils
2323
import org.apache.commons.io.FileUtils
2424
import org.bouncycastle.util.encoders.Hex
@@ -29,7 +29,7 @@ import java.util.*
2929
import java.util.concurrent.ConcurrentHashMap
3030
import java.util.concurrent.atomic.AtomicReference
3131

32-
class BlockPuller internal constructor(configuration: ConfigurationService,
32+
class BlockPuller internal constructor(configuration: Configuration,
3333
private val connectionHandler: BlockExchangeConnectionHandler) {
3434

3535
private val blockCache = BlockCache.getBlockCache(configuration)

bep/src/main/kotlin/net/syncthing/java/bep/BlockPusher.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import net.syncthing.java.bep.BlockExchangeProtos.Vector
1919
import net.syncthing.java.core.beans.FileInfo
2020
import net.syncthing.java.core.beans.FileInfo.Version
2121
import net.syncthing.java.core.beans.IndexInfo
22-
import net.syncthing.java.core.configuration.ConfigurationService
22+
import net.syncthing.java.core.configuration.Configuration
2323
import net.syncthing.java.core.utils.BlockUtils
2424
import net.syncthing.java.core.utils.NetworkUtils
2525
import net.syncthing.java.core.utils.submitLogging
@@ -39,7 +39,7 @@ import java.util.concurrent.Future
3939
import java.util.concurrent.atomic.AtomicBoolean
4040
import java.util.concurrent.atomic.AtomicReference
4141

42-
class BlockPusher internal constructor(private val configuration: ConfigurationService,
42+
class BlockPusher internal constructor(private val configuration: Configuration,
4343
private val connectionHandler: BlockExchangeConnectionHandler,
4444
private val indexHandler: IndexHandler) {
4545

@@ -62,7 +62,7 @@ class BlockPusher internal constructor(private val configuration: ConfigurationS
6262

6363
fun pushFile(inputStream: InputStream, fileInfo: FileInfo?, folder: String, path: String): FileUploadObserver {
6464
// TODO: there is no need for the file copy, as long as we know the file size
65-
val tempFile = File(configuration.temp, UUID.randomUUID().toString())
65+
val tempFile = File(configuration.cacheFolder, UUID.randomUUID().toString() + ".tmp")
6666
tempFile.deleteOnExit()
6767
tempFile.outputStream().use { fileOut ->
6868
inputStream.copyTo(fileOut)
@@ -166,7 +166,7 @@ class BlockPusher internal constructor(private val configuration: ConfigurationS
166166
val nextSequence = indexHandler.sequencer().nextSequence()
167167
val list = oldVersions ?: emptyList()
168168
logger.debug("version list = {}", list)
169-
val id = ByteBuffer.wrap(configuration.deviceId!!.toHashData()).long
169+
val id = ByteBuffer.wrap(configuration.localDeviceId.toHashData()).long
170170
val version = Counter.newBuilder()
171171
.setId(id)
172172
.setValue(nextSequence)
@@ -208,7 +208,7 @@ class BlockPusher internal constructor(private val configuration: ConfigurationS
208208
}
209209
}
210210

211-
inner class IndexEditObserver(private val future: Future<*>, val indexUpdate: IndexUpdate) : Closeable {
211+
inner class IndexEditObserver(private val future: Future<*>, private val indexUpdate: IndexUpdate) : Closeable {
212212

213213
//throw exception if job has errors
214214
@Throws(InterruptedException::class, ExecutionException::class)

bep/src/main/kotlin/net/syncthing/java/bep/IndexHandler.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ package net.syncthing.java.bep
1515

1616
import net.syncthing.java.core.beans.*
1717
import net.syncthing.java.core.beans.FileInfo.Version
18-
import net.syncthing.java.core.configuration.ConfigurationService
18+
import net.syncthing.java.core.configuration.Configuration
1919
import net.syncthing.java.core.interfaces.IndexRepository
2020
import net.syncthing.java.core.interfaces.Sequencer
2121
import net.syncthing.java.core.interfaces.TempRepository
@@ -31,7 +31,7 @@ import java.io.IOException
3131
import java.util.*
3232
import java.util.concurrent.Executors
3333

34-
class IndexHandler(private val configuration: ConfigurationService, val indexRepository: IndexRepository,
34+
class IndexHandler(private val configuration: Configuration, val indexRepository: IndexRepository,
3535
private val tempRepository: TempRepository) : Closeable {
3636
private val logger = LoggerFactory.getLogger(javaClass)
3737
private val folderInfoByFolder = mutableMapOf<String, FolderInfo>()
@@ -79,7 +79,7 @@ class IndexHandler(private val configuration: ConfigurationService, val indexRep
7979

8080
private fun loadFolderInfoFromConfig() {
8181
synchronized(writeAccessLock) {
82-
for (folderInfo in configuration.getFolders()) {
82+
for (folderInfo in configuration.folders) {
8383
folderInfoByFolder.put(folderInfo.folder, folderInfo) //TODO reference 'folder info' repository
8484
}
8585
}

client/src/main/kotlin/net/syncthing/java/client/Main.kt

+9-19
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package net.syncthing.java.client
1616
import net.syncthing.java.core.beans.DeviceId
1717
import net.syncthing.java.core.beans.DeviceInfo
1818
import net.syncthing.java.core.beans.FileInfo
19-
import net.syncthing.java.core.configuration.ConfigurationService
19+
import net.syncthing.java.core.configuration.Configuration
2020
import net.syncthing.java.core.security.KeystoreHandler
2121
import org.apache.commons.cli.*
2222
import org.apache.commons.io.FileUtils
@@ -39,19 +39,12 @@ class Main(private val commandLine: CommandLine) {
3939
formatter.printHelp("s-client", options)
4040
return
4141
}
42-
val configFile = if (cmd.hasOption("C")) File(cmd.getOptionValue("C"))
43-
else File(System.getProperty("user.home"), ".s-client.properties")
42+
val configuration = if (cmd.hasOption("C")) Configuration(File(cmd.getOptionValue("C")))
43+
else Configuration()
4444

45-
ConfigurationService.Loader().loadFrom(configFile).use { configuration ->
46-
SyncthingClient(configuration).use { syncthingClient ->
47-
System.out.println("using config file = $configFile")
48-
FileUtils.cleanDirectory(configuration.temp)
49-
KeystoreHandler.Loader().loadAndStore(configuration)
50-
System.out.println("configuration =\n${configuration.Writer().dumpToString()}")
51-
System.out.println(configuration.getStorageInfo().dumpAvailableSpace())
52-
val main = Main(cmd)
53-
cmd.options.forEach { main.handleOption(it, configuration, syncthingClient) }
54-
}
45+
SyncthingClient(configuration).use { syncthingClient ->
46+
val main = Main(cmd)
47+
cmd.options.forEach { main.handleOption(it, configuration, syncthingClient) }
5548
}
5649
}
5750

@@ -78,7 +71,7 @@ class Main(private val commandLine: CommandLine) {
7871

7972
private val logger = LoggerFactory.getLogger(Main::class.java)
8073

81-
private fun handleOption(option: Option, configuration: ConfigurationService, syncthingClient: SyncthingClient) {
74+
private fun handleOption(option: Option, configuration: Configuration, syncthingClient: SyncthingClient) {
8275
when (option.opt) {
8376
"S" -> {
8477
val peers = option.value
@@ -87,11 +80,8 @@ class Main(private val commandLine: CommandLine) {
8780
.map { DeviceId(it.trim()) }
8881
.toList()
8982
System.out.println("set peers = $peers")
90-
configuration.Editor().setPeers(emptyList())
91-
for (peer in peers) {
92-
configuration.Editor().addPeers(DeviceInfo(peer, null))
93-
}
94-
configuration.Editor().persistNow()
83+
configuration.peers = peers.map { DeviceInfo(it, null) }.toSet()
84+
configuration.persistNow()
9585
}
9686
"p" -> {
9787
val folderAndPath = option.value

client/src/main/kotlin/net/syncthing/java/client/SyncthingClient.kt

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import net.syncthing.java.core.beans.DeviceAddress
2222
import net.syncthing.java.core.beans.DeviceId
2323
import net.syncthing.java.core.beans.FileInfo
2424
import net.syncthing.java.core.cache.BlockCache
25-
import net.syncthing.java.core.configuration.ConfigurationService
25+
import net.syncthing.java.core.configuration.Configuration
2626
import net.syncthing.java.core.security.KeystoreHandler
2727
import net.syncthing.java.devices.DevicesHandler
2828
import net.syncthing.java.discovery.DiscoveryHandler
@@ -35,11 +35,11 @@ import java.util.Collections
3535
import java.util.concurrent.atomic.AtomicBoolean
3636
import kotlin.collections.ArrayList
3737

38-
class SyncthingClient(private val configuration: ConfigurationService) : Closeable {
38+
class SyncthingClient(private val configuration: Configuration) : Closeable {
3939

4040
private val logger = LoggerFactory.getLogger(javaClass)
4141
val discoveryHandler: DiscoveryHandler
42-
private val sqlRepository = SqlRepository(configuration)
42+
private val sqlRepository = SqlRepository(configuration.databaseFolder)
4343
val indexHandler: IndexHandler
4444
private val connections = Collections.synchronizedList(mutableListOf<BlockExchangeConnectionHandler>())
4545
val devicesHandler: DevicesHandler
@@ -53,7 +53,8 @@ class SyncthingClient(private val configuration: ConfigurationService) : Closeab
5353
fun clearCacheAndIndex() {
5454
logger.info("clear cache")
5555
indexHandler.clearIndex()
56-
configuration.Editor().setFolders(emptyList()).persistLater()
56+
configuration.folders = emptySet()
57+
configuration.persistLater()
5758
BlockCache.getBlockCache(configuration).clear()
5859
}
5960

@@ -139,7 +140,7 @@ class SyncthingClient(private val configuration: ConfigurationService) : Closeab
139140
logger.warn("exception while waiting for index", ex)
140141
}
141142
}, {
142-
val indexUpdateFailed = configuration.getPeerIds() - indexUpdateComplete
143+
val indexUpdateFailed = configuration.peerIds - indexUpdateComplete
143144
listener(indexUpdateComplete, indexUpdateFailed)
144145
})
145146
}

client/src/main/kotlin/net/syncthing/java/devices/DevicesHandler.kt

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import net.syncthing.java.core.beans.DeviceAddress
1717
import net.syncthing.java.core.beans.DeviceId
1818
import net.syncthing.java.core.beans.DeviceStats
1919
import net.syncthing.java.core.beans.DeviceStats.DeviceStatus
20-
import net.syncthing.java.core.configuration.ConfigurationService
20+
import net.syncthing.java.core.configuration.Configuration
2121
import net.syncthing.java.core.utils.awaitTerminationSafe
2222
import java.io.Closeable
2323
import java.util.*
2424
import java.util.concurrent.Executors
2525
import java.util.concurrent.TimeUnit
2626

27-
class DevicesHandler(private val configuration: ConfigurationService) : Closeable {
27+
class DevicesHandler(private val configuration: Configuration) : Closeable {
2828

2929
private val deviceStatsMap = Collections.synchronizedMap(mutableMapOf<DeviceId, DeviceStats>())
3030
private val scheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
@@ -72,9 +72,9 @@ class DevicesHandler(private val configuration: ConfigurationService) : Closeabl
7272
}
7373

7474
private fun loadDevicesFromConfiguration() {
75-
for (deviceInfo in configuration.getPeers()) {
75+
for (deviceInfo in configuration.peers) {
7676
if (!deviceStatsMap.containsKey(deviceInfo.deviceId)) {
77-
pushDeviceStats(DeviceStats.newBuilder().setDeviceId(deviceInfo.deviceId).setName(deviceInfo.name).build())
77+
pushDeviceStats(DeviceStats.Builder(deviceInfo.deviceId).setName(deviceInfo.name).build())
7878
}
7979
}
8080
}
@@ -84,7 +84,7 @@ class DevicesHandler(private val configuration: ConfigurationService) : Closeabl
8484
if (deviceStatsMap.containsKey(deviceId)) {
8585
return deviceStatsMap[deviceId]!!
8686
} else {
87-
val deviceStats = DeviceStats.newBuilder().setDeviceId(deviceId).build()
87+
val deviceStats = DeviceStats.Builder(deviceId).build()
8888
pushDeviceStats(deviceStats)
8989
return deviceStats
9090
}

core/src/main/kotlin/net/syncthing/java/core/beans/DeviceStats.kt

+3-23
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,22 @@ class DeviceStats private constructor(val deviceId: DeviceId, name: String, val
3434
OFFLINE, ONLINE_ACTIVE, ONLINE_INACTIVE
3535
}
3636

37-
class Builder {
37+
class Builder(private val deviceId: DeviceId) {
3838

39-
private var deviceId: DeviceId? = null
4039
private var name: String? = null
4140

4241
private var lastActive = Date(0)
4342
private var lastSeen = Date(0)
4443

4544
private var status = DeviceStatus.OFFLINE
4645

47-
internal constructor()
48-
49-
internal constructor(deviceId: DeviceId, name: String, lastActive: Date, lastSeen: Date, status: DeviceStatus) {
50-
this.deviceId = deviceId
46+
internal constructor(deviceId: DeviceId, name: String, lastActive: Date, lastSeen: Date, status: DeviceStatus) : this(deviceId) {
5147
this.name = name
5248
this.lastActive = lastActive
5349
this.lastSeen = lastSeen
5450
this.status = status
5551
}
5652

57-
fun getDeviceId(): DeviceId? {
58-
return deviceId
59-
}
60-
61-
fun setDeviceId(deviceId: DeviceId): Builder {
62-
this.deviceId = deviceId
63-
return this
64-
}
65-
6653
fun getName(): String? {
6754
return name
6855
}
@@ -100,15 +87,8 @@ class DeviceStats private constructor(val deviceId: DeviceId, name: String, val
10087
}
10188

10289
fun build(): DeviceStats {
103-
return DeviceStats(deviceId!!, name!!, lastActive, lastSeen, status)
90+
return DeviceStats(deviceId, name ?: deviceId.deviceId.take(7), lastActive, lastSeen, status)
10491
}
10592

10693
}
107-
108-
companion object {
109-
110-
fun newBuilder(): Builder {
111-
return Builder()
112-
}
113-
}
11494
}

0 commit comments

Comments
 (0)