Search in sources :

Example 1 with GridClientMessage

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);
    }
}
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 2 with GridClientMessage

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);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 3 with GridClientMessage

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;
    }
}
Also used : GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridNioSslFilter(org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 4 with GridClientMessage

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);
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Example 5 with GridClientMessage

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();
}
Also used : GridClientException(org.apache.ignite.internal.client.GridClientException) GridSslContextFactory(org.apache.ignite.internal.client.ssl.GridSslContextFactory) UnknownHostException(java.net.UnknownHostException) IgniteException(org.apache.ignite.IgniteException) SSLContext(javax.net.ssl.SSLContext) InetAddress(java.net.InetAddress) SSLException(javax.net.ssl.SSLException) GridClientMessage(org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)

Aggregations

GridClientMessage (org.apache.ignite.internal.processors.rest.client.message.GridClientMessage)14 ByteBuffer (java.nio.ByteBuffer)7 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)5 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)4 MockNioSession (org.apache.ignite.internal.util.nio.impl.MockNioSession)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 GridClientOptimizedMarshaller (org.apache.ignite.internal.client.marshaller.optimized.GridClientOptimizedMarshaller)3 GridRouterResponse (org.apache.ignite.internal.processors.rest.client.message.GridRouterResponse)3 UnknownHostException (java.net.UnknownHostException)2 GridClientException (org.apache.ignite.internal.client.GridClientException)2 GridClientMarshaller (org.apache.ignite.internal.client.marshaller.GridClientMarshaller)2 GridClientCacheRequest (org.apache.ignite.internal.processors.rest.client.message.GridClientCacheRequest)2 GridClientHandshakeResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientHandshakeResponse)2 GridClientResponse (org.apache.ignite.internal.processors.rest.client.message.GridClientResponse)2 GridNioCodecFilter (org.apache.ignite.internal.util.nio.GridNioCodecFilter)2 InetAddress (java.net.InetAddress)1 ArrayList (java.util.ArrayList)1 SSLContext (javax.net.ssl.SSLContext)1