Version 4.5.0
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ServerBridge.java
CommitLineData
8537d55a
NR
1package be.nikiroo.utils.serial.server;
2
3import java.io.IOException;
452f38c8 4import java.lang.reflect.Array;
8537d55a 5import java.net.Socket;
f4053377 6import java.net.UnknownHostException;
8537d55a 7
452f38c8 8import be.nikiroo.utils.StringUtils;
8537d55a
NR
9import be.nikiroo.utils.TraceHandler;
10import be.nikiroo.utils.Version;
11import be.nikiroo.utils.serial.Importer;
12
13/**
14 * This class implements a simple server that can bridge two other
15 * {@link Server}s.
16 * <p>
17 * It can, of course, inspect the data that goes through it (by default, it
18 * prints traces of the data).
19 * <p>
20 * Note: this {@link ServerBridge} has to be discarded after use (cannot be
21 * started twice).
22 *
23 * @author niki
24 */
25public class ServerBridge extends Server {
26 private final String forwardToHost;
27 private final int forwardToPort;
28 private final boolean forwardToSsl;
29
30 /**
31 * Create a new server that will start listening on the network when
32 * {@link ServerBridge#start()} is called.
33 *
34 * @param port
35 * the port to listen on, or 0 to assign any unallocated port
36 * found (which can later on be queried via
37 * {@link ServerBridge#getPort()}
38 * @param ssl
452f38c8
NR
39 * use an SSL connection (or not)
40 * @param forwardToHost
41 * the host server to forward the calls to
42 * @param forwardToPort
43 * the host port to forward the calls to
44 * @param forwardToSsl
45 * use an SSL connection for the forward server or not
8537d55a
NR
46 *
47 * @throws IOException
48 * in case of I/O error
f4053377
NR
49 * @throws UnknownHostException
50 * if the IP address of the host could not be determined
51 * @throws IllegalArgumentException
52 * if the port parameter is outside the specified range of valid
53 * port values, which is between 0 and 65535, inclusive
8537d55a
NR
54 */
55 public ServerBridge(int port, boolean ssl, String forwardToHost,
56 int forwardToPort, boolean forwardToSsl) throws IOException {
57 super(port, ssl);
58 this.forwardToHost = forwardToHost;
59 this.forwardToPort = forwardToPort;
60 this.forwardToSsl = forwardToSsl;
61 }
62
63 /**
64 * Create a new server that will start listening on the network when
65 * {@link ServerBridge#start()} is called.
66 *
67 * @param name
68 * the server name (only used for debug info and traces)
69 * @param port
70 * the port to listen on
71 * @param ssl
452f38c8
NR
72 * use an SSL connection (or not)
73 * @param forwardToHost
74 * the host server to forward the calls to
75 * @param forwardToPort
76 * the host port to forward the calls to
77 * @param forwardToSsl
78 * use an SSL connection for the forward server or not
8537d55a
NR
79 *
80 * @throws IOException
81 * in case of I/O error
f4053377
NR
82 * @throws UnknownHostException
83 * if the IP address of the host could not be determined
84 * @throws IllegalArgumentException
85 * if the port parameter is outside the specified range of valid
86 * port values, which is between 0 and 65535, inclusive
8537d55a
NR
87 */
88 public ServerBridge(String name, int port, boolean ssl,
89 String forwardToHost, int forwardToPort, boolean forwardToSsl)
90 throws IOException {
91 super(name, port, ssl);
92 this.forwardToHost = forwardToHost;
93 this.forwardToPort = forwardToPort;
94 this.forwardToSsl = forwardToSsl;
95 }
96
97 /**
98 * The traces handler for this {@link Server}.
99 * <p>
452f38c8 100 * The trace levels are handled as follow:
8537d55a 101 * <ul>
452f38c8
NR
102 * <li>1: it will only print basic IN/OUT messages with length</li>
103 * <li>2: it will try to interpret it as an object (SLOW) and print the
104 * object class if possible</li>
105 * <li>3: it will try to print the {@link Object#toString()} value, or the
106 * data if it is not an object</li>
107 * <li>4: it will also print the unzipped serialised value if it is an
108 * object</li>
8537d55a
NR
109 * </ul>
110 *
111 * @param tracer
112 * the new traces handler
113 */
114 @Override
115 public void setTraceHandler(TraceHandler tracer) {
116 super.setTraceHandler(tracer);
117 }
118
119 @Override
120 protected ConnectActionServer createConnectActionServer(Socket s) {
121 return new ConnectActionServerString(s) {
122 @Override
123 public void action(final Version clientVersion) throws Exception {
124 onClientContact(clientVersion);
125 final ConnectActionServerString bridge = this;
126
f4053377
NR
127 try {
128 new ConnectActionClientString(forwardToHost, forwardToPort,
129 forwardToSsl, clientVersion) {
130 @Override
131 public void action(final Version serverVersion)
132 throws Exception {
133 onServerContact(serverVersion);
8537d55a 134
f4053377
NR
135 for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
136 .rec()) {
137 onRec(clientVersion, fromClient);
138 String fromServer = send(fromClient);
139 onSend(serverVersion, fromServer);
140 bridge.send(fromServer);
141 }
d827da2a 142
f4053377
NR
143 getTraceHandler().trace("=== DONE", 1);
144 getTraceHandler().trace("", 1);
145 }
0ff71477 146
f4053377
NR
147 @Override
148 protected void onError(Exception e) {
149 ServerBridge.this.onError(e);
150 }
151 }.connect();
152 } catch (Exception e) {
153 ServerBridge.this.onError(e);
154 }
8537d55a
NR
155 }
156 };
157 }
158
159 /**
160 * This is the method that is called each time a client contact us.
161 *
162 * @param clientVersion
163 * the client version
8537d55a
NR
164 */
165 protected void onClientContact(Version clientVersion) {
d827da2a 166 getTraceHandler().trace(">>> CLIENT " + clientVersion);
8537d55a
NR
167 }
168
169 /**
170 * This is the method that is called each time a client contact us.
171 *
172 * @param serverVersion
173 * the server version
8537d55a
NR
174 */
175 protected void onServerContact(Version serverVersion) {
d827da2a 176 getTraceHandler().trace("<<< SERVER " + serverVersion);
217a3310 177 getTraceHandler().trace("");
8537d55a
NR
178 }
179
180 /**
181 * This is the method that is called each time a client contact us.
182 *
183 * @param clientVersion
184 * the client version
185 * @param data
186 * the data sent by the client
187 */
188 protected void onRec(Version clientVersion, String data) {
d827da2a 189 trace(">>> CLIENT (" + clientVersion + ")", data);
8537d55a
NR
190 }
191
192 /**
193 * This is the method that is called each time the forwarded server contact
194 * us.
195 *
196 * @param serverVersion
197 * the client version
198 * @param data
199 * the data sent by the client
200 */
201 protected void onSend(Version serverVersion, String data) {
d827da2a 202 trace("<<< SERVER (" + serverVersion + ")", data);
8537d55a
NR
203 }
204
f4053377
NR
205 @Override
206 public void run() {
207 getTraceHandler().trace(
208 getName() + ": will forward to " + forwardToHost + ":"
209 + forwardToPort + " ("
210 + (forwardToSsl ? "SSL" : "plain text") + ")");
211 super.run();
212 }
213
8537d55a
NR
214 /**
215 * Trace the data with the given prefix.
216 *
217 * @param prefix
218 * the prefix (client, server, version...)
219 * @param data
220 * the data to trace
221 */
222 private void trace(String prefix, String data) {
f8f2afb4 223 int size = data == null ? 0 : data.length();
452f38c8
NR
224 String ssize = size + " byte";
225 if (size > 1) {
226 ssize = size + " bytes";
227 if (size >= 1000) {
228 size = size / 1000;
229 ssize = size + " kb";
230 if (size > 1000) {
231 size = size / 1000;
232 ssize = size + " MB";
233 }
234 }
235 }
236
237 getTraceHandler().trace(prefix + ": " + ssize, 1);
8537d55a
NR
238
239 if (getTraceHandler().getTraceLevel() >= 2) {
240 try {
a359464f
NR
241 while (data.startsWith("ZIP:") || data.startsWith("B64:")) {
242 if (data.startsWith("ZIP:")) {
243 data = StringUtils.unbase64s(data.substring(4), true);
244 } else if (data.startsWith("B64:")) {
245 data = StringUtils.unbase64s(data.substring(4), false);
246 }
452f38c8
NR
247 }
248
8537d55a
NR
249 Object obj = new Importer().read(data).getValue();
250 if (obj == null) {
452f38c8 251 getTraceHandler().trace("NULL", 2);
8537d55a
NR
252 getTraceHandler().trace("NULL", 3);
253 getTraceHandler().trace("NULL", 4);
254 } else {
452f38c8
NR
255 if (obj.getClass().isArray()) {
256 getTraceHandler().trace(
257 "(" + obj.getClass() + ") with "
258 + Array.getLength(obj) + "element(s)",
259 3);
260 } else {
261 getTraceHandler().trace("(" + obj.getClass() + ")", 2);
262 }
263 getTraceHandler().trace("" + obj.toString(), 3);
264 getTraceHandler().trace(data, 4);
8537d55a 265 }
452f38c8
NR
266 } catch (NoSuchMethodException e) {
267 getTraceHandler().trace("(not an object)", 2);
268 getTraceHandler().trace(data, 3);
269 getTraceHandler().trace("", 4);
270 } catch (NoSuchFieldException e) {
271 getTraceHandler().trace(
217a3310 272 "(incompatible: " + e.getMessage() + ")", 2);
452f38c8
NR
273 getTraceHandler().trace(data, 3);
274 getTraceHandler().trace("", 4);
275 } catch (ClassNotFoundException e) {
276 getTraceHandler().trace(
277 "(unknown object: " + e.getMessage() + ")", 2);
278 getTraceHandler().trace(data, 3);
279 getTraceHandler().trace("", 4);
8537d55a 280 } catch (Exception e) {
217a3310
NR
281 getTraceHandler().trace(
282 "(decode error: " + e.getMessage() + ")", 2);
452f38c8
NR
283 getTraceHandler().trace(data, 3);
284 getTraceHandler().trace("", 4);
8537d55a
NR
285 }
286
452f38c8 287 getTraceHandler().trace("", 2);
8537d55a
NR
288 }
289 }
8537d55a 290}