use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class TcpRestParserSelfTest method testParseClientHandshake.
/**
* Tests correct parsing of client handshake packets.
*
* @throws Exception If failed.
*/
@Test
public void testParseClientHandshake() throws Exception {
for (int splitPos = 1; splitPos < 5; splitPos++) {
log.info("Checking split position: " + splitPos);
ByteBuffer tmp = clientHandshakePacket();
ByteBuffer[] split = split(tmp, splitPos);
GridNioSession ses = new MockNioSession();
ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
GridTcpRestParser parser = new GridTcpRestParser(false);
Collection<GridClientMessage> lst = new ArrayList<>(1);
for (ByteBuffer buf : split) {
GridClientMessage r;
while (buf.hasRemaining() && (r = parser.decode(ses, buf)) != null) lst.add(r);
assertTrue("Parser has left unparsed bytes.", buf.remaining() == 0);
}
assertEquals(1, lst.size());
GridClientHandshakeRequest req = (GridClientHandshakeRequest) F.first(lst);
assertNotNull(req);
assertEquals(U.bytesToShort(new byte[] { 5, 0 }, 0), req.version());
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class TcpRestParserSelfTest method testCustomMessages.
/**
* @throws Exception If failed.
*/
@Test
public void testCustomMessages() throws Exception {
GridClientCacheRequest req = new GridClientCacheRequest(CAS);
req.key("key");
req.value(1);
req.value2(2);
req.clientId(UUID.randomUUID());
ByteBuffer raw = clientRequestPacket(req);
GridNioSession ses = new MockNioSession();
ses.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
GridTcpRestParser parser = new GridTcpRestParser(false);
GridClientMessage msg = parser.decode(ses, raw);
assertNotNull(msg);
assertEquals("Parser leaved unparsed bytes", 0, raw.remaining());
assertTrue(msg instanceof GridClientCacheRequest);
GridClientCacheRequest res = (GridClientCacheRequest) msg;
assertEquals("Invalid operation", req.operation(), res.operation());
assertEquals("Invalid clientId", req.clientId(), res.clientId());
assertEquals("Invalid key", req.key(), res.key());
assertEquals("Invalid value 1", req.value(), res.value());
assertEquals("Invalid value 2", req.value2(), res.value2());
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage 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;
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class GridTcpRestParser method parseClientMessage.
/**
* Parses {@link GridClientMessage} from raw bytes.
*
* @param ses Session.
* @param state Parser state.
* @return A parsed client message.
* @throws IOException On marshaller error.
* @throws IgniteCheckedException If no marshaller was defined for the session.
*/
protected GridClientMessage parseClientMessage(GridNioSession ses, ParserState state) throws IOException, IgniteCheckedException {
GridClientMessage msg;
if (routerClient) {
msg = new GridRouterResponse(state.buffer().toByteArray(), state.header().reqId(), state.header().clientId(), state.header().destinationId());
} else {
GridClientMarshaller marsh = marshaller(ses);
msg = marsh.unmarshal(state.buffer().toByteArray());
msg.requestId(state.header().reqId());
msg.clientId(state.header().clientId());
msg.destinationId(state.header().destinationId());
}
return msg;
}
Aggregations