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 java.net.InetSocketAddress;
32  
33  import org.apache.mina.common.IdleStatus;
34  import org.apache.mina.protocol.ProtocolHandler;
35  import org.apache.mina.protocol.ProtocolSession;
36  
37  import org.apache.log4j.Logger;
38  
39  import uk.ac.rdg.resc.jstyx.messages.TversionMessage;
40  import uk.ac.rdg.resc.jstyx.messages.StyxMessage;
41  import uk.ac.rdg.resc.jstyx.StyxUtils;
42  
43  /***
44   * Protocol handler for the StyxInterloper server
45   *
46   * @author Jon Blower
47   * $Revision: 199 $
48   * $Date: 2005-05-05 17:57:38 +0100 (Thu, 05 May 2005) $
49   * $Log$
50   * Revision 1.5  2005/05/05 16:57:37  jonblower
51   * Updated MINA library to revision 168337 and changed code accordingly
52   *
53   * Revision 1.4  2005/03/16 17:55:53  jonblower
54   * Replaced use of java.nio.ByteBuffer with MINA's ByteBuffer to minimise copying of buffers
55   *
56   * Revision 1.3  2005/03/15 15:51:41  jonblower
57   * Removed hard limit on maximum message size
58   *
59   * Revision 1.2  2005/03/11 14:02:15  jonblower
60   * Merged MINA-Test_20059309 into main line of development
61   *
62   * Revision 1.1.2.2  2005/03/11 08:30:30  jonblower
63   * Moved to log4j logging system (from apache commons logging)
64   *
65   * Revision 1.1.2.1  2005/03/10 14:30:48  jonblower
66   * Replaced SessionListeners with ProtocolHandlers
67   *
68   * Revision 1.1.1.1  2005/02/16 18:58:26  jonblower
69   * Initial import
70   *
71   */
72  class StyxInterloperServerProtocolHandler implements ProtocolHandler
73  {
74      private static final Logger log = Logger.getLogger(StyxInterloperServerProtocolHandler.class);
75      
76      private InetSocketAddress destSockAddr;
77      private InterloperListener listener;
78      
79      public StyxInterloperServerProtocolHandler(InetSocketAddress destSockAddr,
80          InterloperListener listener)
81      {
82          this.destSockAddr = destSockAddr;
83          this.listener = listener;
84      }
85      
86      /***
87       * Invoked when the session is created.  Initialize default socket
88       * parameters and user-defined attributes here.
89       */
90      public void sessionCreated( ProtocolSession session ) throws Exception
91      {
92          log.info("Client connection created.");
93      }
94      
95      public void sessionOpened(ProtocolSession session )
96      {
97          log.info("Client connection established.");
98          // Now connect to the destination server
99          InterloperClient client = new InterloperClient(this.destSockAddr, session, this.listener);
100         if (client.start())
101         {
102             // client started successfully
103             session.setAttachment(client);
104         }
105         else
106         {
107             // Couldn't start the client. close the session.
108             session.close();
109         }
110     }
111     
112     public void sessionClosed(ProtocolSession session )
113     {
114         log.info("Client connection closed.");
115         InterloperClient client = (InterloperClient)session.getAttachment();
116         client.stop();
117     }
118     
119     public void messageReceived(ProtocolSession session, Object message )
120     {
121         if (log.isDebugEnabled())
122         {
123             log.debug("RCVD from client: " + message);
124         }
125         listener.tMessageReceived((StyxMessage)message);
126         
127         // Make sure that we can handle the message size that the client
128         // is requesting; if not, change the TversionMessage before we pass it
129         // through.
130         if (message instanceof TversionMessage)
131         {
132             TversionMessage tVerMsg = (TversionMessage)message;
133             if (tVerMsg.getMaxMessageSize() > StyxUtils.MAX_MESSAGE_SIZE)
134             {
135                 tVerMsg.setMaxMessageSize(StyxUtils.MAX_MESSAGE_SIZE);
136             }
137         }
138         
139         // Forward the message to the destination server
140         InterloperClient client = (InterloperClient)session.getAttachment();
141         client.send(message);
142     }
143     
144     public void messageSent( ProtocolSession session, Object message )
145     {
146         if (log.isDebugEnabled())
147         {
148             log.debug("SENT to client: " + message);
149         }
150         // We notify the InterloperListener from the StyxInterloperClientSessionListener
151         // because if we did it from here, we can end up with a situation in
152         // which the interloper thinks that there are more than one message
153         // with the same tag outstanding
154     }
155     
156     public void sessionIdle( ProtocolSession session, IdleStatus status )
157     {
158         // Sessions are never disconnected if they are idle - is this OK?
159     }
160     
161     public void exceptionCaught( ProtocolSession session, Throwable cause )
162     {
163         if (log.isDebugEnabled())
164         {
165             cause.printStackTrace();
166         }
167         log.error(cause.getMessage());
168     }
169     
170 }