Refactored use of the DefaultMessageListener so it's retrieved from the InternalContext
This commit is contained in:
@ -71,14 +71,13 @@ public abstract class AbstractConnection {
|
||||
|
||||
public AbstractConnection(InternalContext context, Mode mode,
|
||||
NetworkAddress node,
|
||||
NetworkHandler.MessageListener listener,
|
||||
Set<InventoryVector> commonRequestedObjects,
|
||||
long syncTimeout) {
|
||||
this.ctx = context;
|
||||
this.mode = mode;
|
||||
this.host = new NetworkAddress.Builder().ipv6(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).port(0).build();
|
||||
this.node = node;
|
||||
this.listener = listener;
|
||||
this.listener = context.getNetworkListener();
|
||||
this.syncTimeout = (syncTimeout > 0 ? UnixTime.now(+syncTimeout) : 0);
|
||||
this.requestedObjects = Collections.newSetFromMap(new ConcurrentHashMap<InventoryVector, Boolean>(10_000));
|
||||
this.ivCache = new ConcurrentHashMap<>();
|
||||
|
@ -63,29 +63,29 @@ class Connection extends AbstractConnection {
|
||||
private OutputStream out;
|
||||
private boolean socketInitialized;
|
||||
|
||||
public Connection(InternalContext context, Mode mode, Socket socket, MessageListener listener,
|
||||
public Connection(InternalContext context, Mode mode, Socket socket,
|
||||
Set<InventoryVector> requestedObjectsMap) throws IOException {
|
||||
this(context, mode, listener, socket, requestedObjectsMap,
|
||||
this(context, mode, socket, requestedObjectsMap,
|
||||
new NetworkAddress.Builder().ip(socket.getInetAddress()).port(socket.getPort()).stream(1).build(),
|
||||
0);
|
||||
}
|
||||
|
||||
public Connection(InternalContext context, Mode mode, NetworkAddress node, MessageListener listener,
|
||||
public Connection(InternalContext context, Mode mode, NetworkAddress node,
|
||||
Set<InventoryVector> requestedObjectsMap) {
|
||||
this(context, mode, listener, new Socket(), requestedObjectsMap,
|
||||
this(context, mode, new Socket(), requestedObjectsMap,
|
||||
node, 0);
|
||||
}
|
||||
|
||||
private Connection(InternalContext context, Mode mode, MessageListener listener, Socket socket,
|
||||
private Connection(InternalContext context, Mode mode, Socket socket,
|
||||
Set<InventoryVector> commonRequestedObjects, NetworkAddress node, long syncTimeout) {
|
||||
super(context, mode, node, listener, commonRequestedObjects, syncTimeout);
|
||||
super(context, mode, node, commonRequestedObjects, syncTimeout);
|
||||
this.startTime = UnixTime.now();
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public static Connection sync(InternalContext ctx, InetAddress address, int port, MessageListener listener,
|
||||
long timeoutInSeconds) throws IOException {
|
||||
return new Connection(ctx, SYNC, listener, new Socket(address, port),
|
||||
return new Connection(ctx, SYNC, new Socket(address, port),
|
||||
new HashSet<InventoryVector>(),
|
||||
new NetworkAddress.Builder().ip(address).port(port).stream(1).build(),
|
||||
timeoutInSeconds);
|
||||
|
@ -42,11 +42,10 @@ public class ConnectionOrganizer implements Runnable {
|
||||
private Connection initialConnection;
|
||||
|
||||
public ConnectionOrganizer(InternalContext ctx,
|
||||
DefaultNetworkHandler networkHandler,
|
||||
NetworkHandler.MessageListener listener) {
|
||||
DefaultNetworkHandler networkHandler) {
|
||||
this.ctx = ctx;
|
||||
this.networkHandler = networkHandler;
|
||||
this.listener = listener;
|
||||
this.listener = ctx.getNetworkListener();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -91,8 +90,7 @@ public class ConnectionOrganizer implements Runnable {
|
||||
NETWORK_MAGIC_NUMBER - active, ctx.getStreams());
|
||||
boolean first = active == 0 && initialConnection == null;
|
||||
for (NetworkAddress address : addresses) {
|
||||
Connection c = new Connection(ctx, CLIENT, address, listener,
|
||||
networkHandler.requestedObjects);
|
||||
Connection c = new Connection(ctx, CLIENT, address, networkHandler.requestedObjects);
|
||||
if (first) {
|
||||
initialConnection = c;
|
||||
first = false;
|
||||
|
@ -64,9 +64,9 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<?> synchronize(InetAddress server, int port, MessageListener listener, long timeoutInSeconds) {
|
||||
public Future<?> synchronize(InetAddress server, int port, long timeoutInSeconds) {
|
||||
try {
|
||||
Connection connection = Connection.sync(ctx, server, port, listener, timeoutInSeconds);
|
||||
Connection connection = Connection.sync(ctx, server, port, ctx.getNetworkListener(), timeoutInSeconds);
|
||||
Future<?> reader = pool.submit(connection.getReader());
|
||||
pool.execute(connection.getWriter());
|
||||
return reader;
|
||||
@ -97,19 +97,16 @@ public class DefaultNetworkHandler implements NetworkHandler, ContextHolder {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(final MessageListener listener) {
|
||||
if (listener == null) {
|
||||
throw new IllegalStateException("Listener must be set at start");
|
||||
}
|
||||
public void start() {
|
||||
if (running) {
|
||||
throw new IllegalStateException("Network already running - you need to stop first.");
|
||||
}
|
||||
try {
|
||||
running = true;
|
||||
connections.clear();
|
||||
server = new ServerRunnable(ctx, this, listener);
|
||||
server = new ServerRunnable(ctx, this);
|
||||
pool.execute(server);
|
||||
pool.execute(new ConnectionOrganizer(ctx, this, listener));
|
||||
pool.execute(new ConnectionOrganizer(ctx, this));
|
||||
} catch (IOException e) {
|
||||
throw new ApplicationException(e);
|
||||
}
|
||||
|
@ -38,11 +38,10 @@ public class ServerRunnable implements Runnable, Closeable {
|
||||
private final DefaultNetworkHandler networkHandler;
|
||||
private final NetworkHandler.MessageListener listener;
|
||||
|
||||
public ServerRunnable(InternalContext ctx, DefaultNetworkHandler networkHandler,
|
||||
NetworkHandler.MessageListener listener) throws IOException {
|
||||
public ServerRunnable(InternalContext ctx, DefaultNetworkHandler networkHandler) throws IOException {
|
||||
this.ctx = ctx;
|
||||
this.networkHandler = networkHandler;
|
||||
this.listener = listener;
|
||||
this.listener = ctx.getNetworkListener();
|
||||
this.serverSocket = new ServerSocket(ctx.getPort());
|
||||
}
|
||||
|
||||
@ -52,8 +51,7 @@ public class ServerRunnable implements Runnable, Closeable {
|
||||
try {
|
||||
Socket socket = serverSocket.accept();
|
||||
socket.setSoTimeout(Connection.READ_TIMEOUT);
|
||||
networkHandler.startConnection(new Connection(ctx, SERVER, socket, listener,
|
||||
networkHandler.requestedObjects));
|
||||
networkHandler.startConnection(new Connection(ctx, SERVER, socket, networkHandler.requestedObjects));
|
||||
} catch (IOException e) {
|
||||
LOG.debug(e.getMessage(), e);
|
||||
}
|
||||
|
@ -17,13 +17,15 @@
|
||||
package ch.dissem.bitmessage.networking.nio;
|
||||
|
||||
import ch.dissem.bitmessage.InternalContext;
|
||||
import ch.dissem.bitmessage.entity.*;
|
||||
import ch.dissem.bitmessage.entity.GetData;
|
||||
import ch.dissem.bitmessage.entity.MessagePayload;
|
||||
import ch.dissem.bitmessage.entity.NetworkMessage;
|
||||
import ch.dissem.bitmessage.entity.Version;
|
||||
import ch.dissem.bitmessage.entity.valueobject.InventoryVector;
|
||||
import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
|
||||
import ch.dissem.bitmessage.exception.NodeException;
|
||||
import ch.dissem.bitmessage.factory.V3MessageReader;
|
||||
import ch.dissem.bitmessage.networking.AbstractConnection;
|
||||
import ch.dissem.bitmessage.ports.NetworkHandler;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Iterator;
|
||||
@ -43,10 +45,9 @@ public class ConnectionInfo extends AbstractConnection {
|
||||
private boolean syncFinished;
|
||||
private long lastUpdate = System.currentTimeMillis();
|
||||
|
||||
public ConnectionInfo(InternalContext context, Mode mode,
|
||||
NetworkAddress node, NetworkHandler.MessageListener listener,
|
||||
public ConnectionInfo(InternalContext context, Mode mode, NetworkAddress node,
|
||||
Set<InventoryVector> commonRequestedObjects, long syncTimeout) {
|
||||
super(context, mode, node, listener, commonRequestedObjects, syncTimeout);
|
||||
super(context, mode, node, commonRequestedObjects, syncTimeout);
|
||||
headerOut.flip();
|
||||
if (mode == CLIENT || mode == SYNC) {
|
||||
send(new Version.Builder().defaults(ctx.getClientNonce()).addrFrom(host).addrRecv(node).build());
|
||||
|
@ -26,7 +26,7 @@ import ch.dissem.bitmessage.exception.ApplicationException;
|
||||
import ch.dissem.bitmessage.exception.NodeException;
|
||||
import ch.dissem.bitmessage.factory.V3MessageReader;
|
||||
import ch.dissem.bitmessage.ports.NetworkHandler;
|
||||
import ch.dissem.bitmessage.utils.*;
|
||||
import ch.dissem.bitmessage.utils.Property;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -37,7 +37,6 @@ import java.net.NoRouteToHostException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.*;
|
||||
import java.util.*;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
import static ch.dissem.bitmessage.networking.AbstractConnection.Mode.*;
|
||||
@ -47,6 +46,7 @@ import static ch.dissem.bitmessage.utils.Collections.selectRandom;
|
||||
import static ch.dissem.bitmessage.utils.DebugUtils.inc;
|
||||
import static ch.dissem.bitmessage.utils.ThreadFactoryBuilder.pool;
|
||||
import static java.nio.channels.SelectionKey.*;
|
||||
import static java.util.Collections.newSetFromMap;
|
||||
|
||||
/**
|
||||
* Network handler using java.nio, resulting in less threads.
|
||||
@ -63,13 +63,14 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
private InternalContext ctx;
|
||||
private Selector selector;
|
||||
private ServerSocketChannel serverChannel;
|
||||
private Queue<NetworkAddress> connectionQueue = new ConcurrentLinkedQueue<>();
|
||||
private Map<ConnectionInfo, SelectionKey> connections = new ConcurrentHashMap<>();
|
||||
private final Set<InventoryVector> requestedObjects = Collections.newSetFromMap(new ConcurrentHashMap<InventoryVector, Boolean>(10_000));
|
||||
private final Set<InventoryVector> requestedObjects = newSetFromMap(new ConcurrentHashMap<InventoryVector, Boolean>(10_000));
|
||||
|
||||
private Thread starter;
|
||||
|
||||
@Override
|
||||
public Future<Void> synchronize(final InetAddress server, final int port, final MessageListener listener, final long timeoutInSeconds) {
|
||||
public Future<Void> synchronize(final InetAddress server, final int port, final long timeoutInSeconds) {
|
||||
return threadPool.submit(new Callable<Void>() {
|
||||
@Override
|
||||
public Void call() throws Exception {
|
||||
@ -77,7 +78,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
channel.configureBlocking(false);
|
||||
ConnectionInfo connection = new ConnectionInfo(ctx, SYNC,
|
||||
new NetworkAddress.Builder().ip(server).port(port).stream(1).build(),
|
||||
listener, new HashSet<InventoryVector>(), timeoutInSeconds);
|
||||
new HashSet<InventoryVector>(), timeoutInSeconds);
|
||||
while (channel.isConnected() && !connection.isSyncFinished()) {
|
||||
write(channel, connection);
|
||||
read(channel, connection);
|
||||
@ -135,10 +136,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(final MessageListener listener) {
|
||||
if (listener == null) {
|
||||
throw new IllegalStateException("Listener must be set at start");
|
||||
}
|
||||
public void start() {
|
||||
if (selector != null && selector.isOpen()) {
|
||||
throw new IllegalStateException("Network already running - you need to stop first.");
|
||||
}
|
||||
@ -147,42 +145,8 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
} catch (IOException e) {
|
||||
throw new ApplicationException(e);
|
||||
}
|
||||
thread("connection listener", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
serverChannel = ServerSocketChannel.open();
|
||||
serverChannel.socket().bind(new InetSocketAddress(ctx.getPort()));
|
||||
while (selector.isOpen() && serverChannel.isOpen()) {
|
||||
try {
|
||||
SocketChannel accepted = serverChannel.accept();
|
||||
accepted.configureBlocking(false);
|
||||
ConnectionInfo connection = new ConnectionInfo(ctx, SERVER,
|
||||
new NetworkAddress.Builder().address(accepted.getRemoteAddress()).stream(1).build(),
|
||||
listener,
|
||||
requestedObjects, 0
|
||||
);
|
||||
connections.put(
|
||||
connection,
|
||||
accepted.register(selector, OP_READ | OP_WRITE, connection)
|
||||
);
|
||||
} catch (AsynchronousCloseException ignore) {
|
||||
LOG.trace(ignore.getMessage());
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} catch (ClosedSelectorException | AsynchronousCloseException ignore) {
|
||||
} catch (IOException e) {
|
||||
throw new ApplicationException(e);
|
||||
} catch (RuntimeException e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
starter = thread("connection starter", new Runnable() {
|
||||
starter = thread("connection manager", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
while (selector.isOpen()) {
|
||||
@ -197,34 +161,8 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
List<NetworkAddress> addresses = ctx.getNodeRegistry().getKnownAddresses(100, ctx.getStreams());
|
||||
addresses = selectRandom(missing, addresses);
|
||||
for (NetworkAddress address : addresses) {
|
||||
if (isConnectedTo(address)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
SocketChannel channel = SocketChannel.open();
|
||||
channel.configureBlocking(false);
|
||||
channel.connect(new InetSocketAddress(address.toInetAddress(), address.getPort()));
|
||||
ConnectionInfo connection = new ConnectionInfo(ctx, CLIENT,
|
||||
address,
|
||||
listener,
|
||||
requestedObjects, 0
|
||||
);
|
||||
connections.put(
|
||||
connection,
|
||||
channel.register(selector, OP_CONNECT, connection)
|
||||
);
|
||||
} catch (NoRouteToHostException ignore) {
|
||||
// We'll try to connect to many offline nodes, so
|
||||
// this is expected to happen quite a lot.
|
||||
} catch (AsynchronousCloseException e) {
|
||||
// The exception is expected if the network is being
|
||||
// shut down, as we actually do asynchronously close
|
||||
// the connections.
|
||||
if (isRunning()) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
if (!isConnectedTo(address)) {
|
||||
connectionQueue.offer(address);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -252,17 +190,47 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
}
|
||||
});
|
||||
|
||||
thread("processor", new Runnable() {
|
||||
thread("selector worker", new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
serverChannel = ServerSocketChannel.open();
|
||||
serverChannel.configureBlocking(false);
|
||||
serverChannel.socket().bind(new InetSocketAddress(ctx.getPort()));
|
||||
serverChannel.register(selector, OP_ACCEPT, null);
|
||||
|
||||
while (selector.isOpen()) {
|
||||
selector.select(1000);
|
||||
Iterator<SelectionKey> keyIterator = selector.selectedKeys().iterator();
|
||||
while (keyIterator.hasNext()) {
|
||||
SelectionKey key = keyIterator.next();
|
||||
keyIterator.remove();
|
||||
if (key.attachment() instanceof ConnectionInfo) {
|
||||
if (key.attachment() == null) {
|
||||
try {
|
||||
if (key.isAcceptable()) {
|
||||
// handle accept
|
||||
try {
|
||||
SocketChannel accepted = ((ServerSocketChannel) key.channel()).accept();
|
||||
accepted.configureBlocking(false);
|
||||
ConnectionInfo connection = new ConnectionInfo(ctx, SERVER,
|
||||
new NetworkAddress.Builder().address(accepted.getRemoteAddress()).stream(1).build(),
|
||||
requestedObjects, 0
|
||||
);
|
||||
connections.put(
|
||||
connection,
|
||||
accepted.register(selector, OP_READ | OP_WRITE, connection)
|
||||
);
|
||||
} catch (AsynchronousCloseException e) {
|
||||
LOG.trace(e.getMessage());
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
} catch (CancelledKeyException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
} else {
|
||||
// handle read/write
|
||||
SocketChannel channel = (SocketChannel) key.channel();
|
||||
ConnectionInfo connection = (ConnectionInfo) key.attachment();
|
||||
try {
|
||||
@ -290,6 +258,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
}
|
||||
}
|
||||
}
|
||||
// set interest ops
|
||||
for (Map.Entry<ConnectionInfo, SelectionKey> e : connections.entrySet()) {
|
||||
if (e.getValue().isValid()
|
||||
&& (e.getValue().interestOps() & OP_WRITE) == 0
|
||||
@ -298,6 +267,35 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
e.getValue().interestOps(OP_READ | OP_WRITE);
|
||||
}
|
||||
}
|
||||
// start new connections
|
||||
if (!connectionQueue.isEmpty()) {
|
||||
NetworkAddress address = connectionQueue.poll();
|
||||
try {
|
||||
SocketChannel channel = SocketChannel.open();
|
||||
channel.configureBlocking(false);
|
||||
channel.connect(new InetSocketAddress(address.toInetAddress(), address.getPort()));
|
||||
ConnectionInfo connection = new ConnectionInfo(ctx, CLIENT,
|
||||
address,
|
||||
requestedObjects, 0
|
||||
);
|
||||
connections.put(
|
||||
connection,
|
||||
channel.register(selector, OP_CONNECT, connection)
|
||||
);
|
||||
} catch (NoRouteToHostException ignore) {
|
||||
// We'll try to connect to many offline nodes, so
|
||||
// this is expected to happen quite a lot.
|
||||
} catch (AsynchronousCloseException e) {
|
||||
// The exception is expected if the network is being
|
||||
// shut down, as we actually do asynchronously close
|
||||
// the connections.
|
||||
if (isRunning()) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
selector.close();
|
||||
} catch (ClosedSelectorException ignore) {
|
||||
@ -387,7 +385,7 @@ public class NioNetworkHandler implements NetworkHandler, InternalContext.Contex
|
||||
distribution.put(connection, new LinkedList<InventoryVector>());
|
||||
}
|
||||
}
|
||||
if (distribution.isEmpty()){
|
||||
if (distribution.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
InventoryVector next = iterator.next();
|
||||
|
@ -230,9 +230,7 @@ public class NetworkHandlerTest {
|
||||
"V4Pubkey.payload"
|
||||
);
|
||||
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(),
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(), 10);
|
||||
future.get();
|
||||
assertInventorySize(3, nodeInventory);
|
||||
assertInventorySize(3, peerInventory);
|
||||
@ -247,9 +245,7 @@ public class NetworkHandlerTest {
|
||||
|
||||
nodeInventory.init();
|
||||
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(),
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(), 10);
|
||||
future.get();
|
||||
assertInventorySize(2, nodeInventory);
|
||||
assertInventorySize(2, peerInventory);
|
||||
@ -263,9 +259,7 @@ public class NetworkHandlerTest {
|
||||
"V1Msg.payload"
|
||||
);
|
||||
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(),
|
||||
mock(NetworkHandler.MessageListener.class),
|
||||
10);
|
||||
Future<?> future = nodeNetworkHandler.synchronize(peerAddress.toInetAddress(), peerAddress.getPort(), 10);
|
||||
future.get();
|
||||
assertInventorySize(1, nodeInventory);
|
||||
assertInventorySize(1, peerInventory);
|
||||
|
Reference in New Issue
Block a user