-
Notifications
You must be signed in to change notification settings - Fork 4
First cut at phone to robot RPC #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pmalmsten
wants to merge
14
commits into
trc492:master
Choose a base branch
from
DrDab:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4786f29
Added code in preparation of RPC setup.
DrDab 18b22e0
Fixed formatting on a lambda expression.
DrDab f878924
Make sure networking code actually sends string
DrDab 480f0a6
Added JSON-RPC libraries.
DrDab a9c3399
Implement Transport class with AwooCommunicator
DrDab 0a1b68c
Finished test class for RPC
DrDab 1a8eb36
Added annotations to test RPC class
DrDab 894a23d
Update jackson version
DrDab 156c1eb
Implemented RPC service and class CORRECTLY
DrDab 443c2d2
Set method correctly
DrDab a35b43a
Disabled risky commands
DrDab bec74ac
Made RPC fully functional
DrDab 832d63c
Updated all required libraries.
DrDab 30faa69
Removed machine-specific classpaths.
DrDab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
152 changes: 152 additions & 0 deletions
152
src/com/github/arteam/simplejsonrpc/client/JsonRpcClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package com.github.arteam.simplejsonrpc.client; | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.github.arteam.simplejsonrpc.client.builder.BatchRequestBuilder; | ||
| import com.github.arteam.simplejsonrpc.client.builder.NotificationRequestBuilder; | ||
| import com.github.arteam.simplejsonrpc.client.builder.ObjectApiBuilder; | ||
| import com.github.arteam.simplejsonrpc.client.builder.RequestBuilder; | ||
| import com.github.arteam.simplejsonrpc.client.generator.IdGenerator; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.lang.reflect.Proxy; | ||
|
|
||
| /** | ||
| * Date: 8/9/14 | ||
| * Time: 8:58 PM | ||
| * <p/> | ||
| * JSON-RPC client. Represents a factory for a fluent client API {@link com.github.arteam.simplejsonrpc.client.builder.RequestBuilder}. | ||
| * It's parametrized by {@link Transport} and Jackson {@link ObjectMapper} | ||
| * | ||
| * @author Artem Prigoda | ||
| */ | ||
| public class JsonRpcClient { | ||
|
|
||
| /** | ||
| * Transport for performing JSON-RPC requests and returning responses | ||
| */ | ||
| @NotNull | ||
| private Transport transport; | ||
|
|
||
| /** | ||
| * JSON mapper for conversion between JSON and Java types | ||
| */ | ||
| @NotNull | ||
| private ObjectMapper mapper; | ||
|
|
||
| /** | ||
| * Constructs a new JSON-RPC client with a specified transport | ||
| * | ||
| * @param transport transport implementation | ||
| */ | ||
| public JsonRpcClient(@NotNull Transport transport) { | ||
| this(transport, new ObjectMapper()); | ||
| } | ||
|
|
||
| /** | ||
| * Constructs a new JSON-RPC client with a specified transport and user-definder JSON mapper | ||
| * | ||
| * @param transport transport implementation | ||
| * @param mapper JSON mapper | ||
| */ | ||
| public JsonRpcClient(@NotNull Transport transport, @NotNull ObjectMapper mapper) { | ||
| this.transport = transport; | ||
| this.mapper = mapper; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a builder of a JSON-RPC request in initial state | ||
| * | ||
| * @return request builder | ||
| */ | ||
| @NotNull | ||
| public RequestBuilder<Object> createRequest() { | ||
| return new RequestBuilder<Object>(transport, mapper); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a builder of a JSON-RPC notification request in initial state | ||
| * | ||
| * @return notification request builder | ||
| */ | ||
| @NotNull | ||
| public NotificationRequestBuilder createNotification() { | ||
| return new NotificationRequestBuilder(transport, mapper); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a builder of a JSON-RPC batch request in initial state | ||
| * | ||
| * @return batch request builder | ||
| */ | ||
| @NotNull | ||
| public BatchRequestBuilder<?, ?> createBatchRequest() { | ||
| return new BatchRequestBuilder<Object, Object>(transport, mapper); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new proxy for accessing a remote JSON-RPC service through an interface | ||
| * | ||
| * @param clazz interface metadata | ||
| * @param <T> interface type | ||
| * @return a new proxy | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| @NotNull | ||
| public <T> T onDemand(@NotNull Class<T> clazz) { | ||
| return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz}, | ||
| new ObjectApiBuilder(clazz, transport, mapper, null, null)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new proxy for accessing a remote JSON-RPC service through an interface | ||
| * with a custom id generator that overrides the interface generator. | ||
| * | ||
| * @param clazz interface metadata | ||
| * @param idGenerator custom id generator | ||
| * @param <T> interface type | ||
| * @return a new proxy | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| @NotNull | ||
| public <T> T onDemand(@NotNull Class<T> clazz, @NotNull IdGenerator<?> idGenerator) { | ||
| return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz}, | ||
| new ObjectApiBuilder(clazz, transport, mapper, null, idGenerator)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new proxy for accessing a remote JSON-RPC service through an interface | ||
| * with a custom type of request params. | ||
| * It applies for all methods and overrides interface and method level settings. | ||
| * | ||
| * @param clazz interface metadata | ||
| * @param paramsType custom type of request params | ||
| * @param <T> interface type | ||
| * @return a new proxy | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| @NotNull | ||
| public <T> T onDemand(@NotNull Class<T> clazz, @NotNull ParamsType paramsType) { | ||
| return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz}, | ||
| new ObjectApiBuilder(clazz, transport, mapper, paramsType, null)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new proxy for accessing a remote JSON-RPC service through an interface | ||
| * with a custom id generator and custom type of request params. | ||
| * The generator overrides the interface generator. | ||
| * The type applies for all methods and overrides interface and method level settings. | ||
| * | ||
| * @param clazz interface metadata | ||
| * @param idGenerator custom id generator | ||
| * @param paramsType custom type of request params | ||
| * @param <T> interface type | ||
| * @return a new proxy | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| @NotNull | ||
| public <T> T onDemand(Class<T> clazz, @NotNull ParamsType paramsType, @NotNull IdGenerator<?> idGenerator) { | ||
| return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{clazz}, | ||
| new ObjectApiBuilder(clazz, transport, mapper, paramsType, idGenerator)); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.github.arteam.simplejsonrpc.client; | ||
|
|
||
| import com.github.arteam.simplejsonrpc.client.generator.IdGenerator; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Date: 24.08.14 | ||
| * Time: 18:14 | ||
| * | ||
| * @author Artem Prigoda | ||
| */ | ||
| @Target(ElementType.TYPE) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| public @interface JsonRpcId { | ||
|
|
||
| Class<? extends IdGenerator<?>> value(); | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/com/github/arteam/simplejsonrpc/client/JsonRpcParams.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.github.arteam.simplejsonrpc.client; | ||
|
|
||
| import java.lang.annotation.*; | ||
|
|
||
| /** | ||
| * Date: 11/4/14 | ||
| * Time: 10:45 PM | ||
| * | ||
| * @author Artem Prigoda | ||
| */ | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.TYPE, ElementType.METHOD}) | ||
| @Documented | ||
| public @interface JsonRpcParams { | ||
|
|
||
| ParamsType value() default ParamsType.MAP; | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/com/github/arteam/simplejsonrpc/client/ParamsType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.github.arteam.simplejsonrpc.client; | ||
|
|
||
| /** | ||
| * Date: 11/4/14 | ||
| * Time: 10:14 PM | ||
| * Style of JSON-RPC parameters representation (map or array) | ||
| * | ||
| * @author Artem Prigoda | ||
| */ | ||
| public enum ParamsType { | ||
| MAP, ARRAY | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.github.arteam.simplejsonrpc.client; | ||
|
|
||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Date: 8/9/14 | ||
| * Time: 8:52 PM | ||
| * <p/> | ||
| * Abstract transport for JSON-RPC communication | ||
| * | ||
| * @author Artem Prigoda | ||
| */ | ||
| public interface Transport { | ||
|
|
||
| /** | ||
| * Passes a JSON-RPC request in a text form to a backend and | ||
| * returns a JSON-RPC response in a text form as well | ||
| * | ||
| * @param request JSON-RPC request as a string | ||
| * @return JSON-RPC response as a string | ||
| */ | ||
| @NotNull | ||
| public String pass(@NotNull String request) throws IOException; | ||
| } |
103 changes: 103 additions & 0 deletions
103
src/com/github/arteam/simplejsonrpc/client/builder/AbstractBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package com.github.arteam.simplejsonrpc.client.builder; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| import com.fasterxml.jackson.databind.node.ObjectNode; | ||
| import com.fasterxml.jackson.databind.node.ValueNode; | ||
| import com.github.arteam.simplejsonrpc.client.Transport; | ||
| import org.jetbrains.annotations.NotNull; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Date: 10/12/14 | ||
| * Time: 6:48 PM | ||
| * Abstract builder for JSON-RPC requests | ||
| * | ||
| * @author Artem Prigoda | ||
| */ | ||
| public class AbstractBuilder { | ||
|
|
||
| // Protocol constants | ||
| protected static final String VERSION_2_0 = "2.0"; | ||
| protected static final String RESULT = "result"; | ||
| protected static final String ERROR = "error"; | ||
| protected static final String JSONRPC = "jsonrpc"; | ||
| protected static final String ID = "id"; | ||
| protected static final String METHOD = "method"; | ||
| protected static final String PARAMS = "params"; | ||
|
|
||
| /** | ||
| * Transport for performing a text request and returning a text response | ||
| */ | ||
| @NotNull | ||
| protected final Transport transport; | ||
|
|
||
| /** | ||
| * Jackson mapper for JSON processing | ||
| */ | ||
| @NotNull | ||
| protected final ObjectMapper mapper; | ||
|
|
||
| public AbstractBuilder(@NotNull Transport transport, @NotNull ObjectMapper mapper) { | ||
| this.transport = transport; | ||
| this.mapper = mapper; | ||
| } | ||
|
|
||
| /** | ||
| * Builds request params as a JSON array | ||
| * | ||
| * @param values request params | ||
| * @return a new JSON array | ||
| */ | ||
| @NotNull | ||
| protected ArrayNode arrayParams(@NotNull Object[] values) { | ||
| ArrayNode newArrayParams = mapper.createArrayNode(); | ||
| for (Object value : values) { | ||
| newArrayParams.add(mapper.valueToTree(value)); | ||
| } | ||
| return newArrayParams; | ||
| } | ||
|
|
||
| /** | ||
| * Builds request params as a JSON object | ||
| * | ||
| * @param params request params | ||
| * @return a new JSON object | ||
| */ | ||
| @NotNull | ||
| protected ObjectNode objectParams(@NotNull Map<String, ?> params) { | ||
| ObjectNode objectNode = mapper.createObjectNode(); | ||
| for (String key : params.keySet()) { | ||
| objectNode.set(key, mapper.valueToTree(params.get(key))); | ||
| } | ||
| return objectNode; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new JSON-RPC request as a JSON object | ||
| * | ||
| * @param id request id | ||
| * @param method request method | ||
| * @param params request params | ||
| * @return a new request as a JSON object | ||
| */ | ||
| @NotNull | ||
| protected ObjectNode request(@NotNull ValueNode id, @NotNull String method, | ||
| @NotNull JsonNode params) { | ||
| if (method.isEmpty()) { | ||
| throw new IllegalArgumentException("Method is not set"); | ||
| } | ||
| ObjectNode requestNode = mapper.createObjectNode(); | ||
| requestNode.put(JSONRPC, VERSION_2_0); | ||
| requestNode.put(METHOD, method); | ||
| requestNode.set(PARAMS, params); | ||
| if (!id.isNull()) { | ||
| requestNode.set(ID, id); | ||
| } | ||
| return requestNode; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to pull in library code from other teams as jar files instead of as raw source. That would help keep our source tree more minimal, make it easier to navigate, and also make it easier to upgrade library versions over time.
Usually we only import raw source for libraries if we need to alter the library ourselves for some reason and can't or don't want to maintain our own builds of it.