From 2b1a0ad7d0e2a7766c6c4e4f5f31a08fb8d89d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Wed, 11 Jan 2012 12:42:46 +0100 Subject: 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. --- .../src/main/java/com/orbekk/rpc/RpcHandler.java | 40 ++++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java') 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); } } -- cgit v1.2.3