コード例 #1
0
ファイル: ZLoop.java プロジェクト: mathdesc/JyreTest
  private void rebuild() {
    pollact = null;

    pollSize = pollers.size();
    pollset = new Poller(pollSize);

    pollact = new SPoller[pollSize];

    int itemNbr = 0;
    for (SPoller poller : pollers) {
      pollset.register(poller.item);
      pollact[itemNbr] = poller;
      itemNbr++;
    }
    dirty = false;
  }
コード例 #2
0
ファイル: ZLoop.java プロジェクト: mathdesc/JyreTest
  public int start() {
    int rc = 0;

    timers.addAll(newTimers);
    newTimers.clear();

    //  Recalculate all timers now
    for (STimer timer : timers) {
      timer.when = timer.delay + System.currentTimeMillis();
    }

    //  Main reactor loop
    while (!Thread.currentThread().isInterrupted()) {
      if (dirty) {
        // If s_rebuild_pollset() fails, break out of the loop and
        // return its error
        rebuild();
      }
      long wait = ticklessTimer();

      rc = pollset.poll(wait);

      if (rc == -1) {
        if (verbose) {
          System.out.printf("I: zloop: interrupted (%d)\n", rc);
        }
        rc = 0;
        break; //  Context has been shut down
      }
      //  Handle any timers that have now expired
      Iterator<STimer> it = timers.iterator();
      while (it.hasNext()) {
        STimer timer = it.next();
        if (System.currentTimeMillis() >= timer.when && timer.when != -1) {
          if (verbose) {
            System.out.println("I: zloop: call timer handler");
          }
          rc = timer.handler.handle(this, null, timer.arg);
          if (rc == -1) {
            break; //  Timer handler signalled break
          }
          if (timer.times != 0 && --timer.times == 0) {
            it.remove();
          } else {
            timer.when = timer.delay + System.currentTimeMillis();
          }
        }
      }
      if (rc == -1) {
        break; // Some timer signalled break from the reactor loop
      }

      //  Handle any pollers that are ready
      for (int itemNbr = 0; itemNbr < pollSize; itemNbr++) {
        SPoller poller = pollact[itemNbr];
        if (pollset.getItem(itemNbr).isError()) {
          if (verbose) {
            System.out.printf(
                "I: zloop: can't poll %s socket (%s, %s)\n",
                poller.item.getSocket() != null ? poller.item.getSocket().getType() : "RAW",
                poller.item.getSocket(),
                poller.item.getRawSocket());
          }
          //  Give handler one chance to handle error, then kill
          //  poller because it'll disrupt the reactor otherwise.
          if (poller.errors++ > 0) {
            removePoller(poller.item);
          }
        } else {
          poller.errors = 0; //  A non-error happened
        }

        if (pollset.getItem(itemNbr).readyOps() > 0) {
          if (verbose) {
            System.out.printf(
                "I: zloop: call %s socket handler (%s, %s)\n",
                poller.item.getSocket() != null ? poller.item.getSocket().getType() : "RAW",
                poller.item.getSocket(),
                poller.item.getRawSocket());
          }
          rc = poller.handler.handle(this, poller.item, poller.arg);
          if (rc == -1) {
            break; //  Poller handler signalled break
          }
        }
      }

      //  Now handle any timer zombies
      //  This is going to be slow if we have many zombies
      for (Object arg : zombies) {
        it = timers.iterator();
        while (it.hasNext()) {
          STimer timer = it.next();
          if (timer.arg == arg) {
            it.remove();
          }
        }
      }
      zombies.clear();
      //  Now handle any new timers added inside the loop
      timers.addAll(newTimers);
      newTimers.clear();

      if (rc == -1) {
        break;
      }
    }

    return rc;
  }