private static void start(final int num_threads, final int num_msgs, boolean oob, TimeScheduler timer) {
final NAKACK nak=new NAKACK();
final AtomicInteger counter=new AtomicInteger(num_msgs);
final AtomicLong seqno=new AtomicLong(1);
final AtomicInteger delivered_msgs=new AtomicInteger(0);
final Lock lock=new ReentrantLock();
final Condition all_msgs_delivered=lock.newCondition();
final ConcurrentLinkedQueue<Long> delivered_msg_list=new ConcurrentLinkedQueue<Long>();
final Address local_addr=Util.createRandomAddress("A");
final Address sender=Util.createRandomAddress("B");
if(timer == null)
timer=new TimeScheduler2();
nak.setTimer(timer);
System.out.println("timer is a " + timer.getClass());
nak.setDownProtocol(new Protocol() {public Object down(Event evt) {return null;}});
nak.setUpProtocol(new Protocol() {
public Object up(Event evt) {
if(evt.getType() == Event.MSG) {
delivered_msgs.incrementAndGet();
NakAckHeader hdr=(NakAckHeader)((Message)evt.getArg()).getHeader(NAKACK_ID);
if(hdr != null)
delivered_msg_list.add(hdr.getSeqno());
if(delivered_msgs.get() >= num_msgs) {
lock.lock();
try {
all_msgs_delivered.signalAll();
}
finally {
lock.unlock();
}
}
}
return null;
}
});
nak.setDiscardDeliveredMsgs(true);
nak.down(new Event(Event.SET_LOCAL_ADDRESS, local_addr));
nak.down(new Event(Event.BECOME_SERVER));
View view=new View(local_addr, 1, Arrays.asList(local_addr, sender));
nak.down(new Event(Event.VIEW_CHANGE, view));
MutableDigest digest=new MutableDigest();
digest.add(local_addr, 0, 0, 0);
digest.add(sender, 0, 0, 0);
nak.down(new Event(Event.SET_DIGEST, digest));
final CountDownLatch latch=new CountDownLatch(1);
Sender[] adders=new Sender[num_threads];
for(int i=0; i < adders.length; i++) {
adders[i]=new Sender(nak, latch, counter, seqno, oob, sender);
adders[i].start();
}
long start=System.currentTimeMillis();
latch.countDown(); // starts all adders
lock.lock();
try {
while(delivered_msgs.get() < num_msgs) {
try {
all_msgs_delivered.await(1000, TimeUnit.MILLISECONDS);
System.out.println("received " + delivered_msgs.get() + " msgs");
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
}
finally {
lock.unlock();
}
long time=System.currentTimeMillis() - start;
double requests_sec=num_msgs / (time / 1000.0);
System.out.println("\nTime: " + time + " ms, " + Util.format(requests_sec) + " requests / sec\n");