diff src/org/dancres/blitz/remote/transport/Client.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/dancres/blitz/remote/transport/Client.java	Sat Mar 21 11:00:06 2009 +0000
@@ -0,0 +1,74 @@
+package org.dancres.blitz.remote.transport;
+
+import java.net.InetSocketAddress;
+
+import org.apache.mina.common.ConnectFuture;
+import org.apache.mina.common.IoSession;
+import org.apache.mina.common.RuntimeIOException;
+import org.apache.mina.filter.codec.ProtocolCodecFilter;
+import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
+import org.apache.mina.transport.socket.nio.SocketConnector;
+import org.apache.mina.transport.socket.nio.SocketConnectorConfig;
+
+/**
+ */
+public class Client {
+    private static final String HOSTNAME = "localhost";
+    private static final int PORT = 8080;
+    private static final int CONNECT_TIMEOUT = 30; // seconds
+
+    public static void main(String[] args) throws Throwable {
+        SocketConnector connector = new SocketConnector();
+
+        // Change the worker timeout to 1 second to make the I/O thread quit soon
+        // when there's no connection to manage.
+        connector.setWorkerTimeout(1);
+
+        // Configure the service.
+        SocketConnectorConfig cfg = new SocketConnectorConfig();
+        cfg.setConnectTimeout(CONNECT_TIMEOUT);
+            cfg.getFilterChain().addLast(
+                "codec",
+                new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
+        // cfg.getFilterChain().addLast("logger", new LoggingFilter());
+
+        IoSession session;
+
+        ClientSessionHandler myHandler = new ClientSessionHandler();
+
+        for (; ;) {
+            try {
+                ConnectFuture future = connector.connect(
+                    new InetSocketAddress(HOSTNAME, PORT),
+                    myHandler, cfg);
+
+                future.join();
+                session = future.getSession();
+                break;
+            }
+            catch (RuntimeIOException e) {
+                System.err.println("Failed to connect.");
+                e.printStackTrace();
+                Thread.sleep(5000);
+            }
+        }
+
+        int myPingCount = 0;
+
+        while (true) {
+            long myStart = System.currentTimeMillis();
+
+            for (int i = 0; i < 1000; i++) {
+                session.write(new PingMessage(myPingCount++));
+
+                // myHandler.waitForResponse();
+            }
+
+            long myTotal = System.currentTimeMillis() - myStart;
+            System.out.println("1000 iterations in: " + myTotal);
+
+            double myTimePerIter = ((double) myTotal) / (double) 1000;
+            System.out.println("Time per roundtrip: " + myTimePerIter);
+        }
+    }
+}