You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ph...@apache.org on 2011/09/15 00:50:27 UTC

svn commit: r1170882 - in /zookeeper/trunk: CHANGES.txt src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java

Author: phunt
Date: Wed Sep 14 22:50:27 2011
New Revision: 1170882

URL: http://svn.apache.org/viewvc?rev=1170882&view=rev
Log:
ZOOKEEPER-786. Exception in ZooKeeper.toString (Thomas Koch via phunt)

Modified:
    zookeeper/trunk/CHANGES.txt
    zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java

Modified: zookeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1170882&r1=1170881&r2=1170882&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Wed Sep 14 22:50:27 2011
@@ -8,6 +8,9 @@ Backward compatible changes:
 
 BUGFIXES:
 
+  ZOOKEEPER-786. Exception in ZooKeeper.toString
+  (Thomas Koch via phunt)
+
 IMPROVEMENTS:
 
   ZOOKEEPER-1170. Fix compiler (eclipse) warnings: unused imports,

Modified: zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java?rev=1170882&r1=1170881&r2=1170882&view=diff
==============================================================================
--- zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java (original)
+++ zookeeper/trunk/src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java Wed Sep 14 22:50:27 2011
@@ -20,6 +20,7 @@ package org.apache.zookeeper;
 
 import java.io.IOException;
 import java.net.InetSocketAddress;
+import java.net.Socket;
 import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.SelectionKey;
@@ -43,6 +44,10 @@ public class ClientCnxnSocketNIO extends
 
     private SelectionKey sockKey;
 
+    private SocketAddress localSocketAddress;
+
+    private SocketAddress remoteSocketAddress;
+
     ClientCnxnSocketNIO() throws IOException {
         super();
     }
@@ -185,9 +190,7 @@ public class ClientCnxnSocketNIO extends
         sock.socket().setSoLinger(false, -1);
         sock.socket().setTcpNoDelay(true);
         sockKey = sock.register(selector, SelectionKey.OP_CONNECT);
-        if (sock.connect(addr)) {
-            sendThread.primeConnection();
-        }
+        sock.connect(addr);
         initialized = false;
 
         /*
@@ -205,15 +208,7 @@ public class ClientCnxnSocketNIO extends
      */
     @Override
     SocketAddress getRemoteSocketAddress() {
-        // a lot could go wrong here, so rather than put in a bunch of code
-        // to check for nulls all down the chain let's do it the simple
-        // yet bulletproof way
-        try {
-            return ((SocketChannel) sockKey.channel()).socket()
-                    .getRemoteSocketAddress();
-        } catch (NullPointerException e) {
-            return null;
-        }
+        return remoteSocketAddress;
     }
 
     /**
@@ -224,15 +219,13 @@ public class ClientCnxnSocketNIO extends
      */
     @Override
     SocketAddress getLocalSocketAddress() {
-        // a lot could go wrong here, so rather than put in a bunch of code
-        // to check for nulls all down the chain let's do it the simple
-        // yet bulletproof way
-        try {
-            return ((SocketChannel) sockKey.channel()).socket()
-                    .getLocalSocketAddress();
-        } catch (NullPointerException e) {
-            return null;
-        }
+        return localSocketAddress;
+    }
+    
+    private void updateSocketAddresses() {
+        Socket socket = ((SocketChannel) sockKey.channel()).socket();
+        localSocketAddress = socket.getLocalSocketAddress();
+        remoteSocketAddress = socket.getRemoteSocketAddress();
     }
 
     @Override
@@ -257,6 +250,7 @@ public class ClientCnxnSocketNIO extends
             if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
                 if (sc.finishConnect()) {
                     updateLastSendAndHeard();
+                    updateSocketAddresses();
                     sendThread.primeConnection();
                 }
             } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {