1   /*
2    * Copyright 2002-2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.pdark.dsmp;
17  
18  import java.io.IOException;
19  import java.net.ServerSocket;
20  import java.net.Socket;
21  
22  import org.apache.log4j.Logger;
23  
24  /**
25   * Wait for connections from somewhere and pass them on to <code>RequestHandler</code>
26   * for processing.
27   * 
28   * @author digulla
29   *
30   */
31  public class Server
32  {
33      public static final Logger log = Logger.getLogger(Server.class);
34      
35      private int port;
36      private ServerSocket socket;
37      
38      public Server () throws IOException
39      {
40          port = Config.getPort();
41          
42          log.info("Opening connection on port "+port);
43          socket = new ServerSocket (port);
44      }
45  
46      private static boolean run = true;
47      
48      public void terminateAll ()
49      {
50          // TODO Implement a way to gracefully stop the proxy
51          run = false;
52      }
53      
54      public void handleRequests ()
55      {
56          while (run)
57          {
58              Socket clientSocket;
59  
60              try
61              {
62                  clientSocket = socket.accept();
63              }
64              catch (IOException e)
65              {
66                  log.error("Error acception connection from client", e);
67                  continue;
68              }
69              
70              Config.reload ();
71              Thread t = new RequestHandler (clientSocket);
72              t.start();
73          }
74          
75          try
76          {
77              socket.close();
78          }
79          catch (IOException e)
80          {
81              log.error("Error closing server socket", e);
82          }
83      }
84      
85  }