use of org.apache.ignite.internal.client.marshaller.GridClientMarshaller 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.client.marshaller.GridClientMarshaller 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 + ']');
}
}
use of org.apache.ignite.internal.client.marshaller.GridClientMarshaller 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