예제 #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();
  }
예제 #2
0
  /**
   * A convenience method for the following code:
   *
   * <pre>
   * {@link Pipe} inputToOutput = {@link Pipe#open()};
   * {@link Pipe.SourceChannel} source = {@link Pipe#source() inputToOutput.source()};
   * {@link SelectableChannel#configureBlocking(boolean) source.configureBlocking(false)};
   * {@link #addRoute(SelectableChannel, SelectableChannel) addRoute(source, output)};
   *
   * {@link Pipe.SinkChannel} sink = {@link Pipe#sink() inputToOutput.sink()};
   * return {@link #wrapChannelInObjectOutputStream(WritableByteChannel) wrapChannelInObjectOutputStream(sink)};
   * </pre>
   *
   * Users must call {@link ObjectOutputStream#flush()} before attempting to construct a matching
   * {@link ObjectInputStream} to ensure a serialization header is available.
   *
   * @throws IOException pass-through for any {@link IOException} that occurs in the above code
   */
  public <T extends SelectableChannel & WritableByteChannel>
      ObjectOutputStream addRouteFromObjectOutputStream(T output) throws IOException {
    Pipe inputToOutput = Pipe.open();
    Pipe.SourceChannel source = inputToOutput.source();
    source.configureBlocking(false);
    addRoute(source, output);

    Pipe.SinkChannel sink = inputToOutput.sink();
    return wrapChannelInObjectOutputStream(sink);
  }
 public SelectorImpl(SelectorProvider selectorProvider) {
   super(selectorProvider);
   try {
     Pipe mockSelector = selectorProvider.openPipe();
     sink = mockSelector.sink();
     source = mockSelector.source();
     sourcefd = ((FileDescriptorHandler) source).getFD();
     source.configureBlocking(false);
   } catch (IOException e) {
     // do nothing
   }
 }