diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/DataSerializer.kt b/app/src/main/java/io/github/chronosx88/influence/helpers/DataSerializer.kt index 67a3bd4..ef5b4b4 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/DataSerializer.kt +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/DataSerializer.kt @@ -19,7 +19,7 @@ class DataSerializer(private val signatureFactory: SignatureFactory) : EntryBind if (databaseEntry.data == null) { return null } - val dataInput = DataInputStream(ByteArrayInputStream(databaseEntry.data)) + val dataInput = ByteArrayInputStream(databaseEntry.data) var buf = Unpooled.buffer() var data: Data? = null while (data == null) { @@ -41,7 +41,7 @@ class DataSerializer(private val signatureFactory: SignatureFactory) : EntryBind } if (data.isSigned) { me = ByteArray(signatureFactory.signatureSize()) - dataInput.readFully(me) + dataInput.read(me) buf = Unpooled.wrappedBuffer(me) } retVal = data.decodeDone(buf, signatureFactory); @@ -52,8 +52,7 @@ class DataSerializer(private val signatureFactory: SignatureFactory) : EntryBind } override fun objectToEntry(data: Data, databaseEntry: DatabaseEntry) { - val baos = ByteArrayOutputStream() - val out = DataOutputStream(baos) + val out = ByteArrayOutputStream() val acb = Unpooled.buffer() // store data to disk // header first @@ -73,7 +72,7 @@ class DataSerializer(private val signatureFactory: SignatureFactory) : EntryBind throw IOException(e) } out.flush() - databaseEntry.data = baos.toByteArray() + databaseEntry.data = out.toByteArray() out.close() } diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java b/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java index 6ae57fe..0d914e1 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/P2PUtils.java @@ -45,8 +45,10 @@ public class P2PUtils { .all() .start() .awaitUninterruptibly(); - if(futureGet != null && !futureGet.isEmpty()) { - return futureGet.dataMap(); + if(futureGet != null) { + if(!futureGet.isEmpty()) { + return futureGet.dataMap(); + } } return null; } diff --git a/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt b/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt index 525dc62..fba5ea8 100644 --- a/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt +++ b/app/src/main/java/io/github/chronosx88/influence/helpers/StorageBerkeleyDB.kt @@ -51,7 +51,7 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat envConfig.allowCreate = true dbEnvironment = Environment(path, envConfig) - val configMap : HashMap = HashMap() + val configMap : HashMap = HashMap() val compareNumber640 = CompareNumber640() val compareLong = CompareLong() @@ -98,7 +98,9 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat } override fun put(key: Number640?, value: Data?): Data? { - return dataMap.put(key, value) + val oldData = dataMap.put(key, value) + dbEnvironment.sync() + return oldData } override fun get(key: Number640?): Data? { @@ -106,7 +108,9 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat } override fun remove(key: Number640?, returnData: Boolean): Data? { - return dataMap.remove(key) + val oldData = dataMap.remove(key) + dbEnvironment.sync() + return oldData } override fun remove(from: Number640?, to: Number640?): NavigableMap { @@ -116,6 +120,7 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat retVal[entry.key] = entry.value } tmp.clear() + dbEnvironment.sync() return retVal } @@ -126,6 +131,7 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat return } removeRevTimeout(key, oldExpiration) + dbEnvironment.sync() } private fun putIfAbsent2(expiration: Long, key: Number640) { @@ -136,10 +142,11 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat } (timeouts as MutableSet).add(key) timeoutMapRev[expiration] = timeouts + dbEnvironment.sync() } private fun removeRevTimeout(key: Number640, expiration: Long?) { - val tmp = timeoutMapRev[expiration] as MutableSet + val tmp = timeoutMapRev[expiration] as MutableSet? if (tmp != null) { tmp.remove(key) if (tmp.isEmpty()) { @@ -148,6 +155,7 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat timeoutMapRev[expiration!!] = tmp } } + dbEnvironment.sync() } override fun updateResponsibilities(locationKey: Number160, peerId: Number160?): Boolean { @@ -163,12 +171,13 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat } else { hasChanged = true } - var contentIDs: MutableSet? = responsibilityMapRev[peerId] as MutableSet + var contentIDs: MutableSet? = responsibilityMapRev[peerId] as MutableSet? if (contentIDs == null) { contentIDs = HashSet() } contentIDs.add(locationKey) responsibilityMapRev[peerId] = contentIDs + dbEnvironment.sync() return hasChanged } @@ -182,10 +191,12 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat responsibilityMapRev[peerId] = contentIDs } } + dbEnvironment.sync() } override fun protectDomain(key: Number320?, publicKey: PublicKey?): Boolean { protectedDomainMap[key] = publicKey + dbEnvironment.sync() return true } @@ -201,6 +212,8 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat override fun removeTimeout(key: Number640) { val expiration = timeoutMap.remove(key) ?: return removeRevTimeout(key, expiration) + timeoutMapDB.sync() + dbEnvironment.sync() } override fun removeResponsibility(locationKey: Number160) { @@ -208,10 +221,12 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat if (peerId != null) { removeRevResponsibility(peerId, locationKey) } + dbEnvironment.sync() } override fun protectEntry(key: Number480?, publicKey: PublicKey?): Boolean { protectedEntryMap[key] = publicKey + dbEnvironment.sync() return true } @@ -270,5 +285,6 @@ class StorageBerkeleyDB(peerId: Number160, path : File, signatureFactory: Signat protectedEntryMapDB.close() responsibilityMapDB.close() responsibilityMapRevDB.close() + dbEnvironment.close() } } diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java index 09aa5e9..57e8d0f 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/ChatLogic.java @@ -30,7 +30,7 @@ import io.github.chronosx88.influence.models.roomEntities.MessageEntity; public class ChatLogic implements CoreContracts.IChatLogicContract { private static Gson gson = new Gson(); private String chatID; - private String newMessage = ""; + private volatile String newMessage = ""; private ChatEntity chatEntity; private Thread checkNewMessagesThread = null; private KeyPairManager keyPairManager; diff --git a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java index ee7b7f8..3da462e 100644 --- a/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java +++ b/app/src/main/java/io/github/chronosx88/influence/logic/MainLogic.java @@ -44,6 +44,7 @@ import io.github.chronosx88.influence.helpers.JVMShutdownHook; import io.github.chronosx88.influence.helpers.KeyPairManager; import io.github.chronosx88.influence.helpers.NetworkHandler; import io.github.chronosx88.influence.helpers.P2PUtils; +import io.github.chronosx88.influence.helpers.StorageBerkeleyDB; import io.github.chronosx88.influence.helpers.StorageMapDB; import io.github.chronosx88.influence.helpers.actions.UIActions; import io.github.chronosx88.influence.models.PublicUserProfile; @@ -85,9 +86,11 @@ public class MainLogic implements CoreContracts.IMainLogicContract { new Thread(() -> { try { - //StorageBerkeleyDB storageBerkeleyDB = new StorageBerkeleyDB(peerID, context.getFilesDir(), new RSASignatureFactory()); - Storage storageMapDB = new StorageMapDB(peerID, new File(context.getFilesDir(), "dhtDB.db"), new RSASignatureFactory()); - this.storage = storageMapDB; + File dhtDBEnv = new File(context.getFilesDir(), "dhtDBEnv"); + if(!dhtDBEnv.exists()) + dhtDBEnv.mkdirs(); + Storage storage = new StorageBerkeleyDB(peerID, dhtDBEnv, new RSASignatureFactory()); + this.storage = storage; peerDHT = new PeerBuilderDHT( new PeerBuilder(peerID) .ports(7243) @@ -95,9 +98,9 @@ public class MainLogic implements CoreContracts.IMainLogicContract { .channelServerConfiguration(createChannelServerConfig()) .start() ) - .storage(storageMapDB) + .storage(storage) .start(); - Runtime.getRuntime().addShutdownHook(new JVMShutdownHook(storageMapDB)); + Runtime.getRuntime().addShutdownHook(new JVMShutdownHook(storage)); try { String bootstrapIP = this.preferences.getString("bootstrapAddress", null); if(bootstrapIP == null) {