1 package be
.nikiroo
.utils
.serial
.server
;
3 import java
.io
.IOException
;
4 import java
.net
.Socket
;
6 import be
.nikiroo
.utils
.TraceHandler
;
7 import be
.nikiroo
.utils
.Version
;
8 import be
.nikiroo
.utils
.serial
.Importer
;
11 * This class implements a simple server that can bridge two other
14 * It can, of course, inspect the data that goes through it (by default, it
15 * prints traces of the data).
17 * Note: this {@link ServerBridge} has to be discarded after use (cannot be
22 public class ServerBridge
extends Server
{
23 private final String forwardToHost
;
24 private final int forwardToPort
;
25 private final boolean forwardToSsl
;
28 * Create a new server that will start listening on the network when
29 * {@link ServerBridge#start()} is called.
32 * the port to listen on, or 0 to assign any unallocated port
33 * found (which can later on be queried via
34 * {@link ServerBridge#getPort()}
36 * use a SSL connection (or not)
39 * in case of I/O error
41 public ServerBridge(int port
, boolean ssl
, String forwardToHost
,
42 int forwardToPort
, boolean forwardToSsl
) throws IOException
{
44 this.forwardToHost
= forwardToHost
;
45 this.forwardToPort
= forwardToPort
;
46 this.forwardToSsl
= forwardToSsl
;
50 * Create a new server that will start listening on the network when
51 * {@link ServerBridge#start()} is called.
54 * the server name (only used for debug info and traces)
56 * the port to listen on
58 * use a SSL connection (or not)
61 * in case of I/O error
63 public ServerBridge(String name
, int port
, boolean ssl
,
64 String forwardToHost
, int forwardToPort
, boolean forwardToSsl
)
66 super(name
, port
, ssl
);
67 this.forwardToHost
= forwardToHost
;
68 this.forwardToPort
= forwardToPort
;
69 this.forwardToSsl
= forwardToSsl
;
73 * The traces handler for this {@link Server}.
76 * <li>At level 1, it will only print basic IN/OUT messages with length.</li>
77 * <li>At level 2, it will also print the data as a String.</li>
78 * <li>At level 3, it will try to interpret it (SLOW) and print the object
79 * type if possible.</li>
80 * <li>At level 4, it will try to print the {@link Object#toString()} value.
85 * the new traces handler
88 public void setTraceHandler(TraceHandler tracer
) {
89 super.setTraceHandler(tracer
);
93 protected ConnectActionServer
createConnectActionServer(Socket s
) {
94 return new ConnectActionServerString(s
) {
96 public void action(final Version clientVersion
) throws Exception
{
97 onClientContact(clientVersion
);
98 final ConnectActionServerString bridge
= this;
100 new ConnectActionClientString(forwardToHost
, forwardToPort
,
101 forwardToSsl
, clientVersion
) {
103 public void action(final Version serverVersion
)
105 onServerContact(serverVersion
);
107 for (String fromClient
= bridge
.rec(); fromClient
!= null; fromClient
= bridge
109 onRec(clientVersion
, fromClient
);
110 String fromServer
= send(fromClient
);
111 onSend(serverVersion
, fromServer
);
112 bridge
.send(fromServer
);
121 * This is the method that is called each time a client contact us.
123 * @param clientVersion
126 * the data sent by the client
128 protected void onClientContact(Version clientVersion
) {
129 getTraceHandler().trace("CLIENT " + clientVersion
);
133 * This is the method that is called each time a client contact us.
135 * @param serverVersion
138 * the data sent by the client
140 protected void onServerContact(Version serverVersion
) {
141 getTraceHandler().trace("SERVER " + serverVersion
);
145 * This is the method that is called each time a client contact us.
147 * @param clientVersion
150 * the data sent by the client
152 protected void onRec(Version clientVersion
, String data
) {
153 trace("<<< CLIENT (" + clientVersion
+ ")", data
);
157 * This is the method that is called each time the forwarded server contact
160 * @param serverVersion
163 * the data sent by the client
165 protected void onSend(Version serverVersion
, String data
) {
166 trace(">>> SERVER (" + serverVersion
+ ")", data
);
170 * Trace the data with the given prefix.
173 * the prefix (client, server, version...)
177 private void trace(String prefix
, String data
) {
178 getTraceHandler().trace(prefix
+ ": " + data
.length() + " characters",
181 if (getTraceHandler().getTraceLevel() >= 2) {
183 Object obj
= new Importer().read(data
).getValue();
185 getTraceHandler().trace("NULL", 3);
186 getTraceHandler().trace("NULL", 4);
188 getTraceHandler().trace("(" + obj
.getClass() + ")", 3);
189 getTraceHandler().trace("" + obj
.toString(), 4);
191 } catch (Exception e
) {
192 getTraceHandler().trace("(not an object)", 3);
193 getTraceHandler().trace(data
, 4);
196 getTraceHandler().trace("", 4);
201 * Start a bridge between 2 servers.
204 * an array containing:
206 * <li>The bridge name</li>
207 * <li>The bridge port</li>
208 * <li>TRUE for a ssl bridge, FALSE for plain text</li>
209 * <li>The forward server host</li>
210 * <li>The forward server port</li>
211 * <li>TRUE for a ssl forward server, FALSE for plain text</li>
212 * <li>(optional) a trace level</li>
215 public static void main(String
[] args
) {
216 final TraceHandler tracer
= new TraceHandler(true, false, 0);
218 if (args
.length
< 6) {
219 tracer
.error("Invalid syntax.\n"
220 + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level])\n"
221 + "\tname: the bridge name\n"
222 + "\tport: the bridge port\n"
223 + "\tssl: TRUE for an SSL bridge, FALSE for plain text\n"
224 + "\tfhost: the forward server host\n"
225 + "\tfport: the forward server port\n"
226 + "\tfssl: TRUE for an SSL forward server, FALSE for plain text\n"
227 + "\ttrace level: the optional trace level (default is 1)\n");
232 String name
= args
[i
++];
233 int port
= Integer
.parseInt(args
[i
++]);
234 boolean ssl
= Boolean
.parseBoolean(args
[i
++]);
235 String fhost
= args
[i
++];
236 int fport
= Integer
.parseInt(args
[i
++]);
237 boolean fssl
= Boolean
.parseBoolean(args
[i
++]);
240 if (args
.length
> 6) {
241 traceLevel
= Integer
.parseInt(args
[i
++]);
244 ServerBridge bridge
= new ServerBridge(name
, port
, ssl
, fhost
,
246 bridge
.setTraceHandler(new TraceHandler(true, true, traceLevel
));
248 } catch (Exception e
) {