01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.net.core;
05:
06: import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
07:
08: import com.tc.bytes.TCByteBuffer;
09: 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,
24: TCSocketAddress addr, int numMsgs, int dataSize,
25: long sleepFor) {
26: this .connMgr = connMgr;
27: this .addr = addr;
28: this .numMsgs = numMsgs;
29: this .dataSize = dataSize;
30: this .sleepFor = sleepFor;
31: }
32:
33: public void run() throws Exception {
34: final GenericNetworkMessageSink recvSink = new GenericNetworkMessageSink() {
35: public void putMessage(GenericNetworkMessage msg) {
36: final long recv = msgs.increment();
37: if ((recv % 1000) == 0) {
38: System.out.println("Processed "
39: + (recv * msg.getTotalLength())
40: + " bytes...");
41: }
42: }
43: };
44:
45: final TCConnection conn = connMgr
46: .createConnection(new GenericProtocolAdaptor(recvSink));
47: conn.connect(addr, 3000);
48:
49: for (int i = 0; (numMsgs < 0) || (i < numMsgs); i++) {
50: TCByteBuffer data[] = TCByteBufferFactory
51: .getFixedSizedInstancesForLength(false, dataSize);
52: final GenericNetworkMessage msg = new GenericNetworkMessage(
53: conn, data);
54: msg.setSentCallback(new Runnable() {
55: public void run() {
56: msg.setSent();
57: }
58: });
59:
60: conn.putMessage(msg);
61:
62: if (sleepFor < 0) {
63: msg.waitUntilSent();
64: } else {
65: Thread.sleep(sleepFor);
66: }
67: }
68:
69: Thread.sleep(5000);
70: conn.close(3000);
71: }
72:
73: public static void main(String args[]) throws Throwable {
74: try {
75: TCConnectionManager connMgr = new TCConnectionManagerJDK14();
76: SimpleClient client = new SimpleClient(connMgr,
77: new TCSocketAddress(args[0], Integer
78: .parseInt(args[1])), Integer
79: .parseInt(args[3]), Integer
80: .parseInt(args[2]), Integer
81: .parseInt(args[4]));
82: client.run();
83: } catch (Throwable t) {
84: System.err
85: .println("usage: "
86: + SimpleClient.class.getName()
87: + " <host> <port> <msgSize> <numMsgs, -1 for unlimited> <delay, -1 for single fire>\n\n");
88: throw t;
89: }
90: }
91: }
|