@Override
  public void handle(Packet packet) throws Exception {
    assert packet.isHeaderSet(Packet.HEADER_BIND);

    BindMessage bind = ioService.getSerializationService().toObject(packet);
    bind(
        (TcpIpConnection) packet.getConn(),
        bind.getLocalAddress(),
        bind.getTargetAddress(),
        bind.shouldReply());
  }
 @Override
 public void handlePacket(Packet packet) {
   final ClientConnection conn = (ClientConnection) packet.getConn();
   conn.incrementPacketCount();
   if (packet.isHeaderSet(Packet.HEADER_EVENT)) {
     final ClientListenerServiceImpl listenerService =
         (ClientListenerServiceImpl) client.getListenerService();
     listenerService.handleEventPacket(packet);
   } else {
     invocationService.handlePacket(packet);
   }
 }
 public boolean write(SocketWritable socketWritable) {
   final Packet packet = (Packet) socketWritable;
   if (nodeEngine.getNode().isActive()) {
     packet.setConn(thisConnection);
     MemberImpl member = nodeEngine.getClusterService().getMember(thisAddress);
     if (member != null) {
       member.didRead();
     }
     nodeEngine.handlePacket(packet);
     return true;
   }
   return false;
 }
 void sendBindRequest(TcpIpConnection connection, Address remoteEndPoint, boolean replyBack) {
   connection.setEndPoint(remoteEndPoint);
   ioService.onSuccessfulConnection(remoteEndPoint);
   // make sure bind packet is the first packet sent to the end point.
   if (logger.isFinestEnabled()) {
     logger.finest("Sending bind packet to " + remoteEndPoint);
   }
   BindMessage bind = new BindMessage(ioService.getThisAddress(), remoteEndPoint, replyBack);
   byte[] bytes = ioService.getSerializationService().toBytes(bind);
   Packet packet = new Packet(bytes);
   packet.setHeader(Packet.HEADER_BIND);
   connection.write(packet);
   // now you can send anything...
 }
  @Test
  public void whenNoProblemPacket() throws Exception {
    final Packet packet =
        new Packet(serializationService.toBytes(new NormalResponse("foo", 1, 0, false)));
    packet.setFlag(FLAG_OP);
    packet.setFlag(FLAG_RESPONSE);
    asyncHandler.handle(packet);

    assertTrueEventually(
        new AssertTask() {
          @Override
          public void run() throws Exception {
            verify(responsePacketHandler).handle(packet);
          }
        });
  }
 public void execute(Packet packet) {
   try {
     if (packet.isHeaderSet(Packet.HEADER_RESPONSE)) {
       // it is an response packet.
       responseThread.workQueue.add(packet);
     } else {
       // it is an must be an operation packet
       int partitionId = packet.getPartitionId();
       boolean hasPriority = packet.isUrgent();
       execute(packet, partitionId, hasPriority);
     }
   } catch (RejectedExecutionException e) {
     if (node.nodeEngine.isActive()) {
       throw e;
     }
   }
 }
  public boolean send(Response response, Address target) {
    checkNotNull(target, "Target is required!");

    if (thisAddress.equals(target)) {
      throw new IllegalArgumentException(
          "Target is this node! -> " + target + ", response: " + response);
    }

    byte[] bytes = serializationService.toBytes(response);
    Packet packet = new Packet(bytes, -1).setAllFlags(FLAG_OP | FLAG_RESPONSE);

    if (response.isUrgent()) {
      packet.setFlag(FLAG_URGENT);
    }

    ConnectionManager connectionManager = node.getConnectionManager();
    Connection connection = connectionManager.getOrConnect(target);
    return connectionManager.transmit(packet, connection);
  }
  @Test
  @Ignore // https://github.com/hazelcast/hazelcast/issues/7864
  public void whenShutdown() {
    asyncHandler.shutdown();

    final Packet packet =
        new Packet(serializationService.toBytes(new NormalResponse("foo", 1, 0, false)));
    packet.setFlag(FLAG_OP);
    packet.setFlag(FLAG_RESPONSE);
    asyncHandler.handle(packet);

    assertTrueFiveSeconds(
        new AssertTask() {
          @Override
          public void run() throws Exception {
            verifyZeroInteractions(responsePacketHandler);
          }
        });
  }
  @Override
  public boolean send(Operation op, Address target) {
    checkNotNull(target, "Target is required!");

    if (thisAddress.equals(target)) {
      throw new IllegalArgumentException("Target is this node! -> " + target + ", op: " + op);
    }

    byte[] bytes = serializationService.toBytes(op);
    int partitionId = op.getPartitionId();
    Packet packet = new Packet(bytes, partitionId).setFlag(FLAG_OP);

    if (op.isUrgent()) {
      packet.setFlag(FLAG_URGENT);
    }

    ConnectionManager connectionManager = node.getConnectionManager();
    Connection connection = connectionManager.getOrConnect(target);
    return connectionManager.transmit(packet, connection);
  }
 @Override
 public void handleMemberPacket(final Packet packet) {
   final Address endPoint = packet.getConn().getEndPoint();
   if (endPoint != null) {
     final MemberImpl member = node.clusterService.getMember(endPoint);
     if (member != null) {
       member.didRead();
     }
   }
   nodeEngine.handlePacket(packet);
 }
  @Test
  public void whenPacketThrowsException() throws Exception {
    final Packet badPacket =
        new Packet(serializationService.toBytes(new NormalResponse("bad", 1, 0, false)));
    badPacket.setFlag(FLAG_OP);
    badPacket.setFlag(FLAG_RESPONSE);

    final Packet goodPacket =
        new Packet(serializationService.toBytes(new NormalResponse("good", 1, 0, false)));
    goodPacket.setFlag(FLAG_OP);
    goodPacket.setFlag(FLAG_RESPONSE);

    doThrow(new ExpectedRuntimeException()).when(responsePacketHandler).handle(badPacket);

    asyncHandler.handle(badPacket);
    asyncHandler.handle(goodPacket);

    assertTrueEventually(
        new AssertTask() {
          @Override
          public void run() throws Exception {
            verify(responsePacketHandler).handle(goodPacket);
          }
        });
  }
    private Packet readFromPacket(Packet packet) {
      Packet newPacket = new Packet();
      ByteBuffer buffer = ByteBuffer.allocate(4096);
      boolean writeDone;
      boolean readDone;
      do {
        writeDone = packet.writeTo(buffer);
        buffer.flip();
        readDone = newPacket.readFrom(buffer);
        if (buffer.hasRemaining()) {
          throw new IllegalStateException("Buffer should be empty! " + buffer);
        }
        buffer.clear();
      } while (!writeDone);

      if (!readDone) {
        throw new IllegalStateException("Read should be completed!");
      }

      newPacket.setConn(localConnection);
      return newPacket;
    }