use of org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse 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", "TooBroadScope" })
void handleResponse(GridClientMessage res) throws IOException {
lastMsgRcvTime = U.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);
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse in project ignite by apache.
the class GridTcpRouterNioListenerAdapter method onMessage.
/** {@inheritDoc} */
@SuppressWarnings("TypeMayBeWeakened")
@Override
public void onMessage(final GridNioSession ses, final GridClientMessage msg) {
if (msg instanceof GridRouterRequest) {
GridRouterRequest routerMsg = (GridRouterRequest) msg;
final UUID clientId = routerMsg.clientId();
final long reqId = routerMsg.requestId();
try {
client.forwardMessage(routerMsg, routerMsg.destinationId(), ses.<Byte>meta(MARSHALLER_ID.ordinal())).listen(new GridClientFutureListener() {
@Override
public void onDone(GridClientFuture fut) {
try {
GridRouterResponse res = (GridRouterResponse) fut.get();
// Restoring original request id, because it was overwritten by the client.
res.requestId(reqId);
ses.send(res);
} catch (GridClientException e) {
ses.send(makeFailureResponse(e, clientId, reqId));
}
}
});
} catch (GridClientException e) {
ses.send(makeFailureResponse(e, clientId, reqId));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
U.warn(log, "Message forwarding was interrupted (will ignore last message): " + e.getMessage(), "Message forwarding was interrupted.");
}
} 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();
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_ID.ordinal(), marshId);
ses.addMeta(MARSHALLER.ordinal(), marsh);
ses.send(GridClientHandshakeResponse.OK);
}
}
} else if (msg instanceof GridClientPingPacket)
ses.send(GridClientPingPacket.PING_MESSAGE);
else
throw new IllegalArgumentException("Unsupported input message: " + msg);
}
use of org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse 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);
}
use of org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse 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;
}
Aggregations