PageRenderTime 37ms CodeModel.GetById 11ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/razpubs/src/org/cybergarage/http/HTTPServer.java

http://razpub.googlecode.com/
Java | 216 lines | 143 code | 35 blank | 38 comment | 16 complexity | ac0c1fd0dc054e365324d2fca5e45411 MD5 | raw file
  1/******************************************************************
  2*
  3*   CyberHTTP for Java
  4*
  5*   Copyright (C) Satoshi Konno 2002-2003
  6*
  7*   File: HTTPServer.java
  8*
  9*   Revision;
 10*
 11*   12/12/02
 12*       - first revision.
 13*   10/20/03
 14*       - Improved the HTTP server using multithreading.
 15*   08/27/04
 16*       - Changed accept() to set a default timeout, HTTP.DEFAULT_TIMEOUT, to the socket.
 17*   
 18******************************************************************/
 19
 20package org.cybergarage.http;
 21
 22import java.io.*;
 23import java.net.*;
 24
 25import org.cybergarage.http.HTTP;
 26import org.cybergarage.http.HTTPRequest;
 27import org.cybergarage.http.HTTPRequestListener;
 28import org.cybergarage.http.HTTPServerThread;
 29import org.cybergarage.util.*;
 30
 31public class HTTPServer implements Runnable
 32{
 33    ////////////////////////////////////////////////
 34    //  Constants
 35    ////////////////////////////////////////////////
 36
 37    public final static String NAME = "CyberHTTP";
 38    public final static String VERSION = "1.0";
 39
 40    public final static int DEFAULT_PORT = 80;
 41
 42    public static String getName()
 43    {
 44        String osName = System.getProperty("os.name");
 45        String osVer = System.getProperty("os.version");
 46        return osName + "/"  + osVer + " " + NAME + "/" + VERSION;
 47    }
 48    
 49    ////////////////////////////////////////////////
 50    //  Constructor
 51    ////////////////////////////////////////////////
 52    
 53    public HTTPServer()
 54    {
 55        serverSock = null;
 56    }
 57
 58    ////////////////////////////////////////////////
 59    //  ServerSocket
 60    ////////////////////////////////////////////////
 61
 62    private ServerSocket serverSock = null;
 63    private InetAddress bindAddr = null;
 64    private int bindPort = 0;
 65    
 66    public ServerSocket getServerSock()
 67    {
 68        return serverSock;
 69    }
 70
 71    public String getBindAddress()
 72    {
 73            if (bindAddr == null)
 74                return "";
 75            return bindAddr.toString();
 76    }
 77
 78    public int getBindPort()
 79    {
 80        return bindPort;
 81    }
 82    
 83    ////////////////////////////////////////////////
 84    //  open/close
 85    ////////////////////////////////////////////////
 86    
 87    public boolean open(String addr, int port)
 88    {
 89        if (serverSock != null)
 90            return true;
 91        try {
 92            bindAddr = InetAddress.getByName(addr);
 93            bindPort = port;
 94            serverSock = new ServerSocket(bindPort, 0, bindAddr);
 95        }
 96        catch (IOException e) {
 97            return false;
 98        }
 99        return true;
100    }
101
102    public boolean close()
103    {
104        if (serverSock == null)
105            return true;
106        try {
107            serverSock.close();
108            serverSock = null;
109            bindAddr = null;
110            bindPort = 0;
111        }
112        catch (Exception e) {
113            Debug.warning(e);
114            return false;
115        }
116        return true;
117    }
118
119    public Socket accept()
120    {
121        if (serverSock == null)
122            return null;
123        try {
124            Socket sock = serverSock.accept();
125            sock.setSoTimeout(HTTP.DEFAULT_PORT * 1000);
126            return sock;
127        }
128        catch (Exception e) {
129            return null;
130        }
131    }
132
133    public boolean isOpened()
134    {
135        return (serverSock != null) ? true : false;
136    }
137
138    ////////////////////////////////////////////////
139    //  httpRequest
140    ////////////////////////////////////////////////
141
142    private ListenerList httpRequestListenerList = new ListenerList();
143        
144    public void addRequestListener(HTTPRequestListener listener)
145    {
146        httpRequestListenerList.add(listener);
147    }       
148
149    public void removeRequestListener(HTTPRequestListener listener)
150    {
151        httpRequestListenerList.remove(listener);
152    }       
153
154    public void performRequestListener(HTTPRequest httpReq)
155    {
156        int listenerSize = httpRequestListenerList.size();
157        for (int n=0; n<listenerSize; n++) {
158            HTTPRequestListener listener = (HTTPRequestListener)httpRequestListenerList.get(n);
159            listener.httpRequestRecieved(httpReq);
160        }
161    }       
162    
163    ////////////////////////////////////////////////
164    //  run 
165    ////////////////////////////////////////////////
166
167    private Thread httpServerThread = null;
168        
169    public void run()
170    {
171        if (isOpened() == false)
172            return;
173            
174        Thread thisThread = Thread.currentThread();
175        
176        while (httpServerThread == thisThread) {
177            Thread.yield();
178            Socket sock;
179            try {
180                Debug.message("accept ...");
181                sock = accept();
182                if (sock != null)
183                    Debug.message("sock = " + sock.getRemoteSocketAddress());
184            }
185            catch (Exception e){
186                Debug.warning(e);
187                break;
188            }
189            HTTPServerThread httpServThread = new HTTPServerThread(this, sock);
190            
191            /* Razvan 2008-03: name the thread for debugging*/
192            httpServThread.setName("CyberHttpServ"+httpServThread.getName());
193            httpServThread.setDaemon(true);
194        
195            httpServThread.start(); 
196            Debug.message("httpServThread ...");
197        }
198    }
199    
200    public boolean start()
201    {
202        httpServerThread = new Thread(this);
203        
204        /* Razvan 2008-03: name the thread for debugging*/
205        httpServerThread.setName("CyberHttpServer"+httpServerThread.getName());
206        
207        httpServerThread.start();
208        return true;
209    }
210    
211    public boolean stop()
212    {
213        httpServerThread = null;
214        return true;
215    }
216}