Search in sources :

Example 1 with GridRedisMessage

use of org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage in project ignite by apache.

the class GridTcpRestNioListener method onMessage.

/** {@inheritDoc} */
@SuppressWarnings("ConstantConditions")
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
    if (msg instanceof GridMemcachedMessage)
        memcachedLsnr.onMessage(ses, (GridMemcachedMessage) msg);
    else if (msg instanceof GridRedisMessage)
        redisLsnr.onMessage(ses, (GridRedisMessage) msg);
    else {
        if (msg instanceof GridClientPingPacket)
            ses.send(msg);
        else if (msg instanceof GridClientHandshakeRequest) {
            GridClientHandshakeRequest hs = (GridClientHandshakeRequest) msg;
            short ver = hs.version();
            if (!SUPP_VERS.contains(ver)) {
                U.error(log, "Client protocol version is not supported [ses=" + ses + ", ver=" + ver + ", supported=" + SUPP_VERS + ']');
                ses.close();
            } else {
                byte marshId = hs.marshallerId();
                if (marshMapLatch.getCount() > 0)
                    U.awaitQuiet(marshMapLatch);
                GridClientMarshaller marsh = marshMap.get(marshId);
                if (marsh == null) {
                    U.error(log, "Client marshaller ID is invalid. Note that .NET and C++ clients " + "are supported only in enterprise edition [ses=" + ses + ", marshId=" + marshId + ']');
                    ses.close();
                } else {
                    ses.addMeta(MARSHALLER.ordinal(), marsh);
                    ses.send(GridClientHandshakeResponse.OK);
                }
            }
        } else {
            final GridRestRequest req = createRestRequest(ses, msg);
            if (req != null)
                hnd.handleAsync(req).listen(new CI1<IgniteInternalFuture<GridRestResponse>>() {

                    @Override
                    public void apply(IgniteInternalFuture<GridRestResponse> fut) {
                        GridClientResponse res = new GridClientResponse();
                        res.requestId(msg.requestId());
                        res.clientId(msg.clientId());
                        try {
                            GridRestResponse restRes = fut.get();
                            res.sessionToken(restRes.sessionTokenBytes());
                            res.successStatus(restRes.getSuccessStatus());
                            res.errorMessage(restRes.getError());
                            Object o = restRes.getResponse();
                            // In case of metrics a little adjustment is needed.
                            if (o instanceof GridCacheRestMetrics)
                                o = ((GridCacheRestMetrics) o).map();
                            res.result(o);
                        } catch (IgniteCheckedException e) {
                            U.error(log, "Failed to process client request: " + msg, e);
                            res.successStatus(GridClientResponse.STATUS_FAILED);
                            res.errorMessage("Failed to process client request: " + e.getMessage());
                        }
                        GridNioFuture<?> sf = ses.send(res);
                        // Check if send failed.
                        sf.listen(new CI1<IgniteInternalFuture<?>>() {

                            @Override
                            public void apply(IgniteInternalFuture<?> fut) {
                                try {
                                    fut.get();
                                } catch (IgniteCheckedException e) {
                                    U.error(log, "Failed to process client request [ses=" + ses + ", msg=" + msg + ']', e);
                                }
                            }
                        });
                    }
                });
            else
                U.error(log, "Failed to process client request (unknown packet type) [ses=" + ses + ", msg=" + msg + ']');
        }
    }
}
Also used : GridClientResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientResponse) GridClientMarshaller(org.apache.ignite.internal.client.marshaller.GridClientMarshaller) CI1(org.apache.ignite.internal.util.typedef.CI1) GridClientPingPacket(org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket) GridRedisMessage(org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) GridClientHandshakeRequest(org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridRestResponse(org.apache.ignite.internal.processors.rest.GridRestResponse) GridCacheRestMetrics(org.apache.ignite.internal.processors.rest.handlers.cache.GridCacheRestMetrics) GridRestRequest(org.apache.ignite.internal.processors.rest.request.GridRestRequest)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 GridClientMarshaller (org.apache.ignite.internal.client.marshaller.GridClientMarshaller)1 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)1 GridClientHandshakeRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest)1 GridClientPingPacket (org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket)1 GridClientResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)1 GridCacheRestMetrics (org.apache.ignite.internal.processors.rest.handlers.cache.GridCacheRestMetrics)1 GridRedisMessage (org.apache.ignite.internal.processors.rest.protocols.tcp.redis.GridRedisMessage)1 GridRestRequest (org.apache.ignite.internal.processors.rest.request.GridRestRequest)1 CI1 (org.apache.ignite.internal.util.typedef.CI1)1