Example #1
0
  private void talkTo(String av[]) {
    if (av.length >= 1) host = av[0];
    else host = "10.196.2.55";
    // host = "192.168.1.230";
    if (av.length >= 2) portNum = Integer.parseInt(av[1]);
    else portNum = 30004;
    System.out.println("Host " + host + "; port " + portNum);
    try {
      Socket s = new Socket(host, portNum);
      this.addNewADSBHub();
      // Connect the remote to our stdout
      Pipe pipe = new Pipe(s.getInputStream(), System.out);
      // new Pipe(s.getInputStream(), System.out).start();
      pipe.setSensor(sensor);
      pipe.start();

      // Connect our stdin to the remote
      // new Pipe(System.in, s.getOutputStream()).start();

    } catch (IOException e) {
      System.out.println(e);
      return;
    }
    System.out.println("Connected OK");
  }
 /** ElementConnection constructor comment. */
 public ElementConnection(Pipe src, Pipe dest) {
   super();
   srcId = src.getElement().getId();
   srcPipeName = src.getName();
   destId = dest.getElement().getId();
   destPipeName = dest.getName();
 }
Example #3
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 #4
0
 @Override
 public Pipe pipe() {
   Buffer buffer = new Buffer();
   Pipe result = new Pipe();
   result.sink = buffer;
   result.source = new RealBufferedSource(buffer);
   return result;
 }
Example #5
0
  private void informWriterError(NHttpServerConnection conn) {
    Pipe writer = SourceContext.get(conn).getWriter();

    metrics.incrementFaultsSending();

    if (writer != null) {
      writer.consumerError();
    }
  }
Example #6
0
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Pipe pipe = (Pipe) o;

    return this.toStandardUnit() == pipe.toStandardUnit();
  }
Example #7
0
  private void informReaderError(NHttpServerConnection conn) {
    Pipe reader = SourceContext.get(conn).getReader();

    metrics.incrementFaultsReceiving();

    if (reader != null) {
      reader.producerError();
    }
  }
    private synchronized void unloadHandler() {
      if (_pipe == null) {
        throw new IllegalArgumentException("No handler active");
      }

      Pipe pipe = _pipe;
      _pipe = null;

      pipe.push(new Event(Event.UNLOAD));
    }
Example #9
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);
  }
Example #10
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);
  }
Example #11
0
  public void render(SpriteBatch batch) {
    batch.begin();
    for (Platform p : platforms) p.render(batch);
    for (QBlock q : qBlocks) q.render(batch);
    for (Goomba g : goombas) g.render(batch);
    for (Pipe p : pipes) p.render(batch);

    mario.render(batch);
    batch.end();
  }
Example #12
0
 public void addPipe() {
   int r = config.random(0, 7);
   float dy = r * 10;
   r = config.random(0, 1);
   if (r == 0) {
     dy = -dy;
   }
   Pipe pipe1 = new Pipe(Atlas.findRegion("pipe1"), bird, true);
   pipe1.setZIndex(1);
   float x = Flappybird.VIEWPORT.x;
   float y =
       (Flappybird.VIEWPORT.y - config.KlandHeight) / 2
           + config.KlandHeight
           + config.KholeBetwenPipe / 2;
   pipe1.setPosition(x, y + dy);
   Pipe pipe2 = new Pipe(Atlas.findRegion("pipe2"), bird, false);
   pipe2.setZIndex(1);
   y =
       (Flappybird.VIEWPORT.y - config.KlandHeight) / 2
           + config.KlandHeight
           - pipe2.getHeight()
           - config.KholeBetwenPipe / 2;
   pipe2.setPosition(x, y + dy);
   stage.addActor(pipe1);
   stage.addActor(pipe2);
   labelScore.setZIndex(pipe1.getZIndex());
   land.setZIndex(pipe2.getZIndex());
   bird.setZIndex(pipe2.getZIndex());
 }
Example #13
0
 public boolean unsetupPipe(Channel in, Channel out) {
   for (Iterator itr = getPipes().iterator(); itr.hasNext(); ) {
     Pipe tmp = (Pipe) itr.next();
     if (tmp.equals(in, out)) {
       tmp.close();
       getPipes().remove(tmp);
       return true;
     }
   }
   return false;
 }
Example #14
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);
       }
     }
   }
 }
Example #15
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");
    }
  }
Example #16
0
 @Override
 public Pipe pipe() {
   Buffer buffer = new Buffer();
   Pipe result = new Pipe();
   result.sink = buffer;
   result.source =
       new RealBufferedSource(
           new ForwardingSource(buffer) {
             @Override
             public long read(Buffer sink, long byteCount) throws IOException {
               return super.read(sink, Math.min(byteCount, 1L));
             }
           });
   return result;
 }
Example #17
0
 public ReadablePipe getBinding(String stepName, String portName) {
   if (name.equals(stepName) && "error".equals(portName)) {
     return new Pipe(runtime, errorPipe.documents());
   } else {
     return super.getBinding(stepName, portName);
   }
 }
Example #18
0
 public void resetGame() {
   stage.clear();
   Pipe.setPIPE_HIT(1);
   addBackground();
   addBird();
   addScore();
   addLand();
 }
Example #19
0
 private static void read(Pipe p) throws IOException {
   InputStream in = p.getIn();
   for (int cnt = 0; cnt < 256 * 256; cnt++) {
     assertEquals(cnt / 256, in.read());
   }
   assertEquals(-1, in.read());
   in.close();
 }
Example #20
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();
  }
 /**
  * Reads <i>length</i> bytes from the pipe and puts them into the <i>buff</i> array starting from
  * position <i>offset</i>.
  */
 public synchronized int read(byte[] buff, int offset, int length) {
   int available = length();
   if (available >= length) return super.read(buff, offset, length);
   // else
   super.read(buff, offset, available);
   int diff = length - available;
   for (int i = 0; i < diff; i++) buff[offset + available + i] = 0;
   return length;
 }
Example #22
0
 private static void write(Pipe pipe) throws IOException {
   OutputStream os = pipe.getOut();
   byte[] buf = new byte[384];
   for (int i = 0; i < 256; i++) {
     Arrays.fill(buf, (byte) i);
     os.write(buf, 0, 256);
   }
   os.close();
 }
Example #23
0
 @Override
 public void close() throws InterruptedException {
   first.close();
   if (needFlush) {
     pendingData = true;
     if (!runningTask) {
       execute(this);
     }
   }
 }
Example #24
0
  /** Test the "local-write remote-read" pipe. */
  public void testLocalWrite() throws Exception {
    Pipe p = Pipe.createLocalToRemote();
    Future<Integer> f = channel.callAsync(new ReadingCallable(p));

    write(p);

    int r = f.get();
    System.out.println("result=" + r);
    assertEquals(5, r);
  }
Example #25
0
  /** Writer end closes even before the remote computation kicks in. */
  public void testQuickBurstWrite() throws Exception {
    final Pipe p = Pipe.createLocalToRemote();
    Future<Integer> f =
        channel.callAsync(
            new Callable<Integer, IOException>() {
              public Integer call() throws IOException {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                IOUtils.copy(p.getIn(), baos);
                return baos.size();
              }
            });
    OutputStream os = p.getOut();
    os.write(1);
    os.close();

    // at this point the async executable kicks in.
    // TODO: introduce a lock to ensure the ordering.

    assertEquals(1, (int) f.get());
  }
Example #26
0
  public void testLocalWrite2() throws Exception {
    Pipe p = Pipe.createLocalToRemote();
    Future<Integer> f = channel.callAsync(new ReadingCallable(p));

    Thread.sleep(2000); // wait for remote to connect to local.
    write(p);

    int r = f.get();
    System.out.println("result=" + r);
    assertEquals(5, r);
  }
Example #27
0
  public void testSaturation() throws Exception {
    if (channelRunner instanceof InProcessCompatibilityMode)
      return; // can't do this test without the throttling support.

    final Pipe p = Pipe.createLocalToRemote();

    Thread writer =
        new Thread() {
          @Override
          public void run() {
            OutputStream os = p.getOut();
            try {
              byte[] buf = new byte[Channel.PIPE_WINDOW_SIZE * 2 + 1];
              os.write(buf);
            } catch (IOException e) {
              e.printStackTrace();
            }
          }
        };

    // 1. wait until the receiver sees the first byte. at this point the pipe should be completely
    // clogged
    // 2. make sure the writer thread is still alive, blocking
    // 3. read the rest

    ISaturationTest target = channel.call(new CreateSaturationTestProxy(p));

    // make sure the pipe is connected
    target.ensureConnected();
    writer.start();

    // make sure that some data arrived to the receiver
    // at this point the pipe should be fully clogged
    assertEquals(0, target.readFirst());

    // the writer should be still blocked
    Thread.sleep(1000);
    assertTrue(writer.isAlive());

    target.readRest();
  }
 /**
  * Writes <i>length</i> bytes into this pipe from the <i>buff</i> array starting from position
  * <i>offset</i>.
  */
 public synchronized void write(byte[] buff, int offset, int length) {
   int freespace = freespace();
   if (freespace < length) read(aux, 0, length - freespace);
   super.write(buff, offset, length);
 }
 /** Writes the specified byte to this pipe. */
 public synchronized void write(byte b) {
   if (freespace() == 0) read();
   super.write(b);
 }
Example #30
0
 public int size() {
   return last.size();
 }