ClientCnxnSocket.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.zookeeper;
  19. import java.io.IOException;
  20. import java.net.InetSocketAddress;
  21. import java.net.SocketAddress;
  22. import java.nio.ByteBuffer;
  23. import java.util.LinkedList;
  24. import java.util.List;
  25. import org.apache.jute.BinaryInputArchive;
  26. import org.apache.log4j.Logger;
  27. import org.apache.zookeeper.ClientCnxn.Packet;
  28. import org.apache.zookeeper.proto.ConnectResponse;
  29. import org.apache.zookeeper.server.ByteBufferInputStream;
  30. /**
  31. * A ClientCnxnSocket does the lower level communication with a socket
  32. * implementation.
  33. *
  34. * This code has been moved out of ClientCnxn so that a Netty implementation can
  35. * be provided as an alternative to the NIO socket code.
  36. *
  37. */
  38. abstract class ClientCnxnSocket {
  39. private static final Logger LOG = Logger.getLogger(ClientCnxnSocket.class);
  40. protected boolean initialized;
  41. /**
  42. * This buffer is only used to read the length of the incoming message.
  43. */
  44. protected final ByteBuffer lenBuffer = ByteBuffer.allocateDirect(4);
  45. /**
  46. * After the length is read, a new incomingBuffer is allocated in
  47. * readLength() to receive the full message.
  48. */
  49. protected ByteBuffer incomingBuffer = lenBuffer;
  50. protected long sentCount = 0;
  51. protected long recvCount = 0;
  52. protected long lastHeard;
  53. protected long lastSend;
  54. protected long now;
  55. protected ClientCnxn.SendThread sendThread;
  56. protected long sessionId;
  57. void introduce(ClientCnxn.SendThread sendThread, long sessionId) {
  58. this.sendThread = sendThread;
  59. this.sessionId = sessionId;
  60. }
  61. void updateNow() {
  62. now = System.currentTimeMillis();
  63. }
  64. int getIdleRecv() {
  65. return (int) (now - lastHeard);
  66. }
  67. int getIdleSend() {
  68. return (int) (now - lastSend);
  69. }
  70. long getSentCount() {
  71. return sentCount;
  72. }
  73. long getRecvCount() {
  74. return recvCount;
  75. }
  76. void updateLastHeard() {
  77. this.lastHeard = now;
  78. }
  79. void updateLastSend() {
  80. this.lastSend = now;
  81. }
  82. void updateLastSendAndHeard() {
  83. this.lastSend = now;
  84. this.lastHeard = now;
  85. }
  86. protected void readLength() throws IOException {
  87. int len = incomingBuffer.getInt();
  88. if (len < 0 || len >= ClientCnxn.packetLen) {
  89. throw new IOException("Packet len" + len + " is out of range!");
  90. }
  91. incomingBuffer = ByteBuffer.allocate(len);
  92. }
  93. void readConnectResult() throws IOException {
  94. if (LOG.isTraceEnabled()) {
  95. StringBuffer buf = new StringBuffer("0x[");
  96. for (byte b : incomingBuffer.array()) {
  97. buf.append(Integer.toHexString(b) + ",");
  98. }
  99. buf.append("]");
  100. LOG.trace("readConnectRestult " + incomingBuffer.remaining() + " "
  101. + buf.toString());
  102. }
  103. ByteBufferInputStream bbis = new ByteBufferInputStream(incomingBuffer);
  104. BinaryInputArchive bbia = BinaryInputArchive.getArchive(bbis);
  105. ConnectResponse conRsp = new ConnectResponse();
  106. conRsp.deserialize(bbia, "connect");
  107. this.sessionId = conRsp.getSessionId();
  108. sendThread.onConnected(conRsp.getTimeOut(), this.sessionId,
  109. conRsp.getPasswd());
  110. }
  111. abstract boolean isConnected();
  112. abstract void connect(InetSocketAddress addr) throws IOException;
  113. abstract SocketAddress getRemoteSocketAddress();
  114. abstract SocketAddress getLocalSocketAddress();
  115. abstract void cleanup();
  116. abstract void close();
  117. abstract void wakeupCnxn();
  118. abstract void enableWrite();
  119. abstract void enableReadWriteOnly();
  120. abstract void doTransport(int waitTimeOut, List<Packet> pendingQueue,
  121. LinkedList<Packet> outgoingQueue) throws IOException,
  122. InterruptedException;
  123. abstract void testableCloseSocket() throws IOException;
  124. }