Beispiel #1
0
  /**
   * Updates the CRC value based on Packet p.
   *
   * @param p The packet which is used to update the CRC.
   */
  public void updateCRC(Packet p) {
    List<Integer> data = p.getData();
    if (data.size() == 0) { // If there isn't any data, we don't need to update the CRC
      return;
    }

    int regAddress;
    if (p.getPacketType() == PacketType.ONE) {
      RegisterType regType = p.getRegType();
      if (regType == RegisterType.NONE) { // Invalid register type
        return;
      } else if (regType == RegisterType.CMD && data.get(0) == 0x00000007) { // RCRC command
        crcValue = 0;
        return;
      }
      regAddress = regType.Address();
    } else regAddress = 0x00000002; // If type 2, we will assume FDRI

    for (int d : data) {
      for (int i = 0; i < 32; i++) { // Shift in the data one bit at a time
        shiftIn_OneBit(d >> i);
      }
      for (int i = 0; i < 5; i++) { // Shift in the reg address one bit at a time
        shiftIn_OneBit(regAddress >> i);
      }
    }
  } // end UpdateCRC
  @Override
  public void run() {
    while (listening) {
      Packet packet = null;
      try {
        packet = socket.receive();
      } catch (InterruptedException e) {
        e.printStackTrace();
        continue;
      }

      Message message = new Gson().fromJson(new String(packet.getData()), Message.class);

      String packetData = new String(packet.getData());
      switch (message.getType()) {
        case NickChangeMessage.TYPE:
          NickChangeMessage nickChangeMessage =
              new Gson().fromJson(packetData, NickChangeMessage.class);
          state.addUser(nickChangeMessage, packet.getSourceAddress());
          break;
        case TextMessage.TYPE:
          TextMessage textMessage = new Gson().fromJson(packetData, TextMessage.class);
          state.receiveMessage(textMessage, packet.getSourceAddress());
          break;
        case LeaveMessage.TYPE:
          LeaveMessage leaveMessage = new Gson().fromJson(packetData, LeaveMessage.class);
          state.userLeft(leaveMessage);
          break;
        case InviteMessage.TYPE:
          InviteMessage inviteMessage = new Gson().fromJson(packetData, InviteMessage.class);
          state.invite(inviteMessage);
          break;
        case RequestNickMessage.TYPE:
          RequestNickMessage requestNickMessage =
              new Gson().fromJson(packetData, RequestNickMessage.class);
          state.requestNick(requestNickMessage, packet.getSourceAddress());
          break;
        case LeaveConversationMessage.TYPE:
          LeaveConversationMessage leaveConversationMessage =
              new Gson().fromJson(packetData, LeaveConversationMessage.class);
          state.leaveConversation(leaveConversationMessage);
          break;
      }
    }
  }
  public void encodePacket(
      Packet packet, ByteBuf buffer, ByteBufAllocator allocator, boolean binary)
      throws IOException {
    ByteBuf buf = buffer;
    if (!binary) {
      buf = allocateBuffer(allocator);
    }
    byte type = toChar(packet.getType().getValue());
    buf.writeByte(type);

    try {
      switch (packet.getType()) {
        case PONG:
          {
            buf.writeBytes(packet.getData().toString().getBytes(CharsetUtil.UTF_8));
            break;
          }

        case OPEN:
          {
            ByteBufOutputStream out = new ByteBufOutputStream(buf);
            jsonSupport.writeValue(out, packet.getData());
            break;
          }

        case MESSAGE:
          {
            ByteBuf encBuf = null;

            if (packet.getSubType() == PacketType.ERROR) {
              encBuf = allocateBuffer(allocator);

              ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
              jsonSupport.writeValue(out, packet.getData());
            }

            if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.ACK) {

              List<Object> values = new ArrayList<Object>();
              if (packet.getSubType() == PacketType.EVENT) {
                values.add(packet.getName());
              }

              encBuf = allocateBuffer(allocator);

              List<Object> args = packet.getData();
              values.addAll(args);
              ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
              jsonSupport.writeValue(out, values);

              if (!jsonSupport.getArrays().isEmpty()) {
                packet.initAttachments(jsonSupport.getArrays().size());
                for (byte[] array : jsonSupport.getArrays()) {
                  packet.addAttachment(Unpooled.wrappedBuffer(array));
                }
                packet.setSubType(PacketType.BINARY_EVENT);
              }
            }

            byte subType = toChar(packet.getSubType().getValue());
            buf.writeByte(subType);

            if (packet.hasAttachments()) {
              byte[] ackId = toChars(packet.getAttachments().size());
              buf.writeBytes(ackId);
              buf.writeByte('-');
            }

            if (packet.getSubType() == PacketType.CONNECT) {
              if (!packet.getNsp().isEmpty()) {
                buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
              }
            } else {
              if (!packet.getNsp().isEmpty()) {
                buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
                buf.writeByte(',');
              }
            }

            if (packet.getAckId() != null) {
              byte[] ackId = toChars(packet.getAckId());
              buf.writeBytes(ackId);
            }

            if (encBuf != null) {
              buf.writeBytes(encBuf);
              encBuf.release();
            }

            break;
          }
      }
    } finally {
      // we need to write a buffer in any case
      if (!binary) {
        buffer.writeByte(0);
        int length = buf.writerIndex();
        buffer.writeBytes(longToBytes(length));
        buffer.writeByte(0xff);
        buffer.writeBytes(buf);

        buf.release();
      }
    }
  }