📄 httpserver.java
字号:
/************************************************************************
*
* $Id: HttpServer.java,v 1.2 2002/03/04 21:42:57 echtcherbina Exp $
*
* Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Sun Microsystems, Inc. for Project JXTA."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
* must not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact Project JXTA at http://www.jxta.org.
*
* 5. Products derived from this software may not be called "JXTA",
* nor may "JXTA" appear in their name, without prior written
* permission of Sun.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Project JXTA. For more
* information on Project JXTA, please see
* <http://www.jxta.org/>.
*
* This license is based on the BSD license adopted by the Apache Foundation.
*********************************************************************************/
package net.jxta.impl.endpoint.http;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//PDA requirements 18.02.2002
// HashMap -> Hashtable
// import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;
import java.net.BindException;
import java.io.IOException;
import org.apache.log4j.Category; import org.apache.log4j.Priority;
/**
* This thread handles incoming connections
**/
class HttpServer extends Thread {
private static final Category LOG = Category.getInstance(HttpServer.class.getName());
public static final int MaxCnxBacklog = 50; // Java's default is 50
/**
* The transport which owns this Http server.
**/
private HttpTransport owner = null;
/**
* Pool of threads used to receive incoming connections.
**/
private UnicastThreadPool unicastPool = null;
/**
* The socket we listen for connections on.
**/
private ServerSocket serverSocket;
/**
* These are connections we have with clients as a server.
**/
//PDA requirements 18.02.2002
// HashMap -> Hashtable
// private HashMap clientConnections = new Hashtable();
private Hashtable clientConnections = new Hashtable();
/**
* Manages the pool of available incoming connection threads.
**/
class UnicastThreadPool {
/**
* How many threads we should make to listen for incoming connections
**/
public static final int DefaultNbOfUnicastThreads = 5;
/**
* The maximum number of threads we will create.
**/
public static final int MaxNbOfUnicastThreads = 25;
/**
* number of unicast threads in existance. This number may go up and down
**/
private volatile int unicastThreads = 0;
/**
* Counter used for naming unicast threads. Always incrementing.
**/
private volatile int unicastThreadNumber = 0;
/**
* Pool of unicast threads which are not currently processing incoming
* socket connections.
**/
private Vector threadPool = new Vector(MaxNbOfUnicastThreads);
/**
* Initializes the pool of threads used to manage incoming unicast
* connections.
**/
public UnicastThreadPool( ) {
// DO NOT CREATE the threads in the ctor. They need a reference
// to this pool object which is not assigned until the ctor
// returns.
}
/**
* Create the initial set of threads.
*/
public void createThreads() {
createThreads( DefaultNbOfUnicastThreads );
}
/**
* Create the initial set of threads.
*
* @param initialNumber the number of initial threads to create.
*/
public void createThreads( int initialNumber ) {
initialNumber = Math.min( initialNumber, MaxNbOfUnicastThreads );
for( int i = 1; i <= initialNumber; i++ ) {
newIncomingThread( );
}
}
/**
* create a new thread for this pool.
**/
private void newIncomingThread( ) {
synchronized ( threadPool ) {
unicastThreads++;
(new IncomingUnicastThread(
Integer.toString(++unicastThreadNumber) )).start();
}
}
/**
* Process an incoming unicast socket. This consists of finding a thread
* on which to process the request and dispatching the socket to that
* thread.
*
* @param timeOut amount of time to wait for a thread
* @return IncomingUnicastThread on which can be used to handle socket
* connections.
**/
IncomingUnicastThread getIncomingThread( long timeOut )
throws InterruptedException {
IncomingUnicastThread willUse = null;
long realTimeOut = System.currentTimeMillis() + timeOut;
do {
synchronized( threadPool ) {
if( threadPool.size() > 0 ) {
willUse = (IncomingUnicastThread) (threadPool.lastElement());
threadPool.removeElementAt( threadPool.size() - 1 );
break;
}
// Make a new thread if we are not at the max
if( unicastThreads < MaxNbOfUnicastThreads ) {
newIncomingThread();
if (LOG.isEnabledFor(Priority.DEBUG)) LOG.debug( "Made a new unicast thread (#" +
unicastThreadNumber + " of " + unicastThreads + " total)" );
}
// now wait and see if we can get a thread
threadPool.wait(timeOut);
if( threadPool.size() > 0 ) {
willUse = (IncomingUnicastThread) (threadPool.lastElement());
threadPool.removeElementAt( threadPool.size() - 1 );
break;
}
if ( 0 != timeOut ) {
// reduce the wait timeOut by the amount we have already
// waited.
timeOut = (realTimeOut - System.currentTimeMillis());
if( timeOut <= 0 ) {
// we have passed the time at which we said we would stop
break; // so we give up.
}
}
}
} while( true );
return willUse;
}
/**
* put a thread into the pool
*
* @param theThread the thread being added to the pool
*
**/
public void availableThread( IncomingUnicastThread theThread ) {
synchronized( threadPool ) {
threadPool.addElement( theThread );
// tell someone who cares...
threadPool.notify();
}
}
/**
* Notify the pool that a thread has died. This may allow the pool to
* create another thread.
*
* @param theThread the thread which is dying. if currently in the
* pool then it will be removed.
*
**/
public void threadDied( IncomingUnicastThread theThread ) {
synchronized ( threadPool ) {
unicastThreads--;
// ensure that its not still in the pool
threadPool.removeElement( theThread );
}
}
}
/**
* Provides a thread on which to process an incoming socket connection
**/
class IncomingUnicastThread extends Thread {
HttpTransport tp = null;
Socket socket = null;
/**
* Default constructor. We place ourself in the transports thread
* group with the specified name.
*
* @param tp The HttpTransport we are working for.
* @param name the name of this instance.
**/
private IncomingUnicastThread( String name ) {
super( HttpServer.this.getThreadGroup(), (Runnable) null, "incoming unicast " + name );
}
/**
* Set the the socket that the thread should be working on.
**/
void setSocket( Socket theSocket ) {
socket = theSocket;
}
/**
* We wait upon ourself for someone to wake us with a socket to process
* if we are woken prematurely, that probably means we are being asked
* to quit.
**/
public void run() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -