Package com.sun.grizzly

Examples of com.sun.grizzly.TCPSelectorHandler


   
    /* ------------------------------------------------------------ */
    public void open() throws IOException
    {
        controller = new Controller();
        TCPSelectorHandler selectorHandler = new TCPSelectorHandler();
        selectorHandler.setPort(getPort());
        if (getHost() != null)
        {
            selectorHandler.setInet(InetAddress.getByName(getHost()));
        }
        controller.setSelectorHandler(selectorHandler);
        selectorHandler.setSelectionKeyHandler(new DefaultSelectionKeyHandler());
       
        DefaultProtocolChainInstanceHandler instanceHandler
                = new DefaultProtocolChainInstanceHandler()
        {
           
View Full Code Here


        controller.getPipeline().setName("http-proxy-outbound");
        /*
         * TODOD : Implement idle time out so that connections are
         * closed after not being used for certain time.
         */
        selectorHandler = new TCPSelectorHandler(true);
        controller.setSelectorHandler(selectorHandler);
  controller.setHandleReadWriteConcurrently(false);
        cacheableHandlerPool = new HttpClbConnectionCache(
                ProxyConfig.getInstance().getKeepAliveMaxConnections(),
                ProxyConfig.getInstance().getKeepAliveIdleTimeout());
View Full Code Here

        return udpSelector;
    }

    private TCPSelectorHandler startTCPServer(TargetTuple targetTuple,
            ProtocolChainInstanceHandler instanceHandler) {
        TCPSelectorHandler tcpSelector =
                createTcpEndpoint(targetTuple.getSocketAddress());

        SelectionKeyHandler keyHandler =
                createSelectionKeyHandler(keepAliveTimeoutInSeconds);

        tcpSelector.setSelectionKeyHandler(keyHandler);

        tcpSelector.setProtocolChainInstanceHandler(instanceHandler);

        tcpSelector.setSsBackLog(maxPendingCount);

        serverController.addSelectorHandler(tcpSelector);

        return tcpSelector;
    }
View Full Code Here

        return udpClientSelectorHandler;
    }

    private TCPSelectorHandler startTCPClient(
            ProtocolChainInstanceHandler instanceHandler) {
        TCPSelectorHandler tcpClientSelectorHandler =
                new TCPSelectorHandler(true);

        tcpClientSelectorHandler.setProtocolChainInstanceHandler(instanceHandler);

        SelectionKeyHandler keyHandler =
                createSelectionKeyHandler(keepAliveTimeoutInSeconds);

        tcpClientSelectorHandler.setSelectionKeyHandler(keyHandler);

        tcpClientSelectorHandler.setProtocolChainInstanceHandler(instanceHandler);

        clientController.addSelectorHandler(tcpClientSelectorHandler);

        return tcpClientSelectorHandler;
    }
View Full Code Here

    }

    TCPSelectorHandler createTcpEndpoint(InetSocketAddress addr) {
        int linger = -1;

        final TCPSelectorHandler selectorHandler = new TCPSelectorHandler() {

            /**
             * Intercept the accept operations and cache the associated connection.
             */
            @Override
            public boolean onAcceptInterest(SelectionKey key, Context ctx)
                    throws IOException {

                SelectableChannel channel = acceptWithoutRegistration(key);

                if (channel != null) {
                    configureChannel(channel);

                    SelectionKey readKey = channel.register(selector,
                            SelectionKey.OP_READ);

                    // Cache the connection.
                    TargetTuple tt = connectionManager.add(readKey, this);

                    outboundConnectionsTuple.put(readKey, tt);
                }

                return false;
            }

            /*
             * Need this until the fix is available as part of 1.8.6.x grizzly.
             * See issue 491 in Grizzly,
             */
            @Override
            public void shutdown() {
                // If shutdown was called for this SelectorHandler
                if (isShutDown.getAndSet(true)) {
                    return;
                }

                stateHolder.setState(State.STOPPED);

                if (selector != null) {
                    try {
                        boolean isContinue = true;
                        while (isContinue) {
                            try {
                                for (SelectionKey selectionKey : selector.keys()) {
                                    selectionKeyHandler.close(selectionKey);
                                }

                                isContinue = false;
                            } catch (ConcurrentModificationException e) {
                            // ignore
                            }
                        }
                    } catch (ClosedSelectorException e) {
                    // If Selector is already closed - OK
                    }
                }

                try {
                    if (serverSocket != null) {
                        serverSocket.close();
                    }
                } catch (Throwable ex) {
                    logger.log(Level.SEVERE,
                            "Close server socket", ex);
                }

                try {
                    if (serverSocketChannel != null) {
                        serverSocketChannel.close();
                    }
                } catch (Throwable ex) {
                    logger.log(Level.SEVERE,
                            "Close server socket channel", ex);
                }

                try {
                    if (selector != null) {
                        selector.close();
                    }
                } catch (Throwable ex) {
                   logger.log(Level.SEVERE,
                            "Close selector", ex);
                }

                if (asyncQueueReader != null) {
                    asyncQueueReader.close();
                    asyncQueueReader = null;
                }

                if (asyncQueueWriter != null) {
                    asyncQueueWriter.close();
                    asyncQueueWriter = null;
                }

                opToRegister.clear();
                attributes = null;
            }
           
            @Override
            protected void onConnectOp(Context ctx,
                    SelectionKeyOP.ConnectSelectionKeyOP selectionKeyOp)
                    throws IOException {
                SocketAddress remoteAddress = selectionKeyOp.getRemoteAddress();

                SocketAddress localAddress = selectionKeyOp.getLocalAddress();

                CallbackHandler callbackHandler =
                        selectionKeyOp.getCallbackHandler();

                SocketChannel socketChannel = SocketChannel.open();

                socketChannel.socket().setReuseAddress(reuseAddress);

                if (localAddress != null) {
                    socketChannel.socket().bind(localAddress);
                }

                socketChannel.configureBlocking(false);

                SelectionKey key = socketChannel.register(selector,
                        SelectionKey.OP_CONNECT);

                key.attach(ExpiringCallbackHandlerSelectionKeyAttachment.create(
                        key, callbackHandler));

                boolean isConnected;

                try {
                    isConnected = socketChannel.connect(remoteAddress);
                } catch (Exception e) {
                    if (logger.isLoggable(Level.FINE)) {
                        logger.log(Level.FINE,
                                "Exception occured when tried to connect socket",
                                e.getMessage());
                    }

                    if (logger.isLoggable(Level.FINEST)) {
                        logger.log(Level.FINEST,
                                "Exception occured when tried to connect socket",
                                e);
                    }

                    // set isConnected to true to let callback handler to know about the problem happened
                    isConnected = true;
                }

                // if channel was connected immediately or exception occured
                if (isConnected) {
                    onConnectInterest(key, ctx);
                }
            }
            };

        selectorHandler.setPort(addr.getPort());

        selectorHandler.setInet(addr.getAddress());

        selectorHandler.setLinger(linger);

        selectorHandler.setLogger(logger);

        selectorHandler.setReuseAddress(true);

        selectorHandler.setSocketTimeout(keepAliveTimeoutInSeconds * 1000);

        return selectorHandler;
    }
View Full Code Here

   
    public boolean execute(Context ctx) throws IOException {
        if (connector == null) {
            synchronized(sync) {
                if (connector == null) {
                    final TCPSelectorHandler handler = (TCPSelectorHandler) ctx.getSelectorHandler();
                    final String host = handler.getInet().getHostName();
                    final int port = handler.getPort();

                    logger.log(Level.INFO, "Initialize SOAP/TCP protocol for port: " + port);
                   
                    connector = new Connector(host, port, module.getDelegate());

                    final SelectionKeyHandler keyHandler = handler.getSelectionKeyHandler();
                    if (keyHandler instanceof BaseSelectionKeyHandler) {
                        ((BaseSelectionKeyHandler) keyHandler).setConnectionCloseHandler(closeHandler);
                    }
                }
            }
View Full Code Here

   
    /* ------------------------------------------------------------ */
    public void open() throws IOException
    {
        controller = new Controller();
        TCPSelectorHandler selectorHandler = new TCPSelectorHandler();
        selectorHandler.setPort(getPort());
        if (getHost() != null)
        {
            selectorHandler.setInet(InetAddress.getByName(getHost()));
        }
        controller.setSelectorHandler(selectorHandler);
        selectorHandler.setSelectionKeyHandler(new DefaultSelectionKeyHandler());
       
        DefaultProtocolChainInstanceHandler instanceHandler
                = new DefaultProtocolChainInstanceHandler()
        {
           
View Full Code Here

   
    public boolean execute(Context ctx) throws IOException {
        if (connector == null) {
            synchronized(sync) {
                if (connector == null) {
                    final TCPSelectorHandler handler = (TCPSelectorHandler) ctx.getSelectorHandler();
                    final String host = handler.getInet().getHostName();
                    final int port = handler.getPort();

                    logger.log(Level.INFO, "Initialize SOAP/TCP protocol for port: " + port);
                   
                    connector = new Connector(host, port, module.getDelegate());

                    final SelectionKeyHandler keyHandler = handler.getSelectionKeyHandler();
                    if (keyHandler instanceof BaseSelectionKeyHandler) {
                        ((BaseSelectionKeyHandler) keyHandler).setConnectionCloseHandler(closeHandler);
                    }
                }
            }
View Full Code Here

TOP

Related Classes of com.sun.grizzly.TCPSelectorHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.