use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage 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);
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class GridClientZipOptimizedMarshaller method marshal.
/**
* {@inheritDoc}
*/
@Override
public ByteBuffer marshal(Object obj, int off) throws IOException {
try {
if (!(obj instanceof GridClientMessage))
throw new IOException("Message serialization of given type is not supported: " + obj.getClass().getName());
byte[] marshBytes = U.marshal(opMarsh, obj);
boolean zip = marshBytes.length > 512;
byte[] bytes = zip ? zipBytes(marshBytes) : marshBytes;
ByteBuffer buf = ByteBuffer.allocate(off + bytes.length + 1);
buf.position(off);
buf.put((byte) (zip ? 1 : 0));
buf.put(bytes);
buf.flip();
return buf;
} catch (IgniteCheckedException e) {
throw new IOException(e);
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class GridTcpRestProtocol method startTcpServer.
/**
* Tries to start server with given parameters.
*
* @param hostAddr Host on which server should be bound.
* @param port Port on which server should be bound.
* @param lsnr Server message listener.
* @param parser Server message parser.
* @param sslCtx SSL context in case if SSL is enabled.
* @param cfg Configuration for other parameters.
* @return {@code True} if server successfully started, {@code false} if port is used and
* server was unable to start.
*/
private boolean startTcpServer(InetAddress hostAddr, int port, GridNioServerListener<GridClientMessage> lsnr, GridNioParser parser, @Nullable SSLContext sslCtx, ConnectorConfiguration cfg) {
try {
GridNioFilter codec = new GridNioCodecFilter(parser, log, false);
GridNioFilter[] filters;
if (sslCtx != null) {
GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, cfg.isDirectBuffer(), ByteOrder.nativeOrder(), log, ctx.metric().registry(REST_CONNECTOR_METRIC_REGISTRY_NAME));
sslFilter.directMode(false);
boolean auth = cfg.isSslClientAuth();
sslFilter.wantClientAuth(auth);
sslFilter.needClientAuth(auth);
filters = new GridNioFilter[] { codec, sslFilter };
} else
filters = new GridNioFilter[] { codec };
srv = GridNioServer.<GridClientMessage>builder().address(hostAddr).port(port).listener(lsnr).logger(log).selectorCount(cfg.getSelectorCount()).igniteInstanceName(ctx.igniteInstanceName()).serverName("tcp-rest").tcpNoDelay(cfg.isNoDelay()).directBuffer(cfg.isDirectBuffer()).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(cfg.getSendBufferSize()).socketReceiveBufferSize(cfg.getReceiveBufferSize()).sendQueueLimit(cfg.getSendQueueLimit()).filters(filters).directMode(false).metricRegistry(ctx.metric().registry(REST_CONNECTOR_METRIC_REGISTRY_NAME)).build();
srv.idleTimeout(cfg.getIdleTimeout());
srv.start();
ctx.ports().registerPort(port, IgnitePortProtocol.TCP, getClass());
return true;
} catch (IgniteCheckedException e) {
if (log.isDebugEnabled())
log.debug("Failed to start " + name() + " protocol on port " + port + ": " + e.getMessage());
return false;
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class GridClientOptimizedMarshaller method marshal.
/**
* {@inheritDoc}
*/
@Override
public ByteBuffer marshal(Object obj, int off) throws IOException {
try {
if (!(obj instanceof GridClientMessage))
throw new IOException("Message serialization of given type is not supported: " + obj.getClass().getName());
byte[] bytes = U.marshal(opMarsh, obj);
ByteBuffer buf = ByteBuffer.allocate(off + bytes.length);
buf.position(off);
buf.put(bytes);
buf.flip();
return buf;
} catch (IgniteCheckedException e) {
throw new IOException(e);
}
}
use of org.apache.ignite.internal.processors.rest.client.message.GridClientMessage in project ignite by apache.
the class GridTcpRouterImpl method start.
/**
* Starts router.
*
* @throws IgniteException If failed.
*/
@Override
public void start() throws IgniteException {
try {
client = createClient(cfg);
} catch (GridClientException e) {
throw new IgniteException("Failed to initialise embedded client.", e);
}
GridNioServerListener<GridClientMessage> lsnr = new GridTcpRouterNioListenerOsImpl(log, client);
parser = new GridTcpRouterNioParser();
final InetAddress hostAddr;
try {
hostAddr = InetAddress.getByName(cfg.getHost());
} catch (UnknownHostException e) {
throw new IgniteException("Failed to resolve grid address for configured host: " + cfg.getHost(), e);
}
SSLContext sslCtx;
try {
GridSslContextFactory sslCtxFactory = cfg.getSslContextFactory();
sslCtx = sslCtxFactory == null ? null : sslCtxFactory.createSslContext();
} catch (SSLException e) {
throw new IgniteException("Failed to create SSL context.", e);
}
for (int port = cfg.getPort(), last = port + cfg.getPortRange(); port <= last; port++) {
if (startTcpServer(hostAddr, port, lsnr, parser, cfg.isNoDelay(), sslCtx, cfg.isSslClientAuth(), cfg.isSslClientAuth())) {
if (log.isInfoEnabled())
log.info("TCP router successfully started for endpoint: " + hostAddr.getHostAddress() + ":" + port);
bindPort = port;
bindHost = hostAddr.getHostName();
break;
} else
U.warn(log, "TCP REST router failed to start on endpoint: " + hostAddr.getHostAddress() + ":" + port + ". Will try next port within allowed port range.");
}
if (bindPort == 0)
throw new IgniteException("Failed to bind TCP router server (possibly all ports in range " + "are in use) [firstPort=" + cfg.getPort() + ", lastPort=" + (cfg.getPort() + cfg.getPortRange()) + ", addr=" + hostAddr + ']');
registerMBean();
}
Aggregations