001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package xmladder;
016:
017: import java.net.*;
018: import java.io.*;
019: import org.quickserver.net.server.ClientCommandHandler;
020: import org.quickserver.net.server.ClientEventHandler;
021: import org.quickserver.net.server.ClientHandler;
022: import org.quickserver.net.server.DataMode;
023: import org.quickserver.net.server.DataType;
024: import java.util.logging.*;
025:
026: public class CommandHandler implements ClientEventHandler,
027: ClientCommandHandler {
028: private static Logger logger = Logger
029: .getLogger(CommandHandler.class.getName());
030:
031: private final static String welcome = "<welcome name=\"XmlAdder v1.0\">"
032: + "\n" + "<note>Send <quit /> to close connection</note>";
033:
034: //--ClientEventHandler
035: public void gotConnected(ClientHandler handler)
036: throws SocketTimeoutException, IOException {
037: logger.fine("Connection opened : " + handler.getHostAddress());
038: /*
039: //pick from xml
040: handler.setDataMode(DataMode.BYTE, DataType.IN);
041: handler.setDataMode(DataMode.BYTE, DataType.OUT);
042: */
043: handler.sendClientBytes(welcome);
044: }
045:
046: public void lostConnection(ClientHandler handler)
047: throws IOException {
048: logger.fine("Connection lost : " + handler.getHostAddress());
049: }
050:
051: public void closingConnection(ClientHandler handler)
052: throws IOException {
053: handler.sendSystemMsg("Connection closed: "
054: + handler.getHostAddress(), Level.FINE);
055: }
056:
057: //--ClientEventHandler
058:
059: public void handleCommand(ClientHandler handler, String command)
060: throws SocketTimeoutException, IOException {
061: AddNumberReq addNumberReq = null;
062: AddNumberRes addNumberRes = new AddNumberRes();
063: command = command.trim();
064: try {
065: if (command.equals("<quit />")) {
066: handler.sendClientBytes("<bye />");
067: handler.closeConnection();
068: return;
069: }
070:
071: Data data = (Data) handler.getClientData();
072: data.addXmlPart(command);
073:
074: String xmlToProcess = null;
075: while (true) {
076: xmlToProcess = data.getNextXML();
077: if (xmlToProcess == null)
078: break;
079:
080: logger.fine("Got xml to process from data \n:"
081: + xmlToProcess);
082:
083: addNumberReq = AddNumberReq.fromXML(xmlToProcess);
084: if (addNumberReq == null) {
085: addNumberRes.setType("error");
086: addNumberRes.setValue("Bad XML Got!");
087: }
088:
089: int a = addNumberReq.getNumberA();
090: int b = addNumberReq.getNumberB();
091: int c = a + b;
092:
093: addNumberRes.setType("sum");
094: addNumberRes.setValue("" + c);
095: handler.sendClientBytes(addNumberRes.toXML());
096: }
097: } catch (IOException e) {
098: throw e;
099: } catch (Exception e) {
100: addNumberRes.setType("error");
101: addNumberRes.setValue("Exception : " + e);
102: handler.sendClientBytes(addNumberRes.toXML());
103: }
104: }
105: }
|