view src/org/eclipse/jetty/server/nio/SelectChannelConnector.java @ 1054:87275900646e

remove JBuffer.toString()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 08 Nov 2016 01:03:02 -0700
parents 39154cfa58e4
children
line wrap: on
line source

//
//  ========================================================================
//  Copyright (c) 1995-2014 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.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

import org.eclipse.jetty.io.AsyncEndPoint;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.nio.AsyncConnection;
import org.eclipse.jetty.io.nio.SelectChannelEndPoint;
import org.eclipse.jetty.io.nio.SelectorManager;
import org.eclipse.jetty.server.AsyncHttpConnection;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.AbstractHttpConnection;

/* ------------------------------------------------------------------------------- */
/**
 * Selecting NIO connector.
 * <p>
 * This connector uses efficient NIO buffers with a non blocking threading model. Direct NIO buffers
 * are used and threads are only allocated to connections with requests. Synchronization is used to
 * simulate blocking for the servlet API, and any unflushed content at the end of request handling
 * is written asynchronously.
 * </p>
 * <p>
 * This connector is best used when there are a many connections that have idle periods.
 * </p>
 *
 * @org.apache.xbean.XBean element="nioConnector" description="Creates an NIO based socket connector"
 */
public class SelectChannelConnector extends Connector
{
	private final SelectorManager _manager = new SelectorManager(this);

	public SelectChannelConnector(Server server,int port)
	{
		super(server,port);
		addBean(_manager,true);
	}
	
	@Override
	public final void accept() throws IOException
	{
		ServerSocketChannel server = _acceptChannel;

		if (server!=null && server.isOpen() && _manager.isStarted())
		{
			final SocketChannel channel = server.accept();
			channel.configureBlocking(false);
			Socket socket = channel.socket();
			configure(socket);
			this.server.threadPool.execute(new Runnable(){public void run(){
				_manager.register(channel);
			}});
		}
	}

	@Override
	public void customize(AbstractHttpConnection con) throws IOException
	{
		con._endp.setMaxIdleTime(_maxIdleTime);
	}

	@Override
	protected synchronized void doStart() throws Exception
	{
		if (_acceptChannel == null)
		{
			// Create a new server socket
			_acceptChannel = ServerSocketChannel.open();
			// Set to blocking mode
			_acceptChannel.configureBlocking(true);

			// Bind the server socket to the local host and port
			_acceptChannel.socket().setReuseAddress(true);
			InetSocketAddress addr = getHost()==null?new InetSocketAddress(port):new InetSocketAddress(getHost(),port);
			_acceptChannel.bind(addr);
			if( _acceptChannel.socket().getLocalPort() != port )
				throw new IOException("Server channel not bound");
		}

		super.doStart();
	}

	public AsyncConnection newConnection(SocketChannel channel,AsyncEndPoint endpoint)
	{
		return new AsyncHttpConnection(this,endpoint);
	}
}