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.bio;
 
 import java.util.Set;
 
 
 
 /* ------------------------------------------------------------------------------- */
Socket Connector. This connector implements a traditional blocking IO and threading model. Normal JRE sockets are used and a thread is allocated per connection. Buffers are managed so that large buffers are only allocated to active connections. This Connector should only be used if NIO is not available.

Org.apache.xbean.XBean:
element="bioConnector" description="Creates a BIO based socket connector"
 
 public class SocketConnector extends AbstractConnector
 {
     private static final Logger LOG = Log.getLogger(SocketConnector.class);
 
     protected ServerSocket _serverSocket;
     protected final Set<EndPoint_connections;
     protected volatile int _localPort=-1;
 
     /* ------------------------------------------------------------ */
    
Constructor.
 
     public SocketConnector()
     {
         =new HashSet<EndPoint>();
     }
 
     /* ------------------------------------------------------------ */
     public Object getConnection()
     {
         return ;
     }
 
     /* ------------------------------------------------------------ */
     public void open() throws IOException
     {
         // Create a new server socket and set to non blocking mode
         if (==null || .isClosed())
         if (<=0)
             throw new IllegalStateException("port not allocated for "+this);
 
     }
 
     /* ------------------------------------------------------------ */
     protected ServerSocket newServerSocket(String hostint port,int backlogthrows IOException
     {
         ServerSocket sshost==null?
             new ServerSocket(port,backlog):
             new ServerSocket(port,backlog,InetAddress.getByName(host));
 
        return ss;
    }
    /* ------------------------------------------------------------ */
    public void close() throws IOException
    {
        if (!=null)
            .close();
        =null;
        =-2;
    }
    /* ------------------------------------------------------------ */
    @Override
    public void accept(int acceptorID)
    {
        Socket socket = .accept();
        configure(socket);
        ConnectorEndPoint connection=new ConnectorEndPoint(socket);
        connection.dispatch();
    }
    /* ------------------------------------------------------------------------------- */
    
Allows subclass to override Conection if required.
    protected Connection newConnection(EndPoint endpoint)
    {
        return new BlockingHttpConnection(thisendpointgetServer());
    }
    /* ------------------------------------------------------------------------------- */
    @Override
    public void customize(EndPoint endpointRequest request)
        throws IOException
    {
        ConnectorEndPoint connection = (ConnectorEndPoint)endpoint;
        int lrmit = isLowResources()?:;
        connection.setMaxIdleTime(lrmit);
        super.customize(endpointrequest);
    }
    /* ------------------------------------------------------------------------------- */
    public int getLocalPort()
    {
        return ;
    }
    /* ------------------------------------------------------------------------------- */
    @Override
    protected void doStart() throws Exception
    {
        .clear();
        super.doStart();
    }
    /* ------------------------------------------------------------------------------- */
    @Override
    protected void doStop() throws Exception
    {
        super.doStop();
        Set<EndPointset = new HashSet<EndPoint>();
        synchronized()
        {
            set.addAll();
        }
        for (EndPoint endPoint : set)
        {
            ConnectorEndPoint connection = (ConnectorEndPoint)endPoint;
            connection.close();
        }
    }
    @Override
    public void dump(Appendable outString indentthrows IOException
    {
        super.dump(outindent);
        Set<EndPointconnections = new HashSet<EndPoint>();
        synchronized ()
        {
            connections.addAll();
        }
        AggregateLifeCycle.dump(outindentconnections);
    }
    /* ------------------------------------------------------------------------------- */
    /* ------------------------------------------------------------------------------- */
    /* ------------------------------------------------------------------------------- */
    protected class ConnectorEndPoint extends SocketEndPoint implements RunnableConnectedEndPoint
    {
        volatile Connection _connection;
        protected final Socket _socket;
        public ConnectorEndPoint(Socket socketthrows IOException
        {
            super(socket,);
             = newConnection(this);
            =socket;
        }
        public Connection getConnection()
        {
            return ;
        }
        public void setConnection(Connection connection)
        {
            if (!=connection && !=null)
                connectionUpgraded(,connection);
            =connection;
        }
        public void dispatch() throws IOException
        {
            if (getThreadPool()==null || !getThreadPool().dispatch(this))
            {
                .warn("dispatch failed for {}",);
                close();
            }
        }
        @Override
        public int fill(Buffer bufferthrows IOException
        {
            int l = super.fill(buffer);
            if (l<0)
            {
                if (!isInputShutdown())
                    shutdownInput();
                if (isOutputShutdown())
                    close();
            }
            return l;
        }
        @Override
        public void close() throws IOException
        {
            if ( instanceof AbstractHttpConnection)
            super.close();
        }
        public void run()
        {
            try
            {
                connectionOpened();
                synchronized()
                {
                    .add(this);
                }
                while (isStarted() && !isClosed())
                {
                    if (.isIdle())
                    {
                        if (isLowResources())
                            setMaxIdleTime(getLowResourcesMaxIdleTime());
                    }
                    =.handle();
                }
            }
            catch (EofException e)
            {
                .debug("EOF"e);
                try{close();}
                catch(IOException e2){.ignore(e2);}
            }
            catch (SocketException e)
            {
                .debug("EOF"e);
                try{close();}
                catch(IOException e2){.ignore(e2);}
            }
            catch (HttpException e)
            {
                .debug("BAD"e);
                try{close();}
                catch(IOException e2){.ignore(e2);}
            }
            catch(Exception e)
            {
                .warn("handle failed?",e);
                try{close();}
                catch(IOException e2){.ignore(e2);}
            }
            finally
            {
                connectionClosed();
                synchronized()
                {
                    .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);
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X