package cogmac.housefly.directionUnderstanding;

import java.io.IOException;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

/**
 * Server for receiving recognition results from SLS's speech recognizer. 
 * @author stefie10
 *
 */
public class WamiXmlRpcServer {

    
    class TestReceiver {
        public boolean receiveString(String str) {
            System.out.println("Got string: " + str);
            return true;
        }		
    };
    
    
    public static class Receiver {
        public boolean sendCommand(String str) {
        	System.out.println("Got: " + str);
            if(mExec != null) {
                mExec.executeQuery(str);
            }
            return true;
        }
    };
    
    
    private final int mPort;
    private final WebServer mWebServer;
    private final XmlRpcServer mXmlRpcServer;
    private static QueryExecutor mExec;

    public WamiXmlRpcServer(int port, QueryExecutor exec) throws IOException {
        mPort = port;
        mExec = exec;
        mWebServer = new WebServer(mPort);
        mXmlRpcServer = mWebServer.getXmlRpcServer();


        PropertyHandlerMapping phm = new PropertyHandlerMapping();

        try {
            phm.addHandler("du", Receiver.class);
        } catch (XmlRpcException e) {
            throw new IllegalStateException("Couldn't create handler on " + mPort, e);
        }

        mXmlRpcServer.setHandlerMapping(phm);
        XmlRpcServerConfigImpl serverConfig =
            (XmlRpcServerConfigImpl) mXmlRpcServer.getConfig();
        serverConfig.setEnabledForExtensions(true);
        serverConfig.setContentLengthOptional(false);
        System.out.println("config: " + serverConfig);
        mWebServer.start();

    }

    

    public static void main(String[] args) throws Exception {

        System.out.println("Hello world");

        WamiXmlRpcServer server = new WamiXmlRpcServer(4243, null);

    }

}
