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 import java.io.IOException;
33
34 import org.apache.mina.io.IoFilter;
35 import org.apache.mina.io.filter.IoThreadPoolFilter;
36 import org.apache.mina.io.socket.SocketConnector;
37 import org.apache.mina.protocol.ProtocolHandler;
38 import org.apache.mina.protocol.ProtocolFilter;
39 import org.apache.mina.protocol.ProtocolProvider;
40 import org.apache.mina.protocol.ProtocolSession;
41 import org.apache.mina.protocol.filter.ProtocolThreadPoolFilter;
42 import org.apache.mina.protocol.io.IoProtocolConnector;
43
44 import org.apache.log4j.Logger;
45
46 import uk.ac.rdg.resc.jstyx.StyxUtils;
47
48 /***
49 * The client-side part of an Interloper
50 *
51 * @author Jon Blower
52 * $Revision: 313 $
53 * $Date: 2005-07-08 16:23:04 +0100 (Fri, 08 Jul 2005) $
54 * $Log$
55 * Revision 1.5 2005/07/08 15:23:03 jonblower
56 * Upgraded MINA library to 0.7.3-SNAPSHOT
57 *
58 * Revision 1.4 2005/05/05 16:57:37 jonblower
59 * Updated MINA library to revision 168337 and changed code accordingly
60 *
61 * Revision 1.3 2005/03/11 14:01:59 jonblower
62 * Merged MINA-Test_20059309 into main line of development
63 *
64 * Revision 1.2.2.2 2005/03/11 08:30:30 jonblower
65 * Moved to log4j logging system (from apache commons logging)
66 *
67 * Revision 1.2.2.1 2005/03/10 14:31:48 jonblower
68 * Modified for MINA framework
69 *
70 * Revision 1.1.1.1 2005/02/16 18:58:26 jonblower
71 * Initial import
72 *
73 */
74 public class InterloperClient
75 {
76 private static final Logger log = Logger.getLogger(InterloperClient.class);
77
78 private static final int CONNECT_TIMEOUT = 30;
79
80 private ProtocolSession session;
81 private ProtocolSession serverSession;
82 private IoThreadPoolFilter ioThreadPoolFilter;
83 private ProtocolThreadPoolFilter protocolThreadPoolFilter;
84
85 private InterloperListener listener;
86
87 private InetSocketAddress sockAddress;
88
89 /*** Creates a new instance of InterloperClient */
90 public InterloperClient(InetSocketAddress sockAddress,
91 ProtocolSession serverSession, InterloperListener listener)
92 {
93 this.sockAddress = sockAddress;
94 this.serverSession = serverSession;
95 this.listener = listener;
96 }
97
98 /***
99 * Starts the InterloperClient
100 * @return true if the client was started successfully, false otherwise
101 */
102 public boolean start()
103 {
104 this.ioThreadPoolFilter = new IoThreadPoolFilter();
105 this.protocolThreadPoolFilter = new ProtocolThreadPoolFilter();
106
107 this.ioThreadPoolFilter.start();
108 this.protocolThreadPoolFilter.start();
109
110 IoProtocolConnector connector = new IoProtocolConnector( new SocketConnector() );
111
112 connector.getIoConnector().getFilterChain().addLast( "IO Thread pool filter",
113 ioThreadPoolFilter );
114 connector.getFilterChain().addLast( "Protocol Thread pool filter",
115 protocolThreadPoolFilter );
116
117 ProtocolProvider protocolProvider =
118 new StyxInterloperProtocolProvider(this.serverSession, this.listener);
119
120 try
121 {
122 this.session = connector.connect( this.sockAddress, CONNECT_TIMEOUT,
123 protocolProvider );
124 }
125 catch( IOException e )
126 {
127 log.error("Failed to connect: " + e.getMessage());
128 return false;
129 }
130 return true;
131 }
132
133 public void stop()
134 {
135
136 this.ioThreadPoolFilter.stop();
137 this.protocolThreadPoolFilter.start();
138 }
139
140 public void send(Object message)
141 {
142 session.write(message);
143 }
144 }