Search in sources :

Example 11 with GridClientMessage

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());
    }
}
Also used : MockNioSession(org.apache.ignite.internal.util.nio.impl.MockNioSession) GridClientHandshakeRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridClientOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 12 with GridClientMessage

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());
}
Also used : MockNioSession(org.apache.ignite.internal.util.nio.impl.MockNioSession) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridClientOptimizedMarshaller(org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller) GridClientCacheRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 13 with GridClientMessage

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;
}
Also used : GridClientHandshakeResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse) IOException(java.io.IOException) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage) Nullable(org.jetbrains.annotations.Nullable)

Example 14 with GridClientMessage

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;
}
Also used : GridRouterResponse(org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Aggregations

GridClientMessage (org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)14 ByteBuffer (java.nio.ByteBuffer)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)4 MockNioSession (org.apache.ignite.internal.util.nio.impl.MockNioSession)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 GridClientOptimizedMarshaller (org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller)3 GridRouterResponse (org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse)3 UnknownHostException (java.net.UnknownHostException)2 GridClientException (org.apache.ignite.internal.client.GridClientException)2 GridClientMarshaller (org.apache.ignite.internal.client.marshaller.GridClientMarshaller)2 GridClientCacheRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest)2 GridClientHandshakeResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse)2 GridClientResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)2 GridNioCodecFilter (org.apache.ignite.internal.util.nio.GridNioCodecFilter)2 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 SSLContext (javax.net.ssl.SSLContext)1