summaryrefslogtreecommitdiff
path: root/jsonrpc/src
diff options
context:
space:
mode:
Diffstat (limited to 'jsonrpc/src')
-rw-r--r--jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java49
-rw-r--r--jsonrpc/src/main/java/com/orbekk/rpc/App.java29
-rw-r--r--jsonrpc/src/main/java/com/orbekk/rpc/Client.java25
-rw-r--r--jsonrpc/src/main/java/com/orbekk/rpc/PingService.java5
-rw-r--r--jsonrpc/src/main/java/com/orbekk/rpc/PingServiceImpl.java8
-rw-r--r--jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java27
-rw-r--r--jsonrpc/src/main/resources/log4j.properties5
7 files changed, 148 insertions, 0 deletions
diff --git a/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java b/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java
new file mode 100644
index 0000000..50cdbc7
--- /dev/null
+++ b/jsonrpc/src/main/java/com/orbekk/net/BroadcastListener.java
@@ -0,0 +1,49 @@
+package com.orbekk.net;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.SocketException;
+
+import org.apache.log4j.Logger;
+
+public class BroadcastListener {
+ private int port;
+ private Logger logger = Logger.getLogger(getClass());
+
+ public BroadcastListener(int port) {
+ this.port = port;
+ }
+
+ public boolean listen() {
+ logger.info("Waiting for broadcast on port " + port);
+ DatagramSocket socket;
+ try {
+ socket = new DatagramSocket(port);
+ } catch (SocketException e) {
+ logger.warn("Failed to create socket.", e.fillInStackTrace());
+ return true;
+ }
+ try {
+ socket.setBroadcast(true);
+ } catch (SocketException e) {
+ logger.warn(e.fillInStackTrace());
+ }
+ byte[] buffer = new byte[2048];
+ DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
+ try {
+ socket.receive(packet);
+ } catch (IOException e) {
+ logger.warn(e.fillInStackTrace());
+ }
+ logger.info("Received broadcast from " + packet.getAddress() +
+ ": " + new String(packet.getData()));
+ return true;
+ }
+
+ public static void main(String[] args) {
+ int port = Integer.parseInt(args[0]);
+ BroadcastListener listener = new BroadcastListener(port);
+ System.out.println("Received broadcast: " + listener.listen());
+ }
+}
diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/App.java b/jsonrpc/src/main/java/com/orbekk/rpc/App.java
new file mode 100644
index 0000000..e112008
--- /dev/null
+++ b/jsonrpc/src/main/java/com/orbekk/rpc/App.java
@@ -0,0 +1,29 @@
+package com.orbekk.rpc;
+
+import org.eclipse.jetty.server.Server;
+
+import com.googlecode.jsonrpc4j.JsonRpcServer;
+
+public class App {
+ public static void main(String[] args) {
+ PingService service = new PingServiceImpl();
+ JsonRpcServer jsonServer = new JsonRpcServer(service, PingService.class);
+
+ Server server = new Server(10080);
+ RpcHandler rpcHandler = new RpcHandler(jsonServer);
+ 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
new file mode 100644
index 0000000..bfb6c52
--- /dev/null
+++ b/jsonrpc/src/main/java/com/orbekk/rpc/Client.java
@@ -0,0 +1,25 @@
+package com.orbekk.rpc;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
+import com.googlecode.jsonrpc4j.ProxyUtil;
+
+public class Client {
+
+ public static void main(String[] args) {
+ JsonRpcHttpClient client = null;
+ try {
+ client = new JsonRpcHttpClient(
+ new URL("http://10.0.0.96:10080/PingService.json"));
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ PingService service = ProxyUtil.createProxy(
+ client.getClass().getClassLoader(),
+ PingService.class,
+ client);
+ System.out.println(service.ping());
+ }
+}
diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/PingService.java b/jsonrpc/src/main/java/com/orbekk/rpc/PingService.java
new file mode 100644
index 0000000..353f7c5
--- /dev/null
+++ b/jsonrpc/src/main/java/com/orbekk/rpc/PingService.java
@@ -0,0 +1,5 @@
+package com.orbekk.rpc;
+
+public interface PingService {
+ String ping();
+}
diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/PingServiceImpl.java b/jsonrpc/src/main/java/com/orbekk/rpc/PingServiceImpl.java
new file mode 100644
index 0000000..9472846
--- /dev/null
+++ b/jsonrpc/src/main/java/com/orbekk/rpc/PingServiceImpl.java
@@ -0,0 +1,8 @@
+package com.orbekk.rpc;
+
+public class PingServiceImpl implements PingService {
+ @Override
+ public String ping() {
+ return "Pong";
+ }
+}
diff --git a/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java b/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java
new file mode 100644
index 0000000..bc76e4e
--- /dev/null
+++ b/jsonrpc/src/main/java/com/orbekk/rpc/RpcHandler.java
@@ -0,0 +1,27 @@
+package com.orbekk.rpc;
+
+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;
+
+public class RpcHandler extends AbstractHandler {
+ private JsonRpcServer rpcServer;
+
+ public RpcHandler(JsonRpcServer rpcServer) {
+ this.rpcServer = rpcServer;
+ }
+
+ @Override
+ public void handle(String target, Request baseRequest,
+ HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException {
+ rpcServer.handle(request, response);
+ }
+}
diff --git a/jsonrpc/src/main/resources/log4j.properties b/jsonrpc/src/main/resources/log4j.properties
new file mode 100644
index 0000000..6ca6c6f
--- /dev/null
+++ b/jsonrpc/src/main/resources/log4j.properties
@@ -0,0 +1,5 @@
+log4j.rootLogger=DEBUG, A1
+
+log4j.appender.A1=org.apache.log4j.ConsoleAppender
+log4j.appender.A1.layout=org.apache.log4j.PatternLayout
+log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n