-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathRouterStub.java
More file actions
295 lines (243 loc) · 9.68 KB
/
Copy pathRouterStub.java
File metadata and controls
295 lines (243 loc) · 9.68 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package org.jgroups.stack;
import org.jgroups.Address;
import org.jgroups.PhysicalAddress;
import org.jgroups.logging.Log;
import org.jgroups.logging.LogFactory;
import org.jgroups.protocols.PingData;
import org.jgroups.protocols.TUNNEL.StubReceiver;
import org.jgroups.util.Util;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
/**
* Client stub that talks to a remote GossipRouter
* @author Bela Ban
* @version $Id: RouterStub.java,v 1.62 2010/06/09 14:22:00 belaban Exp $
*/
public class RouterStub {
public static enum ConnectionStatus {INITIAL, CONNECTION_BROKEN, CONNECTION_ESTABLISHED, CONNECTED,DISCONNECTED};
private final String router_host; // name of the router host
private final int router_port; // port on which router listens on
private Socket sock=null; // socket connecting to the router
private DataOutputStream output=null;
private DataInputStream input=null;
private volatile ConnectionStatus connectionState=ConnectionStatus.INITIAL;
private static final Log log=LogFactory.getLog(RouterStub.class);
private final ConnectionListener conn_listener;
private final InetAddress bind_addr;
private int sock_conn_timeout=3000; // max number of ms to wait for socket establishment to
// GossipRouter
private int sock_read_timeout=3000; // max number of ms to wait for socket reads (0 means block
// forever, or until the sock is closed)
private boolean tcp_nodelay=true;
private StubReceiver receiver;
public interface ConnectionListener {
void connectionStatusChange(RouterStub stub, ConnectionStatus state);
}
/**
* Creates a stub for a remote Router object.
* @param routerHost The name of the router's host
* @param routerPort The router's port
* @throws SocketException
*/
public RouterStub(String routerHost, int routerPort, InetAddress bindAddress, ConnectionListener l) {
router_host=routerHost != null? routerHost : "localhost";
router_port=routerPort;
bind_addr=bindAddress;
conn_listener=l;
}
public synchronized void setReceiver(StubReceiver receiver) {
this.receiver = receiver;
}
public synchronized StubReceiver getReceiver() {
return receiver;
}
public boolean isTcpNoDelay() {
return tcp_nodelay;
}
public void setTcpNoDelay(boolean tcp_nodelay) {
this.tcp_nodelay=tcp_nodelay;
}
public synchronized void interrupt() {
if(receiver != null) {
Thread thread = receiver.getThread();
if(thread != null)
thread.interrupt();
}
}
public synchronized void join(long wait) throws InterruptedException {
if(receiver != null) {
Thread thread = receiver.getThread();
if(thread != null)
thread.join(wait);
}
}
public int getSocketConnectionTimeout() {
return sock_conn_timeout;
}
public void setSocketConnectionTimeout(int sock_conn_timeout) {
this.sock_conn_timeout=sock_conn_timeout;
}
public int getSocketReadTimeout() {
return sock_read_timeout;
}
public void setSocketReadTimeout(int sock_read_timeout) {
this.sock_read_timeout=sock_read_timeout;
}
public boolean isConnected() {
return !(connectionState == ConnectionStatus.CONNECTION_BROKEN || connectionState == ConnectionStatus.INITIAL);
}
public ConnectionStatus getConnectionStatus() {
return connectionState;
}
/**
* Register this process with the router under <code>group</code>.
* @param group The name of the group under which to register
*/
public synchronized void connect(String group, Address addr, String logical_name, List<PhysicalAddress> phys_addrs) throws Exception {
doConnect();
GossipData request=new GossipData(GossipRouter.CONNECT, group, addr, logical_name, phys_addrs);
request.writeTo(output);
output.flush();
byte result = input.readByte();
if(result == GossipRouter.CONNECT_OK) {
connectionStateChanged(ConnectionStatus.CONNECTED);
} else {
connectionStateChanged(ConnectionStatus.DISCONNECTED);
throw new Exception("Connect failed received from GR " + getGossipRouterAddress());
}
}
public synchronized void doConnect() throws Exception {
if(!isConnected()) {
try {
sock=new Socket();
sock.bind(new InetSocketAddress(bind_addr, 0));
sock.setSoTimeout(sock_read_timeout);
sock.setSoLinger(true, 2);
sock.setTcpNoDelay(tcp_nodelay);
sock.setKeepAlive(true);
Util.connect(sock, new InetSocketAddress(router_host, router_port), sock_conn_timeout);
output=new DataOutputStream(sock.getOutputStream());
input=new DataInputStream(sock.getInputStream());
connectionStateChanged(ConnectionStatus.CONNECTION_ESTABLISHED);
}
catch(Exception e) {
Util.close(sock);
Util.close(input);
Util.close(output);
connectionStateChanged(ConnectionStatus.CONNECTION_BROKEN);
throw new Exception("Could not connect to " + getGossipRouterAddress() , e);
}
}
}
/**
* Checks whether the connection is open
* @return
*/
public synchronized void checkConnection() {
GossipData request=new GossipData(GossipRouter.PING);
try {
request.writeTo(output);
output.flush();
}
catch(IOException e) {
connectionStateChanged(ConnectionStatus.CONNECTION_BROKEN);
}
}
public synchronized void disconnect(String group, Address addr) {
try {
GossipData request=new GossipData(GossipRouter.DISCONNECT, group, addr);
request.writeTo(output);
output.flush();
}
catch(Exception e) {
} finally {
connectionStateChanged(ConnectionStatus.DISCONNECTED);
}
}
public synchronized void destroy() {
try {
GossipData request = new GossipData(GossipRouter.CLOSE);
request.writeTo(output);
output.flush();
} catch (Exception e) {
} finally {
Util.close(output);
Util.close(input);
Util.close(sock);
}
}
/*
* Used only in testing, never access socket directly
*
*/
public Socket getSocket() {
return sock;
}
public synchronized List<PingData> getMembers(final String group) throws Exception {
List<PingData> retval=new ArrayList<PingData>();
try {
if(!isConnected() || input == null) throw new Exception ("not connected");
// we might get a spurious SUSPECT message from the router, just ignore it
if(input.available() > 0) // fixes https://jira.jboss.org/jira/browse/JGRP-1151
input.skipBytes(input.available());
GossipData request=new GossipData(GossipRouter.GOSSIP_GET, group, null);
request.writeTo(output);
output.flush();
short num_rsps=input.readShort();
for(int i=0; i < num_rsps; i++) {
PingData rsp=new PingData();
rsp.readFrom(input);
retval.add(rsp);
}
}
catch(Exception e) {
connectionStateChanged(ConnectionStatus.CONNECTION_BROKEN);
throw new Exception("Connection to " + getGossipRouterAddress() + " broken. Could not send GOSSIP_GET request", e);
}
return retval;
}
public InetSocketAddress getGossipRouterAddress() {
return new InetSocketAddress(router_host, router_port);
}
public String toString() {
return "RouterStub[localsocket=" + ((sock != null) ? sock.getLocalSocketAddress().toString()
: "null")+ ",router_host=" + router_host + "::" + router_port
+ ",connected=" + isConnected() + "]";
}
public void sendToAllMembers(String group, byte[] data, int offset, int length) throws Exception {
sendToMember(group, null, data, offset, length); // null destination represents mcast
}
public synchronized void sendToMember(String group, Address dest, byte[] data, int offset, int length) throws Exception {
try {
GossipData request = new GossipData(GossipRouter.MESSAGE, group, dest, data, offset, length);
request.writeTo(output);
output.flush();
} catch (Exception e) {
connectionStateChanged(ConnectionStatus.CONNECTION_BROKEN);
throw new Exception("Connection to " + getGossipRouterAddress()
+ " broken. Could not send message to " + dest, e);
}
}
public DataInputStream getInputStream() {
return input;
}
private void connectionStateChanged(ConnectionStatus newState) {
boolean notify=connectionState != newState;
connectionState=newState;
if(notify && conn_listener != null) {
try {
conn_listener.connectionStatusChange(this, newState);
}
catch(Throwable t) {
log.error("failed notifying ConnectionListener " + conn_listener, t);
}
}
}
}