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 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
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
108
109
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
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 }