/** * Unwatches an event. * * @param name event name * @throws IOException I/O exception */ public void unwatch(final String name) throws IOException { out.write(11); send(name); info = receive(); if (!ok()) throw new IOException(info); notifiers.remove(name); }
/** * 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 { out.write(10); if (esocket == null) { final int eport = Integer.parseInt(receive()); // initialize event socket esocket = new Socket(); esocket.connect(new InetSocketAddress(ehost, eport), 5000); final OutputStream os = esocket.getOutputStream(); receive(in, os); os.write(0); os.flush(); final InputStream is = esocket.getInputStream(); is.read(); listen(is); } send(name); info = receive(); if (!ok()) throw new IOException(info); notifiers.put(name, notifier); }
/** * Sends a command, argument, and input. * * @param cmd command * @param path path to document * @param input xml input * @throws IOException I/O exception */ private void send(final int cmd, final String path, final InputStream input) throws IOException { out.write(cmd); send(path); send(input); }
/** * Receives a string and writes it to the specified output stream. * * @param is input stream * @param os output stream * @throws IOException I/O exception */ static void receive(final InputStream is, final OutputStream os) throws IOException { for (int b; (b = is.read()) > 0; ) { // read next byte if 0xFF is received os.write(b == 0xFF ? is.read() : b); } }
/** * Sends a string to the server. * * @param s string to be sent * @throws IOException I/O exception */ void send(final String s) throws IOException { out.write((s + '\0').getBytes(UTF8)); }
/** * Checks the next success flag. * * @return value of check * @throws IOException Exception */ boolean ok() throws IOException { out.flush(); return in.read() == 0; }
/** * Closes the session. * * @throws IOException Exception */ public void close() throws IOException { send("exit"); out.flush(); if (esocket != null) esocket.close(); socket.close(); }