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
29 package uk.ac.rdg.resc.jstyx.interloper;
30
31 import uk.ac.rdg.resc.jstyx.StyxException;
32 import uk.ac.rdg.resc.jstyx.server.StyxServer;
33 import uk.ac.rdg.resc.jstyx.messages.StyxMessage;
34 import uk.ac.rdg.resc.jstyx.StyxUtils;
35
36 import java.net.InetSocketAddress;
37 import java.io.IOException;
38
39
40 /***
41 * A StyxInterloper listens for Styx messages, then forwards them directly to
42 * another Styx server. The replies from the other Styx server are sent back to
43 * the client. This allows the Styx messages sent between systems to be
44 * investigated.
45 *
46 * @author Jon Blower
47 * $Revision: 507 $
48 * $Date: 2005-12-01 08:21:56 +0000 (Thu, 01 Dec 2005) $
49 * $Log$
50 * Revision 1.8 2005/12/01 08:21:56 jonblower
51 * Fixed javadoc comments
52 *
53 * Revision 1.7 2005/05/05 16:57:37 jonblower
54 * Updated MINA library to revision 168337 and changed code accordingly
55 *
56 * Revision 1.6 2005/03/11 14:01:59 jonblower
57 * Merged MINA-Test_20059309 into main line of development
58 *
59 * Revision 1.5.2.2 2005/03/10 20:55:37 jonblower
60 * Removed references to Netty
61 *
62 * Revision 1.5.2.1 2005/03/10 14:31:48 jonblower
63 * Modified for MINA framework
64 *
65 * Revision 1.5 2005/02/28 12:08:18 jonblower
66 * Tidied up interaction between StyxInterloper and StyxMon
67 *
68 * Revision 1.1.1.1 2005/02/16 18:58:26 jonblower
69 * Initial import
70 *
71 */
72 public class StyxInterloper implements InterloperListener
73 {
74
75 protected int port;
76 protected InetSocketAddress destSockAddr;
77 protected StyxServer styxServer;
78
79 /***
80 * Creates a new StyxInterloper.
81 * @param port The port on which this server will listen
82 * @param serverHost The host of the destination to which this server will connect
83 * @param serverPort The port of the destination to which this server will connect
84 * @throws IOException if there was an error starting the Styx server
85 */
86 public StyxInterloper(int port, String serverHost, int serverPort)
87 throws IOException
88 {
89 this.port = port;
90 InetSocketAddress destSockAddr = new InetSocketAddress(serverHost, serverPort);
91 this.styxServer = new StyxServer(port,
92 new StyxInterloperProtocolProvider(destSockAddr, this));
93 this.styxServer.start();
94 }
95
96 /***
97 * Called when a Tmessage arrives from a client. Does nothing here (the
98 * message will already have been logged)
99 */
100 public void tMessageReceived(StyxMessage message) {}
101
102 /***
103 * Called when an Rmessage has been sent back to the client. Does nothing
104 * here (the message will already have been logged)
105 */
106 public void rMessageSent(StyxMessage message) {}
107
108 public static void main (String[] args)
109 {
110 try
111 {
112 checkArgs(args);
113 new StyxInterloper(Integer.parseInt(args[0]), args[1],
114 Integer.parseInt(args[2]));
115 }
116 catch(Exception e)
117 {
118 System.err.println(e.getMessage());
119 }
120 }
121
122 /***
123 * Checks the command-line arguments, throwing an Exception if there is
124 * a problem
125 */
126 protected static void checkArgs(String[] args) throws Exception
127 {
128 if (args.length != 3)
129 {
130 throw new Exception("Usage: java StyxMon <port> <remote host> <remote port>");
131 }
132 int port;
133 int remotePort;
134 try
135 {
136 port = Integer.parseInt(args[0]);
137 if (port < 0 || port > StyxUtils.MAXUSHORT)
138 {
139 throw new Exception("Port number must be between 0 and " + StyxUtils.MAXUSHORT);
140 }
141 }
142 catch(NumberFormatException nfe)
143 {
144 throw new Exception("Invalid port number");
145 }
146 try
147 {
148 remotePort = Integer.parseInt(args[2]);
149 }
150 catch(NumberFormatException nfe)
151 {
152 throw new Exception("Invalid remote port number");
153 }
154 }
155
156 }