2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.test.remoting.clientaddress;
24
import java.io.InputStream;
25
import java.io.OutputStream;
26
import java.net.InetAddress;
27
import java.net.ServerSocket;
28
import java.net.Socket;
29
import java.util.HashMap;
32
import javax.management.MBeanServer;
34
import junit.framework.TestCase;
36
import org.apache.log4j.ConsoleAppender;
37
import org.apache.log4j.Level;
38
import org.apache.log4j.Logger;
39
import org.apache.log4j.PatternLayout;
40
import org.jboss.remoting.Client;
41
import org.jboss.remoting.InvocationRequest;
42
import org.jboss.remoting.InvokerLocator;
43
import org.jboss.remoting.Remoting;
44
import org.jboss.remoting.ServerInvocationHandler;
45
import org.jboss.remoting.ServerInvoker;
46
import org.jboss.remoting.callback.Callback;
47
import org.jboss.remoting.callback.HandleCallbackException;
48
import org.jboss.remoting.callback.InvokerCallbackHandler;
49
import org.jboss.remoting.transport.Connector;
50
import org.jboss.remoting.transport.PortUtil;
53
public abstract class ClientAddressTestParent extends TestCase
55
private static Logger log = Logger.getLogger(ClientAddressTestParent.class);
57
private static final String GET_ADDRESS = "getAddress";
58
private static final String OPEN_CONNECTION = "openConnection";
59
private static final String COPY = "copy:";
60
private static final String SEND_CALLBACK = "sendCallback";
61
private static final int ANSWER = 17;
63
private static boolean firstTime = true;
65
protected Connector connector;
66
protected InvokerLocator serverLocator;
67
protected String locatorURI;
68
protected String host;
70
protected TestInvocationHandler invocationHandler;
71
protected int callbackPort;
74
public void setUp() throws Exception
79
Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
80
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
81
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
82
PatternLayout layout = new PatternLayout(pattern);
83
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
84
Logger.getRootLogger().addAppender(consoleAppender);
89
public void tearDown()
95
* Verifies that a valid InetAddress for the client is passed to the
96
* org.jboss.remoting.ServerInvocationHandler in the
97
* org.jboss.remoting.InvocationRequest requestPayload.
100
public void testClientAddress() throws Throwable
102
log.info("entering " + getName());
108
InvokerLocator clientLocator1 = new InvokerLocator(locatorURI);
109
HashMap clientConfig = new HashMap();
110
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
111
addExtraClientConfig(clientConfig);
112
Client client = new Client(clientLocator1, clientConfig);
114
log.info("client is connected");
116
// Test client address facility.
117
final InetAddress localAddressFromServer = (InetAddress) client.invoke(GET_ADDRESS);
118
log.info("local address according to server: " + localAddressFromServer);
119
final int randomPort = PortUtil.findFreePort(localAddressFromServer.getHostAddress());
120
callbackPort = randomPort;
122
class TestThread extends Thread
124
public int result = -1;
130
ServerSocket ss = new ServerSocket(randomPort, 200, localAddressFromServer);
131
log.info("created ServerSocket bound to: " + ss.getLocalSocketAddress());
132
Socket s = ss.accept();
133
log.info("accepted socket: " + s);
134
InputStream is = s.getInputStream();
136
log.info("read: " + result);
147
TestThread t = new TestThread();
150
HashMap metadata = new HashMap();
151
metadata.put("callbackPort", new Integer(callbackPort));
152
client.invoke(OPEN_CONNECTION, metadata);
154
assertEquals(ANSWER, t.result);
158
log.info(getName() + " PASSES");
163
* Verifies that the address returned by
164
* org.jboss.remoting.Client.getAddressSeenByServer() is valid.
166
public void testGetAddressSeenByServer() throws Throwable
168
log.info("entering " + getName());
174
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
175
HashMap clientConfig = new HashMap();
176
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
177
addExtraClientConfig(clientConfig);
178
Client client = new Client(clientLocator, clientConfig);
180
log.info("client is connected: " + locatorURI);
183
assertEquals("abc", client.invoke("copy:abc"));
184
log.info("connection is good");
186
// Get address as seen by server and create callback Connector with that address.
187
InetAddress callbackAddress = client.getAddressSeenByServer();
188
log.info("client address seen by server: " + callbackAddress);
189
String callbackHost = callbackAddress.getHostAddress();
190
int callbackPort = PortUtil.findFreePort(callbackHost);
191
String callbackLocatorURI = getCallbackTransport() + "://" + callbackHost + ":" + callbackPort;
192
callbackLocatorURI += "/?timeout=10000";
193
InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
194
log.info("callback locator: " + callbackLocator);
195
HashMap callbackConfig = new HashMap();
196
addExtraCallbackConfig(callbackConfig);
197
Connector callbackConnector = new Connector(callbackLocator, callbackConfig);
198
callbackConnector.start();
199
TestCallbackHandler callbackHandler = new TestCallbackHandler();
200
client.addListener(callbackHandler, callbackLocator);
202
// Tell server to send a callback, and verify it was received.
203
client.invoke(SEND_CALLBACK);
204
assertEquals(1, callbackHandler.counter);
206
client.removeListener(callbackHandler);
207
callbackConnector.stop();
210
log.info(getName() + " PASSES");
214
protected abstract String getTransport();
217
protected String getCallbackTransport()
219
return getTransport();
223
protected void setupServer() throws Exception
225
host = InetAddress.getLocalHost().getHostAddress();
226
port = PortUtil.findFreePort(host);
227
locatorURI = getTransport() + "://" + host + ":" + port;
228
serverLocator = new InvokerLocator(locatorURI);
229
log.info("Starting remoting server with locator uri of: " + locatorURI);
230
HashMap config = new HashMap();
231
config.put(InvokerLocator.FORCE_REMOTE, "true");
232
addExtraServerConfig(config);
233
connector = new Connector(serverLocator, config);
235
invocationHandler = new TestInvocationHandler();
236
connector.addInvocationHandler("test", invocationHandler);
241
protected void shutdownServer()
243
if (connector != null)
248
protected String reconstructLocator(InetAddress address)
250
return getTransport() + "://" + address.getHostAddress() + ":" + port;
254
protected void addExtraClientConfig(Map config) {}
255
protected void addExtraServerConfig(Map config) {}
256
protected void addExtraCallbackConfig(Map config) {}
259
class TestInvocationHandler implements ServerInvocationHandler
261
private InvokerCallbackHandler callbackHandler;
263
public void addListener(InvokerCallbackHandler callbackHandler)
265
this.callbackHandler = callbackHandler;
268
public Object invoke(final InvocationRequest invocation) throws Throwable
270
Object o = invocation.getParameter();
271
if (! (o instanceof String))
272
throw new Exception("command should be a String: " + o);
274
String command = (String) o;
275
log.info("command: " + command);
277
if (GET_ADDRESS.equals(command))
279
return invocation.getRequestPayload().get(Remoting.CLIENT_ADDRESS);
281
else if (OPEN_CONNECTION.equals(command))
283
InetAddress addr = (InetAddress) invocation.getRequestPayload().get(Remoting.CLIENT_ADDRESS);
284
log.info("creating socket connected to: " + addr);
285
Integer callbackPortInt = (Integer) invocation.getRequestPayload().get("callbackPort");
286
int callbackPort = callbackPortInt.intValue();
287
Socket s = new Socket(addr, callbackPort);
288
log.info("created socket connected to: " + addr);
289
OutputStream os = s.getOutputStream();
291
log.info("wrote answer");
295
else if (SEND_CALLBACK.equals(command))
297
callbackHandler.handleCallback(new Callback("callback"));
300
else if (command.startsWith(COPY))
302
return command.substring(5);
306
throw new Exception("unrecognized command: " + command);
310
public void removeListener(InvokerCallbackHandler callbackHandler) {}
311
public void setMBeanServer(MBeanServer server) {}
312
public void setInvoker(ServerInvoker invoker) {}
316
class TestCallbackHandler implements InvokerCallbackHandler
320
public void handleCallback(Callback callback) throws HandleCallbackException
323
log.info("received callback");
b'\\ No newline at end of file'