Esempio n. 1
0
  /** {@inheritDoc} */
  @Nullable
  @Override
  public GridClientMessage decode(GridNioSession ses, ByteBuffer buf)
      throws IOException, GridException {
    ParserState state = ses.removeMeta(PARSER_STATE_META_NAME);

    if (state == null) state = new ParserState();

    PacketType type = state.packetType();

    if (type == null) {
      byte hdr = buf.get(buf.position());

      switch (hdr) {
        case MEMCACHE_REQ_FLAG:
          state.packet(new GridTcpRestPacket());
          state.packetType(PacketType.MEMCACHE);

          break;
        case GRIDGAIN_REQ_FLAG:
          // Skip header.
          buf.get();

          state.packetType(PacketType.GRIDGAIN);

          break;

        default:
          throw new IOException(
              "Failed to parse incoming packet (invalid packet start) [ses="
                  + ses
                  + ", b="
                  + Integer.toHexString(hdr & 0xFF)
                  + ']');
      }
    }

    GridClientMessage result = null;

    switch (state.packetType()) {
      case MEMCACHE:
        result = parseMemcachePacket(ses, buf, state);

        break;
      case GRIDGAIN:
        result = parseCustomPacket(ses, buf, state);

        break;
    }

    if (result == null)
      // Packet was not fully parsed yet.
      ses.addMeta(PARSER_STATE_META_NAME, state);

    return result;
  }
Esempio n. 2
0
  /** {@inheritDoc} */
  @Nullable
  @Override
  public Object decode(GridNioSession ses, ByteBuffer buf) throws IOException, GridException {
    GridTcpCommunicationMessageAdapter msg = ses.removeMeta(MSG_META_KEY);
    UUID nodeId = ses.meta(GridNioServer.DIFF_VER_NODE_ID_META_KEY);

    if (msg == null && buf.hasRemaining())
      msg = GridTcpCommunicationMessageFactory.create(buf.get());

    boolean finished = false;

    if (buf.hasRemaining()) finished = msgReader.read(nodeId, msg, buf);

    if (finished) return msg;
    else {
      ses.addMeta(MSG_META_KEY, msg);

      return null;
    }
  }