diff options
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/rpc')
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/rpc/App.java | 47 | ||||
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/rpc/Client.java | 66 | ||||
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java | 52 |
3 files changed, 0 insertions, 165 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/App.java b/jsonrpc/src/main/java/com/orbekk/rpc/App.java deleted file mode 100644 index 0e9dce3..0000000 --- a/jsonrpc/src/main/java/com/orbekk/rpc/App.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.orbekk.rpc; - -import com.googlecode.jsonrpc4j.JsonRpcServer; -import com.orbekk.same.ConnectionManagerImpl; -import com.orbekk.same.SameState; -import com.orbekk.same.SameService; -import com.orbekk.same.SameServiceImpl; -import org.eclipse.jetty.server.Server; - -public class App { - public static void main(String[] args) { - if (args.length < 3) { - System.err.println("Arguments: port networkName clientId"); - System.exit(1); - } - int port = Integer.parseInt(args[0]); - String networkName = args[1]; - String clientId = args[2]; - - ConnectionManagerImpl connections = new ConnectionManagerImpl(); - - SameState sameState = new SameState(networkName, clientId, - connections); - sameState.start(); - - SameServiceImpl service = new SameServiceImpl(sameState); - JsonRpcServer jsonServer = new JsonRpcServer(service, - SameService.class); - - Server server = new Server(port); - RpcHandler rpcHandler = new RpcHandler(jsonServer, sameState); - server.setHandler(rpcHandler); - - try { - server.start(); - } catch (Exception e) { - System.out.println("Could not start jetty server."); - e.printStackTrace(); - } - - try { - server.join(); - } catch (InterruptedException e) { - System.out.println("Interrupt"); - } - } -} diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/Client.java b/jsonrpc/src/main/java/com/orbekk/rpc/Client.java deleted file mode 100644 index 225347a..0000000 --- a/jsonrpc/src/main/java/com/orbekk/rpc/Client.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.orbekk.rpc; - -import com.googlecode.jsonrpc4j.JsonRpcServer; -import com.orbekk.same.ConnectionManagerImpl; -import com.orbekk.same.SameState; -import com.orbekk.same.SameService; -import com.orbekk.same.SameServiceImpl; -import com.orbekk.net.HttpUtil; -import org.eclipse.jetty.server.Server; - -public class Client { - - public static void main(String[] args) { - if (args.length < 4) { - System.err.println("Arguments: port clientId thisNetworkName " + - "remoteNetworkAddr"); - System.exit(1); - } - int port = Integer.parseInt(args[0]); - String clientId = args[1]; - String networkName = args[2]; - String remoteAddr = args[3]; - - ConnectionManagerImpl connections = new ConnectionManagerImpl(); - - SameState sameState = new SameState(networkName, clientId, - connections); - sameState.start(); - - SameServiceImpl service = new SameServiceImpl(sameState); - JsonRpcServer jsonServer = new JsonRpcServer(service, - SameService.class); - - Server server = new Server(port); - RpcHandler rpcHandler = new RpcHandler(jsonServer, sameState); - server.setHandler(rpcHandler); - - try { - server.start(); - } catch (Exception e) { - System.out.println("Could not start jetty server."); - e.printStackTrace(); - } - - while (sameState.getUrl() == null) { - HttpUtil.sendHttpRequest(remoteAddr + "ping?port=" + port); - try { - Thread.sleep(500); - } catch (InterruptedException e) { - // Ignore interrupt in wait loop. - } - } - - SameService remoteService = connections.getConnection(remoteAddr); - remoteService.notifyNetwork("NoNetwork"); - remoteService.participateNetwork("FirstNetwork", - sameState.getClientId(), sameState.getUrl()); - - try { - server.join(); - } catch (InterruptedException e) { - System.out.println("Interrupt"); - } - - } -} diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java b/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java deleted file mode 100644 index 0bafad4..0000000 --- a/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.orbekk.rpc; - -import com.googlecode.jsonrpc4j.JsonRpcServer; -import com.orbekk.net.HttpUtil; -import com.orbekk.same.UrlReceiver; -import java.io.IOException; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.handler.AbstractHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class RpcHandler extends AbstractHandler { - private Logger logger = LoggerFactory.getLogger(getClass()); - private JsonRpcServer rpcServer; - private UrlReceiver urlReceiver; - - public RpcHandler(JsonRpcServer rpcServer, - UrlReceiver urlReceiver) { - this.rpcServer = rpcServer; - this.urlReceiver = urlReceiver; - } - - @Override - public synchronized void handle(String target, Request baseRequest, - HttpServletRequest request, HttpServletResponse response) - throws IOException, ServletException { - logger.info("Handling request to target: " + target); - - if (urlReceiver != null) { - String sameServiceUrl = "http://" + request.getLocalAddr() + - ":" + request.getLocalPort() + "/SameService.json"; - urlReceiver.setUrl(sameServiceUrl); - urlReceiver = null; - } - - if (target.equals("/ping")) { - int remotePort = Integer.parseInt(request.getParameter("port")); - String pongUrl = "http://" + request.getRemoteAddr() + ":" + - remotePort + "/pong"; - logger.info("Got ping. Sending pong to {}", pongUrl); - HttpUtil.sendHttpRequest(pongUrl); - } else if (target.equals("/pong")) { - logger.info("Received pong from {}", request.getRemoteAddr()); - } else { - rpcServer.handle(request, response); - } - baseRequest.setHandled(true); - } -} |