package org.jgroups.tests; import org.jgroups.*; import org.jgroups.blocks.PullPushAdapter; import java.io.Serializable; import java.util.ArrayList; import java.util.Vector; public class Tester extends Thread implements MessageListener, MembershipListener { private JChannel _channel; private ArrayList _messages; private static long _sleepTime; private Address _address; private Address _sendAddress; private PullPushAdapter _pullPushAdapter; private int _numSent; public Tester() throws ChannelException { _channel=new JChannel("c:\\tcp.xml"); _messages=new ArrayList(); _channel.connect("mygroup"); _pullPushAdapter=new PullPushAdapter(_channel, this, this); _address=_channel.getLocalAddress(); _numSent=0; System.out.println("My address is:" + _address.toString()); } public static void main(String[] args) { if(args[0].equals("server")) { _sleepTime=100; } else if(args[0].equals("client")) { _sleepTime=2000; //client } try { Tester j=new Tester(); j.start(); } catch(ChannelException e) { e.printStackTrace(); } } public void run() { while(_channel.getView().getMembers().size() < 2) { try { Thread.sleep(50); } catch(InterruptedException e) { //Don't lose big sleep over this exception. } } System.out.println("2 members found!"); Vector vec=_channel.getView().getMembers(); for(int i=0; i < vec.size(); i++) { if( ((Address)vec.elementAt(i)).toString().equals(_address.toString())) { System.out.println("Rejected Address is:" + ((Address)vec.elementAt(i)).toString()); } else { _sendAddress=(Address)vec.elementAt(i); System.out.println("Thier address is:" + _sendAddress.toString()); } } while(_messages.size() < 500) { if(_numSent++ > 520) { System.out.println("Sent 520 Messages, finishing..."); printStats(); return; } try { JGMessage msg=new JGMessage(); msg.sent_timeStamp=System.currentTimeMillis(); try { _channel.send(_sendAddress, _address, msg); System.out.println("Message Sent!"); //_channel.send(null,_address,msg); } catch(ChannelNotConnectedException e1) { System.out.println("Deadly Exception Channel not Connected"); System.exit(0); } catch(ChannelClosedException e1) { System.out.println("Deadly Exception Channel Closed"); System.exit(0); } Thread.sleep(_sleepTime); } catch(InterruptedException e) { System.out.println("Woken from sleep"); } } printStats(); } private void printStats() { long average=0; long max=0; long min=9999999; for(int i=0; i < _messages.size(); i++) { JGMessage msg=(JGMessage)_messages.get(i); long calc=msg.recv_timeStamp - msg.sent_timeStamp; System.out.println("This messages time for response is:" + calc); if(calc > max) { max=calc; } if(calc < min) { min=calc; } average=average + calc; } average=average / (long)_messages.size(); System.out.println("Average receiving time(in milliseconds): " + average); System.out.println("Max reciving time(in milliseconds: " + max); System.out.println("Min reciving time(in milliseconds: " + min); } /* (non-Javadoc) * @see org.jgroups.MessageListener#receive(org.jgroups.Message) */ public void receive(Message arg0) { System.out.println("Message recieved!"); JGMessage myMessage=(JGMessage)arg0.getObject(); myMessage.recv_timeStamp=System.currentTimeMillis(); _messages.add(myMessage); } /* (non-Javadoc) * @see org.jgroups.MessageListener#getState() */ public byte[] getState() { // TODO Auto-generated method stub return null; } /* (non-Javadoc) * @see org.jgroups.MessageListener#setState(byte[]) */ public void setState(byte[] arg0) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see org.jgroups.MembershipListener#viewAccepted(org.jgroups.View) */ public void viewAccepted(View arg0) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see org.jgroups.MembershipListener#suspect(org.jgroups.Address) */ public void suspect(Address arg0) { // TODO Auto-generated method stub } /* (non-Javadoc) * @see org.jgroups.MembershipListener#block() */ public void block() { // TODO Auto-generated method stub } public static class JGMessage implements Serializable { public long sent_timeStamp; public long recv_timeStamp; private byte[] myBytes=new byte[10000]; // Make 10KB messages } }