[jboss-remoting-commits] JBoss Remoting SVN: r4757 - remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple.

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Tue Dec 16 18:07:10 EST 2008


Author: david.lloyd at jboss.com
Date: 2008-12-16 18:07:10 -0500 (Tue, 16 Dec 2008)
New Revision: 4757

Added:
   remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexClientExample.java
   remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexServerExample.java
Modified:
   remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/StringRot13RequestListener.java
Log:
Add more examples

Added: remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexClientExample.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexClientExample.java	                        (rev 0)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexClientExample.java	2008-12-16 23:07:10 UTC (rev 4757)
@@ -0,0 +1,127 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, 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.simple;
+
+import org.jboss.remoting.Endpoint;
+import org.jboss.remoting.Remoting;
+import org.jboss.remoting.ClientSource;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.protocol.multiplex.MultiplexProtocol;
+import org.jboss.remoting.protocol.multiplex.MultiplexConfiguration;
+import org.jboss.remoting.protocol.multiplex.MultiplexConnection;
+import org.jboss.remoting.spi.NamedServiceRegistry;
+import org.jboss.remoting.spi.QualifiedName;
+import org.jboss.remoting.spi.RequestHandlerSource;
+import org.jboss.remoting.spi.Handle;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.Xnio;
+import org.jboss.xnio.CloseableTcpConnector;
+import org.jboss.xnio.ConfigurableFactory;
+import org.jboss.xnio.ChannelSource;
+import org.jboss.xnio.IoFuture;
+import org.jboss.xnio.channels.Channels;
+import org.jboss.xnio.channels.AllocatedMessageChannel;
+import org.jboss.river.RiverMarshallerFactory;
+import org.jboss.marshalling.MarshallingConfiguration;
+import java.io.IOException;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.InetSocketAddress;
+
+import java.util.logging.Logger;
+import java.util.logging.Level;
+
+/**
+ *
+ */
+public final class MultiplexClientExample {
+
+    static {
+        final Logger l = Logger.getLogger("");
+        l.getHandlers()[0].setLevel(Level.ALL);
+        l.setLevel(Level.INFO);
+    }
+
+    private MultiplexClientExample() {
+    }
+
+    public static void main(String[] args) {
+        final Endpoint endpoint = Remoting.createEndpoint("example-client-endpoint");
+        try {
+            // now create the client
+            final NamedServiceRegistry serviceRegistry = new NamedServiceRegistry();
+            final MultiplexConfiguration config = new MultiplexConfiguration();
+            config.setNamedServiceRegistry(serviceRegistry);
+            config.setAllocator(Buffers.createHeapByteBufferAllocator(1024));
+            config.setMarshallerFactory(new RiverMarshallerFactory());
+            config.setExecutor(IoUtils.directExecutor());
+            config.setLinkMetric(100);
+            config.setMarshallingConfiguration(new MarshallingConfiguration());
+            final Xnio xnio = Xnio.create();
+            try {
+                final ConfigurableFactory<CloseableTcpConnector> tcpConnectorFactory = xnio.createTcpConnector();
+                final CloseableTcpConnector closeableTcpConnector = tcpConnectorFactory.create();
+                try {
+                    final ChannelSource<AllocatedMessageChannel> channelSource = Channels.convertStreamToAllocatedMessage(closeableTcpConnector.createChannelSource(new InetSocketAddress("localhost", 10000)), 1024, 1024);
+                    final IoFuture<MultiplexConnection> futureConnection = MultiplexProtocol.connect(endpoint, config, channelSource);
+                    final MultiplexConnection connection = futureConnection.get();
+                    try {
+                        final Handle<RequestHandlerSource> handle = connection.openRemoteService(QualifiedName.parse("/jboss/example/string-rot-13"));
+                        try {
+                            final ClientSource<String, String> clientSource = endpoint.createClientSource(handle.getResource(), String.class, String.class);
+                            try {
+                                final Client<String, String> client = clientSource.createClient();
+                                try {
+                                    System.out.println("Enter text, send EOF to terminate");
+                                    final BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
+                                    String line;
+                                    while ((line = inputReader.readLine()) != null) {
+                                        System.out.println("Response: " + client.invoke(line));
+                                    }
+                                    System.out.println("Done!");
+                                } finally {
+                                    IoUtils.safeClose(client);
+                                }
+                            } finally {
+                                IoUtils.safeClose(clientSource);
+                            }
+                        } finally {
+                            IoUtils.safeClose(handle);
+                        }
+                    } finally {
+                        IoUtils.safeClose(connection);
+                    }
+                } finally {
+                    IoUtils.safeClose(closeableTcpConnector);
+                }
+            } finally {
+                IoUtils.safeClose(xnio);
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            IoUtils.safeClose(endpoint);
+        }
+    }
+}

Added: remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexServerExample.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexServerExample.java	                        (rev 0)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/MultiplexServerExample.java	2008-12-16 23:07:10 UTC (rev 4757)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, 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.simple;
+
+import org.jboss.remoting.Remoting;
+import org.jboss.remoting.Endpoint;
+import org.jboss.remoting.LocalServiceConfiguration;
+import org.jboss.remoting.protocol.multiplex.MultiplexProtocol;
+import org.jboss.remoting.protocol.multiplex.MultiplexConfiguration;
+import org.jboss.remoting.spi.RequestHandlerSource;
+import org.jboss.remoting.spi.Handle;
+import org.jboss.remoting.spi.NamedServiceRegistry;
+import org.jboss.remoting.spi.QualifiedName;
+import org.jboss.xnio.IoUtils;
+import org.jboss.xnio.Buffers;
+import org.jboss.xnio.IoHandlerFactory;
+import org.jboss.xnio.Xnio;
+import org.jboss.xnio.ConfigurableFactory;
+import org.jboss.xnio.CloseableExecutor;
+import org.jboss.xnio.channels.AllocatedMessageChannel;
+import org.jboss.xnio.channels.Channels;
+import org.jboss.xnio.channels.StreamChannel;
+import org.jboss.river.RiverMarshallerFactory;
+import org.jboss.marshalling.MarshallingConfiguration;
+import org.jboss.marshalling.reflect.SunReflectiveCreator;
+import java.io.IOException;
+import java.io.Closeable;
+import java.net.InetSocketAddress;
+
+import java.util.logging.Logger;
+import java.util.logging.Level;
+
+/**
+ *
+ */
+public final class MultiplexServerExample {
+
+    static {
+        final Logger l = Logger.getLogger("");
+        l.getHandlers()[0].setLevel(Level.ALL);
+        l.setLevel(Level.INFO);
+    }
+
+    private MultiplexServerExample() {
+    }
+
+    public static void main(String[] args) {
+        final CloseableExecutor executor = Remoting.createExecutor(10);
+        try {
+            final Endpoint endpoint = Remoting.createEndpoint(executor, "example-endpoint");
+            try {
+                final StringRot13RequestListener listener = new StringRot13RequestListener();
+                final LocalServiceConfiguration<String, String> config = new LocalServiceConfiguration<String, String>(listener, String.class, String.class);
+                config.setGroupName("main");
+                config.setServiceType("jboss.example.streaming-rot-13");
+                final Handle<RequestHandlerSource> handle = endpoint.registerService(config);
+                try {
+                    // now create the server...
+                    final NamedServiceRegistry serviceRegistry = new NamedServiceRegistry();
+                    final Handle<RequestHandlerSource> connHandle = serviceRegistry.registerService(QualifiedName.parse("/jboss/example/string-rot-13"), handle.getResource());
+                    try {
+                        final MultiplexConfiguration multiplexConfig = new MultiplexConfiguration();
+                        multiplexConfig.setNamedServiceRegistry(serviceRegistry);
+                        multiplexConfig.setAllocator(Buffers.createHeapByteBufferAllocator(1024));
+                        multiplexConfig.setLinkMetric(100);
+                        multiplexConfig.setMarshallerFactory(new RiverMarshallerFactory());
+                        multiplexConfig.setExecutor(executor);
+                        final MarshallingConfiguration marshallingConfig = new MarshallingConfiguration();
+                        marshallingConfig.setCreator(new SunReflectiveCreator());
+                        multiplexConfig.setMarshallingConfiguration(marshallingConfig);
+                        final IoHandlerFactory<AllocatedMessageChannel> handlerFactory = MultiplexProtocol.createServer(endpoint, multiplexConfig);
+                        final IoHandlerFactory<StreamChannel> streamHandlerFactory = Channels.convertStreamToAllocatedMessage(handlerFactory, 1024, 1024);
+                        // finally, bind it
+                        final Xnio xnio = Xnio.create();
+                        try {
+                            final ConfigurableFactory<? extends Closeable> tcpServerFactory = xnio.createTcpServer(streamHandlerFactory, new InetSocketAddress(10000));
+                            final Closeable server = tcpServerFactory.create();
+                            try {
+                                System.out.println("Press enter to terminate.");
+                                while (System.in.read() != '\n');
+                            } finally {
+                                IoUtils.safeClose(server);
+                            }
+                        } finally {
+                            IoUtils.safeClose(xnio);
+                        }
+                    } finally {
+                        IoUtils.safeClose(connHandle);
+                    }
+                } finally {
+                    IoUtils.safeClose(handle);
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            } finally {
+                IoUtils.safeClose(endpoint);
+            }
+        } finally {
+            IoUtils.safeClose(executor);
+        }
+    }
+
+}

Modified: remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/StringRot13RequestListener.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/StringRot13RequestListener.java	2008-12-16 23:06:58 UTC (rev 4756)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting/samples/simple/StringRot13RequestListener.java	2008-12-16 23:07:10 UTC (rev 4757)
@@ -4,19 +4,25 @@
 import org.jboss.remoting.AbstractRequestListener;
 import org.jboss.remoting.RemoteExecutionException;
 import org.jboss.remoting.RequestContext;
+import org.jboss.xnio.log.Logger;
 
 /**
  *
  */
 public final class StringRot13RequestListener extends AbstractRequestListener<String, String> {
 
+    private static final Logger log = Logger.getLogger("jboss.example.string-rot-13");
+
     public void handleRequest(final RequestContext<String> readerRequestContext, final String request) throws RemoteExecutionException {
+        log.info("Received request: \"%s\"", request);
         try {
             StringBuilder b = new StringBuilder(request.length());
             for (int i = 0; i < request.length(); i ++) {
                 b.append(rot13(request.charAt(i)));
             }
-            readerRequestContext.sendReply(b.toString());
+            final String reply = b.toString();
+            log.info("Sending reply: \"%s\"", reply);
+            readerRequestContext.sendReply(reply);
         } catch (IOException e) {
             throw new RemoteExecutionException("Failed to send reply", e);
         }




More information about the jboss-remoting-commits mailing list