KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > Ping


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.tctest;
5
6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
7
8 import com.tc.config.schema.dynamic.FixedValueConfigItem;
9 import com.tc.exception.TCRuntimeException;
10 import com.tc.net.core.ConnectionInfo;
11 import com.tc.net.protocol.PlainNetworkStackHarnessFactory;
12 import com.tc.net.protocol.tcm.ClientMessageChannel;
13 import com.tc.net.protocol.tcm.CommunicationsManager;
14 import com.tc.net.protocol.tcm.CommunicationsManagerImpl;
15 import com.tc.net.protocol.tcm.NullMessageMonitor;
16 import com.tc.net.protocol.tcm.TCMessage;
17 import com.tc.net.protocol.tcm.TCMessageSink;
18 import com.tc.net.protocol.tcm.TCMessageType;
19 import com.tc.net.protocol.tcm.UnsupportedMessageTypeException;
20 import com.tc.net.protocol.tcm.msgs.PingMessage;
21 import com.tc.net.protocol.transport.NullConnectionPolicy;
22 import com.tc.object.session.NullSessionManager;
23
24 /**
25  * TODO: document me!
26  *
27  * @author teck
28  */

29 public class Ping implements TCMessageSink {
30
31   private String JavaDoc hostname;
32   private int port;
33   private LinkedQueue queue = new LinkedQueue();
34
35   Ping(String JavaDoc args[]) {
36     switch (args.length) {
37       case 0: {
38         usage();
39         break;
40       }
41       case 1: {
42         hostname = args[0];
43         break;
44       }
45       case 2: {
46         hostname = args[0];
47         port = Integer.parseInt(args[1]);
48
49         if ((port <= 0) || (port > 0xFFFF)) {
50           usage();
51         }
52         break;
53       }
54       default: {
55         usage();
56       }
57     }
58   }
59
60   private void usage() {
61     System.err.println("usage: ping <hostname> [port]");
62     System.exit(1);
63   }
64
65   public void ping() throws Exception JavaDoc {
66     CommunicationsManager comms = new CommunicationsManagerImpl(new NullMessageMonitor(),
67                                                                 new PlainNetworkStackHarnessFactory(), new NullConnectionPolicy());
68
69     ClientMessageChannel channel = null;
70     try {
71       channel = comms.createClientChannel(new NullSessionManager(), 0, this.hostname, this.port, 3000,
72                                           new FixedValueConfigItem(new ConnectionInfo[0]));
73       channel.open();
74       channel.routeMessageType(TCMessageType.PING_MESSAGE, this);
75       for (int i = 0; i < 400; i++) {
76         PingMessage pingMsg = (PingMessage) channel.createMessage(TCMessageType.PING_MESSAGE);
77
78         long start = System.currentTimeMillis();
79         pingMsg.send();
80         TCMessage pong = getMessage(5000);
81         long end = System.currentTimeMillis();
82
83         if(pong != null) {
84           System.out.println("RTT millis: " + (end - start));
85         }
86         else {
87           System.err.println("Ping Request Timeout : " + (end - start) + " ms");
88         }
89       }
90     } finally {
91       if(channel != null) {
92         channel.unrouteMessageType(TCMessageType.PING_MESSAGE);
93       }
94       comms.shutdown();
95     }
96   }
97
98   private TCMessage getMessage(long timeout) {
99     try {
100       return (TCMessage) queue.poll(timeout);
101     } catch (InterruptedException JavaDoc e) {
102       throw new TCRuntimeException(e);
103     }
104   }
105
106   public static void main(String JavaDoc[] args) throws Throwable JavaDoc {
107     Ping ping = new Ping(args);
108     ping.ping();
109   }
110
111   public void putMessage(TCMessage message) throws UnsupportedMessageTypeException {
112     try {
113       queue.put(message);
114     } catch (InterruptedException JavaDoc e) {
115       throw new TCRuntimeException(e);
116     }
117   }
118
119 }
120
Popular Tags