123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.apache.zookeeper;
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.net.SocketAddress;
- import java.nio.ByteBuffer;
- import java.util.LinkedList;
- import java.util.List;
- import org.apache.jute.BinaryInputArchive;
- import org.apache.log4j.Logger;
- import org.apache.zookeeper.ClientCnxn.Packet;
- import org.apache.zookeeper.proto.ConnectResponse;
- import org.apache.zookeeper.server.ByteBufferInputStream;
- /**
- * A ClientCnxnSocket does the lower level communication with a socket
- * implementation.
- *
- * This code has been moved out of ClientCnxn so that a Netty implementation can
- * be provided as an alternative to the NIO socket code.
- *
- */
- abstract class ClientCnxnSocket {
- private static final Logger LOG = Logger.getLogger(ClientCnxnSocket.class);
- protected boolean initialized;
- /**
- * This buffer is only used to read the length of the incoming message.
- */
- protected final ByteBuffer lenBuffer = ByteBuffer.allocateDirect(4);
- /**
- * After the length is read, a new incomingBuffer is allocated in
- * readLength() to receive the full message.
- */
- protected ByteBuffer incomingBuffer = lenBuffer;
- protected long sentCount = 0;
- protected long recvCount = 0;
- protected long lastHeard;
- protected long lastSend;
- protected long now;
- protected ClientCnxn.SendThread sendThread;
- protected long sessionId;
- void introduce(ClientCnxn.SendThread sendThread, long sessionId) {
- this.sendThread = sendThread;
- this.sessionId = sessionId;
- }
- void updateNow() {
- now = System.currentTimeMillis();
- }
- int getIdleRecv() {
- return (int) (now - lastHeard);
- }
- int getIdleSend() {
- return (int) (now - lastSend);
- }
- long getSentCount() {
- return sentCount;
- }
- long getRecvCount() {
- return recvCount;
- }
- void updateLastHeard() {
- this.lastHeard = now;
- }
- void updateLastSend() {
- this.lastSend = now;
- }
- void updateLastSendAndHeard() {
- this.lastSend = now;
- this.lastHeard = now;
- }
- protected void readLength() throws IOException {
- int len = incomingBuffer.getInt();
- if (len < 0 || len >= ClientCnxn.packetLen) {
- throw new IOException("Packet len" + len + " is out of range!");
- }
- incomingBuffer = ByteBuffer.allocate(len);
- }
- void readConnectResult() throws IOException {
- if (LOG.isTraceEnabled()) {
- StringBuffer buf = new StringBuffer("0x[");
- for (byte b : incomingBuffer.array()) {
- buf.append(Integer.toHexString(b) + ",");
- }
- buf.append("]");
- LOG.trace("readConnectRestult " + incomingBuffer.remaining() + " "
- + buf.toString());
- }
- ByteBufferInputStream bbis = new ByteBufferInputStream(incomingBuffer);
- BinaryInputArchive bbia = BinaryInputArchive.getArchive(bbis);
- ConnectResponse conRsp = new ConnectResponse();
- conRsp.deserialize(bbia, "connect");
- this.sessionId = conRsp.getSessionId();
- sendThread.onConnected(conRsp.getTimeOut(), this.sessionId,
- conRsp.getPasswd());
- }
- abstract boolean isConnected();
- abstract void connect(InetSocketAddress addr) throws IOException;
- abstract SocketAddress getRemoteSocketAddress();
- abstract SocketAddress getLocalSocketAddress();
- abstract void cleanup();
- abstract void close();
- abstract void wakeupCnxn();
- abstract void enableWrite();
- abstract void enableReadWriteOnly();
- abstract void doTransport(int waitTimeOut, List<Packet> pendingQueue,
- LinkedList<Packet> outgoingQueue) throws IOException,
- InterruptedException;
- abstract void testableCloseSocket() throws IOException;
- }
|