Beispiel #1
0
  /** Selects on sockets and informs their Communicator when there is something to do. */
  public void communicate(int timeout) {

    try {
      selector.select(timeout);
    } catch (IOException e) {
      // Not really sure why/when this happens yet
      return;
    }

    Iterator<SelectionKey> keys = selector.selectedKeys().iterator();

    while (keys.hasNext()) {
      SelectionKey key = keys.next();
      keys.remove();
      if (!key.isValid()) continue; // WHY
      Communicator communicator = (Communicator) key.attachment();

      if (key.isReadable()) communicator.onReadable();
      if (key.isWritable()) communicator.onWritable();
      if (key.isAcceptable()) communicator.onAcceptable();
    }

    // Go through the queue and handle each communicator
    while (!queue.isEmpty()) {
      Communicator c = queue.poll();
      c.onMemo();
    }
  }
Beispiel #2
0
  public Overlord() {
    try {
      selector = Selector.open();
      queue = new ConcurrentLinkedQueue<Communicator>();

      // open the pipe and register it with our selector
      pipe = Pipe.open();
      pipe.sink().configureBlocking(false);
      pipe.source().configureBlocking(false);
      pipe.source().register(selector, SelectionKey.OP_READ);
    } catch (IOException e) {
      throw new RuntimeException("select() failed");
    }
  }
Beispiel #3
0
  public float generate(CA c, int start, int end, boolean stopOnSame, boolean over, Updater u) {
    if (_b == null || _b.getWidth() != c.getWidth() || _b.getHeight() != c.getHeight()) {
      _b = new CA(c.getWidth(), c.getHeight());
    }
    int times = end - start;
    CA b1 = c, b2 = _b; // double buffer
    // int[] sur = new int[_len*_len];
    int[] sur = new int[_len - 1];
    int dim = _len;
    int offset = (dim - 1) / 2;
    Selector sel = selector();
    for (int t = 0; t < times; t++) {
      for (int x = 0; x < b1.getWidth(); x++) {
        for (int y = 0; y < b1.getHeight(); y++) {
          int idx = 0;
          int dx = x - offset;
          int dy = y - offset;
          if (false
              && dx >= 0
              && dy >= 0
              && dx + dim < b1.getWidth()
              && dy + dim < b1.getHeight()) {
            b1.getBlock(sur, dx, dy, _len);
          } else {
            for (int i = x - 1; i <= x + 1; i++) {
              for (int j = y - 1; j <= y + 1; j++) {
                sur[idx++] = get(b1, i, j);
              }
            }
          }
          int n = sel.next(sur, 0);
          if (n != -1) {
            b2.set(x, y, _colors[n]);
            if (_interceptor != null) {
              _interceptor.set(x, y, _colors[n]);
            }
            // System.err.print(" "+n);
          }
          /*
          int dx = x - offset;
          int dy = y - offset;
          if(dx>=0&&dy>=0&&dx+dim<b1.getWidth()&&dy+dim<b1.getHeight()) {
              b1.getBlock(sur, dx, dy, _len);
          }
          int n = sel.next(sur, 0);
          if(n!=-1) {
              b2.set(x, y, n);
          }
          */
        }
      }

      // swap
      CA temp = b1;
      b1 = b2;
      b2 = temp;
    }
    c.setData(b1.getData());
    // System.err.print(".");
    return 0f;
  }
Beispiel #4
0
 /** If the selector is waiting, wake it up */
 public void interrupt() {
   selector.wakeup();
 }