1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
package net.mtu.eggplant.util.network; |
29 | |
|
30 | |
import java.io.BufferedReader; |
31 | |
import java.io.IOException; |
32 | |
import java.io.InputStreamReader; |
33 | |
import java.io.PrintWriter; |
34 | |
import java.net.ServerSocket; |
35 | |
import java.net.Socket; |
36 | |
|
37 | |
import org.slf4j.Logger; |
38 | |
import org.slf4j.LoggerFactory; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class TCPServer extends Object implements |
46 | |
Runnable, |
47 | |
Cloneable { |
48 | |
|
49 | 0 | private static final Logger LOGGER = LoggerFactory.getLogger(TCPServer.class); |
50 | |
|
51 | |
public static final int DEFAULT_PORT = 6789; |
52 | |
|
53 | |
private int _port; |
54 | |
|
55 | |
private ServerSocket _listenSocket; |
56 | |
|
57 | 0 | private Socket _clientSocket = null; |
58 | |
|
59 | 0 | private BufferedReader _input = null; |
60 | |
|
61 | 0 | private PrintWriter _output = null; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public TCPServer() { |
67 | 0 | this(DEFAULT_PORT); |
68 | 0 | } |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 0 | public TCPServer(final int port) { |
78 | 0 | if (port < 0 || port > 65535) { |
79 | 0 | _port = DEFAULT_PORT; |
80 | |
} else { |
81 | 0 | _port = port; |
82 | |
} |
83 | |
|
84 | |
try { |
85 | 0 | _listenSocket = new ServerSocket(_port); |
86 | 0 | } catch (final IOException e) { |
87 | 0 | LOGGER.error("Exception creating server socket", e); |
88 | 0 | } |
89 | 0 | LOGGER.error("Server: listening on port " + _port); |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public void run() { |
98 | |
|
99 | 0 | if (_clientSocket != null) { |
100 | 0 | initializeConnection(); |
101 | |
} |
102 | |
|
103 | |
try { |
104 | |
while (true) { |
105 | 0 | Socket clientSocket = _listenSocket.accept(); |
106 | |
try { |
107 | 0 | final TCPServer clone = (TCPServer) this.clone(); |
108 | 0 | clone.setSocket(clientSocket); |
109 | 0 | final ThreadGroup tg = new ThreadGroup(clientSocket.getInetAddress() |
110 | 0 | .getHostName()); |
111 | 0 | Thread t = new Thread(tg, clone); |
112 | 0 | t.start(); |
113 | 0 | } catch (final CloneNotSupportedException cnse) { |
114 | 0 | LOGGER.error("Clone Not supported?!"); |
115 | 0 | } |
116 | 0 | } |
117 | 0 | } catch (final IOException e) { |
118 | 0 | LOGGER.error("Exception while listening for connections", e); |
119 | |
} |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public void initializeConnection() { |
127 | |
try { |
128 | 0 | _input = new BufferedReader(new InputStreamReader(getSocket() |
129 | 0 | .getInputStream())); |
130 | |
|
131 | 0 | _output = new PrintWriter(getSocket().getOutputStream(), true); |
132 | 0 | } catch (final IOException e) { |
133 | |
try { |
134 | 0 | getSocket().close(); |
135 | 0 | } catch (final IOException e2) { |
136 | 0 | if (LOGGER.isDebugEnabled()) { |
137 | 0 | LOGGER.debug(e2.getMessage(), e2); |
138 | |
} |
139 | 0 | } |
140 | 0 | LOGGER.error("Exception while getting socket streams: " + e); |
141 | 0 | return; |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
String line; |
146 | |
try { |
147 | |
while (true) { |
148 | |
|
149 | 0 | line = readLine(); |
150 | 0 | if (processData(line)) { |
151 | 0 | break; |
152 | |
} |
153 | |
} |
154 | 0 | } catch (final IOException e) { |
155 | 0 | if (LOGGER.isDebugEnabled()) { |
156 | 0 | LOGGER.debug(e.getMessage(), e); |
157 | |
} |
158 | |
} finally { |
159 | 0 | try { |
160 | 0 | getSocket().close(); |
161 | 0 | } catch (final IOException e2) { |
162 | 0 | if (LOGGER.isDebugEnabled()) { |
163 | 0 | LOGGER.debug(e2.getMessage(), e2); |
164 | |
} |
165 | 0 | } |
166 | 0 | } |
167 | |
|
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
protected boolean processData(final String line) { |
179 | 0 | if (line.equals("Quit")) { |
180 | 0 | return true; |
181 | |
} else { |
182 | 0 | print(line); |
183 | |
} |
184 | 0 | return false; |
185 | |
} |
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public void print(final String message) { |
194 | 0 | if (_output != null) { |
195 | 0 | _output.println(getClass().getName() + ": " + message); |
196 | |
} |
197 | 0 | } |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public String readLine() throws IOException { |
205 | 0 | if (_input != null) { |
206 | 0 | return _input.readLine(); |
207 | |
} else { |
208 | 0 | return null; |
209 | |
} |
210 | |
} |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
protected Socket getSocket() { |
218 | 0 | return _clientSocket; |
219 | |
} |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
protected void setSocket(final Socket s) { |
228 | 0 | _clientSocket = s; |
229 | 0 | } |
230 | |
|
231 | |
public static void main(final String[] args) { |
232 | |
TCPServer server; |
233 | 0 | if (args.length == 1) { |
234 | |
try { |
235 | 0 | server = new TCPServer(Integer.parseInt(args[0])); |
236 | 0 | } catch (final NumberFormatException e) { |
237 | 0 | LOGGER.warn("Could not parse " + args[0] |
238 | |
+ " as a number, falling back to default port of " + DEFAULT_PORT); |
239 | 0 | server = new TCPServer(); |
240 | 0 | } |
241 | |
} else { |
242 | 0 | server = new TCPServer(); |
243 | |
} |
244 | 0 | final Thread t = new Thread(server); |
245 | 0 | t.start(); |
246 | 0 | } |
247 | |
} |