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
This commit is contained in:
2015-06-18 13:41:11 +02:00
parent 1dc4582012
commit 6be8d51f6d
8 changed files with 41 additions and 27 deletions

View File

@ -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'
}

View File

@ -82,32 +82,34 @@ public class NetworkNode implements NetworkHandler, ContextHolder {
@Override
public void run() {
while (!Thread.interrupted()) {
int active = 0;
synchronized (connections) {
for (Iterator<Connection> 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<NetworkAddress> 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<Connection> 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<NetworkAddress> 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();