[jboss-remoting-commits] JBoss Remoting SVN: r4117 - in remoting2/branches/2.x/src/main/org/jboss/remoting/samples: bisocket and 1 other directory.
jboss-remoting-commits at lists.jboss.org
jboss-remoting-commits at lists.jboss.org
Thu May 1 23:16:37 EDT 2008
Author: ron.sigal at jboss.com
Date: 2008-05-01 23:16:37 -0400 (Thu, 01 May 2008)
New Revision: 4117
Added:
remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/
remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleClient.java
remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleServer.java
Log:
JBREM-773: New bisocket example.
Added: remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleClient.java
===================================================================
--- remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleClient.java (rev 0)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleClient.java 2008-05-02 03:16:37 UTC (rev 4117)
@@ -0,0 +1,161 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.remoting.samples.bisocket;
+
+import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.bisocket.Bisocket;
+import org.jboss.remoting.transport.bisocket.BisocketServerInvoker;
+import org.jboss.remoting.transport.socket.SocketServerInvoker;
+
+
+/**
+ * This class and org.jboss.remoting.samples.bisocket.BisocketSampleServer
+ * demonstrate how to how to make an invocation and how to set up push callbacks
+ * over the bisocket transport.
+ *
+ * The reason for the existance of the bisocket transport, which is derived from the
+ * socket transport, is to make it possible to do push callbacks to a client which
+ * is unable to create a ServerSocket, either due to security restrictions or due
+ * to the fact that it is behind a firewall.
+ *
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright May 1, 2008
+ * </p>
+ */
+public class BisocketSampleClient
+{
+ private static Logger log = Logger.getLogger(BisocketSampleClient.class);
+
+
+ public void makeInvocation(String locatorURI) throws Throwable
+ {
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ Client client = new Client(clientLocator);
+ client.connect();
+ log.info("client is connected");
+
+ // Test connection.
+ if ("abc".equals(client.invoke("abc")))
+ log.info("connection is good");
+ else
+ log.info("Should have gotten \"abc\" in reply");
+
+ // Set up push callbacks. Tell the Connector created by Client.addListener()
+ // that it is a callback Connector, which tells the BisocketServerInvoker
+ // not to create a ServerSocket.
+ HashMap metadata = new HashMap();
+ metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
+ TestCallbackHandler callbackHandler = new TestCallbackHandler();
+ client.addListener(callbackHandler, metadata);
+
+ // Use reflection to verify that the callback BisocketServerInvoker did not
+ // create a ServerSocket.
+ Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
+ if (callbackConnectors.size() != 1)
+ {
+ log.info("There should be one callback Connector");
+ }
+ else
+ {
+ Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
+ BisocketServerInvoker serverInvoker = (BisocketServerInvoker) callbackConnector.getServerInvoker();
+ Field field = SocketServerInvoker.class.getDeclaredField("serverSockets");
+ field.setAccessible(true);
+ List serverSockets = (List) field.get(serverInvoker);
+ log.info("number of ServerSockets held by callback BisocketServerInvoker: " + serverSockets.size());
+ }
+
+ // Request callback.
+ client.invoke("CALLBACK");
+ if (callbackHandler.getCounter() == 1)
+ log.info("received callback");
+ else
+ log.info("didn't receive callback");
+
+ client.removeListener(callbackHandler);
+ client.disconnect();
+ }
+
+
+ public static void main(String[] args)
+ {
+ // Configure logging.
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+
+ try
+ {
+ String host = InetAddress.getLocalHost().getHostName();
+ int port = BisocketSampleServer.port;
+ String locatorURI = "bisocket://" + host + ":" + port;
+ BisocketSampleClient client = new BisocketSampleClient();
+ client.makeInvocation(locatorURI);
+ }
+ catch(Throwable e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * An InvokerCallbackHandler is registered with the callback Connector, which
+ * passes push callbacks to it by way of the handleCallback() method.
+ * </p>
+ */
+ static class TestCallbackHandler implements InvokerCallbackHandler
+ {
+ private int counter;
+
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ counter++;
+ }
+
+ public int getCounter()
+ {
+ return counter;
+ }
+ }
+}
\ No newline at end of file
Added: remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleServer.java
===================================================================
--- remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleServer.java (rev 0)
+++ remoting2/branches/2.x/src/main/org/jboss/remoting/samples/bisocket/BisocketSampleServer.java 2008-05-02 03:16:37 UTC (rev 4117)
@@ -0,0 +1,168 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2005, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.remoting.samples.bisocket;
+
+import java.net.InetAddress;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import javax.management.MBeanServer;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.logging.XLevel;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+
+
+/**
+ * This class and org.jboss.remoting.samples.bisocket.BisocketSampleClient
+ * demonstrate how to how to make an invocation and how to set up push callbacks
+ * over the bisocket transport.
+ *
+ * The reason for the existance of the bisocket transport, which is derived from the
+ * socket transport, is to make it possible to do push callbacks to a client which
+ * is unable to create a ServerSocket, either due to security restrictions or due
+ * to the fact that it is behind a firewall.
+ *
+ * @author <a href="ron.sigal at jboss.com">Ron Sigal</a>
+ * @version $Revision: 1.1 $
+ * <p>
+ * Copyright May 1, 2008
+ * </p>
+ */
+public class BisocketSampleServer
+{
+ public static int port = 4567;
+
+ private static Logger log = Logger.getLogger(BisocketSampleServer.class);
+
+ protected Connector connector;
+
+
+ protected void setupServer(String locatorURI) throws Exception
+ {
+ // Create the InvokerLocator based on url string format
+ // to indicate the transport, host, and port to use for the
+ // server invoker.
+ InvokerLocator serverLocator = new InvokerLocator(locatorURI);
+
+ connector = new Connector(serverLocator);
+
+ // Creates all the connector's needed resources, such as the server invoker.
+ connector.create();
+
+ // Create the handler to receive the invocation request from the client for processing.
+ SampleInvocationHandler invocationHandler = new SampleInvocationHandler();
+ connector.addInvocationHandler("test", invocationHandler);
+
+ // Start server.
+ connector.start();
+ log.info("Started remoting server with locator uri of: " + locatorURI);
+ }
+
+
+ protected void shutdownServer() throws Exception
+ {
+ if (connector != null)
+ {
+ connector.stop();
+ log.info("shut down server");
+ }
+ }
+
+
+ public static void main(String[] args)
+ {
+ try
+ {
+ // Configure logging.
+ Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+
+ // Create and start server.
+ String host = InetAddress.getLocalHost().getHostAddress();
+ String locatorURI = "bisocket://" + host + ":" + port;
+ BisocketSampleServer server = new BisocketSampleServer();
+ server.setupServer(locatorURI);
+
+ // Wait until user types a character on command line.
+ System.in.read();
+
+ // Shut down server.
+ server.shutdownServer();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Invocations are handled by the ServerInvocationHandler.
+ */
+ static class SampleInvocationHandler implements ServerInvocationHandler
+ {
+ private HashSet callbackHandlers = new HashSet();
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ callbackHandlers.add(callbackHandler);
+ }
+
+ public Object invoke(final InvocationRequest invocation) throws Throwable
+ {
+ Object param = invocation.getParameter();
+ if ("CALLBACK".equals(param))
+ {
+ Iterator it = callbackHandlers.iterator();
+ while (it.hasNext())
+ {
+ InvokerCallbackHandler handler = (InvokerCallbackHandler) it.next();
+ handler.handleCallback(new Callback("callback"));
+ }
+ }
+ return param;
+ }
+
+ public void removeListener(InvokerCallbackHandler callbackHandler)
+ {
+ callbackHandlers.remove(callbackHandler);
+ }
+
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+}
\ No newline at end of file
More information about the jboss-remoting-commits
mailing list