start(NUM_THREADS, NUM_MSGS, true, MAX_MSG_BATCH_SIZE, timer);
}
private static void start(final int num_threads, final int num_msgs, boolean oob, int max_msg_batch_size, TimeScheduler timer) {
final UNICAST2 unicast=new UNICAST2();
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();
final Address sender=Util.createRandomAddress();
if(timer == null)
timer=new TimeScheduler2();
unicast.setTimer(timer);
System.out.println("timer is a " + timer.getClass());
unicast.setDownProtocol(new Protocol() {
public Object down(Event evt) {return null;}
});
unicast.setUpProtocol(new Protocol() {
public Object up(Event evt) {
if(evt.getType() == Event.MSG) {
delivered_msgs.incrementAndGet();
UNICAST2.Unicast2Header hdr=(UNICAST2.Unicast2Header)((Message)evt.getArg()).getHeader(UNICAST_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;
}
});
unicast.down(new Event(Event.SET_LOCAL_ADDRESS, local_addr));
unicast.setMaxMessageBatchSize(max_msg_batch_size);
unicast.setValue("max_bytes", 20000);
// send the first message manually, to initialize the AckReceiverWindow tables
Message msg=createMessage(local_addr, sender, 1L, oob, true);
unicast.up(new Event(Event.MSG, msg));
Util.sleep(500);
final CountDownLatch latch=new CountDownLatch(1);
Sender[] adders=new Sender[num_threads];
for(int i=0; i < adders.length; i++) {
adders[i]=new Sender(unicast, latch, counter, seqno, oob, local_addr, 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();
}
}