From 6be8d51f6d0df944fe5e4291c1022d9e87adba89 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Thu, 18 Jun 2015 13:41:11 +0200 Subject: [PATCH] Distributable JAR build - connection manager should now be rock stable - does try to create new connections as long as there are less than eight active connections, which might result in more than eight outgoing connections, but this shouldn't be a problem - some minor improvements and bug fixes --- demo/build.gradle | 8 +++- .../java/ch/dissem/bitmessage/demo/Main.java | 7 +++- .../dissem/bitmessage/BitmessageContext.java | 3 ++ .../bitmessage/DefaultMessageListener.java | 2 +- .../bitmessage/entity/valueobject/Label.java | 3 +- networking/build.gradle | 1 - .../bitmessage/networking/NetworkNode.java | 42 ++++++++++--------- .../repository/JdbcMessageRepositoryTest.java | 2 +- 8 files changed, 41 insertions(+), 27 deletions(-) diff --git a/demo/build.gradle b/demo/build.gradle index 50e80ca..3c7930f 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -1,4 +1,10 @@ -apply plugin: 'java' +plugins { + id "us.kirchmeier.capsule" version "1.0-rc1" +} + +task fatCapsule(type: FatCapsule) { + applicationClass 'ch.dissem.bitmessage.demo.Main' +} sourceCompatibility = 1.7 version = '0.0.1' diff --git a/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java b/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java index 37b58e1..cf29110 100644 --- a/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java +++ b/demo/src/main/java/ch/dissem/bitmessage/demo/Main.java @@ -20,8 +20,11 @@ import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "DEBUG"); - System.setProperty("org.slf4j.simpleLogger.logFile", "./jabit.log"); + if (System.getProperty("org.slf4j.simpleLogger.defaultLogLevel") == null) + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "ERROR"); + if (System.getProperty("org.slf4j.simpleLogger.logFile") == null) + System.setProperty("org.slf4j.simpleLogger.logFile", "./jabit.log"); + new Application(); } } diff --git a/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index bcfb463..a1aca88 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -21,6 +21,7 @@ import ch.dissem.bitmessage.entity.ObjectMessage; import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.entity.payload.*; import ch.dissem.bitmessage.entity.payload.Pubkey.Feature; +import ch.dissem.bitmessage.entity.valueobject.Label; import ch.dissem.bitmessage.entity.valueobject.PrivateKey; import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.factory.Factory; @@ -114,6 +115,7 @@ public class BitmessageContext { 0 ); msg.setStatus(SENT); + msg.addLabels(ctx.getMessageRepository().getLabels(Label.Type.BROADCAST, Label.Type.SENT)); ctx.getMessageRepository().save(msg); } @@ -148,6 +150,7 @@ public class BitmessageContext { ctx.getExtraBytes(to) ); msg.setStatus(SENT); + msg.addLabels(ctx.getMessageRepository().getLabels(Label.Type.SENT)); ctx.getMessageRepository().save(msg); } } diff --git a/domain/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java b/domain/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java index d034a79..b521464 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java +++ b/domain/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java @@ -145,7 +145,7 @@ class DefaultMessageListener implements NetworkHandler.MessageListener { LOG.warn("Broadcast with IV " + object.getInventoryVector() + " was successfully decrypted, but signature check failed. Ignoring."); } else { broadcast.getPlaintext().setStatus(RECEIVED); - broadcast.getPlaintext().addLabels(ctx.getMessageRepository().getLabels(Label.Type.INBOX, Label.Type.UNREAD)); + broadcast.getPlaintext().addLabels(ctx.getMessageRepository().getLabels(Label.Type.INBOX, Label.Type.BROADCAST, Label.Type.UNREAD)); broadcast.getPlaintext().setInventoryVector(object.getInventoryVector()); ctx.getMessageRepository().save(broadcast.getPlaintext()); listener.receive(broadcast.getPlaintext()); diff --git a/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/Label.java b/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/Label.java index 1f186c0..b77220b 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/Label.java +++ b/domain/src/main/java/ch/dissem/bitmessage/entity/valueobject/Label.java @@ -73,7 +73,8 @@ public class Label { public enum Type { INBOX, - DRAFTS, + BROADCAST, + DRAFT, SENT, UNREAD, TRASH diff --git a/networking/build.gradle b/networking/build.gradle index fa10825..0a8d301 100644 --- a/networking/build.gradle +++ b/networking/build.gradle @@ -9,7 +9,6 @@ repositories { dependencies { compile project(':domain') - compile 'com.google.guava:guava-concurrent:r03' testCompile 'org.slf4j:slf4j-simple:1.7.12' testCompile 'junit:junit:4.11' } \ No newline at end of file diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java b/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java index 17d335c..e8e32ab 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/NetworkNode.java @@ -82,32 +82,34 @@ public class NetworkNode implements NetworkHandler, ContextHolder { @Override public void run() { while (!Thread.interrupted()) { - int active = 0; - synchronized (connections) { - for (Iterator iterator = connections.iterator(); iterator.hasNext(); ) { - Connection c = iterator.next(); - if (c.getState() == DISCONNECTED) { - // Remove the current element from the iterator and the list. - iterator.remove(); - } - if (c.getState() == ACTIVE) { - active++; - } - } - } - if (active < 8) { - List addresses = ctx.getNodeRegistry().getKnownAddresses(8 - active, ctx.getStreams()); - for (NetworkAddress address : addresses) { - startConnection(new Connection(ctx, CLIENT, address, listener)); - } - } try { + int active = 0; + synchronized (connections) { + for (Iterator iterator = connections.iterator(); iterator.hasNext(); ) { + Connection c = iterator.next(); + if (c.getState() == DISCONNECTED) { + // Remove the current element from the iterator and the list. + iterator.remove(); + } + if (c.getState() == ACTIVE) { + active++; + } + } + } + if (active < 8) { + List addresses = ctx.getNodeRegistry().getKnownAddresses(8 - active, ctx.getStreams()); + for (NetworkAddress address : addresses) { + startConnection(new Connection(ctx, CLIENT, address, listener)); + } + } Thread.sleep(30000); } catch (InterruptedException e) { - LOG.debug(e.getMessage(), e); Thread.currentThread().interrupt(); + } catch (Exception e) { + LOG.error("Error in connection manager. Ignored.", e); } } + LOG.debug("Connection manager shutting down."); } }, "connection-manager"); connectionManager.start(); diff --git a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.java b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.java index 585c8e3..b99e44f 100644 --- a/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.java +++ b/repositories/src/test/java/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.java @@ -67,7 +67,7 @@ public class JdbcMessageRepositoryTest { addressRepo.save(identity); inbox = repo.getLabels(Label.Type.INBOX).get(0); - drafts = repo.getLabels(Label.Type.DRAFTS).get(0); + drafts = repo.getLabels(Label.Type.DRAFT).get(0); addMessage(contactA, identity, Plaintext.Status.RECEIVED, inbox); addMessage(identity, contactA, Plaintext.Status.DRAFT, drafts);