HTTPServer.java
上传用户:huihesys
上传日期:2007-01-04
资源大小:3877k
文件大小:6k
- /* CVS ID: $Id: HTTPServer.java,v 1.2 2000/04/06 08:02:02 wastl Exp $ */
- package net.wastl.webmail.standalone;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import net.wastl.webmail.server.*;
- import net.wastl.webmail.server.http.*;
- import net.wastl.webmail.ui.html.*;
- import net.wastl.webmail.debug.ErrorHandler;
- import net.wastl.webmail.config.ConfigurationListener;
- /**
- * HTTPServer.java
- *
- * Created: Tue Feb 2 12:15:48 1999
- *
- * Copyright (C) 1999-2000 Sebastian Schaffert
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- /**
- *
- *
- *
- * @author Sebastian Schaffert
- * @version $Revision: 1.2 $
- */
- public class HTTPServer extends Thread implements ConfigurationListener {
- protected boolean shutdown=false;
- protected int port;
- protected int max_connections;
- protected int current_connections;
- protected ConnectionTimer timer;
- protected ServerSocket socket;
- protected String socketstatus;
- protected WebMailServer parent;
- protected long start_time;
- protected long nr_connections=0;
- /** Don't accept connections if less than 5MB RAM available */
- protected static final long required_free_memory=5000000;
- protected HTTPServer() {
- super();
- }
- public HTTPServer(WebMailServer parent) {
- super();
- parent.getConfigScheme().configRegisterIntegerKey(this,"HTTP PORT","6789",
- "Port where the HTTP server will accept connections");
- parent.getConfigScheme().configRegisterStringKey(this,"HTTP ADDRESS","0.0.0.0",
- "Address for the HTTP Server to listen on (default: all addresses)");
- parent.getConfigScheme().configRegisterIntegerKey(this,"HTTP BACKLOG","50",
- "HTTP Server Socket Backlog (how many connections to hold in Queue)");
- parent.getConfigScheme().configRegisterIntegerKey(this,"HTTP CONNECTION LIMIT","50",
- "Maximum number of simultaneous connections (reduce to avoid server "+
- "crash on machines with low memory, 20 should be ok for medium-high load machines)");
- parent.getConfigScheme().configRegisterIntegerKey(this,"HTTP TIMEOUT","60000",
- "Timeout in ms for a HTTP connection (default 1 minute)");
- System.err.print("- HTTP Server ...");
- this.port=port;
- this.timer=parent.getConnectionTimer();
- this.parent=parent;
- port=6789;
- try {
- port=Integer.parseInt(parent.getStorage().getConfig("HTTP PORT"));
- } catch(NumberFormatException e) {
- }
- max_connections=50;
- current_connections=0;
- try {
- max_connections=Integer.parseInt(parent.getStorage().getConfig("HTTP CONNECTION LIMIT"));
- } catch(NumberFormatException e) {
- }
- int backlog=50;
- try {
- backlog=Integer.parseInt(parent.getStorage().getConfig("HTTP BACKLOG"));
- } catch(NumberFormatException e) {
- }
- try {
- socket=new ServerSocket(port,backlog,InetAddress.getByName(parent.getStorage().getConfig("HTTP ADDRESS")));
- // Only block 1 second so we may shut down properly and in time.
- socket.setSoTimeout(1000);
- this.start();
- } catch(IOException ex) {
- new ErrorHandler(ex);
- }
- System.err.println(" initialization complete. Listening on port "+port+".");
- start_time=System.currentTimeMillis();
- }
-
- public WebMailServer getParent() {
- return parent;
- }
- public void shutdown() {
- shutdown=true;
- System.err.print("- HTTP Server shutdown requested ...");
- this.interrupt();
- try {
- socket.close();
- } catch(Exception e) {}
- socket=null;
- timer.removeAll();
- try {
- // Give connections enough time to terminate
- sleep(2000);
- } catch(InterruptedException ex) {}
- //this.stop();
- System.err.println("complete!");
- }
- public String getStatus() {
- String alive=isAlive()?"alive":"not alive";
- // Disabled because it hangs on certain systems
- //String status="HTTP Server listening on "+socket.getInetAddress()+", Port "+port;
- String status="HTTP Server listening on "+parent.getStorage().getConfig("HTTP ADDRESS")+", Port "+port;
- long up=System.currentTimeMillis()-start_time;
- status+="nUptime: "+up/1000+" secondsn";
- status+="Number of connections so far: "+nr_connections+", average "+ (nr_connections*60000/up) +" conn/minn";
- status+="There are currently "+current_connections+" connections out of a maximum of "+max_connections+".n";
-
- return status;
- }
- public synchronized void lockConnection() {
- current_connections++;
- //System.err.println("Added connection. Now "+current_connections+" connected.");
- }
- public synchronized void unlockConnection() {
- current_connections--;
- //System.err.println("Removed connection. Now "+current_connections+" connected.");
- this.notifyAll();
- }
- public void notifyConfigurationChange(String key) {
- parent.reinitServer("HTTP");
- }
- public void run() {
- while(!shutdown) {
- /* Enter critical resource. Connections must release this on termination! */
- if(current_connections < max_connections) {
-
- try {
- if(Runtime.getRuntime().freeMemory() > required_free_memory) {
- Connection conn=new Connection(socket.accept(),parent,this);
- nr_connections++;
- } else {
- parent.getStorage().log(Storage.LOG_ERR,"Error: Ran out of memory. Garbage collecting. You might want to increase the minimum memory size in webmail.sh to avoid this.");
- System.gc();
- }
- } catch(InterruptedIOException ex) {
- // this is valid if the server is shut down
- //System.err.println("Interrupted!");
- } catch(IOException ex) {
- if(!ex.getMessage().equals("Socket closed")) {
- new ErrorHandler(ex);
- }
- }
- } else {
- parent.getStorage().log(Storage.LOG_DEBUG,"HTTP Server: Maximum number of HTTP connections reached. You might want to increase the HTTP CONNECTION LIMIT if your server is fast enough");
- try {
- synchronized(this) {
- wait();
- }
- } catch(InterruptedException ex) {}
- }
- }
- }
- } // Server