Exemplo n.º 1
0
 public static void main(String[] args) {
   System.out.println("This programs tests the features of the counter class.");
   Counter c1 = new Counter(), c2 = new Counter();
   tester(c1, c2);
   c2.decrement();
   tester(c1, c2);
   c1.setCounter(0);
   c2.increment();
   tester(c1, c2);
   c2.setCounter(c1.getCounter());
   tester(c1, c2);
   c1.decrement();
   tester(c1, c2);
   System.out.println("Test complete.");
 }
Exemplo n.º 2
0
 protected void processLeftOvers(Member member, boolean force) {
   MessageOrder tmp = incoming.get(member);
   if (force) {
     Counter cnt = getInCounter(member);
     cnt.setCounter(Integer.MAX_VALUE);
   }
   if (tmp != null) processIncoming(tmp);
 }
Exemplo n.º 3
0
  /**
   * @param order MessageOrder
   * @return boolean - true if a message expired and was processed
   */
  protected boolean processIncoming(MessageOrder order) {
    boolean result = false;
    Member member = order.getMessage().getAddress();
    Counter cnt = getInCounter(member);

    MessageOrder tmp = incoming.get(member);
    if (tmp != null) {
      order = MessageOrder.add(tmp, order);
    }

    while ((order != null) && (order.getMsgNr() <= cnt.getCounter())) {
      // we are right on target. process orders
      if (order.getMsgNr() == cnt.getCounter()) cnt.inc();
      else if (order.getMsgNr() > cnt.getCounter()) cnt.setCounter(order.getMsgNr());
      super.messageReceived(order.getMessage());
      order.setMessage(null);
      order = order.next;
    }
    MessageOrder head = order;
    MessageOrder prev = null;
    tmp = order;
    // flag to empty out the queue when it larger than maxQueue
    boolean empty = order != null ? order.getCount() >= maxQueue : false;
    while (tmp != null) {
      // process expired messages or empty out the queue
      if (tmp.isExpired(expire) || empty) {
        // reset the head
        if (tmp == head) head = tmp.next;
        cnt.setCounter(tmp.getMsgNr() + 1);
        if (getForwardExpired()) super.messageReceived(tmp.getMessage());
        tmp.setMessage(null);
        tmp = tmp.next;
        if (prev != null) prev.next = tmp;
        result = true;
      } else {
        prev = tmp;
        tmp = tmp.next;
      }
    }
    if (head == null) incoming.remove(member);
    else incoming.put(member, head);
    return result;
  }