package umich.cbe.distview.demos; import java.lang.*; import java.util.*; import java.io.*; import umich.cbe.distview.io.*; import umich.cbe.distview.server.*; import umich.cbe.distview.util.DVApplet; /** * A TestClient is a simple client which connects to the server, joins a * group, sends/receives broadcast messages and measures roundtrip delays * for these messages. * Alternatively, a TestClient can be used to cause the server print status * information. */ public class TestClient extends DVApplet { DVSharedState myGroup; //for testing //public long t2, t3, t4, t5, t6; Thread senderThread; long tstart[], tstop[], t0, t1, total=0; int t[], cnt=0, no=0; public TestClient(String hostName, int portNumber, String grpName) { serverHostName = hostName; serverPort = portNumber; appletGroupName = grpName; userName = "TestClient"; //userName = "Snooper"; } /** * Starts a session. Joins a collaboration group. */ public void startSession() { int ret = 0; stateSize = 0; if (group == null) getGroup(); // this sets group if possible // join the collaboration group if needed if (group != null) { prepareToJoin(group); ret = group.join(false, stateSize); if (ret == 0) { group.disconnect(); group = null; } else { joined(group); } } } /** * Performs application-specific operations, e.g., set a bcast handler * and add a shared object before joining. */ public void prepareToJoin(DVSharedState group) { TCBcastHandler cHandler = new TCBcastHandler(this); CBEDebug.debug("prepareToJoin called"); group.setBcastHandler(cHandler); myGroup = group; } /** * Notifies that this applet has successfully joined the specified group. */ public void joined(DVSharedState group) { group.setGroupProperty(DVGroupProperty.GROUP_TYPE, "Chat"); } public void hideApplet() {} public void killApplet() { stopSession(); } /** * Creates a new TestClient. * @param args[] port# hostname */ public static void main(String args[]) { String grpname, grptype; int port, delay = 0; String host; // to be used with serverInfo() /* if(args.length < 2){ System.out.println("Usage : TestClient [port#] [hostname]"); System.exit(0); } grpname = new String("Snooper"); try { port = Integer.parseInt(args[0],10); } catch(Exception e) { System.out.println("Defaulting to port 12345"); port = 12345; } host = args[1]; */ // to be used with action() if(args.length < 3){ System.out.println("Usage : TestClient [Group_name] [port#] [hostname]"); System.exit(0); } grpname = args[0]; try { port = Integer.parseInt(args[1],10); } catch(Exception e) { System.out.println("Defaulting to port 12345"); port = 12345; } host = args[2]; TestClient TC = new TestClient(host, port, grpname); CBEDebug.setLevel(0); TC.startSession(); /* use either serverInfo() or action(), but not both * serverInfo() causes the server to print server information * action() sends broadcast messages and measures the roundtrip delay */ //TC.serverInfo(); TC.action(); } /** * Sends SERVER_INFO messages to the sever, causing the server to display * status information. */ private void serverInfo() { int i=0, size = 0; String command = null; size = myGroup.getGroupSize(); System.out.println("GROUP SIZE = " + size); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(true) { System.out.print("\nPrint server info ? (y/n): "); System.out.flush(); try { command = br.readLine(); } catch(Exception e) { System.out.println("input read error"); System.exit(0); } if(!command.equals("y")) { stopSession(); System.exit(0); } myGroup.serverInfo(); } } /** * Sends a String message. * @param aMsg the message */ public void sendMsg(String aMsg) { if (myGroup != null) { try { Message msg = new Message(Message.GROUP_MESSAGE, Message.STRING); msg.setMsgData(aMsg); //t2 = System.currentTimeMillis(); myGroup.bcast(msg); //t3 = System.currentTimeMillis(); } catch(Exception e) { System.out.println("TestClient: exception in newMsg(): "+ e.getMessage()); } } } /** * Sends a byte array message. * @param aMsg the message */ public void sendMsg(byte aMsg[]) { if (myGroup != null) { try { Message msg = new Message(Message.GROUP_MESSAGE, Message.B_ARRAY); msg.setMsgBuf(aMsg, aMsg.length); //t2 = System.currentTimeMillis(); myGroup.bcast(msg); //t3 = System.currentTimeMillis(); } catch(Exception e) { System.out.println("TestClient: exception in newMsg(): "+ e.getMessage()); } } } /** * Sends broadcast messages in a loop and measures the roundtrip delay for * these messages. */ private void action() { int size=0, delay=0, i; size = myGroup.getGroupSize(); System.out.println("GROUP SIZE = " + size); // the next line is optional myGroup.serverInfo(); senderThread = Thread.currentThread(); senderThread.setPriority(2); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); DataInputStream in = new DataInputStream(System.in); while(true) { System.out.print("\nSize of the messages to send(chars): "); System.out.flush(); try { size = Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println("Number format error"); System.exit(0); } if(size <= 0) { stopSession(); System.exit(0); } byte sbuf[] = new byte[size]; System.out.print("Number of messages to send : "); System.out.flush(); try { no = Integer.parseInt(br.readLine()); } catch(Exception e) { System.out.println("Number format error"); System.exit(0); } if(no <= 0) { stopSession(); System.exit(0); } tstart = new long[no]; tstop = new long[no]; System.out.print("Delay between messages : "); System.out.flush(); try { delay = Integer.parseInt(br.readLine()); } catch(Exception e) { delay = 0; } if(no < 0) { delay = 0; } t0 = System.currentTimeMillis(); for(i=0; i