/** * Constructor. * * @param host server name * @param port server port * @param user user name * @param pass password * @throws IOException Exception */ public BaseXClient(final String host, final int port, final String user, final String pass) throws IOException { socket = new Socket(); socket.connect(new InetSocketAddress(host, port), 5000); in = new BufferedInputStream(socket.getInputStream()); out = socket.getOutputStream(); ehost = host; // receive timestamp final String ts = receive(); // send {Username}0 and hashed {Password/Timestamp}0 send(user); send(md5(md5(pass) + ts)); // receive success flag if (!ok()) throw new IOException("Access denied."); }
/** * 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); }
/** * Closes the session. * * @throws IOException Exception */ public void close() throws IOException { send("exit"); out.flush(); if (esocket != null) esocket.close(); socket.close(); }