Add SimpleRPC
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user