Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server.nio;
 
 import java.util.Set;
 
 
 
 /* ------------------------------------------------------------------------------- */
Blocking NIO connector. This connector uses efficient NIO buffers with a traditional blocking thread model. Direct NIO buffers are used and a thread is allocated per connections. This connector is best used when there are a few very active connections.

Org.apache.xbean.XBean:
element="blockingNioConnector" description="Creates a blocking NIO based socket connector"
 
 {
     private static final Logger LOG = Log.getLogger(BlockingChannelConnector.class);
 
     private transient ServerSocketChannel _acceptChannel;
 
 
     /* ------------------------------------------------------------ */
    
Constructor.
 
     public BlockingChannelConnector()
     {
     }
 
     /* ------------------------------------------------------------ */
     public Object getConnection()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
    
 
     @Override
     protected void doStart() throws Exception
     {
         super.doStart();
         getThreadPool().dispatch(new Runnable()
         {
 
             public void run()
             {
                 while (isRunning())
                 {
                     try
                     {
                         Thread.sleep(400);
                         long now=System.currentTimeMillis();
                         for (BlockingChannelEndPoint endp : )
                         {
                             endp.checkIdleTimestamp(now);
                        }
                    }
                    catch(InterruptedException e)
                    {
                        .ignore(e);
                    }
                    catch(Exception e)
                    {
                        .warn(e);
                    }
                }
            }
        });
    }
    /* ------------------------------------------------------------ */
    public void open() throws IOException
    {
        // Create a new server socket and set to non blocking mode
        = ServerSocketChannel.open();
        .configureBlocking(true);
        // Bind the server socket to the local host and port
        InetSocketAddress addr = getHost()==null?new InetSocketAddress(getPort()):new InetSocketAddress(getHost(),getPort());
        .socket().bind(addr,getAcceptQueueSize());
    }
    /* ------------------------------------------------------------ */
    public void close() throws IOException
    {
        if ( != null)
            .close();
        =null;
    }
    /* ------------------------------------------------------------ */
    @Override
    public void accept(int acceptorID)
    {
        SocketChannel channel = .accept();
        channel.configureBlocking(true);
        Socket socket=channel.socket();
        configure(socket);
        BlockingChannelEndPoint connection=new BlockingChannelEndPoint(channel);
        connection.dispatch();
    }
    /* ------------------------------------------------------------------------------- */
    @Override
    public void customize(EndPoint endpointRequest request)
        throws IOException
    {
        super.customize(endpointrequest);
        endpoint.setMaxIdleTime();
        configure(((SocketChannel)endpoint.getTransport()).socket());
    }
    /* ------------------------------------------------------------------------------- */
    public int getLocalPort()
    {
        if (==null || !.isOpen())
            return -1;
        return .socket().getLocalPort();
    }
    /* ------------------------------------------------------------------------------- */
    /* ------------------------------------------------------------------------------- */
    /* ------------------------------------------------------------------------------- */
    private class BlockingChannelEndPoint extends ChannelEndPoint implements RunnableConnectedEndPoint
    {
        private Connection _connection;
        private int _timeout;
        private volatile long _idleTimestamp;
        BlockingChannelEndPoint(ByteChannel channel)
            throws IOException
        {
            super(channel,BlockingChannelConnector.this.);
             = new BlockingHttpConnection(BlockingChannelConnector.this,this,getServer());
        }
        /* ------------------------------------------------------------ */
        
Get the connection.

Returns:
the connection
        public Connection getConnection()
        {
            return ;
        }
        /* ------------------------------------------------------------ */
        public void setConnection(Connection connection)
        {
            =connection;
        }
        /* ------------------------------------------------------------ */
        public void checkIdleTimestamp(long now)
        {
            if (!=0 && >0 && now>(+))
            {
                idleExpired();
            }
        }
        /* ------------------------------------------------------------ */
        protected void idleExpired()
        {
            try
            {
                super.close();
            }
            catch (IOException e)
            {
                .ignore(e);
            }
        }
        /* ------------------------------------------------------------ */
        void dispatch() throws IOException
        {
            if (!getThreadPool().dispatch(this))
            {
                .warn("dispatch failed for  {}",);
                super.close();
            }
        }
        /* ------------------------------------------------------------ */
        
        @Override
        public int fill(Buffer bufferthrows IOException
        {
            =System.currentTimeMillis();
            return super.fill(buffer);
        }
        /* ------------------------------------------------------------ */
        
        @Override
        public int flush(Buffer bufferthrows IOException
        {
            =System.currentTimeMillis();
            return super.flush(buffer);
        }
        /* ------------------------------------------------------------ */
        
        @Override
        public int flush(Buffer headerBuffer bufferBuffer trailerthrows IOException
        {
            =System.currentTimeMillis();
            return super.flush(header,buffer,trailer);
        }
        /* ------------------------------------------------------------ */
        public void run()
        {
            try
            {
                =getMaxIdleTime();
                connectionOpened();
                .add(this);
                while (isOpen())
                {
                    =System.currentTimeMillis();
                    if (.isIdle())
                    {
                        if (getServer().getThreadPool().isLowOnThreads())
                        {
                            int lrmit = getLowResourcesMaxIdleTime();
                            if (lrmit>=0 && != lrmit)
                            {
                                =lrmit;
                            }
                        }
                    }
                    else
                    {
                        if (!=getMaxIdleTime())
                        {
                            =getMaxIdleTime();
                        }
                    }
                     = .handle();
                }
            }
            catch (EofException e)
            {
                .debug("EOF"e);
                try{BlockingChannelEndPoint.this.close();}
                catch(IOException e2){.ignore(e2);}
            }
            catch (HttpException e)
            {
                .debug("BAD"e);
                try{super.close();}
                catch(IOException e2){.ignore(e2);}
            }
            catch(Throwable e)
            {
                .warn("handle failed",e);
                try{super.close();}
                catch(IOException e2){.ignore(e2);}
            }
            finally
            {
                connectionClosed();
                .remove(this);
                // wait for client to close, but if not, close ourselves.
                try
                {
                    if (!.isClosed())
                    {
                        long timestamp=System.currentTimeMillis();
                        int max_idle=getMaxIdleTime();
                        .setSoTimeout(getMaxIdleTime());
                        int c=0;
                        do
                        {
                            c = .getInputStream().read();
                        }
                        while (c>=0 && (System.currentTimeMillis()-timestamp)<max_idle);
                        if (!.isClosed())
                            .close();
                    }
                }
                catch(IOException e)
                {
                    .ignore(e);
                }
            }
        }
        /* ------------------------------------------------------------ */
        @Override
        public String toString()
        {
            return String.format("BCEP@%x{l(%s)<->r(%s),open=%b,ishut=%b,oshut=%b}-{%s}",
                    hashCode(),
                    .getRemoteSocketAddress(),
                    .getLocalSocketAddress(),
                    isOpen(),
                    isInputShutdown(),
                    isOutputShutdown(),
                    );
        }
    }
New to GrepCode? Check out our FAQ X