KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > core > SimpleClient


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.core;
5
6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
7
8 import com.tc.bytes.TCByteBuffer;
9 import com.tc.bytes.TCByteBufferFactory;
10 import com.tc.net.TCSocketAddress;
11 import com.tc.net.protocol.GenericNetworkMessage;
12 import com.tc.net.protocol.GenericNetworkMessageSink;
13 import com.tc.net.protocol.GenericProtocolAdaptor;
14
15 public class SimpleClient {
16   private final int numMsgs;
17   private final TCConnectionManager connMgr;
18   private final TCSocketAddress addr;
19   private final int dataSize;
20   private final SynchronizedLong msgs = new SynchronizedLong(0);
21   private final long sleepFor;
22
23   public SimpleClient(TCConnectionManager connMgr, TCSocketAddress addr, int numMsgs, int dataSize, long sleepFor) {
24     this.connMgr = connMgr;
25     this.addr = addr;
26     this.numMsgs = numMsgs;
27     this.dataSize = dataSize;
28     this.sleepFor = sleepFor;
29   }
30
31   public void run() throws Exception JavaDoc {
32     final GenericNetworkMessageSink recvSink = new GenericNetworkMessageSink() {
33       public void putMessage(GenericNetworkMessage msg) {
34         final long recv = msgs.increment();
35         if ((recv % 1000) == 0) {
36           System.out.println("Processed " + (recv * msg.getTotalLength()) + " bytes...");
37         }
38       }
39     };
40
41     final TCConnection conn = connMgr.createConnection(new GenericProtocolAdaptor(recvSink));
42     conn.connect(addr, 3000);
43
44     for (int i = 0; (numMsgs < 0) || (i < numMsgs); i++) {
45       TCByteBuffer data[] = TCByteBufferFactory.getFixedSizedInstancesForLength(false, dataSize);
46       final GenericNetworkMessage msg = new GenericNetworkMessage(conn, data);
47       msg.setSentCallback(new Runnable JavaDoc() {
48         public void run() {
49           msg.setSent();
50         }
51       });
52
53       conn.putMessage(msg);
54
55       if (sleepFor < 0) {
56         msg.waitUntilSent();
57       } else {
58         Thread.sleep(sleepFor);
59       }
60     }
61
62     Thread.sleep(5000);
63     conn.close(3000);
64   }
65
66   public static void main(String JavaDoc args[]) throws Throwable JavaDoc {
67     try {
68       TCConnectionManager connMgr = new TCConnectionManagerFactory().getInstance();
69       SimpleClient client = new SimpleClient(connMgr, new TCSocketAddress(args[0], Integer.parseInt(args[1])), Integer
70           .parseInt(args[3]), Integer.parseInt(args[2]), Integer.parseInt(args[4]));
71       client.run();
72     } catch (Throwable JavaDoc t) {
73       System.err.println("usage: " + SimpleClient.class.getName()
74                          + " <host> <port> <msgSize> <numMsgs, -1 for unlimited> <delay, -1 for single fire>\n\n");
75       throw t;
76     }
77   }
78 }
Popular Tags