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 org.apache.mina.common.IdleStatus;
32  import org.apache.mina.protocol.ProtocolHandler;
33  import org.apache.mina.protocol.ProtocolSession;
34  
35  import org.apache.log4j.Logger;
36  
37  import uk.ac.rdg.resc.jstyx.messages.StyxMessage;
38  
39  /***
40   * Protocol handler for the StyxInterloperClient
41   *
42   * @author Jon Blower
43   * $Revision: 199 $
44   * $Date: 2005-05-05 17:57:38 +0100 (Thu, 05 May 2005) $
45   * $Log$
46   * Revision 1.3  2005/05/05 16:57:37  jonblower
47   * Updated MINA library to revision 168337 and changed code accordingly
48   *
49   * Revision 1.2  2005/03/11 14:01:59  jonblower
50   * Merged MINA-Test_20059309 into main line of development
51   *
52   * Revision 1.1.2.2  2005/03/11 08:30:30  jonblower
53   * Moved to log4j logging system (from apache commons logging)
54   *
55   * Revision 1.1.2.1  2005/03/10 14:30:48  jonblower
56   * Replaced SessionListeners with ProtocolHandlers
57   *
58   * Revision 1.1.1.1  2005/02/16 18:58:26  jonblower
59   * Initial import
60   *
61   */
62  class StyxInterloperClientProtocolHandler implements ProtocolHandler
63  {
64      private static final Logger log = Logger.getLogger(StyxInterloperClientProtocolHandler.class);
65      
66      private ProtocolSession serverSession;
67      private InterloperListener listener;
68      
69      private boolean connected;
70      
71      public StyxInterloperClientProtocolHandler(ProtocolSession serverSession,
72          InterloperListener listener)
73      {
74          this.connected = false;
75          this.serverSession = serverSession;
76          this.listener = listener;
77      }
78      
79      /***
80       * Invoked when the session is created.  Initialize default socket
81       * parameters and user-defined attributes here.
82       */
83      public void sessionCreated( ProtocolSession session ) throws Exception
84      {
85          // TODO: not sure what we're supposed to do in this method
86          log.info("Destination connection created.");
87      }
88      
89      public void sessionOpened(ProtocolSession session )
90      {
91          log.info("Destination connection established.");
92          this.connected = true;
93      }
94      
95      public void sessionClosed(ProtocolSession session )
96      {
97          log.info("Destination connection closed.");
98          this.connected = false;
99      }
100     
101     public void messageReceived(ProtocolSession session, Object message )
102     {
103         if (log.isDebugEnabled())
104         {
105             log.debug("RCVD from destination: " + message);
106         }
107         // Have to notify the listener before we write the message back to the
108         // client, otherwise we can get a situation where the interloper thinks
109         // that there are more than one message outstanding with the same tag.
110         this.listener.rMessageSent((StyxMessage)message);
111         this.serverSession.write(message);
112     }
113     
114     public void messageSent( ProtocolSession session, Object message )
115     {
116         if (log.isDebugEnabled())
117         {
118             log.debug("SENT to destination: " + message);
119         }
120     }
121     
122     public void sessionIdle( ProtocolSession session, IdleStatus status )
123     {
124         // Sessions are never disconnected if they are idle - is this OK?
125     }
126     
127     public void exceptionCaught( ProtocolSession session, Throwable cause )
128     {
129         log.error(cause.getMessage());
130     }
131     
132     public boolean isConnected()
133     {
134         return this.connected;
135     }
136     
137 }