1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }