Search in sources :

Example 1 with GridNioParser

use of org.apache.ignite.internal.util.nio.GridNioParser in project ignite by apache.

the class GridTcpRestProtocol method start.

/** {@inheritDoc} */
@SuppressWarnings("BusyWait")
@Override
public void start(final GridRestProtocolHandler hnd) throws IgniteCheckedException {
    assert hnd != null;
    ConnectorConfiguration cfg = ctx.config().getConnectorConfiguration();
    assert cfg != null;
    lsnr = new GridTcpRestNioListener(log, this, hnd, ctx);
    GridNioParser parser = new GridTcpRestParser(false);
    try {
        host = resolveRestTcpHost(ctx.config());
        SSLContext sslCtx = null;
        if (cfg.isSslEnabled()) {
            Factory<SSLContext> igniteFactory = ctx.config().getSslContextFactory();
            Factory<SSLContext> factory = cfg.getSslFactory();
            // This factory deprecated and will be removed.
            GridSslContextFactory depFactory = cfg.getSslContextFactory();
            if (factory == null && depFactory == null && igniteFactory == null)
                // Thrown SSL exception instead of IgniteCheckedException for writing correct warning message into log.
                throw new SSLException("SSL is enabled, but SSL context factory is not specified.");
            if (factory != null)
                sslCtx = factory.create();
            else if (depFactory != null)
                sslCtx = depFactory.createSslContext();
            else
                sslCtx = igniteFactory.create();
        }
        int startPort = cfg.getPort();
        int portRange = cfg.getPortRange();
        int lastPort = portRange == 0 ? startPort : startPort + portRange - 1;
        for (int port0 = startPort; port0 <= lastPort; port0++) {
            if (startTcpServer(host, port0, lsnr, parser, sslCtx, cfg)) {
                port = port0;
                if (log.isInfoEnabled())
                    log.info(startInfo());
                return;
            }
        }
        U.warn(log, "Failed to start TCP binary REST server (possibly all ports in range are in use) " + "[firstPort=" + cfg.getPort() + ", lastPort=" + lastPort + ", host=" + host + ']');
    } catch (SSLException e) {
        U.warn(log, "Failed to start " + name() + " protocol on port " + port + ": " + e.getMessage(), "Failed to start " + name() + " protocol on port " + port + ". Check if SSL context factory is " + "properly configured.");
    } catch (IOException e) {
        U.warn(log, "Failed to start " + name() + " protocol on port " + port + ": " + e.getMessage(), "Failed to start " + name() + " protocol on port " + port + ". " + "Check restTcpHost configuration property.");
    }
}
Also used : GridSslContextFactory(org.apache.ignite.internal.client.ssl.GridSslContextFactory) GridNioParser(org.apache.ignite.internal.util.nio.GridNioParser) ConnectorConfiguration(org.apache.ignite.configuration.ConnectorConfiguration) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException)

Example 2 with GridNioParser

use of org.apache.ignite.internal.util.nio.GridNioParser in project ignite by apache.

the class SocketStreamer method start.

/**
     * Starts streamer.
     *
     * @throws IgniteException If failed.
     */
public void start() {
    A.ensure(getSingleTupleExtractor() != null || getMultipleTupleExtractor() != null, "tupleExtractor (single or multiple)");
    A.notNull(getStreamer(), "streamer");
    A.notNull(getIgnite(), "ignite");
    A.ensure(threads > 0, "threads > 0");
    log = getIgnite().log();
    GridNioServerListener<byte[]> lsnr = new GridNioServerListenerAdapter<byte[]>() {

        @Override
        public void onConnected(GridNioSession ses) {
            assert ses.accepted();
            if (log.isDebugEnabled())
                log.debug("Accepted connection: " + ses.remoteAddress());
        }

        @Override
        public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
            if (e != null)
                log.error("Connection failed with exception", e);
        }

        @Override
        public void onMessage(GridNioSession ses, byte[] msg) {
            addMessage(converter.convert(msg));
        }
    };
    ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
    GridNioParser parser = F.isEmpty(delim) ? new GridBufferedParser(directMode, byteOrder) : new GridDelimitedParser(delim, directMode);
    if (converter == null)
        converter = new DefaultConverter<>(getIgnite().name());
    GridNioFilter codec = new GridNioCodecFilter(parser, log, directMode);
    GridNioFilter[] filters = new GridNioFilter[] { codec };
    try {
        srv = new GridNioServer.Builder<byte[]>().address(addr == null ? InetAddress.getLocalHost() : addr).serverName("sock-streamer").port(port).listener(lsnr).logger(log).selectorCount(threads).byteOrder(byteOrder).filters(filters).build();
    } catch (IgniteCheckedException | UnknownHostException e) {
        throw new IgniteException(e);
    }
    srv.start();
    if (log.isDebugEnabled())
        log.debug("Socket streaming server started on " + addr + ':' + port);
}
Also used : GridBufferedParser(org.apache.ignite.internal.util.nio.GridBufferedParser) GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridNioParser(org.apache.ignite.internal.util.nio.GridNioParser) UnknownHostException(java.net.UnknownHostException) ByteOrder(java.nio.ByteOrder) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) UnknownHostException(java.net.UnknownHostException) GridDelimitedParser(org.apache.ignite.internal.util.nio.GridDelimitedParser) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridNioServerListenerAdapter(org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GridNioParser (org.apache.ignite.internal.util.nio.GridNioParser)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ByteOrder (java.nio.ByteOrder)1 SSLContext (javax.net.ssl.SSLContext)1 SSLException (javax.net.ssl.SSLException)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 ConnectorConfiguration (org.apache.ignite.configuration.ConnectorConfiguration)1 GridSslContextFactory (org.apache.ignite.internal.client.ssl.GridSslContextFactory)1 GridBufferedParser (org.apache.ignite.internal.util.nio.GridBufferedParser)1 GridDelimitedParser (org.apache.ignite.internal.util.nio.GridDelimitedParser)1 GridNioCodecFilter (org.apache.ignite.internal.util.nio.GridNioCodecFilter)1 GridNioFilter (org.apache.ignite.internal.util.nio.GridNioFilter)1 GridNioServerListenerAdapter (org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter)1 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)1 Nullable (org.jetbrains.annotations.Nullable)1