diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 12:42:46 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-01-11 12:42:46 +0100 |
commit | 2b1a0ad7d0e2a7766c6c4e4f5f31a08fb8d89d63 (patch) | |
tree | f5e183b5e09dc12610f2f8227fb49e8bb15d0a71 /jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java | |
parent | a88ef239b58ba926f9ce29e2b87d636fae18aaf4 (diff) |
Add IP discovery.
Before a client wants to join a network, it may send a HTTP "ping"
request to a known participant. The participant will send a HTTP "pong"
request in response, which informs the client of its IP address.
The IP address of a client is automatically retrieved from the first
HTTP request received by Jetty.
Diffstat (limited to 'jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java')
-rw-r--r-- | jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java b/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java index 39676fe..0bafad4 100644 --- a/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java +++ b/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java @@ -1,34 +1,52 @@ 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 com.googlecode.jsonrpc4j.JsonRpcServer; -import com.orbekk.same.CallerInfoListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class RpcHandler extends AbstractHandler { + private Logger logger = LoggerFactory.getLogger(getClass()); private JsonRpcServer rpcServer; - private CallerInfoListener callerInfoListener; + private UrlReceiver urlReceiver; public RpcHandler(JsonRpcServer rpcServer, - CallerInfoListener callerInfoListener) { + UrlReceiver urlReceiver) { this.rpcServer = rpcServer; - this.callerInfoListener = callerInfoListener; + this.urlReceiver = urlReceiver; } @Override public synchronized void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - if (callerInfoListener != null) { - callerInfoListener.setCaller(request.getRemoteAddr()); + 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); } - rpcServer.handle(request, response); + baseRequest.setHandled(true); } } |