view src/org/eclipse/jetty/server/nio/SelectChannelConnector.java @ 970:d82eb99e8df6

remove ConnectorSelectorManager
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 14 Oct 2016 05:24:18 -0600
parents 0d20943cfea2
children f997df37cec1
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.Connection;
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;

/* ------------------------------------------------------------------------------- */
/**
 * 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(EndPoint endpoint, Request request) throws IOException
	{
		request.setTimeStamp(System.currentTimeMillis());
		endpoint.setMaxIdleTime(_maxIdleTime);
		super.customize(endpoint, request);
	}

	@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(SelectChannelConnector.this,endpoint,server);
	}
}