From dad9de7ca24d7350e9d98f33882833336b5962b4 Mon Sep 17 00:00:00 2001 From: leventov Date: Fri, 2 Oct 2015 02:43:44 +0300 Subject: [PATCH] added connection listener --- .../hash/replication/ConnectionListener.java | 7 +++---- .../openhft/chronicle/map/TcpReplicator.java | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/hash/replication/ConnectionListener.java b/src/main/java/net/openhft/chronicle/hash/replication/ConnectionListener.java index c0f4fab08..4147ccf82 100644 --- a/src/main/java/net/openhft/chronicle/hash/replication/ConnectionListener.java +++ b/src/main/java/net/openhft/chronicle/hash/replication/ConnectionListener.java @@ -16,7 +16,7 @@ package net.openhft.chronicle.hash.replication; -import java.net.SocketAddress; +import java.net.InetAddress; public interface ConnectionListener { @@ -27,15 +27,14 @@ public interface ConnectionListener { * we made the connection to a remote host then {@code isServer} is {@code * false} */ - void onConnect(SocketAddress address, byte identifier, boolean isServer); + void onConnect(InetAddress address, byte identifier, boolean isServer); /** * @param address the address that we have been disconnected from * @param identifier the identifer the address that we have been disconnected from or Byte - * .MIN_VALUE if not known */ - void onDisconnect(SocketAddress address, byte identifier); + void onDisconnect(InetAddress address, byte identifier); } \ No newline at end of file diff --git a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java index ab7901829..57c8a4b4d 100644 --- a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java @@ -394,9 +394,10 @@ public final class TcpReplicator extends AbstractChannelReplicator impleme if (LOG.isDebugEnabled()) LOG.debug("", e); - // todo OZAN : // null check (Attached)key.attachment(); - // connectionListener.onDiconnect( ((SocketChannel)key.channel()).socket().getInetAddress(), ((Attached)key.attachment()).remoteIdentifier); + if (key.channel() != null && key.attachment() != null) + connectionListener.onDisconnect(((SocketChannel) key.channel()).socket().getInetAddress(), + ((Attached) key.attachment()).remoteIdentifier); closeEarlyAndQuietly(key.channel()); @@ -509,7 +510,7 @@ public final class TcpReplicator extends AbstractChannelReplicator impleme */ boolean isValidVersionNumber(String versionNumber) { - if (versionNumber.length()<=2) + if (versionNumber.length() <= 2) return false; for (char c : versionNumber.toCharArray()) { @@ -587,10 +588,11 @@ public final class TcpReplicator extends AbstractChannelReplicator impleme attached.remoteIdentifier = remoteIdentifier; - - // todo OZAN : add call to onConnectedListener. - //connectionListener. onConnected(channel.socket().getInetAddress() , attached - // .remoteIdentifier, attached.isServer ); + final SocketChannel channel = (SocketChannel) key.channel(); + if (channel != null && channel.socket() != null) { + connectionListener.onConnect(channel.socket().getInetAddress(), + attached.remoteIdentifier, attached.isServer); + } // we use the as iterating the activeKeys via the bitset wont create and Objects // but if we use the selector.keys() this will. @@ -1130,8 +1132,8 @@ public final class TcpReplicator extends AbstractChannelReplicator impleme /** * writes all the entries that have changed, to the buffer which will later be written to * TCP/IP - * @param modificationIterator a record of which entries have modification * + * @param modificationIterator a record of which entries have modification */ void entriesToBuffer(@NotNull final Replica.ModificationIterator modificationIterator) {