This commit is contained in:
crusader 2018-04-22 19:55:13 +09:00
commit 58d6e8fe4a
19 changed files with 475 additions and 0 deletions

88
.gitignore vendored Normal file
View File

@ -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

27
.vscode/launch.json vendored Normal file
View File

@ -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
}
]
}

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "automatic"
}

33
pom.xml Normal file
View File

@ -0,0 +1,33 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.loafle</groupId>
<artifactId>maven_parent_jar</artifactId>
<version>1.0.0-RELEASE</version>
</parent>
<groupId>com.loafle.commons</groupId>
<artifactId>rpc-java</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>com.loafle.commons.rpc-java</name>
<properties>
<gson.version>2.8.2</gson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
<build>
</build>
</project>

View File

@ -0,0 +1,8 @@
package com.loafle.commons.rpc;
/**
* RPCException
*/
public class RPCException extends Exception {
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -0,0 +1,8 @@
package com.loafle.commons.rpc.protocol.json;
/**
* JSONRPC
*/
public class JSONRPC {
public static final String version = "2.0";
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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<String> params;
@SerializedName("id")
public Object id;
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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<String, Object> receivers) throws RPCException;
}

0
src/main/resources/_ Normal file
View File

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="3 seconds">
<contextName>commons_java</contextName>
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
<logger name="com.loafle.overflow" level="ALL" />
</configuration>