View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }