use of org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse in project ignite by apache.
the class GridTcpRouterNioParser method encode.
/** {@inheritDoc} */
@Override
public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException {
sndCnt++;
if (msg instanceof GridRouterResponse) {
GridRouterResponse resp = (GridRouterResponse) msg;
ByteBuffer res = ByteBuffer.allocate(resp.body().length + 45);
res.put(IGNITE_REQ_FLAG);
res.putInt(resp.body().length + 40);
res.putLong(resp.requestId());
res.put(U.uuidToBytes(resp.clientId()));
res.put(U.uuidToBytes(resp.destinationId()));
res.put(resp.body());
res.flip();
return res;
} else if (msg instanceof GridClientResponse) {
GridClientMarshaller marsh = marshaller(ses);
GridClientMessage clientMsg = (GridClientMessage) msg;
ByteBuffer res = marsh.marshal(msg, 45);
ByteBuffer slice = res.slice();
slice.put(IGNITE_REQ_FLAG);
slice.putInt(res.remaining() - 5);
slice.putLong(clientMsg.requestId());
slice.put(U.uuidToBytes(clientMsg.clientId()));
slice.put(U.uuidToBytes(clientMsg.destinationId()));
return res;
} else if (msg instanceof GridClientPingPacket || msg instanceof GridClientHandshakeResponse)
return super.encode(ses, msg);
else
throw new IgniteCheckedException("Unsupported message: " + msg);
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse in project ignite by apache.
the class GridTcpRestParser method decode.
/** {@inheritDoc} */
@Nullable
@Override
public GridClientMessage decode(GridNioSession ses, ByteBuffer buf) throws IOException, IgniteCheckedException {
ParserState state = ses.removeMeta(PARSER_STATE.ordinal());
if (state == null)
state = new ParserState();
GridClientPacketType type = state.packetType();
if (type == null) {
byte hdr = buf.get(buf.position());
switch(hdr) {
case MEMCACHE_REQ_FLAG:
state.packet(new GridMemcachedMessage());
state.packetType(GridClientPacketType.MEMCACHE);
break;
case RESP_REQ_FLAG:
state.packetType(GridClientPacketType.REDIS);
break;
case IGNITE_REQ_FLAG:
// Skip header.
buf.get();
state.packetType(GridClientPacketType.IGNITE);
break;
case IGNITE_HANDSHAKE_FLAG:
// Skip header.
buf.get();
state.packetType(GridClientPacketType.IGNITE_HANDSHAKE);
break;
case IGNITE_HANDSHAKE_RES_FLAG:
buf.get();
state.packetType(GridClientPacketType.IGNITE_HANDSHAKE_RES);
break;
default:
throw new IOException("Failed to parse incoming packet (invalid packet start) [ses=" + ses + ", b=" + Integer.toHexString(hdr & 0xFF) + ']');
}
}
GridClientMessage res = null;
switch(state.packetType()) {
case MEMCACHE:
res = parseMemcachePacket(ses, buf, state);
break;
case REDIS:
res = GridRedisProtocolParser.readArray(buf);
break;
case IGNITE_HANDSHAKE:
res = parseHandshake(buf, state);
break;
case IGNITE_HANDSHAKE_RES:
if (buf.hasRemaining())
res = new GridClientHandshakeResponse(buf.get());
break;
case IGNITE:
res = parseCustomPacket(ses, buf, state);
break;
}
if (res == null)
// Packet was not fully parsed yet.
ses.addMeta(PARSER_STATE.ordinal(), state);
return res;
}
Aggregations