summaryrefslogtreecommitdiff
path: root/jsonrpc
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-10 15:34:55 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-10 15:34:55 +0100
commit9a554911174e096b95e99527eac04627400c41ba (patch)
tree2d36439a69ded1ca54051e3a1636106423c3d7c9 /jsonrpc
Initial import of maven projects.
jsonrpc - Common classes. rpctest - A small xmlrpc project. master - Android application containing broadcast code and ping service.
Diffstat (limited to 'jsonrpc')
-rw-r--r--jsonrpc/pom.xml89
-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
-rw-r--r--jsonrpc/target/classes/log4j.properties5
9 files changed, 242 insertions, 0 deletions
diff --git a/jsonrpc/pom.xml b/jsonrpc/pom.xml
new file mode 100644
index 0000000..4d06cfd
--- /dev/null
+++ b/jsonrpc/pom.xml
@@ -0,0 +1,89 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.orbekk</groupId>
+ <artifactId>jsonrpc</artifactId>
+ <version>0.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+
+ <name>jsonrpc</name>
+ <url>http://maven.apache.org</url>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <repositories>
+ <repository>
+ <id>jsonrpc3j-webdav-maven-repo</id>
+ <name>jsonrpc4j maven repository</name>
+ <url>http://jsonrpc4j.googlecode.com/svn/maven/repo/</url>
+ <layout>default</layout>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.googlecode</groupId>
+ <artifactId>jsonrpc4j</artifactId>
+ <version>0.18</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.codehaus.jackson</groupId>
+ <artifactId>jackson-mapper-asl</artifactId>
+ <version>1.7.5</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.10</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>1.2.16</version>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.5</version>
+ </dependency>
+
+ <dependency>
+ <groupId>javax.portlet</groupId>
+ <artifactId>portlet-api</artifactId>
+ <version>2.0</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-servlet</artifactId>
+ <version>8.0.0.M3</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-server</artifactId>
+ <version>8.0.0.M3</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
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
diff --git a/jsonrpc/target/classes/log4j.properties b/jsonrpc/target/classes/log4j.properties
new file mode 100644
index 0000000..6ca6c6f
--- /dev/null
+++ b/jsonrpc/target/classes/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