Search in sources :

Example 1 with GridClientHandshakeResponse

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);
}
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 2 with GridClientHandshakeResponse

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;
}
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)

Aggregations

GridClientHandshakeResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse)2 GridClientMessage (org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 GridClientMarshaller (org.apache.ignite.internal.client.marshaller.GridClientMarshaller)1 GridClientPingPacket (org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket)1 GridClientResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)1 GridRouterResponse (org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse)1 Nullable (org.jetbrains.annotations.Nullable)1