ConfigItem easier to extends
[fanfix.git] / main / bridge.java
1 package be.nikiroo.utils.main;
2
3 import be.nikiroo.utils.TraceHandler;
4 import be.nikiroo.utils.serial.server.ServerBridge;
5
6 /**
7 * Serialiser bridge (starts a {@link ServerBridge} and can thus intercept
8 * communication between a client and a server).
9 *
10 * @author niki
11 */
12 public class bridge {
13 /**
14 * The optional options that can be passed to the program.
15 *
16 * @author niki
17 */
18 private enum Option {
19 /**
20 * The encryption key for the input data (optional, but can also be
21 * empty <b>which is different</b> (it will then use an empty encryption
22 * key)).
23 */
24 KEY,
25 /**
26 * The encryption key for the output data (optional, but can also be
27 * empty <b>which is different</b> (it will then use an empty encryption
28 * key)).
29 */
30 FORWARD_KEY,
31 /** The trace level (1, 2, 3.. default is 1). */
32 TRACE_LEVEL,
33 /**
34 * The maximum length after which to truncate data to display (the whole
35 * data will still be sent).
36 */
37 MAX_DISPLAY_SIZE,
38 /** The help message. */
39 HELP,
40 }
41
42 static private String getSyntax() {
43 return "Syntax: (--options) (--) [NAME] [PORT] [FORWARD_HOST] [FORWARD_PORT]\n"//
44 + "\tNAME : the bridge name for display/debug purposes\n"//
45 + "\tPORT : the port to listen on\n"//
46 + "\tFORWARD_HOST : the host to connect to\n"//
47 + "\tFORWARD_PORT : the port to connect to\n"//
48 + "\n" //
49 + "\tOptions: \n" //
50 + "\t-- : no more options in the rest of the parameters\n" //
51 + "\t--help : this help message\n" //
52 + "\t--key : the INCOMING encryption key\n" //
53 + "\t--forward-key : the OUTGOING encryption key\n" //
54 + "\t--trace-level : the trace level (1, 2, 3... default is 1)\n" //
55 + "\t--max-display-size : the maximum size after which to \n"//
56 + "\t truncate the messages to display (the full message will still be sent)\n" //
57 ;
58 }
59
60 /**
61 * Start a bridge between 2 servers.
62 *
63 * @param args
64 * the parameters, which can be seen by passing "--help" or just
65 * calling the program without parameters
66 */
67 public static void main(String[] args) {
68 final TraceHandler tracer = new TraceHandler(true, false, 0);
69 try {
70 if (args.length == 0) {
71 tracer.error(getSyntax());
72 System.exit(0);
73 }
74
75 String key = null;
76 String fkey = null;
77 int traceLevel = 1;
78 int maxPrintSize = 0;
79
80 int i = 0;
81 while (args[i].startsWith("--")) {
82 String arg = args[i];
83 i++;
84
85 if (arg.equals("--")) {
86 break;
87 }
88
89 arg = arg.substring(2).toUpperCase().replace("-", "_");
90 try {
91 Option opt = Enum.valueOf(Option.class, arg);
92 switch (opt) {
93 case HELP:
94 tracer.trace(getSyntax());
95 System.exit(0);
96 break;
97 case FORWARD_KEY:
98 fkey = args[i++];
99 break;
100 case KEY:
101 key = args[i++];
102 break;
103 case MAX_DISPLAY_SIZE:
104 maxPrintSize = Integer.parseInt(args[i++]);
105 break;
106 case TRACE_LEVEL:
107 traceLevel = Integer.parseInt(args[i++]);
108 break;
109 }
110 } catch (Exception e) {
111 tracer.error(getSyntax());
112 System.exit(1);
113 }
114 }
115
116 if ((args.length - i) != 4) {
117 tracer.error(getSyntax());
118 System.exit(2);
119 }
120
121 String name = args[i++];
122 int port = Integer.parseInt(args[i++]);
123 String fhost = args[i++];
124 int fport = Integer.parseInt(args[i++]);
125
126 ServerBridge bridge = new ServerBridge(name, port, key, fhost,
127 fport, fkey);
128 bridge.setTraceHandler(new TraceHandler(true, true, traceLevel,
129 maxPrintSize));
130 bridge.run();
131 } catch (Exception e) {
132 tracer.error(e);
133 System.exit(42);
134 }
135 }
136 }