comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz.remote.transport;
2
3 import java.net.InetSocketAddress;
4
5 import org.apache.mina.common.ConnectFuture;
6 import org.apache.mina.common.IoSession;
7 import org.apache.mina.common.RuntimeIOException;
8 import org.apache.mina.filter.codec.ProtocolCodecFilter;
9 import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
10 import org.apache.mina.transport.socket.nio.SocketConnector;
11 import org.apache.mina.transport.socket.nio.SocketConnectorConfig;
12
13 /**
14 */
15 public class Client {
16 private static final String HOSTNAME = "localhost";
17 private static final int PORT = 8080;
18 private static final int CONNECT_TIMEOUT = 30; // seconds
19
20 public static void main(String[] args) throws Throwable {
21 SocketConnector connector = new SocketConnector();
22
23 // Change the worker timeout to 1 second to make the I/O thread quit soon
24 // when there's no connection to manage.
25 connector.setWorkerTimeout(1);
26
27 // Configure the service.
28 SocketConnectorConfig cfg = new SocketConnectorConfig();
29 cfg.setConnectTimeout(CONNECT_TIMEOUT);
30 cfg.getFilterChain().addLast(
31 "codec",
32 new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
33 // cfg.getFilterChain().addLast("logger", new LoggingFilter());
34
35 IoSession session;
36
37 ClientSessionHandler myHandler = new ClientSessionHandler();
38
39 for (; ;) {
40 try {
41 ConnectFuture future = connector.connect(
42 new InetSocketAddress(HOSTNAME, PORT),
43 myHandler, cfg);
44
45 future.join();
46 session = future.getSession();
47 break;
48 }
49 catch (RuntimeIOException e) {
50 System.err.println("Failed to connect.");
51 e.printStackTrace();
52 Thread.sleep(5000);
53 }
54 }
55
56 int myPingCount = 0;
57
58 while (true) {
59 long myStart = System.currentTimeMillis();
60
61 for (int i = 0; i < 1000; i++) {
62 session.write(new PingMessage(myPingCount++));
63
64 // myHandler.waitForResponse();
65 }
66
67 long myTotal = System.currentTimeMillis() - myStart;
68 System.out.println("1000 iterations in: " + myTotal);
69
70 double myTimePerIter = ((double) myTotal) / (double) 1000;
71 System.out.println("Time per roundtrip: " + myTimePerIter);
72 }
73 }
74 }