Search in sources :

Example 1 with ConnectResponse

use of org.apache.zookeeper.proto.ConnectResponse in project zookeeper by apache.

the class ZooKeeperServer method finishSessionInit.

public void finishSessionInit(ServerCnxn cnxn, boolean valid) {
    // register with JMX
    try {
        if (valid) {
            if (serverCnxnFactory != null && serverCnxnFactory.cnxns.contains(cnxn)) {
                serverCnxnFactory.registerConnection(cnxn);
            } else if (secureServerCnxnFactory != null && secureServerCnxnFactory.cnxns.contains(cnxn)) {
                secureServerCnxnFactory.registerConnection(cnxn);
            }
        }
    } catch (Exception e) {
        LOG.warn("Failed to register with JMX", e);
    }
    try {
        ConnectResponse rsp = new ConnectResponse(0, valid ? cnxn.getSessionTimeout() : 0, // send 0 if session is no
        valid ? cnxn.getSessionId() : 0, // longer valid
        valid ? generatePasswd(cnxn.getSessionId()) : new byte[16]);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BinaryOutputArchive bos = BinaryOutputArchive.getArchive(baos);
        bos.writeInt(-1, "len");
        rsp.serialize(bos, "connect");
        if (!cnxn.isOldClient) {
            bos.writeBool(this instanceof ReadOnlyZooKeeperServer, "readOnly");
        }
        baos.close();
        ByteBuffer bb = ByteBuffer.wrap(baos.toByteArray());
        bb.putInt(bb.remaining() - 4).rewind();
        cnxn.sendBuffer(bb);
        if (valid) {
            LOG.debug("Established session 0x{} with negotiated timeout {} for client {}", Long.toHexString(cnxn.getSessionId()), cnxn.getSessionTimeout(), cnxn.getRemoteSocketAddress());
            cnxn.enableRecv();
        } else {
            LOG.info("Invalid session 0x{} for client {}, probably expired", Long.toHexString(cnxn.getSessionId()), cnxn.getRemoteSocketAddress());
            cnxn.sendBuffer(ServerCnxnFactory.closeConn);
        }
    } catch (Exception e) {
        LOG.warn("Exception while establishing session, closing", e);
        cnxn.close(ServerCnxn.DisconnectReason.IO_EXCEPTION_IN_SESSION_INIT);
    }
}
Also used : BinaryOutputArchive(org.apache.jute.BinaryOutputArchive) ConnectResponse(org.apache.zookeeper.proto.ConnectResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) SaslException(javax.security.sasl.SaslException) RequestProcessorException(org.apache.zookeeper.server.RequestProcessor.RequestProcessorException) SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) CloseRequestException(org.apache.zookeeper.server.ServerCnxn.CloseRequestException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ReadOnlyZooKeeperServer(org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer)

Example 2 with ConnectResponse

use of org.apache.zookeeper.proto.ConnectResponse in project zookeeper by apache.

the class ClientCnxnSocket method readConnectResult.

void readConnectResult() throws IOException {
    if (LOG.isTraceEnabled()) {
        StringBuilder buf = new StringBuilder("0x[");
        for (byte b : incomingBuffer.array()) {
            buf.append(Integer.toHexString(b)).append(",");
        }
        buf.append("]");
        if (LOG.isTraceEnabled()) {
            LOG.trace("readConnectResult {} {}", incomingBuffer.remaining(), buf.toString());
        }
    }
    ByteBufferInputStream bbis = new ByteBufferInputStream(incomingBuffer);
    BinaryInputArchive bbia = BinaryInputArchive.getArchive(bbis);
    ConnectResponse conRsp = new ConnectResponse();
    conRsp.deserialize(bbia, "connect");
    // read "is read-only" flag
    boolean isRO = false;
    try {
        isRO = bbia.readBool("readOnly");
    } catch (IOException e) {
        // this is ok -- just a packet from an old server which
        // doesn't contain readOnly field
        LOG.warn("Connected to an old server; r-o mode will be unavailable");
    }
    this.sessionId = conRsp.getSessionId();
    sendThread.onConnected(conRsp.getTimeOut(), this.sessionId, conRsp.getPasswd(), isRO);
}
Also used : BinaryInputArchive(org.apache.jute.BinaryInputArchive) ConnectResponse(org.apache.zookeeper.proto.ConnectResponse) ByteBufferInputStream(org.apache.zookeeper.server.ByteBufferInputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ConnectResponse (org.apache.zookeeper.proto.ConnectResponse)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ByteBuffer (java.nio.ByteBuffer)1 SaslException (javax.security.sasl.SaslException)1 BinaryInputArchive (org.apache.jute.BinaryInputArchive)1 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)1 KeeperException (org.apache.zookeeper.KeeperException)1 SessionExpiredException (org.apache.zookeeper.KeeperException.SessionExpiredException)1 ByteBufferInputStream (org.apache.zookeeper.server.ByteBufferInputStream)1 RequestProcessorException (org.apache.zookeeper.server.RequestProcessor.RequestProcessorException)1 CloseRequestException (org.apache.zookeeper.server.ServerCnxn.CloseRequestException)1 ReadOnlyZooKeeperServer (org.apache.zookeeper.server.quorum.ReadOnlyZooKeeperServer)1