Review Board 1.7.22


ZOOKEEPER-786 Exception in ZooKeeper.toString

Review Request #1714 - Created Sept. 5, 2011 and submitted

Thomas Koch
ZOOKEEPER-786
Reviewers
zookeeper
zookeeper-git
.

 

Diff revision 1 (Latest)

  1. src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java: Loading...
src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java
Revision 626da04 New Change
[20] 17 lines
[+20]
18

    
   
18

   
19
package org.apache.zookeeper;
19
package org.apache.zookeeper;
20

    
   
20

   
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.net.InetSocketAddress;
22
import java.net.InetSocketAddress;

    
   
23
import java.net.Socket;
23
import java.net.SocketAddress;
24
import java.net.SocketAddress;
24
import java.nio.ByteBuffer;
25
import java.nio.ByteBuffer;
25
import java.nio.channels.SelectionKey;
26
import java.nio.channels.SelectionKey;
26
import java.nio.channels.Selector;
27
import java.nio.channels.Selector;
27
import java.nio.channels.SocketChannel;
28
import java.nio.channels.SocketChannel;
[+20] [20] 14 lines
[+20] [+] public class ClientCnxnSocketNIO extends ClientCnxnSocket {
42

    
   
43

   
43
    private final Selector selector = Selector.open();
44
    private final Selector selector = Selector.open();
44

    
   
45

   
45
    private SelectionKey sockKey;
46
    private SelectionKey sockKey;
46

    
   
47

   

    
   
48
    private SocketAddress localSocketAddress;

    
   
49

   

    
   
50
    private SocketAddress remoteSocketAddress;

    
   
51

   
47
    ClientCnxnSocketNIO() throws IOException {
52
    ClientCnxnSocketNIO() throws IOException {
48
        super();
53
        super();
49
    }
54
    }
50

    
   
55

   
51
    @Override
56
    @Override
[+20] [20] 132 lines
[+20] public class ClientCnxnSocketNIO extends ClientCnxnSocket {
184
        sock = SocketChannel.open();
189
        sock = SocketChannel.open();
185
        sock.configureBlocking(false);
190
        sock.configureBlocking(false);
186
        sock.socket().setSoLinger(false, -1);
191
        sock.socket().setSoLinger(false, -1);
187
        sock.socket().setTcpNoDelay(true);
192
        sock.socket().setTcpNoDelay(true);
188
        sockKey = sock.register(selector, SelectionKey.OP_CONNECT);
193
        sockKey = sock.register(selector, SelectionKey.OP_CONNECT);
189
        if (sock.connect(addr)) {
194
        sock.connect(addr);
190
            sendThread.primeConnection();

   
191
        }

   
192
        initialized = false;
195
        initialized = false;
193

    
   
196

   
194
        /*
197
        /*
195
         * Reset incomingBuffer
198
         * Reset incomingBuffer
196
         */
199
         */
[+20] [20] 7 lines
[+20] public class ClientCnxnSocketNIO extends ClientCnxnSocket {
204
     * @return ip address of the remote side of the connection or null if not
207
     * @return ip address of the remote side of the connection or null if not
205
     *         connected
208
     *         connected
206
     */
209
     */
207
    @Override
210
    @Override
208
    SocketAddress getRemoteSocketAddress() {
211
    SocketAddress getRemoteSocketAddress() {
209
        // a lot could go wrong here, so rather than put in a bunch of code
212
        return remoteSocketAddress;
210
        // to check for nulls all down the chain let's do it the simple

   
211
        // yet bulletproof way

   
212
        try {

   
213
            return ((SocketChannel) sockKey.channel()).socket()

   
214
                    .getRemoteSocketAddress();

   
215
        } catch (NullPointerException e) {

   
216
            return null;

   
217
        }

   
218
    }
213
    }
219

    
   
214

   
220
    /**
215
    /**
221
     * Returns the local address to which the socket is bound.
216
     * Returns the local address to which the socket is bound.
222
     * 
217
     * 
223
     * @return ip address of the remote side of the connection or null if not
218
     * @return ip address of the remote side of the connection or null if not
224
     *         connected
219
     *         connected
225
     */
220
     */
226
    @Override
221
    @Override
227
    SocketAddress getLocalSocketAddress() {
222
    SocketAddress getLocalSocketAddress() {
228
        // a lot could go wrong here, so rather than put in a bunch of code
223
        return localSocketAddress;
229
        // to check for nulls all down the chain let's do it the simple

   
230
        // yet bulletproof way

   
231
        try {

   
232
            return ((SocketChannel) sockKey.channel()).socket()

   
233
                    .getLocalSocketAddress();

   
234
        } catch (NullPointerException e) {

   
235
            return null;

   
236
        }
224
    }

    
   
225
    

    
   
226
    private void updateSocketAddresses() {

    
   
227
        Socket socket = ((SocketChannel) sockKey.channel()).socket();

    
   
228
        localSocketAddress = socket.getLocalSocketAddress();

    
   
229
        remoteSocketAddress = socket.getRemoteSocketAddress();
237
    }
230
    }
238

    
   
231

   
239
    @Override
232
    @Override
240
    synchronized void wakeupCnxn() {
233
    synchronized void wakeupCnxn() {
241
        selector.wakeup();
234
        selector.wakeup();
[+20] [20] 14 lines
[+20] public class ClientCnxnSocketNIO extends ClientCnxnSocket { private void updateSocketAddresses() {
256
        for (SelectionKey k : selected) {
249
        for (SelectionKey k : selected) {
257
            SocketChannel sc = ((SocketChannel) k.channel());
250
            SocketChannel sc = ((SocketChannel) k.channel());
258
            if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
251
            if ((k.readyOps() & SelectionKey.OP_CONNECT) != 0) {
259
                if (sc.finishConnect()) {
252
                if (sc.finishConnect()) {
260
                    updateLastSendAndHeard();
253
                    updateLastSendAndHeard();

    
   
254
                    updateSocketAddresses();
261
                    sendThread.primeConnection();
255
                    sendThread.primeConnection();
262
                }
256
                }
263
            } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
257
            } else if ((k.readyOps() & (SelectionKey.OP_READ | SelectionKey.OP_WRITE)) != 0) {
264
                doIO(pendingQueue, outgoingQueue);
258
                doIO(pendingQueue, outgoingQueue);
265
            }
259
            }
[+20] [20] 47 lines
  1. src/java/main/org/apache/zookeeper/ClientCnxnSocketNIO.java: Loading...