use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class TcpRestParserSelfTest method testSimplePacketParsing.
/**
* @throws Exception If failed.
*/
@Test
public void testSimplePacketParsing() throws Exception {
GridNioSession ses = new MockNioSession();
GridTcpRestParser parser = new GridTcpRestParser(false);
byte hdr = MEMCACHE_REQ_FLAG;
byte[] opCodes = { 0x01, 0x02, 0x03 };
byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
String key = "key";
String val = "value";
for (byte opCode : opCodes) {
ByteBuffer raw = rawPacket(hdr, opCode, opaque, key.getBytes(), val.getBytes(), EXTRAS);
GridClientMessage msg = parser.decode(ses, raw);
assertTrue(msg instanceof GridMemcachedMessage);
GridMemcachedMessage packet = (GridMemcachedMessage) msg;
assertEquals("Parser leaved unparsed bytes", 0, raw.remaining());
assertEquals("Invalid opcode", opCode, packet.operationCode());
assertEquals("Invalid key", key, packet.key());
assertEquals("Invalid value", val, packet.value());
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class TcpRestParserSelfTest method testMixedParsing.
/**
* @throws Exception If failed.
*/
@Test
public void testMixedParsing() throws Exception {
GridNioSession ses1 = new MockNioSession();
GridNioSession ses2 = new MockNioSession();
ses1.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
ses2.addMeta(MARSHALLER.ordinal(), new GridClientOptimizedMarshaller());
GridTcpRestParser parser = new GridTcpRestParser(false);
GridClientCacheRequest req = new GridClientCacheRequest(CAS);
req.key("key");
String val = "value";
req.value(val);
req.value2(val);
req.clientId(UUID.randomUUID());
byte[] opaque = new byte[] { 0x01, 0x02, 0x03, (byte) 0xFF };
String key = "key";
ByteBuffer raw1 = rawPacket(MEMCACHE_REQ_FLAG, (byte) 0x01, opaque, key.getBytes(), val.getBytes(), EXTRAS);
ByteBuffer raw2 = clientRequestPacket(req);
raw1.mark();
raw2.mark();
int splits = Math.min(raw1.remaining(), raw2.remaining());
for (int i = 1; i < splits; i++) {
ByteBuffer[] packet1 = split(raw1, i);
ByteBuffer[] packet2 = split(raw2, i);
GridClientMessage msg = parser.decode(ses1, packet1[0]);
assertNull(msg);
msg = parser.decode(ses2, packet2[0]);
assertNull(msg);
msg = parser.decode(ses1, packet1[1]);
assertTrue(msg instanceof GridMemcachedMessage);
assertEquals(key, ((GridMemcachedMessage) msg).key());
assertEquals(val, ((GridMemcachedMessage) msg).value());
msg = parser.decode(ses2, packet2[1]);
assertTrue(msg instanceof GridClientCacheRequest);
assertEquals(val, ((GridClientCacheRequest) msg).value());
assertEquals(val, ((GridClientCacheRequest) msg).value2());
raw1.reset();
raw2.reset();
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class ClientTestRestServer method start.
/**
* Starts the server.
*
* @throws IgniteCheckedException If failed.
*/
public void start() throws IgniteCheckedException {
try {
String igniteInstanceName = "test";
srv = GridNioServer.<GridClientMessage>builder().address(InetAddress.getByName("127.0.0.1")).port(port).listener(new TestListener()).logger(log).selectorCount(2).igniteInstanceName(igniteInstanceName).byteOrder(ByteOrder.nativeOrder()).tcpNoDelay(true).directBuffer(false).filters(new GridNioAsyncNotifyFilter(igniteInstanceName, Executors.newFixedThreadPool(2), log), new GridNioCodecFilter(new TestParser(), log, false)).build();
} catch (UnknownHostException e) {
throw new IgniteCheckedException("Failed to determine localhost address.", e);
}
srv.start();
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage 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.GridClientMessage in project ignite by apache.
the class GridClientNioTcpConnection method handleClientResponse.
/**
* Handler responses addressed to this client.
*
* @param fut Response future.
* @param resp Response.
*/
@SuppressWarnings("unchecked")
private void handleClientResponse(TcpClientFuture fut, GridClientResponse resp) {
if (resp.sessionToken() != null)
sesTok = resp.sessionToken();
GridClientMessage src = fut.pendingMessage();
switch(fut.retryState()) {
case TcpClientFuture.STATE_INITIAL:
{
if (resp.successStatus() == GridClientResponse.STATUS_AUTH_FAILURE) {
if (credentials() == null) {
fut.onDone(new GridClientAuthenticationException("Authentication failed on server " + "(client has no credentials) [clientId=" + clientId + ", srvAddr=" + serverAddress() + ", errMsg=" + resp.errorMessage() + ']'));
removePending(resp.requestId());
return;
}
fut.retryState(TcpClientFuture.STATE_AUTH_RETRY);
GridClientAuthenticationRequest req = buildAuthRequest();
req.requestId(resp.requestId());
ses.send(req);
return;
}
break;
}
case TcpClientFuture.STATE_AUTH_RETRY:
{
if (resp.successStatus() == GridClientResponse.STATUS_SUCCESS) {
fut.retryState(TcpClientFuture.STATE_REQUEST_RETRY);
src.sessionToken(sesTok);
ses.send(src);
return;
}
break;
}
}
removePending(resp.requestId());
if (resp.successStatus() == GridClientResponse.STATUS_AUTH_FAILURE)
fut.onDone(new GridClientAuthenticationException("Client authentication failed [clientId=" + clientId + ", srvAddr=" + serverAddress() + ", errMsg=" + resp.errorMessage() + ']'));
else if (resp.successStatus() == GridClientResponse.STATUS_ILLEGAL_ARGUMENT)
fut.onDone(new IllegalArgumentException(resp.errorMessage()));
else if (resp.errorMessage() != null)
fut.onDone(new GridClientException(resp.errorMessage()));
else
fut.onDone(resp.result());
}
Aggregations