# Class ClientCnxn

java.lang.Object
org.apache.zookeeper.ClientCnxn

public class ClientCnxn extends Object
This class manages the socket i/o for the client. ClientCnxn maintains a list of available servers to connect to and "transparently" switches servers it is connected to as needed.

## Field Summary

Fields
Modifier and Type
Field
Description
`static final int`
`AUTHPACKET_XID`
`static final int`
`NOTIFICATION_XID`
`static final int`
`PING_XID`
`static final int`
`SET_WATCHES_XID`
`protected int`
`xid`

## Constructor Summary

Constructors
Constructor
Description
`ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZKClientConfig clientConfig, Watcher defaultWatcher, org.apache.zookeeper.ClientCnxnSocket clientCnxnSocket, boolean canBeReadOnly)`
Creates a connection object.
`ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZKClientConfig clientConfig, Watcher defaultWatcher, org.apache.zookeeper.ClientCnxnSocket clientCnxnSocket, long sessionId, byte[] sessionPasswd, boolean canBeReadOnly)`
Creates a connection object.

## Method Summary

Modifier and Type
Method
Description
`void`
`addAuthInfo(String scheme, byte[] auth)`
`void`
`close()`
Close the connection, which includes; send session disconnect to the server, shutdown the send/event threads.
`void`
`disconnect()`
Shutdown the send/event threads.
`protected void`
`finishPacket(org.apache.zookeeper.ClientCnxn.Packet p)`
`long`
`getLastZxid()`
`long`
`getSessionId()`
`byte[]`
`getSessionPasswd()`
`int`
`getSessionTimeout()`
`int`
`getXid()`
`ZooKeeperSaslClient`
`getZooKeeperSaslClient()`
`static boolean`
`isInEventThread()`
Tests that current thread is the main event loop.
`protected void`
`onConnecting(InetSocketAddress addr)`
`org.apache.zookeeper.ClientCnxn.Packet`
`queuePacket(RequestHeader h, ReplyHeader r, Record request, Record response, AsyncCallback cb, String clientPath, String serverPath, Object ctx, ZooKeeper.WatchRegistration watchRegistration)`
`org.apache.zookeeper.ClientCnxn.Packet`
`queuePacket(RequestHeader h, ReplyHeader r, Record request, Record response, AsyncCallback cb, String clientPath, String serverPath, Object ctx, ZooKeeper.WatchRegistration watchRegistration, WatchDeregistration watchDeregistration)`
`void`
`saslCompleted()`
`void`
`sendPacket(Record request, Record response, AsyncCallback cb, int opCode)`
`void`
`start()`
`ReplyHeader`
`submitRequest(RequestHeader h, Record request, Record response, ZooKeeper.WatchRegistration watchRegistration)`
`ReplyHeader`
`submitRequest(RequestHeader h, Record request, Record response, ZooKeeper.WatchRegistration watchRegistration, WatchDeregistration watchDeregistration)`
`String`
`toString()`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Field Details

### NOTIFICATION_XID

public static final int NOTIFICATION_XID

See Also:
  - Constant Field Values

### PING_XID

public static final int PING_XID

See Also:
  - Constant Field Values

### AUTHPACKET_XID

public static final int AUTHPACKET_XID

See Also:
  - Constant Field Values

### SET_WATCHES_XID

public static final int SET_WATCHES_XID

See Also:
  - Constant Field Values

### xid

protected int xid

## Constructor Details

### ClientCnxn

public ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZKClientConfig clientConfig, Watcher defaultWatcher, org.apache.zookeeper.ClientCnxnSocket clientCnxnSocket, boolean canBeReadOnly) throws IOException
Creates a connection object. The actual network connect doesn't get established until needed. The start() instance method must be called subsequent to construction.

Parameters:
  `chrootPath` - the chroot of this client. Should be removed from this Class in ZOOKEEPER-838
  `hostProvider` - the list of ZooKeeper servers to connect to
  `sessionTimeout` - the timeout for connections.
  `clientConfig` - the client configuration.
  `defaultWatcher` - default watcher for this connection
  `clientCnxnSocket` - the socket implementation used (e.g. NIO/Netty)
  `canBeReadOnly` - whether the connection is allowed to go to read-only mode in case of partitioning
Throws:
  `IOException`

### ClientCnxn

public ClientCnxn(String chrootPath, HostProvider hostProvider, int sessionTimeout, ZKClientConfig clientConfig, Watcher defaultWatcher, org.apache.zookeeper.ClientCnxnSocket clientCnxnSocket, long sessionId, byte[] sessionPasswd, boolean canBeReadOnly) throws IOException
Creates a connection object. The actual network connect doesn't get established until needed. The start() instance method must be called subsequent to construction.

Parameters:
  `chrootPath` - the chroot of this client. Should be removed from this Class in ZOOKEEPER-838
  `hostProvider` - the list of ZooKeeper servers to connect to
  `sessionTimeout` - the timeout for connections.
  `clientConfig` - the client configuration.
  `defaultWatcher` - default watcher for this connection
  `clientCnxnSocket` - the socket implementation used (e.g. NIO/Netty)
  `sessionId` - session id if re-establishing session
  `sessionPasswd` - session passwd if re-establishing session
  `canBeReadOnly` - whether the connection is allowed to go to read-only mode in case of partitioning
Throws:
  `IOException` - in cases of broken network

## Method Details

### getSessionId

public long getSessionId()

### getSessionPasswd

public byte[] getSessionPasswd()

### getSessionTimeout

public int getSessionTimeout()

### toString

public String toString()

Overrides:
  `toString` in class `Object`

### start

public void start()

### isInEventThread

public static boolean isInEventThread()
Tests that current thread is the main event loop. This method is useful only for tests inside ZooKeeper project it is not a public API intended for use by external applications.

Returns:
  true if Thread.currentThread() is an EventThread.

### finishPacket

protected void finishPacket(org.apache.zookeeper.ClientCnxn.Packet p)

### onConnecting

protected void onConnecting(InetSocketAddress addr)

### getLastZxid

public long getLastZxid()

### disconnect

public void disconnect()
Shutdown the send/event threads. This method should not be called directly - rather it should be called as part of close operation. This method is primarily here to allow the tests to verify disconnection behavior.

### close

public void close() throws IOException
Close the connection, which includes; send session disconnect to the server, shutdown the send/event threads.

Throws:
  `IOException`

### getXid

public int getXid()

### submitRequest

public ReplyHeader submitRequest(RequestHeader h, Record request, Record response, ZooKeeper.WatchRegistration watchRegistration) throws InterruptedException

Throws:
  `InterruptedException`

### submitRequest

public ReplyHeader submitRequest(RequestHeader h, Record request, Record response, ZooKeeper.WatchRegistration watchRegistration, WatchDeregistration watchDeregistration) throws InterruptedException

Throws:
  `InterruptedException`

### saslCompleted

public void saslCompleted()

### sendPacket

public void sendPacket(Record request, Record response, AsyncCallback cb, int opCode) throws IOException

Throws:
  `IOException`

### queuePacket

public org.apache.zookeeper.ClientCnxn.Packet queuePacket(RequestHeader h, ReplyHeader r, Record request, Record response, AsyncCallback cb, String clientPath, String serverPath, Object ctx, ZooKeeper.WatchRegistration watchRegistration)

### queuePacket

public org.apache.zookeeper.ClientCnxn.Packet queuePacket(RequestHeader h, ReplyHeader r, Record request, Record response, AsyncCallback cb, String clientPath, String serverPath, Object ctx, ZooKeeper.WatchRegistration watchRegistration, WatchDeregistration watchDeregistration)

### addAuthInfo

public void addAuthInfo(String scheme, byte[] auth)

### getZooKeeperSaslClient

public ZooKeeperSaslClient getZooKeeperSaslClient()