Examples of SocketChannel


Examples of java.nio.channels.SocketChannel

                    continue;
                }

                Iterator it = _selector.selectedKeys().iterator();
                SelectionKey selKey = null;
                SocketChannel cChannel = null;
                ConnectionHeader header = null;

                while ( it.hasNext() && !isInterrupted() )
                {
                    selKey = (SelectionKey) it.next();
                    it.remove();

                    try
                    {
                        // validate the key
                        if ( !selKey.isValid() )
                        {
                            continue;
                        }

                        // socket is ready for establishing a connection?
                        if ( selKey.isAcceptable() )
                        {
                            // establish the connection
                            cChannel = this._channel.accept();

                            // a little bit paranoia
                            if ( cChannel != null )
                            {
                                // little bit tuning
                                cChannel.socket().setTrafficClass( EJConstants.IPTOS_THROUGHPUT );
                                cChannel.socket().setReuseAddress( true );
                                cChannel.socket().setSoLinger( false, 0 );
                               
                                // ensure that the socket channel is prepared
                                // for non-blocking operations
                                cChannel.configureBlocking( false );
                                // create a new ConnectionHeader for all
                                // upcoming operations on the
                                // accepted socket connection
                                header = new ConnectionHeader( cChannel, cChannel.socket().getRemoteSocketAddress()
                                        .toString(), true );
                                // register the channel on another thread which
                                // will do further connection handling
                                getProcessor().register( header, SelectionKey.OP_READ );

                                if ( logger.isLoggable( Level.FINE ) )
                                {
                                    logger.log( Level.FINE, "Connection accepted from "
                                            + cChannel.socket().getRemoteSocketAddress() );
                                }
                            }
                        }
                    }
                    catch ( CancelledKeyException cke )
View Full Code Here

Examples of java.nio.channels.SocketChannel

     * @see java.lang.Runnable#run()
     */
    public void run()
    {
        Object request = null;
        SocketChannel channel = this._senderInfo.getChannel();

        try
        {
            if ( !this._senderInfo.isConnected() )
            {
                log.log( Level.FINEST, "Handshaking client..." );
                // handshake the client, get infos about compression...
                this._senderInfo = DataChannel.getInstance().handshake( this._receiverInfo, channel,
                                                                        EJConstants.EJOE_CONNECTION_TIMEOUT );
            }

            if ( this._senderInfo != null )
            {
                log.log( Level.FINEST, "Remote requested " + this._senderInfo.getAdapterName() );

                SerializeAdapter adapter = AdapterFactory.createAdapter( this._senderInfo.getAdapterName() );

                if ( this._receiverInfo.hasNonBlockingReadWrite() )
                {
                    log.log( Level.FINEST, "Going to read client request on a non blocking socket..." );
                    request = read( adapter );
                }
                else
                {
                    log.log( Level.FINEST, "Going to read client request on a blocking socket..." );
                    request = readBlocked( adapter );
                }

                this._senderInfo.releaseAttachment();
                this._senderInfo.releaseWaitingBuffer();

                log.log( Level.FINE, "Client request read." );

                Object result = handleObject( request );

                if ( this._registrar.isValid() )
                {
                    this._senderInfo.setAttachment( result );
                    this._registrar.register( this._senderInfo, SelectionKey.OP_WRITE );
                }
            }
            else
            {
                log.log( Level.WARNING, "Connection timeout reached while waiting for Handshake complete. "
                        + "Closing connection." );
                shutdownConnection( channel );
            }
        }
        // partial read detected, registering for read again
        catch ( IncompleteIOException ioe )
        {
            this._senderInfo.setWaitingBuffer( ioe.getIOBuffer() );
            this._registrar.register( this._senderInfo, SelectionKey.OP_READ );
        }
        catch ( EOFException eof )
        {
            log.log( Level.FINEST, "EOF received while reading client data " + "- closing connection." );
            shutdownConnection( channel );
        }
        catch ( ParseException pe )
        {
            log.log( Level.WARNING, "Unparseable connection header detected!", pe );
            if ( !this._senderInfo.isHttp() )
            {
                this._senderInfo.setAttachment( new RemoteException( "Unparseable connection header!", pe ) );
            }
            else
            {
                this._senderInfo.setAttachment( new RemoteException( "Unparseable connection header!", pe ),
                                                HttpResponse.HTTP_BAD_REQUEST );
            }

            this._registrar.register( this._senderInfo, SelectionKey.OP_WRITE );
        }
        catch ( SocketTimeoutException ste )
        {
            log.log( Level.FINE, "Timeout occured while waiting for client data!", ste );
            shutdownConnection( channel );
        }
        catch ( RemoteException re )
        {
            if ( !this._senderInfo.isHttp() )
            {
                this._senderInfo.setAttachment( re );
            }
            else
            {
                this._senderInfo.setAttachment( re, HttpResponse.HTTP_INTERNAL_SERVER_ERROR );
            }
            this._registrar.register( this._senderInfo, SelectionKey.OP_WRITE );
        }
        // the client did something strange with the channel, probably the
        // client is just too slow for
        catch ( NonReadableChannelException e )
        {
            log.log( Level.INFO, "Connection probably closed by client." );
            shutdownConnection( channel );
        }
        // the client did close the connection, or the connection was
        // disconnected
        catch ( ClosedChannelException cce )
        {
            log.log( Level.INFO, "Connection closed by client." );
            shutdownConnection( channel );
        }
        catch ( Throwable e )
        {
            if ( !channel.isBlocking() && channel.isConnected() && channel.isOpen() )
            {
                // something goes completely wrong!
                log.log( Level.WARNING, "!!! Exception while reading client data !!! "
                        + "Probably the client just closed the connection but it could also be a serious failure.", e );
            }
View Full Code Here

Examples of java.nio.channels.SocketChannel

        }

        try
        {
            Iterator it;
            SocketChannel cChannel;
            SelectionKey selKey;
            ConnectionHeader clientInfo;
            Set keys;

            while ( !isInterrupted() )
            {
                this._load = 0;

                // (pre)register interested socket channels
                registerAspirants();

                // just try endless new selects until there are interested
                // socket channels
                if ( _selector.select() == 0 ) continue;

                keys = this._selector.selectedKeys();
                this._load = keys.size();
                it = this._selector.selectedKeys().iterator();

                // loop over all selected channels, just take care of thread
                // interruption
                while ( it.hasNext() && !isInterrupted() )
                {
                    selKey = (SelectionKey) it.next();
                    // remove the SelectionKey from the Iterator otherwise it
                    // will be lost
                    it.remove();

                    try
                    {
                        // validate the key
                        if ( !selKey.isValid() ) continue;

                        // at least our ConnectionAcceptor has created a client
                        // ConnectionHeader
                        clientInfo = (ConnectionHeader) selKey.attachment();

                        // first check read-availbility
                        if ( selKey.isReadable() )
                        {
                            // get the underlying socket channel
                            cChannel = (SocketChannel) selKey.channel();
                            // cancel the channels registration with our
                            // Selector
                            selKey.cancel();

                            // little bit paranoia
                            if ( cChannel != null && cChannel.isOpen() )
                            {
                                // don't we support NIO?
                                if ( !this._serverInfo.hasNonBlockingReadWrite() )
                                {
                                    logger.log( Level.FINEST,
                                                "Setting socket to blocking mode for further io operations..." );

                                    // prepare the channel for upcoming blocking
                                    // network operations
                                    cChannel.configureBlocking( true );
                                }

                                // schedule a asynchronious read-process operation
                                this._readpool.invokeLater( new ConnectionReader( this, this._serverInfo, clientInfo ) );
                            }
                        }
                        else if ( selKey.isWritable() )
                        {
                            // get the underlying socket channel
                            cChannel = (SocketChannel) selKey.channel();
                            // cancel the channels registration with our
                            // Selector
                            selKey.cancel();

                            // little bit paranoia
                            if ( cChannel != null && cChannel.isOpen() )
                            {
                                // don't we support NIO?
                                if ( !this._serverInfo.hasNonBlockingReadWrite() )
                                {
                                    if ( !clientInfo.hasAttachment() ) continue;
                                    // prepare the channel for upcoming blocking
                                    // network operations
                                    cChannel.configureBlocking( true );
                                }

                                // schedule a asynchronious socket-write
                                // operation
                                this._writePool
View Full Code Here

Examples of java.nio.channels.SocketChannel

        {
            // clean out cancelled key list
            this._selector.selectNow();
            Object[] arr = null;
            ConnectionHeader header = null;
            SocketChannel channel = null;
            SelectionKey sk = null;

            for ( ; count < size; count++ )
            {
                arr = (Object[]) this._aspirants.remove( 0 );
                header = (ConnectionHeader) arr[0];
                channel = header.getChannel();
                // set a previously blocking socket channel to non-blocking
                // mode to allow selector operations on it
                if ( channel.isBlocking() )
                {
                    logger.log( Level.FINEST,
                                "Setting socket temporarily to non-blocking until further connection processing..." );
                    channel.configureBlocking( false );
                }

                sk = channel.keyFor( this._selector );
                if ( sk == null )
                {
                    // register with no interest
                    sk = channel.register( this._selector, 0 );
                }

                // attach the client connection header
                sk.attach( header );
                // now set the requested interest
View Full Code Here

Examples of org.jboss.netty.channel.socket.SocketChannel

            }
        });
    }

    void connectReal(final SocketAddress remoteAddress, final ChannelFuture future) {
        final SocketChannel virtualChannel = this;
        realChannel.connect(remoteAddress).addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture f) {
                final String serverName = config.getServerName();
                final int serverPort = ((InetSocketAddress) remoteAddress).getPort();
                final String serverPath = config.getServerPath();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.