public Session findSession(String id) { int n = clients.size(); Log.fine(Name + ": looking for client with id " + id); for (int i = 0; i < n; i++) { Session ias = clients.elementAt(i); Log.fine(Name + ": client " + i + " id is " + ias.sid); if (ias.sid.equals(id)) return ias; } return null; }
public Session findSessionTo(String to) { int n = clients.size(); Log.fine(Name + ": looking for client to " + to); for (int i = 0; i < n; i++) { Session ias = clients.elementAt(i); Log.fine(Name + ": client " + i + " id is " + ias.sid); if (ias.sid.startsWith(to)) return ias; } return null; }
public void execute(String command) { try { Vector<String> tokens = new Vector<String>(); StringTokenizer st = new StringTokenizer(command); while (st.hasMoreTokens()) { String token = st.nextToken(); tokens.add(token); } int ntok = tokens.size(); if (command.startsWith("connect")) { if (ntok != 3) { Log.warning("Syntax: connect host port"); return; } String host = tokens.elementAt(1); int port = Integer.parseInt(tokens.elementAt(2)); Socket link = new Socket(host, port); int count = sessionCount(); String id = Name + "_" + count; SphinxResponder rtest = new SphinxResponder(); SpeechSession ias = new SpeechSession(id, this, link, rtest); rtest.setOwner(ias); addSession(ias); Log.fine("Made connection client id " + id); } else if (command.startsWith("disconnect")) { if (ntok != 2) { Log.warning("Sytax: disconnect sessionid"); return; } String id = tokens.elementAt(1); Session ias = findSession(id); if (ias == null) { Log.warning("No session with id " + id); return; } // send a terminate message to the session Log.info("disconnecting from " + id); String req = "{action: JviaTerminate, from: " + getId() + ", to: " + ias.getSid(); req += ", message: JviaTerminate}"; ias.outbuffer = req; sleep(waitTime); ias.terminate(); // then terminate it } else if (command.startsWith("send")) { // write it to the outstream of the session? if (ntok < 3) { Log.warning("Sytax: send sessionid message"); return; } String id = tokens.elementAt(1); Session ias = findSession(id); if (ias == null) { Log.warning("No session with id " + id); return; } StringBuffer sb = new StringBuffer(); sb.append(tokens.elementAt(2)); for (int i = 3; i < ntok; i++) { sb.append(' '); sb.append(tokens.elementAt(i)); } String message = new String(sb); // String req = "{action: send, from: "+getId ()+", to: "+ias.getSid (); // req += ", message: "+message+"}"; // send a terminate message to the session ias.outbuffer = message; Log.fine("sending \"" + message + "\" to " + id); if (message.equals(TestResponder.terminateRequest)) { sleep(waitTime); ias.terminate(); } } else if (command.equals("terminate")) { // terminate all clients, then stop Vector<Session> clients = getClients(); for (int i = 0; i < clients.size(); i++) { Session ias = clients.elementAt(i); String req = "{action: JviaTerminate, from: " + getId() + ", to: " + ias.getSid(); req += ", message: JviaTerminate}"; ias.outbuffer = req; sleep(waitTime); Log.info("Terminating " + ias.getSid()); ias.terminate(); } server.close(); interrupt(); return; } else { Log.warning("illegal command: " + command); } } catch (Exception e) { Log.severe("sphinxServer:execute " + e.toString()); } }