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();
  }
Example #2
0
  private static void scScatter() throws Exception {
    Pipe p = Pipe.open();
    Pipe.SinkChannel sink = p.sink();
    Pipe.SourceChannel source = p.source();
    sink.configureBlocking(false);

    ByteBuffer outgoingdata = ByteBuffer.allocateDirect(30);
    byte[] someBytes = new byte[30];
    generator.nextBytes(someBytes);
    outgoingdata.put(someBytes);
    outgoingdata.flip();

    int totalWritten = 0;
    while (totalWritten < 30) {
      int written = sink.write(outgoingdata);
      if (written < 0) throw new Exception("Write failed");
      totalWritten += written;
    }

    ByteBuffer[] bufs = new ByteBuffer[3];
    for (int i = 0; i < 3; i++) bufs[i] = ByteBuffer.allocateDirect(10);
    long numBytesRead = source.read(bufs);
    if (numBytesRead < 30) throw new Exception("Pipe test failed");
    sink.close();
    source.close();
  }
Example #3
0
  /**
   * A convenience method for the following code:
   *
   * <pre>
   * {@link Pipe} inputToOutput = {@link Pipe#open()};
   * {@link Pipe.SinkChannel} sink = {@link Pipe#sink() inputToOutput.sink()};
   * {@link SelectableChannel#configureBlocking(boolean) sink.configureBlocking(false)};
   * {@link #addRoute(SelectableChannel, SelectableChannel) addRoute(input, sink)};
   *
   * {@link Pipe.SourceChannel} source = {@link Pipe#source() inputToOutput.source()};
   * return {@link #wrapChannelInObjectInputStream(ReadableByteChannel) wrapChannelInObjectInputStream(source)};
   * </pre>
   *
   * @throws IOException pass-through for any {@link IOException} that occurs in the above code
   */
  public <T extends SelectableChannel & ReadableByteChannel>
      ObjectInputStream addRouteToObjectInputStream(T input) throws IOException {
    Pipe inputToOutput = Pipe.open();
    Pipe.SinkChannel sink = inputToOutput.sink();
    sink.configureBlocking(false);
    addRoute(input, sink);

    Pipe.SourceChannel source = inputToOutput.source();
    return wrapChannelInObjectInputStream(source);
  }
 /*
  * @see java.nio.channels.Selector#wakeup()
  */
 public Selector wakeup() {
   try {
     sink.write(ByteBuffer.allocate(MOCK_WRITEBUF_SIZE));
   } catch (IOException e) {
     // do nothing
   }
   return this;
 }
Example #5
0
  public void send() {
    int nbytes = 0;
    ByteBuffer dummy = ByteBuffer.allocate(1);

    while (true) {
      try {
        Thread.interrupted();
        nbytes = w.write(dummy);
      } catch (IOException e) {
        throw new ZError.IOException(e);
      }
      if (nbytes == 0) {
        continue;
      }
      assert (nbytes == 1);
      wcursor.incrementAndGet();
      break;
    }
  }
Example #6
0
 @Override
 public void close() throws IOException {
   IOException exception = null;
   try {
     r.close();
   } catch (IOException e) {
     exception = e;
   }
   try {
     w.close();
   } catch (IOException e) {
     exception = e;
   }
   try {
     selector.close();
   } catch (IOException e) {
     exception = e;
   }
   if (exception != null) {
     throw exception;
   }
 }
 public SelectionKey keyFor(final Selector sel) {
   return wrapped.keyFor(sel);
 }
 public int write(final ByteBuffer src) throws IOException {
   return wrapped.write(src);
 }
 public long write(final ByteBuffer[] srcs, final int offset, final int length)
     throws IOException {
   return wrapped.write(srcs, offset, length);
 }
 public SelectableChannel configureBlocking(final boolean block) throws IOException {
   return wrapped.configureBlocking(block);
 }
 protected void implCloseChannel() throws IOException {
   wrapped.close();
 }
 public SelectorProvider provider() {
   return wrapped.provider();
 }
 public int validOps() {
   return wrapped.validOps();
 }
 public boolean isRegistered() {
   return wrapped.isRegistered();
 }
 public long write(final ByteBuffer[] srcs) throws IOException {
   return wrapped.write(srcs);
 }
 public SelectionKey register(final Selector sel, final int ops, final Object att)
     throws ClosedChannelException {
   return wrapped.register(sel, ops, att);
 }
 public boolean isBlocking() {
   return wrapped.isBlocking();
 }
 public Object blockingLock() {
   return wrapped.blockingLock();
 }