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.remoting.samples.bisocket;
24
import java.lang.reflect.Field;
25
import java.net.InetAddress;
26
import java.util.HashMap;
27
import java.util.List;
30
import org.apache.log4j.ConsoleAppender;
31
import org.apache.log4j.Level;
32
import org.apache.log4j.Logger;
33
import org.apache.log4j.PatternLayout;
34
import org.jboss.logging.XLevel;
35
import org.jboss.remoting.Client;
36
import org.jboss.remoting.InvokerLocator;
37
import org.jboss.remoting.callback.Callback;
38
import org.jboss.remoting.callback.HandleCallbackException;
39
import org.jboss.remoting.callback.InvokerCallbackHandler;
40
import org.jboss.remoting.transport.Connector;
41
import org.jboss.remoting.transport.bisocket.Bisocket;
42
import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
43
import org.jboss.remoting.transport.socket.SocketServerInvoker;
47
* This class and org.jboss.remoting.samples.bisocket.BisocketSampleServer
48
* demonstrate how to how to make an invocation and how to set up push callbacks
49
* over the bisocket transport.
51
* The reason for the existance of the bisocket transport, which is derived from the
52
* socket transport, is to make it possible to do push callbacks to a client which
53
* is unable to create a ServerSocket, either due to security restrictions or due
54
* to the fact that it is behind a firewall.
56
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
57
* @version $Revision: 1.1 $
59
* Copyright May 1, 2008
62
public class BisocketSampleClient
64
private static Logger log = Logger.getLogger(BisocketSampleClient.class);
67
public void makeInvocation(String locatorURI) throws Throwable
70
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
71
Client client = new Client(clientLocator);
73
log.info("client is connected");
76
if ("abc".equals(client.invoke("abc")))
77
log.info("connection is good");
79
log.info("Should have gotten \"abc\" in reply");
81
// Set up push callbacks. Tell the Connector created by Client.addListener()
82
// that it is a callback Connector, which tells the BisocketServerInvoker
83
// not to create a ServerSocket.
84
HashMap metadata = new HashMap();
85
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
86
TestCallbackHandler callbackHandler = new TestCallbackHandler();
87
client.addListener(callbackHandler, metadata);
89
// Use reflection to verify that the callback BisocketServerInvoker did not
90
// create a ServerSocket.
91
Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
92
if (callbackConnectors.size() != 1)
94
log.info("There should be one callback Connector");
98
Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
99
BisocketServerInvoker serverInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
100
Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
101
field.setAccessible(true);
102
List serverSockets = (List) field.get(serverInvoker);
103
log.info("number of ServerSockets held by callback BisocketServerInvoker: " + serverSockets.size());
107
client.invoke("CALLBACK");
108
if (callbackHandler.getCounter() == 1)
109
log.info("received callback");
111
log.info("didn't receive callback");
113
client.removeListener(callbackHandler);
118
public static void main(String[] args)
120
// Configure logging.
121
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
122
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
123
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
124
PatternLayout layout = new PatternLayout(pattern);
125
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
126
Logger.getRootLogger().addAppender(consoleAppender);
130
String host = InetAddress.getLocalHost().getHostName();
131
int port = BisocketSampleServer.port;
132
String locatorURI = "bisocket://" + host + ":" + port;
133
BisocketSampleClient client = new BisocketSampleClient();
134
client.makeInvocation(locatorURI);
143
* An InvokerCallbackHandler is registered with the callback Connector, which
144
* passes push callbacks to it by way of the handleCallback() method.
147
static class TestCallbackHandler implements InvokerCallbackHandler
151
public void handleCallback(Callback callback) throws HandleCallbackException
156
public int getCounter()
b'\\ No newline at end of file'