Пример #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
  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();
  }
Пример #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);
  }
Пример #4
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);
  }
Пример #5
0
 public PipeManager() {
   setPipes(new Vector());
   for (Iterator itr = Config.getConfig().getPipes().iterator(); itr.hasNext(); ) {
     String tmp = itr.next().toString();
     if (tmp != null) {
       Pipe p = getPipe(tmp);
       if (p != null) {
         p.open();
         getPipes().add(p);
       }
     }
   }
 }
Пример #6
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");
    }
  }
Пример #7
0
  public static void main(String[] args) throws IOException {
    ChannelRouter channelRouter =
        new ChannelRouter(
            new ExceptionListener<IOException>() {
              @Override
              public void exceptionThrown(IOException exception) {
                exception.printStackTrace();
              }
            });

    int port = Integer.parseInt(args[1]);
    SocketChannel socketChannel;
    if (args[0].equals("--server")) {
      System.out.println("Server mode started; listening on port " + port);
      ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.socket().bind(new InetSocketAddress(port));
      socketChannel = serverSocketChannel.accept();
      System.out.println("Client connected");
      serverSocketChannel.close();
    } else {
      String host = args[0];
      System.out.println("Client mode started; connecting to " + host + " on port " + port);
      socketChannel = SocketChannel.open(new InetSocketAddress(host, port));
      System.out.println("Connected to server");
    }
    socketChannel.configureBlocking(false);

    Pipe localToRemote = Pipe.open();
    localToRemote.source().configureBlocking(false);
    channelRouter.addRoute(localToRemote.source(), socketChannel);

    final Pipe remoteToLocal = Pipe.open();
    remoteToLocal.sink().configureBlocking(false);
    channelRouter.addRoute(socketChannel, remoteToLocal.sink());

    Thread channelRouterThread = new Thread(channelRouter, "ChannelRouter");
    channelRouterThread.start();

    Thread remoteReaderThread =
        new Thread("RemoteReader") {
          @Override
          public void run() {
            try {
              // BufferedReader.readLine() blocks until an end-of-line is
              // written, so make sure they get written by the main thread
              InputStream temporaryInputStream = Channels.newInputStream(remoteToLocal.source());
              BufferedReader in = new BufferedReader(new InputStreamReader(temporaryInputStream));
              String line = in.readLine();
              while (null != line) {
                System.out.println(line);
                line = in.readLine();
              }
            } catch (ClosedByInterruptException e) {
              // The main thread wants us to close, so do nothing
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        };
    remoteReaderThread.start();

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    Writer temporaryWriter = new OutputStreamWriter(Channels.newOutputStream(localToRemote.sink()));
    temporaryWriter = new BufferedWriter(temporaryWriter);
    PrintWriter out = new PrintWriter(temporaryWriter, true);

    System.out.println(
        "Press end-of-file to terminate (Ctrl+Z on Windows, Ctrl+D on other platforms)");
    String line = in.readLine();
    while (null != line) {
      // The end-of-line is required for BufferedReader.readLine() to
      // return and this will automatically flush due to the auto-flush
      // parameter on construction
      out.println(line);
      line = in.readLine();
    }

    remoteReaderThread.interrupt();
    channelRouterThread.interrupt();
    socketChannel.close();
  }
Пример #8
0
 public boolean addPipe(Pipe p) {
   p.open();
   return getPipes().add(p);
 }
Пример #9
0
 public boolean setupLurkPipe(Channel in, Channel out) {
   Pipe p = new LurkPipe(in, out);
   p.open();
   getPipes().add(p);
   return true;
 }