From c8dfc3b45946667ff65afe681013534a103fe0c9 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 25 Aug 2017 21:11:10 +0200 Subject: [PATCH] Added option to save labels and other improvements and fixes used for exports and imports --- build.gradle | 2 +- .../bitmessage/DefaultMessageListener.kt | 22 +++--- .../bitmessage/entity/valueobject/Label.kt | 3 +- .../ports/AlreadyStoredException.kt | 23 ++++++ .../bitmessage/ports/MessageRepository.kt | 2 + .../bitmessage/BitmessageContextTest.kt | 6 +- .../bitmessage/DefaultMessageListenerTest.kt | 4 +- .../bitmessage/exports/MessageExport.kt | 2 +- .../repository/JdbcMessageRepository.kt | 79 +++++++++++++++---- 9 files changed, 111 insertions(+), 32 deletions(-) create mode 100644 core/src/main/kotlin/ch/dissem/bitmessage/ports/AlreadyStoredException.kt diff --git a/build.gradle b/build.gradle index 669ad8e..33583e4 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.1.3-2' + ext.kotlin_version = '1.1.4-2' repositories { mavenCentral() } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/DefaultMessageListener.kt b/core/src/main/kotlin/ch/dissem/bitmessage/DefaultMessageListener.kt index ac30587..ad32bf9 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/DefaultMessageListener.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/DefaultMessageListener.kt @@ -23,6 +23,7 @@ import ch.dissem.bitmessage.entity.Plaintext.Status.PUBKEY_REQUESTED import ch.dissem.bitmessage.entity.payload.* import ch.dissem.bitmessage.entity.valueobject.InventoryVector import ch.dissem.bitmessage.exception.DecryptionFailedException +import ch.dissem.bitmessage.ports.AlreadyStoredException import ch.dissem.bitmessage.ports.Labeler import ch.dissem.bitmessage.ports.NetworkHandler import ch.dissem.bitmessage.utils.Strings.hex @@ -65,7 +66,7 @@ open class DefaultMessageListener( protected fun receive(objectMessage: ObjectMessage, getPubkey: GetPubkey) { val identity = ctx.addressRepository.findIdentity(getPubkey.ripeTag) - if (identity != null && identity.privateKey != null && !identity.isChan) { + if (identity?.privateKey != null && !identity.isChan) { LOG.info("Got pubkey request for identity " + identity) // FIXME: only send pubkey if it wasn't sent in the last TTL.pubkey() days ctx.sendPubkey(identity, objectMessage.stream) @@ -90,7 +91,6 @@ open class DefaultMessageListener( } } catch (_: DecryptionFailedException) { } - } private fun updatePubkey(address: BitmessageAddress, pubkey: Pubkey) { @@ -157,14 +157,18 @@ open class DefaultMessageListener( msg.inventoryVector = iv labeler.setLabels(msg) - ctx.messageRepository.save(msg) - listener.receive(msg) + try { + ctx.messageRepository.save(msg) + listener.receive(msg) - if (msg.type == Plaintext.Type.MSG && msg.to!!.has(Pubkey.Feature.DOES_ACK)) { - msg.ackMessage?.let { - ctx.inventory.storeObject(it) - ctx.networkHandler.offer(it.inventoryVector) - } ?: LOG.debug("ack message expected") + if (msg.type == Plaintext.Type.MSG && msg.to!!.has(Pubkey.Feature.DOES_ACK)) { + msg.ackMessage?.let { + ctx.inventory.storeObject(it) + ctx.networkHandler.offer(it.inventoryVector) + } ?: LOG.debug("ack message expected") + } + } catch (e: AlreadyStoredException) { + LOG.trace("Message was already received before.", e) } } diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/Label.kt b/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/Label.kt index 6fd4fd6..3bba8d6 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/Label.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/entity/valueobject/Label.kt @@ -25,7 +25,8 @@ data class Label( /** * RGBA representation for the color. */ - var color: Int = 0 + var color: Int = 0, + var ord: Int = 1000 ) : Serializable { var id: Any? = null diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/ports/AlreadyStoredException.kt b/core/src/main/kotlin/ch/dissem/bitmessage/ports/AlreadyStoredException.kt new file mode 100644 index 0000000..619f546 --- /dev/null +++ b/core/src/main/kotlin/ch/dissem/bitmessage/ports/AlreadyStoredException.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Christian Basler + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.dissem.bitmessage.ports + +/** + * Should be thrown if a received and decrypted message can't be stored because it has already been received and stored. + * (So it's not announced again to the client.) + */ +class AlreadyStoredException(message: String? = null, cause: Throwable? = null) : Exception(message, cause) diff --git a/core/src/main/kotlin/ch/dissem/bitmessage/ports/MessageRepository.kt b/core/src/main/kotlin/ch/dissem/bitmessage/ports/MessageRepository.kt index 9279dc0..68fac7a 100644 --- a/core/src/main/kotlin/ch/dissem/bitmessage/ports/MessageRepository.kt +++ b/core/src/main/kotlin/ch/dissem/bitmessage/ports/MessageRepository.kt @@ -28,6 +28,8 @@ interface MessageRepository { fun getLabels(vararg types: Label.Type): List