Add SimpleRPC

This commit is contained in:
Mark Vainomaa 2016-10-08 18:07:03 +03:00
parent 3747db125d
commit 0d6b255187
5 changed files with 163 additions and 0 deletions

38
SimpleRPC/pom.xml Normal file
View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>utils</artifactId>
<groupId>eu.mikroskeem</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>simplerpc</artifactId>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations-java5</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,36 @@
package eu.mikroskeem.utils.simplerpc;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lombok.Data;
import lombok.ToString;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.util.List;
@Data @ToString
public class RPCRequest implements Serializable {
private String id;
private String method;
private List<Object> arguments;
@Nullable public byte[] toByteArray() {
try {
return new Gson().toJson(this, RPCRequest.class).getBytes("UTF-8");
} catch (JsonSyntaxException|UnsupportedEncodingException e){
e.printStackTrace();
}
return null;
}
@Nullable public static RPCRequest fromByteArray(byte[] serialized){
try {
return new Gson().fromJson(new String(serialized, "UTF-8"), RPCRequest.class);
} catch (JsonSyntaxException|UnsupportedEncodingException e){
e.printStackTrace();
}
return null;
}
}

View File

@ -0,0 +1,44 @@
package eu.mikroskeem.utils.simplerpc;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import lombok.Data;
import lombok.ToString;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.util.List;
/**
* RPCResponse class
*/
@Data @ToString
public class RPCResponse implements Serializable {
private String id;
private Status status;
private String message;
private List<Object> response;
@Nullable public byte[] toByteArray() {
try {
return new Gson().toJson(this, RPCResponse.class).getBytes("UTF-8");
} catch (JsonSyntaxException |UnsupportedEncodingException e){
e.printStackTrace();
}
return null;
}
@Nullable public static RPCResponse fromByteArray(byte[] serialized){
try {
return new Gson().fromJson(new String(serialized, "UTF-8"), RPCResponse.class);
} catch (JsonSyntaxException|UnsupportedEncodingException e){
e.printStackTrace();
}
return null;
}
public enum Status {
SUCCESS,
ERROR
}
}

View File

@ -0,0 +1,44 @@
package eu.mikroskeem.utils.test.simplerpc;
import eu.mikroskeem.utils.simplerpc.RPCRequest;
import eu.mikroskeem.utils.simplerpc.RPCResponse;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.UUID;
public class TestRPC {
@Test public void testRPCRequestSerialize() throws Exception {
RPCRequest rpcRequest = new RPCRequest();
rpcRequest.setId(UUID.randomUUID().toString());
rpcRequest.setMethod("wut");
rpcRequest.setArguments(new ArrayList<Object>(){{
add("njeeger");
add(20D); // Apparently Gson always converts int/float to double when passed as Object
add("õäöu");
add("( ͡° ͜ʖ ͡°)");
}});
RPCRequest unserialized = RPCRequest.fromByteArray(
rpcRequest.toByteArray()
);
Assert.assertEquals(rpcRequest.toString(), unserialized.toString());
}
@Test public void testRPCResponseSerialize() throws Exception {
RPCResponse rpcResponse = new RPCResponse();
rpcResponse.setStatus(RPCResponse.Status.SUCCESS);
rpcResponse.setMessage("hey");
rpcResponse.setResponse(new ArrayList<Object>(){{
add("my name is jeff");
}});
RPCResponse unserialized = RPCResponse.fromByteArray(
rpcResponse.toByteArray()
);
Assert.assertEquals(rpcResponse.toString(), unserialized.toString());
}
}

View File

@ -16,6 +16,7 @@
<module>Itemutils</module>
<module>TextUtils</module>
<module>BukkitUtils</module>
<module>SimpleRPC</module>
</modules>
<!-- Properties -->