Ejemplo n.º 1
0
  public static void main(String[] argv) throws Exception {
    Pipe[] pipes = new Pipe[PIPES_COUNT];
    Pipe pipe = Pipe.open();
    Pipe.SinkChannel sink = pipe.sink();
    Pipe.SourceChannel source = pipe.source();
    Selector sel = Selector.open();
    source.configureBlocking(false);
    source.register(sel, SelectionKey.OP_READ);

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i] = Pipe.open();
      Pipe.SourceChannel sc = pipes[i].source();
      sc.configureBlocking(false);
      sc.register(sel, SelectionKey.OP_READ);
      Pipe.SinkChannel sc2 = pipes[i].sink();
      sc2.configureBlocking(false);
      sc2.register(sel, SelectionKey.OP_WRITE);
    }

    for (int i = 0; i < LOOPS; i++) {
      sink.write(ByteBuffer.allocate(BUF_SIZE));
      int x = sel.selectNow();
      sel.selectedKeys().clear();
      source.read(ByteBuffer.allocate(BUF_SIZE));
    }

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i].sink().close();
      pipes[i].source().close();
    }
    pipe.sink().close();
    pipe.source().close();
    sel.close();
  }
Ejemplo n.º 2
0
  public Signaler() {
    //  Create the socketpair for signaling.
    Pipe pipe;

    try {
      pipe = Pipe.open();
    } catch (IOException e) {
      throw new ZError.IOException(e);
    }
    r = pipe.source();
    w = pipe.sink();

    //  Set both fds to non-blocking mode.
    try {
      Utils.unblockSocket(w);
      Utils.unblockSocket(r);
    } catch (IOException e) {
      throw new ZError.IOException(e);
    }

    try {
      selector = Selector.open();
      r.register(selector, SelectionKey.OP_READ);
    } catch (IOException e) {
      throw new ZError.IOException(e);
    }
  }