From 58d6e8fe4aa83d56607fbf8fc0e36bdc2f61247d Mon Sep 17 00:00:00 2001 From: crusader Date: Sun, 22 Apr 2018 19:55:13 +0900 Subject: [PATCH] ing --- .gitignore | 88 +++++++++++++++++++ .vscode/launch.json | 27 ++++++ .vscode/settings.json | 3 + pom.xml | 33 +++++++ .../com/loafle/commons/rpc/RPCException.java | 8 ++ .../loafle/commons/rpc/protocol/RPCError.java | 44 ++++++++++ .../rpc/protocol/RPCRegistryCodec.java | 13 +++ .../commons/rpc/protocol/RPCServerCodec.java | 11 +++ .../rpc/protocol/RPCServerRequestCodec.java | 11 +++ .../commons/rpc/protocol/json/JSONRPC.java | 8 ++ .../rpc/protocol/json/JSONRPCServerCodec.java | 40 +++++++++ .../json/JSONRPCServerRequestCodec.java | 71 +++++++++++++++ .../protocol/json/JSONServerNotification.java | 19 ++++ .../rpc/protocol/json/JSONServerRequest.java | 23 +++++ .../rpc/protocol/json/JSONServerResponse.java | 32 +++++++ .../commons/rpc/registry/RPCInvoker.java | 12 +++ .../commons/rpc/registry/RPCRegistry.java | 15 ++++ src/main/resources/_ | 0 src/test/resources/logback.xml | 17 ++++ 19 files changed, 475 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 pom.xml create mode 100644 src/main/java/com/loafle/commons/rpc/RPCException.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/RPCError.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/RPCRegistryCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/RPCServerCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPC.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java create mode 100644 src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java create mode 100644 src/main/java/com/loafle/commons/rpc/registry/RPCInvoker.java create mode 100644 src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java create mode 100644 src/main/resources/_ create mode 100644 src/test/resources/logback.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..60af490 --- /dev/null +++ b/.gitignore @@ -0,0 +1,88 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Maven template + +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties + +# Avoid ignoring Maven wrapper jar file (.jar files are usually ignored) +!/.mvn/wrapper/maven-wrapper.jar +### Java template +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt +14 +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +.idea/ +*.iml +/target/ +.settings/ +.classpath +.project diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..81d81a5 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "type": "java", + "name": "Debug", + "request": "launch", + "cwd": "${workspaceFolder}", + "console": "internalConsole", + "stopOnEntry": false, + "mainClass": "com.loafle.overflow.central.Central", + "projectName": "central", + "args": "50006" + }, + { + "type": "java", + "name": "Debug (Attach)", + "request": "attach", + "hostName": "localhost", + "port": 0 + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1133129 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic" +} \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..aef29fb --- /dev/null +++ b/pom.xml @@ -0,0 +1,33 @@ + + + 4.0.0 + + + com.loafle + maven_parent_jar + 1.0.0-RELEASE + + + com.loafle.commons + rpc-java + jar + 1.0.0-SNAPSHOT + com.loafle.commons.rpc-java + + + 2.8.2 + + + + + com.google.code.gson + gson + ${gson.version} + + + + + + \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/RPCException.java b/src/main/java/com/loafle/commons/rpc/RPCException.java new file mode 100644 index 0000000..5e83be5 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/RPCException.java @@ -0,0 +1,8 @@ +package com.loafle.commons.rpc; + +/** + * RPCException + */ +public class RPCException extends Exception { + +} diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCError.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCError.java new file mode 100644 index 0000000..f8b7740 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCError.java @@ -0,0 +1,44 @@ +package com.loafle.commons.rpc.protocol; + +/** + * Error + */ +public class RPCError { + public final int code; + public final String message; + public final Object data; + + /** + * Creates the error. + * + * @param code the code + * @param message the message + * @param data the data + */ + public RPCError(int code, String message, Object data) { + this.code = code; + this.message = message; + this.data = data; + } + + /** + * @return the code + */ + int getCode() { + return code; + } + + /** + * @return the message + */ + String getMessage() { + return message; + } + + /** + * @return the data + */ + Object getData() { + return data; +} +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCRegistryCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCRegistryCodec.java new file mode 100644 index 0000000..4c90d7a --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCRegistryCodec.java @@ -0,0 +1,13 @@ +package com.loafle.commons.rpc.protocol; + +import java.lang.reflect.Type; + +import com.loafle.commons.rpc.RPCException; + +/** + * RegistryCodec + */ +public interface RPCRegistryCodec { + String method(); + Object[] params(Type[] paramTypes) throws RPCException; +} diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCServerCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCServerCodec.java new file mode 100644 index 0000000..9e51c13 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCServerCodec.java @@ -0,0 +1,11 @@ +package com.loafle.commons.rpc.protocol; + +import com.loafle.commons.rpc.RPCException; + +/** + * ServerCodec + */ +public interface RPCServerCodec { + RPCServerRequestCodec request(byte[] buf) throws RPCException; + byte[] notification(String method, Object... params) throws RPCException; +} diff --git a/src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java new file mode 100644 index 0000000..2d83db5 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/RPCServerRequestCodec.java @@ -0,0 +1,11 @@ +package com.loafle.commons.rpc.protocol; + +import com.loafle.commons.rpc.RPCException; + +/** + * ServerRequestCodec + */ +public interface RPCServerRequestCodec extends RPCRegistryCodec { + boolean hasResponse(); + byte[] response(Object reply, Error error) throws RPCException; +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPC.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPC.java new file mode 100644 index 0000000..2589af2 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPC.java @@ -0,0 +1,8 @@ +package com.loafle.commons.rpc.protocol.json; + +/** + * JSONRPC + */ +public class JSONRPC { + public static final String version = "2.0"; +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java new file mode 100644 index 0000000..4e50637 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerCodec.java @@ -0,0 +1,40 @@ +package com.loafle.commons.rpc.protocol.json; + +import com.google.gson.Gson; +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCServerCodec; +import com.loafle.commons.rpc.protocol.RPCServerRequestCodec; + +/** + * JSONRPCServerCodec + */ +public class JSONRPCServerCodec implements RPCServerCodec { + private Gson gson; + + public JSONRPCServerCodec() { + this.gson = new Gson(); + } + public JSONRPCServerCodec(Gson gson) { + this.gson = gson; + } + + public void setGson(Gson gson) { + this.gson = gson; + } + + public Gson getGson() { + return this.gson; + } + + public RPCServerRequestCodec request(byte[] buff) throws RPCException { + return new JSONRPCServerRequestCodec(this.gson, buff); + } + + public byte[] notification(String method, Object... params) throws RPCException { + JSONServerNotification notification = new JSONServerNotification(method, params); + JSONServerResponse response = new JSONServerResponse(JSONRPC.version, notification, null); + + String json = this.gson.toJson(response); + return json.getBytes(); + } +} diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java new file mode 100644 index 0000000..71cf544 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONRPCServerRequestCodec.java @@ -0,0 +1,71 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.lang.reflect.Type; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCError; +import com.loafle.commons.rpc.protocol.RPCServerRequestCodec; + +/** + * JSONRPCServerRequestCodec + */ +class JSONRPCServerRequestCodec implements RPCServerRequestCodec { + private Gson gson; + private JSONServerRequest request; + + public JSONRPCServerRequestCodec(Gson gson, byte[] buff) throws RPCException { + this.gson = gson; + try { + this.request = this.gson.fromJson(new String(buff), JSONServerRequest.class); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + } + } + + public String method() { + return this.request.method; + } + + public Object[] params(Type[] paramTypes) throws RPCException { + if (null == paramTypes && null == this.request.params) { + return null; + } + + if ((null != paramTypes && null == this.request.params) || null == paramTypes && null != this.request.params) { + throw new RPCException(); + } + + if (paramTypes.length != this.request.params.size()) { + throw new RPCException(); + } + + Object[] result = new Object[paramTypes.length]; + + for (int i = 0; i < paramTypes.length; i++) { + Type paramType = paramTypes[i]; + String param = this.request.params.get(i); + + result[i] = this.gson.fromJson(param, paramType); + } + + return result; + } + + public boolean hasResponse() { + return null != this.request.id; + } + + public byte[] response(Object reply, Error error) throws RPCException { + RPCError rpcError = null; + if (null != error) { + rpcError = new RPCError(1, "", error); + } + JSONServerResponse response = new JSONServerResponse(JSONRPC.version, reply, rpcError); + response.id = this.request.id; + + String json = this.gson.toJson(response); + return json.getBytes(); + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java new file mode 100644 index 0000000..7015702 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerNotification.java @@ -0,0 +1,19 @@ +package com.loafle.commons.rpc.protocol.json; + +import com.google.gson.annotations.SerializedName; + +/** + * JSONServerNotification + */ +class JSONServerNotification { + @SerializedName("method") + public String method; + + @SerializedName("params") + public Object[] params; + + JSONServerNotification(String method, Object[] params) { + this.method = method; + this.params = params; + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java new file mode 100644 index 0000000..bbc18de --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerRequest.java @@ -0,0 +1,23 @@ +package com.loafle.commons.rpc.protocol.json; + +import java.util.List; + +import com.google.gson.annotations.SerializedName; + +/** + * JSONServerRequest + */ +class JSONServerRequest { + @SerializedName("jsonrpc") + public String version; + + @SerializedName("method") + public String method; + + @SerializedName("params") + public List params; + + @SerializedName("id") + public Object id; + +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java new file mode 100644 index 0000000..ffc76e0 --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/protocol/json/JSONServerResponse.java @@ -0,0 +1,32 @@ +package com.loafle.commons.rpc.protocol.json; + +import com.google.gson.annotations.SerializedName; +import com.loafle.commons.rpc.protocol.RPCError; + +/** + * JSONServerResponse + */ +class JSONServerResponse { + @SerializedName("jsonrpc") + public String version; + + @SerializedName("result") + public Object result; + + @SerializedName("error") + public RPCError error; + + @SerializedName("id") + public Object id; + + JSONServerResponse(String version, Object result, RPCError error) { + this(version, result, error, null); + } + + JSONServerResponse(String version, Object result, RPCError error, Object id) { + this.version = version; + this.result = result; + this.error = error; + this.id = id; + } +} \ No newline at end of file diff --git a/src/main/java/com/loafle/commons/rpc/registry/RPCInvoker.java b/src/main/java/com/loafle/commons/rpc/registry/RPCInvoker.java new file mode 100644 index 0000000..b719ffb --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/registry/RPCInvoker.java @@ -0,0 +1,12 @@ +package com.loafle.commons.rpc.registry; + +import com.loafle.commons.rpc.RPCException; +import com.loafle.commons.rpc.protocol.RPCRegistryCodec; + +/** + * RPCInvoker + */ +public interface RPCInvoker { + boolean hasMethod(String method); + Object invoke(RPCRegistryCodec codec, Object... leadingParams) throws RPCException; +} diff --git a/src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java b/src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java new file mode 100644 index 0000000..3bc52ee --- /dev/null +++ b/src/main/java/com/loafle/commons/rpc/registry/RPCRegistry.java @@ -0,0 +1,15 @@ +package com.loafle.commons.rpc.registry; + +import java.util.Map; + +import com.loafle.commons.rpc.RPCException; + +/** + * RPCRegistry + */ +public interface RPCRegistry extends RPCInvoker { + Object getService(String name); + void registerService(Object receiver, String name) throws RPCException; + void registerServices(Object... receivers) throws RPCException; + void registerServices(Map receivers) throws RPCException; +} \ No newline at end of file diff --git a/src/main/resources/_ b/src/main/resources/_ new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml new file mode 100644 index 0000000..575281e --- /dev/null +++ b/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + commons_java + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n + + + + + + + + + \ No newline at end of file