001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
008:
009: import com.tc.exception.TCRuntimeException;
010: import com.tc.net.core.ConnectionAddressProvider;
011: import com.tc.net.core.ConnectionInfo;
012: import com.tc.net.protocol.PlainNetworkStackHarnessFactory;
013: import com.tc.net.protocol.tcm.ClientMessageChannel;
014: import com.tc.net.protocol.tcm.CommunicationsManager;
015: import com.tc.net.protocol.tcm.CommunicationsManagerImpl;
016: import com.tc.net.protocol.tcm.NullMessageMonitor;
017: import com.tc.net.protocol.tcm.TCMessage;
018: import com.tc.net.protocol.tcm.TCMessageSink;
019: import com.tc.net.protocol.tcm.TCMessageType;
020: import com.tc.net.protocol.tcm.UnsupportedMessageTypeException;
021: import com.tc.net.protocol.tcm.msgs.PingMessage;
022: import com.tc.net.protocol.transport.NullConnectionPolicy;
023: import com.tc.object.session.NullSessionManager;
024:
025: /**
026: * TODO: document me!
027: *
028: * @author teck
029: */
030: public class Ping implements TCMessageSink {
031:
032: private String hostname;
033: private int port;
034: private LinkedQueue queue = new LinkedQueue();
035:
036: Ping(String args[]) {
037: switch (args.length) {
038: case 0: {
039: usage();
040: break;
041: }
042: case 1: {
043: hostname = args[0];
044: break;
045: }
046: case 2: {
047: hostname = args[0];
048: port = Integer.parseInt(args[1]);
049:
050: if ((port <= 0) || (port > 0xFFFF)) {
051: usage();
052: }
053: break;
054: }
055: default: {
056: usage();
057: }
058: }
059: }
060:
061: private void usage() {
062: System.err.println("usage: ping <hostname> [port]");
063: System.exit(1);
064: }
065:
066: public void ping() throws Exception {
067: CommunicationsManager comms = new CommunicationsManagerImpl(
068: new NullMessageMonitor(),
069: new PlainNetworkStackHarnessFactory(),
070: new NullConnectionPolicy());
071:
072: ClientMessageChannel channel = null;
073: try {
074: channel = comms.createClientChannel(
075: new NullSessionManager(), 0, this .hostname,
076: this .port, 3000, new ConnectionAddressProvider(
077: new ConnectionInfo[0]));
078: channel.open();
079: channel.routeMessageType(TCMessageType.PING_MESSAGE, this );
080: for (int i = 0; i < 400; i++) {
081: PingMessage pingMsg = (PingMessage) channel
082: .createMessage(TCMessageType.PING_MESSAGE);
083:
084: long start = System.currentTimeMillis();
085: pingMsg.send();
086: TCMessage pong = getMessage(5000);
087: long end = System.currentTimeMillis();
088:
089: if (pong != null) {
090: System.out.println("RTT millis: " + (end - start));
091: } else {
092: System.err.println("Ping Request Timeout : "
093: + (end - start) + " ms");
094: }
095: }
096: } finally {
097: if (channel != null) {
098: channel.unrouteMessageType(TCMessageType.PING_MESSAGE);
099: }
100: comms.shutdown();
101: }
102: }
103:
104: private TCMessage getMessage(long timeout) {
105: try {
106: return (TCMessage) queue.poll(timeout);
107: } catch (InterruptedException e) {
108: throw new TCRuntimeException(e);
109: }
110: }
111:
112: public static void main(String[] args) throws Throwable {
113: Ping ping = new Ping(args);
114: ping.ping();
115: }
116:
117: public void putMessage(TCMessage message)
118: throws UnsupportedMessageTypeException {
119: try {
120: queue.put(message);
121: } catch (InterruptedException e) {
122: throw new TCRuntimeException(e);
123: }
124: }
125:
126: }
|