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 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
99 InterloperClient client = new InterloperClient(this.destSockAddr, session, this.listener);
100 if (client.start())
101 {
102
103 session.setAttachment(client);
104 }
105 else
106 {
107
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
128
129
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
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
151
152
153
154 }
155
156 public void sessionIdle( ProtocolSession session, IdleStatus status )
157 {
158
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 }