From d9090eb70cf75938a2483e5ab5295d10037b3dd6 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Mon, 13 Mar 2017 22:49:40 +0100 Subject: [PATCH] Some code to work with conversations --- README.md | 2 +- core/build.gradle | 2 +- .../bitmessage/DefaultMessageListener.java | 2 +- .../dissem/bitmessage/entity/Plaintext.java | 37 ++++- .../ports/AbstractMessageRepository.java | 22 +++ .../bitmessage/ports/MessageRepository.java | 23 +++ .../dissem/bitmessage/ports/NodeRegistry.java | 6 + .../bitmessage/utils/ConversationService.java | 112 +++++++++++++ .../ch/dissem/bitmessage/utils/Decode.java | 2 +- .../utils/ConversationServiceTest.java | 126 +++++++++++++++ .../dissem/bitmessage/TestNodeRegistry.java | 5 + .../repository/JdbcMessageRepository.java | 85 +++++++++- .../repository/JdbcNodeRegistry.java | 13 ++ .../V4.0__Create_table_message_parent.sql | 11 ++ .../repository/JdbcMessageRepositoryTest.java | 148 +++++++++++++++--- .../bitmessage/repository/TestJdbcConfig.java | 16 ++ 16 files changed, 573 insertions(+), 39 deletions(-) create mode 100644 core/src/main/java/ch/dissem/bitmessage/utils/ConversationService.java create mode 100644 core/src/test/java/ch/dissem/bitmessage/utils/ConversationServiceTest.java create mode 100644 repositories/src/main/resources/db/migration/V4.0__Create_table_message_parent.sql diff --git a/README.md b/README.md index 0f536d9..75f2b28 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ A Java implementation for the Bitmessage protocol. To build, use command `./grad Please note that it still has its limitations, but the API should now be stable. Jabit uses Semantic Versioning, meaning as long as the major version doesn't change, nothing should break if you update. -Be aware though that this doesn't necessarily applies for SNAPSHOT builds and the development branch, notably when it comes to database updates. _In other words, they may break your installation!_ +Be aware though that this doesn't necessarily applies for SNAPSHOT builds and the development branch, notably when it comes to database updates. In other words, they may break your installation!_ #### Master [![Build Status](https://travis-ci.org/Dissem/Jabit.svg?branch=master)](https://travis-ci.org/Dissem/Jabit) diff --git a/core/build.gradle b/core/build.gradle index 2fe6d50..2fb78cd 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -25,7 +25,7 @@ artifacts { dependencies { compile 'org.slf4j:slf4j-api:1.7.12' - compile 'ch.dissem.msgpack:msgpack:development-SNAPSHOT' + compile 'ch.dissem.msgpack:msgpack:1.0.0' testCompile 'junit:junit:4.12' testCompile 'org.hamcrest:hamcrest-library:1.3' testCompile 'org.mockito:mockito-core:1.10.19' diff --git a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java index b61f747..bdb61b6 100644 --- a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java +++ b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java @@ -87,7 +87,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener, Internal BitmessageAddress identity = ctx.getAddressRepository().findIdentity(getPubkey.getRipeTag()); if (identity != null && identity.getPrivateKey() != null && !identity.isChan()) { LOG.info("Got pubkey request for identity " + identity); - // FIXME: only send pubkey if it wasn't sent in the last 28 days + // FIXME: only send pubkey if it wasn't sent in the last TTL.pubkey() days ctx.sendPubkey(identity, object.getStream()); } } diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java index e46fd2a..dad561d 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java @@ -18,22 +18,20 @@ package ch.dissem.bitmessage.entity; import ch.dissem.bitmessage.entity.payload.Msg; import ch.dissem.bitmessage.entity.payload.Pubkey.Feature; -import ch.dissem.bitmessage.entity.valueobject.extended.Attachment; import ch.dissem.bitmessage.entity.valueobject.ExtendedEncoding; import ch.dissem.bitmessage.entity.valueobject.InventoryVector; import ch.dissem.bitmessage.entity.valueobject.Label; +import ch.dissem.bitmessage.entity.valueobject.extended.Attachment; import ch.dissem.bitmessage.entity.valueobject.extended.Message; import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.factory.ExtendedEncodingFactory; import ch.dissem.bitmessage.factory.Factory; -import ch.dissem.bitmessage.utils.Decode; -import ch.dissem.bitmessage.utils.Encode; -import ch.dissem.bitmessage.utils.TTL; -import ch.dissem.bitmessage.utils.UnixTime; +import ch.dissem.bitmessage.utils.*; import java.io.*; import java.nio.ByteBuffer; import java.util.*; +import java.util.Collections; import static ch.dissem.bitmessage.entity.Plaintext.Encoding.EXTENDED; import static ch.dissem.bitmessage.entity.Plaintext.Encoding.SIMPLE; @@ -50,6 +48,7 @@ public class Plaintext implements Streamable { private final long encoding; private final byte[] message; private final byte[] ackData; + private final UUID conversationId; private ExtendedEncoding extendedData; private ObjectMessage ackMessage; private Object id; @@ -90,6 +89,7 @@ public class Plaintext implements Streamable { ttl = builder.ttl; retries = builder.retries; nextTry = builder.nextTry; + conversationId = builder.conversation; } public static Plaintext read(Type type, InputStream in) throws IOException { @@ -390,7 +390,7 @@ public class Plaintext implements Streamable { } public List getParents() { - if (Message.TYPE.equals(getExtendedData().getType())) { + if (getExtendedData() != null && Message.TYPE.equals(getExtendedData().getType())) { return ((Message) extendedData.getContent()).getParents(); } else { return Collections.emptyList(); @@ -405,6 +405,10 @@ public class Plaintext implements Streamable { } } + public UUID getConversationId() { + return conversationId; + } + @Override public boolean equals(Object o) { if (this == o) return true; @@ -470,6 +474,16 @@ public class Plaintext implements Streamable { return initialHash; } + @Override + public String toString() { + String subject = getSubject(); + if (subject == null || subject.length() == 0) { + return Strings.hex(initialHash).toString(); + } else { + return subject; + } + } + public enum Encoding { IGNORE(0), TRIVIAL(1), SIMPLE(2), EXTENDED(3); @@ -527,12 +541,13 @@ public class Plaintext implements Streamable { private byte[] ackMessage; private byte[] signature; private long sent; - private long received; + private Long received; private Status status; private Set