3
* JBoss, Home of Professional Open Source
4
* Copyright 2005, JBoss Inc., and individual contributors as indicated
5
* by the @authors tag. See the copyright.txt in the distribution for a
6
* full listing of individual contributors.
8
* This is free software; you can redistribute it and/or modify it
9
* under the terms of the GNU Lesser General Public License as
10
* published by the Free Software Foundation; either version 2.1 of
11
* the License, or (at your option) any later version.
13
* This software is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this software; if not, write to the Free
20
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23
package org.jboss.test.remoting.transport.bisocket.multihome;
25
import java.lang.reflect.Field;
26
import java.net.InetAddress;
27
import java.net.NetworkInterface;
28
import java.net.ServerSocket;
29
import java.util.ArrayList;
30
import java.util.Enumeration;
31
import java.util.HashMap;
32
import java.util.HashSet;
33
import java.util.Iterator;
34
import java.util.List;
38
import org.jboss.remoting.Home;
39
import org.jboss.remoting.InvokerLocator;
40
import org.jboss.remoting.transport.AddressUtil;
41
import org.jboss.remoting.transport.Connector;
42
import org.jboss.remoting.transport.PortUtil;
43
import org.jboss.remoting.transport.bisocket.Bisocket;
44
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
45
import org.jboss.test.remoting.multihome.MultihomeTestParent;
46
import org.jboss.test.remoting.multihome.TestInvocationHandler;
50
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
51
* @version $Revision: 1.1 $
53
* Copyright Jan 8, 2008
56
public class BisocketMultihomeTestCase extends MultihomeTestParent
58
protected ArrayList interfaces = new ArrayList();
60
protected String getTransport()
65
protected void addExtraCallbackConfig(Map config)
67
config.put(Bisocket.IS_CALLBACK_SERVER, "true");
70
public void testSecondaryBindPorts() throws Throwable
72
log.info("entering " + getName());
75
localCreateLocatorURI();
76
InetAddress address = (InetAddress) interfaces.get(0);
77
int port = PortUtil.findFreePort(address.getHostAddress());
78
Set secondaryBindPorts = new HashSet();
79
secondaryBindPorts.add(new Integer(port));
80
StringBuffer sb = new StringBuffer(Integer.toString(port));
81
for (int i = 1; i < interfaces.size(); i++)
83
address = (InetAddress) interfaces.get(i);
84
port = PortUtil.findFreePort(address.getHostAddress());
85
sb.append('!').append(port);
86
secondaryBindPorts.add(new Integer(port));
88
String secondaryBindPortsString = sb.toString();
89
log.info("secondaryBindPorts: " + secondaryBindPortsString);
90
locatorURI += "&" + Bisocket.SECONDARY_BIND_PORTS + "=" + secondaryBindPortsString;
91
HashMap config = new HashMap();
92
addExtraServerConfig(config);
93
connector = new Connector(locatorURI, config);
95
invocationHandler = new TestInvocationHandler();
96
connector.addInvocationHandler("test", invocationHandler);
99
// Check bind ports in secondary locator.
100
BisocketServerInvoker invoker = (BisocketServerInvoker) connector.getServerInvoker();
101
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
102
field.setAccessible(true);
103
InvokerLocator secondaryLocator = (InvokerLocator) field.get(invoker);
104
log.info("secondaryLocator: " + secondaryLocator);
105
List homes = secondaryLocator.getHomeList();
106
Home h = (Home) homes.get(0);
107
StringBuffer sb2 = new StringBuffer(Integer.toString(h.port));
108
for (int i = 1; i < homes.size(); i++)
110
h = (Home) homes.get(i);
111
sb2.append('!').append(h.port);
113
log.info("sb2: " + sb2.toString());
114
assertEquals(secondaryBindPortsString, sb2.toString());
116
// Check bind ports in secondary server sockets.
117
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
118
field.setAccessible(true);
119
Set secondaryServerSockets = (Set) field.get(invoker);
120
assertEquals(secondaryBindPorts.size(), secondaryServerSockets.size());
121
Set ports = new HashSet();
122
Iterator it = secondaryServerSockets.iterator();
125
ServerSocket ss = (ServerSocket) it.next();
126
ports.add(new Integer(ss.getLocalPort()));
128
assertEquals(secondaryBindPorts, ports);
131
log.info(getName() + " PASSES");
135
public void testSecondaryConnectPorts() throws Throwable
137
log.info("entering " + getName());
140
localCreateLocatorURI();
141
InetAddress address = (InetAddress) interfaces.get(0);
142
int port = PortUtil.findFreePort(address.getHostAddress());
143
Set secondaryBindPorts = new HashSet();
144
secondaryBindPorts.add(new Integer(port));
145
StringBuffer sb = new StringBuffer(Integer.toString(port));
146
for (int i = 1; i < interfaces.size(); i++)
148
address = (InetAddress) interfaces.get(i);
149
port = PortUtil.findFreePort(address.getHostAddress());
150
sb.append('!').append(port);
151
secondaryBindPorts.add(new Integer(port));
153
String secondaryBindPortsString = sb.toString();
154
log.info("secondaryBindPorts: " + secondaryBindPortsString);
155
locatorURI += "&" + Bisocket.SECONDARY_BIND_PORTS + "=" + secondaryBindPortsString;
157
address = (InetAddress) interfaces.get(0);
158
port = PortUtil.findFreePort(address.getHostAddress());
159
StringBuffer sb2 = new StringBuffer(Integer.toString(port));
160
for (int i = 1; i < interfaces.size(); i++)
162
address = (InetAddress) interfaces.get(i);
163
port = PortUtil.findFreePort(address.getHostAddress());
164
sb2.append('!').append(port);
166
String secondaryConnectPortsString = sb2.toString();
167
log.info("secondaryConnectPorts: " + secondaryConnectPortsString);
168
locatorURI += "&" + Bisocket.SECONDARY_CONNECT_PORTS + "=" + secondaryConnectPortsString;
169
log.info("locatorURI: " + locatorURI);
170
HashMap config = new HashMap();
171
addExtraServerConfig(config);
172
connector = new Connector(locatorURI, config);
174
invocationHandler = new TestInvocationHandler();
175
connector.addInvocationHandler("test", invocationHandler);
178
// Check connect ports in secondary locator.
179
BisocketServerInvoker invoker = (BisocketServerInvoker) connector.getServerInvoker();
180
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
181
field.setAccessible(true);
182
InvokerLocator secondaryLocator = (InvokerLocator) field.get(invoker);
183
log.info("secondaryLocator: " + secondaryLocator);
184
List homes = secondaryLocator.getHomeList();
185
Home h = (Home) homes.get(0);
186
StringBuffer sb3 = new StringBuffer(Integer.toString(h.port));
187
for (int i = 1; i < homes.size(); i++)
189
h = (Home) homes.get(i);
190
sb3.append('!').append(h.port);
192
log.info("sb3: " + sb3.toString());
193
assertEquals(secondaryConnectPortsString, sb3.toString());
195
// Check bind ports in secondary server sockets.
196
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
197
field.setAccessible(true);
198
Set secondaryServerSockets = (Set) field.get(invoker);
199
assertEquals(secondaryBindPorts.size(), secondaryServerSockets.size());
200
Set ports = new HashSet();
201
Iterator it = secondaryServerSockets.iterator();
204
ServerSocket ss = (ServerSocket) it.next();
205
ports.add(new Integer(ss.getLocalPort()));
207
assertEquals(secondaryBindPorts, ports);
210
log.info(getName() + " PASSES");
214
public void testMismatchedSecondaryPorts() throws Throwable
216
log.info("entering " + getName());
219
localCreateLocatorURI();
220
InetAddress address = (InetAddress) interfaces.get(0);
221
int port = PortUtil.findFreePort(address.getHostAddress());
222
Set secondaryBindPorts = new HashSet();
223
secondaryBindPorts.add(new Integer(port));
224
StringBuffer sb = new StringBuffer(Integer.toString(port));
225
for (int i = 1; i < interfaces.size(); i++)
227
address = (InetAddress) interfaces.get(i);
228
port = PortUtil.findFreePort(address.getHostAddress());
229
sb.append('!').append(port);
230
secondaryBindPorts.add(new Integer(port));
232
String secondaryBindPortsString = sb.toString();
233
log.info("secondaryBindPorts: " + secondaryBindPortsString);
234
locatorURI += "&" + Bisocket.SECONDARY_BIND_PORTS + "=" + secondaryBindPortsString;
236
address = (InetAddress) interfaces.get(0);
237
port = PortUtil.findFreePort(address.getHostAddress());
238
StringBuffer sb2 = new StringBuffer(Integer.toString(port));
239
for (int i = 1; i < interfaces.size(); i++)
241
address = (InetAddress) interfaces.get(i);
242
port = PortUtil.findFreePort(address.getHostAddress());
243
sb2.append('!').append(port);
246
// Add extra secondary connect port.
247
sb2.append('!').append(9876);
248
String secondaryConnectPortsString = sb2.toString();
249
log.info("secondaryConnectPorts: " + secondaryConnectPortsString);
250
locatorURI += "&" + Bisocket.SECONDARY_CONNECT_PORTS + "=" + secondaryConnectPortsString;
251
log.info("locatorURI: " + locatorURI);
252
HashMap config = new HashMap();
253
addExtraServerConfig(config);
254
connector = new Connector(locatorURI, config);
256
invocationHandler = new TestInvocationHandler();
257
connector.addInvocationHandler("test", invocationHandler);
260
// Check bind ports in secondary locator.
261
BisocketServerInvoker invoker = (BisocketServerInvoker) connector.getServerInvoker();
262
Field field = BisocketServerInvoker.class.getDeclaredField("secondaryLocator");
263
field.setAccessible(true);
264
InvokerLocator secondaryLocator = (InvokerLocator) field.get(invoker);
265
log.info("secondaryLocator: " + secondaryLocator);
266
List homes = secondaryLocator.getHomeList();
267
Home h = (Home) homes.get(0);
268
StringBuffer sb3 = new StringBuffer(Integer.toString(h.port));
269
for (int i = 1; i < homes.size(); i++)
271
h = (Home) homes.get(i);
272
sb3.append('!').append(h.port);
274
log.info("sb3: " + sb3.toString());
275
assertEquals(secondaryBindPortsString, sb3.toString());
277
// Check bind ports in secondary server sockets.
278
field = BisocketServerInvoker.class.getDeclaredField("secondaryServerSockets");
279
field.setAccessible(true);
280
Set secondaryServerSockets = (Set) field.get(invoker);
281
assertEquals(secondaryBindPorts.size(), secondaryServerSockets.size());
282
Set ports = new HashSet();
283
Iterator it = secondaryServerSockets.iterator();
286
ServerSocket ss = (ServerSocket) it.next();
287
ports.add(new Integer(ss.getLocalPort()));
289
assertEquals(secondaryBindPorts, ports);
292
log.info(getName() + " PASSES");
296
protected void localCreateLocatorURI() throws Exception
298
StringBuffer sb = new StringBuffer();
299
Enumeration e1 = NetworkInterface.getNetworkInterfaces();
300
boolean first = true;
303
loop: while (e1.hasMoreElements())
305
NetworkInterface iface = (NetworkInterface) e1.nextElement();
306
Enumeration e2 = iface.getInetAddresses();
307
while (e2.hasMoreElements())
309
if (++counter > 5) break loop;
310
InetAddress address = (InetAddress) e2.nextElement();
311
String host = address.getHostAddress();
313
if (AddressUtil.checkAddress(host))
315
log.info("host is functional: " + host);
316
int port = PortUtil.findFreePort(host);
321
sb.append(host).append(':').append(port);
322
interfaces.add(address);
326
log.info("skipping host: " + host);
331
locatorURI = getTransport() + "://" + InvokerLocator.MULTIHOME + getPath() + "/?";
332
locatorURI += InvokerLocator.HOMES_KEY + "=" + sb.toString();