示例#1
0
  /**
   * Watches an event.
   *
   * @param name event name
   * @param notifier event notification
   * @throws IOException I/O exception
   */
  public void watch(final String name, final EventNotifier notifier) throws IOException {

    sout.write(ServerCmd.WATCH.code);
    if (esocket == null) {
      sout.flush();
      final BufferInput bi = new BufferInput(sin);
      final int eport = Integer.parseInt(bi.readString());
      // initialize event socket
      esocket = new Socket();
      esocket.connect(new InetSocketAddress(ehost, eport), 5000);
      final OutputStream so = esocket.getOutputStream();
      so.write(bi.readBytes());
      so.write(0);
      so.flush();
      final InputStream is = esocket.getInputStream();
      is.read();
      listen(is);
    }
    send(name);
    sout.flush();
    receive(null);
    notifiers.put(name, notifier);
  }
示例#2
0
 /**
  * Retrieves data from the server.
  *
  * @param bi buffered server input
  * @param os output stream
  * @throws IOException I/O exception
  */
 protected static void receive(final BufferInput bi, final OutputStream os) throws IOException {
   final DecodingInput di = new DecodingInput(bi);
   for (int b; (b = di.read()) != -1; ) os.write(b);
 }