AsyncServerDemo.java
01 package com.flat502.rox.demo;
02 
03 import java.net.InetAddress;
04 
05 import com.flat502.rox.marshal.RpcCall;
06 import com.flat502.rox.marshal.xmlrpc.XmlRpcMethodResponse;
07 import com.flat502.rox.server.AsynchronousRequestHandler;
08 import com.flat502.rox.server.ResponseChannel;
09 import com.flat502.rox.server.RpcCallContext;
10 import com.flat502.rox.server.XmlRpcServer;
11 
12 /**
13  * A demo asynchronous server illustrating the {@link com.flat502.rox.server.AsynchronousRequestHandler}
14  * interface.
15  */
16 public class AsyncServerDemo implements AsynchronousRequestHandler {
17   public void handleRequest(RpcCall call, RpcCallContext context, ResponseChannel rspChannelthrows Exception {
18     Object[] params = call.getParameters();
19     System.out.println("Method [" + call.getName() "] called with "
20         + params.length + " parameters");
21     for (int i = 0; i < params.length; i++) {
22       System.out.println("   Param " (i + 1" [" + params[i"]");
23     }
24 
25     // This could just as easily be done from another thread.
26     rspChannel.respond(new XmlRpcMethodResponse(new TimeInfo()));
27   }
28 
29   /**
30    * Start an instance of this demo server.
31    <p>
32    * The following XML-RPC methods are supported by
33    * this server:
34    <ul>
35    <li>{@link RMIServerInterface#sum(int[]) rmi.sum}</li>
36    <li>{@link RMIServerInterface#getDate() rmi.getDate}</li>
37    <li>{@link RMIServerInterface#getVersionInfo(boolean) rmi.getVersion}</li>
38    </ul>
39    @param args
40    *   A list of parameters indicating
41    *   the <code>host/address</code> and
42    *   <code>port</code> to bind to. These default to 
43    *   <code>localhost</code> and <code>8080</code> if
44    *   not specified.
45    */
46   public static void main(String[] args) {
47     try {
48       String host = "localhost";
49       int port = 8080;
50 
51       if (args != null && args.length > 0) {
52         host = args[0];
53         if (args.length > 1) {
54           port = Integer.parseInt(args[1]);
55         }
56       }
57       System.out.println("Starting server on " + host + ":" + port);
58 
59       XmlRpcServer server = new XmlRpcServer(InetAddress.getByName(host), port);
60       server.registerHandler(null, "^example\\."new AsyncServerDemo());
61       server.start();
62     catch (Exception e) {
63       e.printStackTrace();
64     }
65   }
66 }