Search in sources :

Example 6 with GridClientMessage

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());
    }
}
Also used : MockNioSession(org.apache.ignite.internal.util.nio.impl.MockNioSession) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) 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 7 with GridClientMessage

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();
    }
}
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 8 with GridClientMessage

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();
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UnknownHostException(java.net.UnknownHostException) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage) GridNioAsyncNotifyFilter(org.apache.ignite.internal.util.nio.GridNioAsyncNotifyFilter)

Example 9 with GridClientMessage

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

Example 10 with GridClientMessage

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());
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridClientAuthenticationRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientAuthenticationRequest) GridClientAuthenticationException(org.apache.ignite.internal.client.GridClientAuthenticationException) 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