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

jboss-remoting-commits at lists.jboss.org jboss-remoting-commits at lists.jboss.org
Wed Sep 16 16:46:48 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-09-16 16:46:48 -0400 (Wed, 16 Sep 2009)
New Revision: 5506

Added:
   remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13ClientListener.java
Removed:
   remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/LocalStreamExampleMain.java
   remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StreamingRot13RequestListener.java
Modified:
   remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexClientExample.java
   remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexServerExample.java
   remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13RequestListener.java
Log:
Fix samples

Deleted: remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/LocalStreamExampleMain.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/LocalStreamExampleMain.java	2009-09-16 20:31:34 UTC (rev 5505)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/LocalStreamExampleMain.java	2009-09-16 20:46:48 UTC (rev 5506)
@@ -1,51 +0,0 @@
-package org.jboss.remoting3.samples.simple;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
-import org.jboss.remoting3.Client;
-import org.jboss.remoting3.Endpoint;
-import org.jboss.remoting3.Remoting;
-import org.jboss.xnio.IoUtils;
-
-/**
- *
- */
-public final class LocalStreamExampleMain {
-
-    private LocalStreamExampleMain() {
-    }
-
-    public static void main(String[] args) throws IOException {
-        final StreamingRot13RequestListener listener = new StreamingRot13RequestListener();
-        final Endpoint endpoint = Remoting.createEndpoint("simple");
-        try {
-            final Client<Reader,Reader> client = Remoting.createLocalClient(endpoint, listener, null, null);
-            try {
-                final String original = "The Secret Message\n";
-                final StringReader originalReader = new StringReader(original);
-                try {
-                    final Reader reader = client.send(originalReader).get();
-                    try {
-                        final BufferedReader bufferedReader = new BufferedReader(reader);
-                        try {
-                            final String secretLine = bufferedReader.readLine();
-                            System.out.printf("The secret message \"%s\" became \"%s\"!\n", original.trim(), secretLine);
-                        } finally {
-                            IoUtils.safeClose(bufferedReader);
-                        }
-                    } finally {
-                        IoUtils.safeClose(reader);
-                    }
-                } finally {
-                    IoUtils.safeClose(originalReader);
-                }
-            } finally {
-                IoUtils.safeClose(client);
-            }
-        } finally {
-            IoUtils.safeClose(endpoint);
-        }
-    }
-}

Modified: remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexClientExample.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexClientExample.java	2009-09-16 20:31:34 UTC (rev 5505)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexClientExample.java	2009-09-16 20:46:48 UTC (rev 5506)
@@ -24,24 +24,12 @@
 
 import org.jboss.remoting3.Endpoint;
 import org.jboss.remoting3.Remoting;
+import org.jboss.remoting3.OptionMap;
+import org.jboss.remoting3.Connection;
 import org.jboss.remoting3.Client;
-import org.jboss.remoting3.multiplex.MultiplexProtocol;
-import org.jboss.remoting3.multiplex.MultiplexConfiguration;
-import org.jboss.remoting3.multiplex.MultiplexConnection;
 import org.jboss.xnio.IoUtils;
-import org.jboss.xnio.Buffers;
-import org.jboss.xnio.Xnio;
-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.net.URI;
 
 import java.util.logging.Logger;
 import java.util.logging.Level;
@@ -64,54 +52,18 @@
         try {
             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();
+                final Connection connection = endpoint.connect(URI.create(args[0]), OptionMap.EMPTY).get();
                 try {
-                    final ConfigurableFactory<CloseableTcpConnector> tcpConnectorFactory = xnio.createTcpConnector();
-                    final CloseableTcpConnector closeableTcpConnector = tcpConnectorFactory.create();
+                    final Client<String,String> client = connection.openClient("samples.rot13", "*", String.class, String.class).get();
                     try {
-                        final ChannelSource<AllocatedMessageChannel> channelSource = Channels.convertStreamToAllocatedMessage(closeableTcpConnector.createChannelSource(new InetSocketAddress("localhost", 10000)), 1024, 1024);
-                        final IoFuture<MultiplexConnection> futureConnection = MultiplexProtocol.connect(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);
-                        }
+                        final String original = "The Secret Message\n";
+                        final String result = client.invoke(original);
+                        System.out.printf("The secret message \"%s\" became \"%s\"!\n", original.trim(), result.trim());
                     } finally {
-                        IoUtils.safeClose(closeableTcpConnector);
+                        IoUtils.safeClose(client);
                     }
                 } finally {
-                    IoUtils.safeClose(xnio);
+                    IoUtils.safeClose(connection);
                 }
             } finally {
                 IoUtils.safeClose(endpoint);

Modified: remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexServerExample.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexServerExample.java	2009-09-16 20:31:34 UTC (rev 5505)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/MultiplexServerExample.java	2009-09-16 20:46:48 UTC (rev 5506)
@@ -25,23 +25,16 @@
 import org.jboss.remoting3.Remoting;
 import org.jboss.remoting3.Endpoint;
 import org.jboss.remoting3.LocalServiceConfiguration;
-import org.jboss.remoting3.multiplex.MultiplexProtocol;
-import org.jboss.remoting3.multiplex.MultiplexConfiguration;
+import org.jboss.remoting3.SimpleCloseable;
+import org.jboss.remoting3.spi.ConnectionProviderRegistration;
+import org.jboss.remoting3.multiplex.MultiplexConnectionProviderFactory;
+import org.jboss.remoting3.multiplex.MultiplexServerFactory;
 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.Xnio;
+import org.jboss.xnio.TcpServer;
 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;
@@ -60,57 +53,41 @@
     private MultiplexServerExample() {
     }
 
-    public static void main(String[] args) {
+    public static void main(String[] args) throws InterruptedException, IOException {
         final CloseableExecutor executor = Remoting.createExecutor(10);
         try {
-            final Endpoint endpoint = Remoting.createEndpoint(executor, "example-endpoint");
+            Xnio xnio = Xnio.create();
             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);
+                final Endpoint endpoint = Remoting.createEndpoint(executor, "example-endpoint");
                 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());
+                    final LocalServiceConfiguration<String, String> config = LocalServiceConfiguration.create(new StringRot13ClientListener(), String.class, String.class);
+                    config.setGroupName("main");
+                    config.setServiceType("simple.rot13");
+                    final SimpleCloseable handle = endpoint.registerService(config);
                     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(multiplexConfig);
-                        final IoHandlerFactory<StreamChannel> streamHandlerFactory = Channels.convertStreamToAllocatedMessage(handlerFactory, 1024, 1024);
-                        // finally, bind it
-                        final Xnio xnio = Xnio.create();
+                        // now create the server...
+                        final MultiplexConnectionProviderFactory multiplexConnectionProviderFactory = new MultiplexConnectionProviderFactory(xnio.createTcpConnector().create());
+                        final ConnectionProviderRegistration<MultiplexServerFactory> cpHandle = endpoint.addConnectionProvider("multiplex", multiplexConnectionProviderFactory);
                         try {
-                            final ConfigurableFactory<? extends Closeable> tcpServerFactory = xnio.createTcpServer(streamHandlerFactory, new InetSocketAddress(10000));
-                            final Closeable server = tcpServerFactory.create();
+                            final TcpServer tcpServer = xnio.createTcpServer(Channels.convertStreamToAllocatedMessage(cpHandle.getProviderInterface().getHandlerFactory(), 0x1000, 0x1000)).create();
                             try {
-                                System.out.println("Press enter to terminate.");
-                                while (System.in.read() != '\n');
+                                // now just wait for 15 seconds, and then shut it all down
+                                Thread.sleep(15000L);
                             } finally {
-                                IoUtils.safeClose(server);
+                                IoUtils.safeClose(tcpServer);
                             }
                         } finally {
-                            IoUtils.safeClose(xnio);
+                            IoUtils.safeClose(cpHandle);
                         }
                     } finally {
-                        IoUtils.safeClose(connHandle);
+                        IoUtils.safeClose(handle);
                     }
                 } finally {
-                    IoUtils.safeClose(handle);
+                    IoUtils.safeClose(endpoint);
                 }
             } finally {
-                IoUtils.safeClose(endpoint);
+                IoUtils.safeClose(xnio);
             }
-        } catch (IOException e) {
-            e.printStackTrace();
         } finally {
             IoUtils.safeClose(executor);
         }

Deleted: remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StreamingRot13RequestListener.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StreamingRot13RequestListener.java	2009-09-16 20:31:34 UTC (rev 5505)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StreamingRot13RequestListener.java	2009-09-16 20:46:48 UTC (rev 5506)
@@ -1,55 +0,0 @@
-package org.jboss.remoting3.samples.simple;
-
-import java.io.IOException;
-import java.io.Reader;
-import org.jboss.remoting3.RemoteExecutionException;
-import org.jboss.remoting3.RequestContext;
-
-/**
- *
- */
-public final class StreamingRot13RequestListener extends AbstractRequestListener<Reader, Reader> {
-
-    public void handleRequest(final RequestContext<Reader> readerRequestContext, final Reader request) throws RemoteExecutionException {
-        try {
-            readerRequestContext.sendReply(new Reader() {
-
-                public int read() throws IOException {
-                    final int i = request.read();
-                    if (i > 0) {
-                        return rot13((char) i);
-                    } else {
-                        return i;
-                    }
-                }
-
-                public int read(final char cbuf[], final int off, final int len) throws IOException {
-                    for (int i = 0; i < len; i++) {
-                        final int c = read();
-                        if (c == -1) {
-                            return i;
-                        }
-                        cbuf[off + i] = (char) c;
-                    }
-                    return len;
-                }
-
-                public void close() throws IOException {
-                    request.close();
-                }
-            });
-        } catch (IOException e) {
-            throw new RemoteExecutionException("Failed to send reply", e);
-        }
-    }
-
-    private char rot13(final char i) {
-        if (i >= 'A' && i <= 'M' || i >= 'a' && i <= 'm') {
-            return (char) (i + 13);
-        } else if (i >= 'N' && i <= 'Z' || i >= 'n' && i <= 'z') {
-            return (char) (i - 13);
-        } else {
-            return i;
-        }
-    }
-}

Added: remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13ClientListener.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13ClientListener.java	                        (rev 0)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13ClientListener.java	2009-09-16 20:46:48 UTC (rev 5506)
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, 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.remoting3.samples.simple;
+
+import org.jboss.remoting3.ClientListener;
+import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.ClientContext;
+
+public final class StringRot13ClientListener implements ClientListener<String, String> {
+
+    public RequestListener<String, String> handleClientOpen(final ClientContext clientContext) {
+        return new StringRot13RequestListener();
+    }
+}

Modified: remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13RequestListener.java
===================================================================
--- remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13RequestListener.java	2009-09-16 20:31:34 UTC (rev 5505)
+++ remoting3/trunk/samples/src/main/java/org/jboss/remoting3/samples/simple/StringRot13RequestListener.java	2009-09-16 20:46:48 UTC (rev 5506)
@@ -3,12 +3,13 @@
 import java.io.IOException;
 import org.jboss.remoting3.RemoteExecutionException;
 import org.jboss.remoting3.RequestContext;
+import org.jboss.remoting3.RequestListener;
 import org.jboss.xnio.log.Logger;
 
 /**
  *
  */
-public final class StringRot13RequestListener extends AbstractRequestListener<String, String> {
+public final class StringRot13RequestListener implements RequestListener<String, String> {
 
     private static final Logger log = Logger.getLogger("jboss.example.string-rot-13");
 
@@ -27,6 +28,9 @@
         }
     }
 
+    public void handleClose() {
+    }
+
     private char rot13(final char i) {
         if (i >= 'A' && i <= 'M' || i >= 'a' && i <= 'm') {
             return (char) (i + 13);



More information about the jboss-remoting-commits mailing list