Updated how to access storage (via a property now)

This commit is contained in:
Robinson 2021-04-29 10:01:07 +02:00
parent f5ab178948
commit 2828758fee
3 changed files with 37 additions and 42 deletions

View File

@ -96,8 +96,12 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A
@Volatile @Volatile
private var shutdownWaiter: SuspendWaiter = SuspendWaiter() private var shutdownWaiter: SuspendWaiter = SuspendWaiter()
// we only want one instance of these created. These will be called appropriately /**
val settingsStore: SettingsStore * Returns the storage used by this endpoint. This is the backing data structure for key/value pairs, and can be a database, file, etc
*
* Only one instance of these is created for an endpoint.
*/
val storage: SettingsStore
internal val rmiGlobalSupport = RmiManagerGlobal<CONNECTION>(logger, actionDispatch, config.serialization) internal val rmiGlobalSupport = RmiManagerGlobal<CONNECTION>(logger, actionDispatch, config.serialization)
@ -122,9 +126,9 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A
handshakeKryo = serialization.initHandshakeKryo() handshakeKryo = serialization.initHandshakeKryo()
// we have to be able to specify the property store // we have to be able to specify the property store
settingsStore = createSettingsStore(logger) storage = config.settingsStore.create(logger)
crypto = CryptoManagement(logger, settingsStore, type, config.enableRemoteSignatureValidation) crypto = CryptoManagement(logger, storage, type, config.enableRemoteSignatureValidation)
// Only starts the media driver if we are NOT already running! // Only starts the media driver if we are NOT already running!
try { try {
@ -136,13 +140,6 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A
} }
} }
/**
* Open so users can override which/how a settings store is created
*/
open fun createSettingsStore(logger: KLogger): SettingsStore {
return config.settingsStore.create(logger)
}
internal fun initEndpointState() { internal fun initEndpointState() {
shutdown.getAndSet(false) shutdown.getAndSet(false)
shutdownWaiter = SuspendWaiter() shutdownWaiter = SuspendWaiter()
@ -188,14 +185,6 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A
connections.remove(connection) connections.remove(connection)
} }
/**
* Returns the property store used by this endpoint. The property store can store via properties,
* a database, etc, or can be a "null" property store, which does nothing
*/
fun getStorage(): SettingsStore {
return settingsStore
}
/** /**
* This method allows the connections used by the client/server to be subclassed (with custom implementations). * This method allows the connections used by the client/server to be subclassed (with custom implementations).
* *
@ -629,7 +618,7 @@ internal constructor(val type: Class<*>, internal val config: Configuration) : A
} }
// the storage is closed via this as well. // the storage is closed via this as well.
settingsStore.close() storage.close()
// Connections are closed first, because we want to make sure that no RMI messages can be received // Connections are closed first, because we want to make sure that no RMI messages can be received
// when we close the RMI support objects (in which case, weird - but harmless - errors show up) // when we close the RMI support objects (in which case, weird - but harmless - errors show up)

View File

@ -146,7 +146,7 @@ internal class ClientHandshake<CONNECTION: Connection>(private val crypto: Crypt
suspend fun handshakeHello(handshakeConnection: MediaDriverConnection, connectionTimeoutMS: Long) : ClientConnectionInfo { suspend fun handshakeHello(handshakeConnection: MediaDriverConnection, connectionTimeoutMS: Long) : ClientConnectionInfo {
failed = null failed = null
oneTimeKey = endPoint.crypto.secureRandom.nextInt() oneTimeKey = endPoint.crypto.secureRandom.nextInt()
val publicKey = endPoint.settingsStore.getPublicKey()!! val publicKey = endPoint.storage.getPublicKey()!!
// Send the one-time pad to the server. // Send the one-time pad to the server.
val publication = handshakeConnection.publication val publication = handshakeConnection.publication

View File

@ -19,12 +19,11 @@ import dorkbox.network.Client
import dorkbox.network.Server import dorkbox.network.Server
import dorkbox.network.connection.Connection import dorkbox.network.connection.Connection
import dorkbox.network.storage.SettingsStore import dorkbox.network.storage.SettingsStore
import dorkbox.network.storage.types.ChronicleMapStore import dorkbox.network.storage.StorageType
import dorkbox.network.storage.types.LmdbStore import dorkbox.network.storage.types.*
import dorkbox.network.storage.types.MemoryStore
import dorkbox.network.storage.types.PropertyStore
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import mu.KLogger import mu.KLogger
import mu.KotlinLogging
import org.junit.Assert import org.junit.Assert
import org.junit.Test import org.junit.Test
import java.io.File import java.io.File
@ -33,24 +32,31 @@ class StorageTest : BaseTest() {
@Test @Test
fun sharedStoreTest() { fun sharedStoreTest() {
// we want the server + client to have the SAME info // we want the server + client to have the SAME info
val store = MemoryStore.type().create() val store = MemoryAccess(KotlinLogging.logger("StorageType"))
val server = object : Server<Connection>(serverConfig()) { val sharedStore = object : StorageType {
override fun createSettingsStore(logger: KLogger): SettingsStore { override fun create(logger: KLogger): SettingsStore {
return store return SettingsStore(logger, store)
} }
} }
val client = object: Client<Connection>(clientConfig()) {
override fun createSettingsStore(logger: KLogger): SettingsStore {
return store
}
}
val serverConfig = serverConfig {
settingsStore = sharedStore
}
val server = Server<Connection>(serverConfig)
val config = clientConfig {
settingsStore = sharedStore
}
val client = Client<Connection>(config)
server.bind() server.bind()
runBlocking { runBlocking {
client.connect("localhost") client.connect("localhost")
Assert.assertFalse(server.storage.getSalt().contentEquals(client.storage.getSalt()))
server.close() server.close()
} }
} }
@ -60,8 +66,8 @@ class StorageTest : BaseTest() {
fun memoryTest() { fun memoryTest() {
val salt1 = MemoryStore.type().create().use { it.getSalt() } val salt1 = MemoryStore.type().create().use { it.getSalt() }
val salt2 = Server<Connection>(serverConfig().apply { settingsStore = MemoryStore.type() }).use { it.settingsStore.getSalt() } val salt2 = Server<Connection>(serverConfig().apply { settingsStore = MemoryStore.type() }).use { it.storage.getSalt() }
val salt3 = Server<Connection>(serverConfig().apply { settingsStore = MemoryStore.type() }).use { it.settingsStore.getSalt() } val salt3 = Server<Connection>(serverConfig().apply { settingsStore = MemoryStore.type() }).use { it.storage.getSalt() }
Assert.assertFalse(salt1.contentEquals(salt2)) Assert.assertFalse(salt1.contentEquals(salt2))
Assert.assertFalse(salt1.contentEquals(salt3)) Assert.assertFalse(salt1.contentEquals(salt3))
@ -80,8 +86,8 @@ class StorageTest : BaseTest() {
file.delete() file.delete()
fileLock.delete() fileLock.delete()
val salt3 = Server<Connection>(serverConfig().apply { settingsStore = LmdbStore.type(file) }).use { it.settingsStore.getSalt() } val salt3 = Server<Connection>(serverConfig().apply { settingsStore = LmdbStore.type(file) }).use { it.storage.getSalt() }
val salt4 = Server<Connection>(serverConfig().apply { settingsStore = LmdbStore.type(file) }).use { it.settingsStore.getSalt() } val salt4 = Server<Connection>(serverConfig().apply { settingsStore = LmdbStore.type(file) }).use { it.storage.getSalt() }
Assert.assertArrayEquals(salt3, salt4) Assert.assertArrayEquals(salt3, salt4)
Assert.assertFalse(salt1.contentEquals(salt4)) Assert.assertFalse(salt1.contentEquals(salt4))
@ -99,8 +105,8 @@ class StorageTest : BaseTest() {
Assert.assertArrayEquals(salt1, salt2) Assert.assertArrayEquals(salt1, salt2)
file.delete() file.delete()
val salt3 = Server<Connection>(serverConfig().apply { settingsStore = PropertyStore.type(file) }).use { it.settingsStore.getSalt() } val salt3 = Server<Connection>(serverConfig().apply { settingsStore = PropertyStore.type(file) }).use { it.storage.getSalt() }
val salt4 = Server<Connection>(serverConfig().apply { settingsStore = PropertyStore.type(file) }).use { it.settingsStore.getSalt() } val salt4 = Server<Connection>(serverConfig().apply { settingsStore = PropertyStore.type(file) }).use { it.storage.getSalt() }
Assert.assertArrayEquals(salt3, salt4) Assert.assertArrayEquals(salt3, salt4)
Assert.assertFalse(salt1.contentEquals(salt4)) Assert.assertFalse(salt1.contentEquals(salt4))
@ -117,8 +123,8 @@ class StorageTest : BaseTest() {
Assert.assertArrayEquals(salt1, salt2) Assert.assertArrayEquals(salt1, salt2)
file.delete() file.delete()
val salt3 = Server<Connection>(serverConfig().apply { settingsStore = ChronicleMapStore.type(file) }).use { it.settingsStore.getSalt() } val salt3 = Server<Connection>(serverConfig().apply { settingsStore = ChronicleMapStore.type(file) }).use { it.storage.getSalt() }
val salt4 = Server<Connection>(serverConfig().apply { settingsStore = ChronicleMapStore.type(file) }).use { it.settingsStore.getSalt() } val salt4 = Server<Connection>(serverConfig().apply { settingsStore = ChronicleMapStore.type(file) }).use { it.storage.getSalt() }
Assert.assertArrayEquals(salt3, salt4) Assert.assertArrayEquals(salt3, salt4)
Assert.assertFalse(salt1.contentEquals(salt4)) Assert.assertFalse(salt1.contentEquals(salt4))