Search in sources :

Example 1 with GridClientResponse

use of org.apache.ignite.internal.processors.rest.client.message.GridClientResponse in project ignite by apache.

the class ClientTestRestServer method makeResponseFor.

/**
 * Prepares response stub.
 * @param msg Mesage to respond to.
 * @return Response.
 */
private static GridClientResponse makeResponseFor(GridClientMessage msg) {
    GridClientResponse res = new GridClientResponse();
    res.clientId(msg.clientId());
    res.requestId(msg.requestId());
    res.successStatus(GridClientResponse.STATUS_SUCCESS);
    res.sessionToken(EMPTY_SES_TOKEN);
    return res;
}
Also used : GridClientResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)

Example 2 with GridClientResponse

use of org.apache.ignite.internal.processors.rest.client.message.GridClientResponse in project ignite by apache.

the class GridClientNioTcpConnection method handleResponse.

/**
 * Handles incoming response message. If this connection is closed this method would signal empty event
 * if there is no more pending requests.
 *
 * @param res Incoming response data.
 */
@SuppressWarnings({ "unchecked" })
void handleResponse(GridClientMessage res) throws IOException {
    lastMsgRcvTime = System.currentTimeMillis();
    TcpClientFuture fut = pendingReqs.get(res.requestId());
    if (fut == null) {
        log.warning("Response for an unknown request is received, ignoring. " + "[res=" + res + ", ses=" + ses + ']');
        return;
    }
    if (fut.forward()) {
        removePending(res.requestId());
        fut.onDone(res);
    } else {
        GridClientMessage res0 = res;
        if (res instanceof GridRouterResponse) {
            res0 = marsh.unmarshal(((GridRouterResponse) res).body());
            res0.requestId(res.requestId());
            res0.clientId(res.clientId());
            res0.destinationId(res.destinationId());
        }
        if (res0 instanceof GridClientResponse)
            handleClientResponse(fut, (GridClientResponse) res0);
        else
            log.warning("Unsupported response type received: " + res0);
    }
}
Also used : GridClientResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientResponse) GridRouterResponse(org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 3 with GridClientResponse

use of org.apache.ignite.internal.processors.rest.client.message.GridClientResponse in project ignite by apache.

the class GridTcpRouterNioListenerAdapter method makeFailureResponse.

/**
 * Creates a failure response, based on the given exception.
 *
 * @param e Exception to extract failure report from.
 * @param clientId Client id.
 * @param reqId Request id.
 * @return Failure response.
 */
private GridClientResponse makeFailureResponse(GridClientException e, UUID clientId, Long reqId) {
    U.error(log, "Failed to process message on router.", e);
    GridClientResponse res = new GridClientResponse();
    res.clientId(clientId);
    res.requestId(reqId);
    res.successStatus(GridClientResponse.STATUS_FAILED);
    res.errorMessage("Failed to process message on router " + "[exception=" + e.getClass().getSimpleName() + ", message=" + e.getMessage() + ']');
    return res;
}
Also used : GridClientResponse(org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)

Example 4 with GridClientResponse

use of org.apache.ignite.internal.processors.rest.client.message.GridClientResponse 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 5 with GridClientResponse

use of org.apache.ignite.internal.processors.rest.client.message.GridClientResponse in project ignite by apache.

the class GridTcpRestNioListener method onMessage.

/**
 * {@inheritDoc}
 */
@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 + ']');
            onSessionClosed(ses);
        } else {
            byte marshId = hs.marshallerId();
            if (marshMapLatch.getCount() > 0) {
                try {
                    U.await(marshMapLatch);
                } catch (IgniteInterruptedCheckedException e) {
                    U.error(log, "Marshaller is not initialized.", e);
                    onSessionClosed(ses);
                    return;
                }
            }
            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 + ']');
                onSessionClosed(ses);
            } else {
                ses.addMeta(MARSHALLER.ordinal(), marsh);
                ses.send(GridClientHandshakeResponse.OK);
            }
        }
    } else {
        final GridRestRequest req = createRestRequest(ses, msg);
        if (req != null) {
            IgniteInternalFuture<GridRestResponse> taskFut = hnd.handleAsync(req);
            if (isInterruptible(msg))
                addFutureToSession(ses, taskFut);
            taskFut.listen(new CI1<IgniteInternalFuture<GridRestResponse>>() {

                @Override
                public void apply(IgniteInternalFuture<GridRestResponse> fut) {
                    removeFutureFromSession(ses, taskFut);
                    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) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) 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

GridClientResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 GridClientMarshaller (org.apache.ignite.internal.client.marshaller.GridClientMarshaller)2 GridClientMessage (org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)2 GridClientPingPacket (org.apache.ignite.internal.processors.rest.client.message.GridClientPingPacket)2 GridRouterResponse (org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse)2 ByteBuffer (java.nio.ByteBuffer)1 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)1 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)1 GridRestResponse (org.apache.ignite.internal.processors.rest.GridRestResponse)1 GridClientHandshakeRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeRequest)1 GridClientHandshakeResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse)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