Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
<classpathentry kind="var" path="USERLIBS_DIR/CTRE_Phoenix-sources.jar"/>
<classpathentry kind="var" path="USERLIBS_DIR/CTRE_Phoenix.jar" sourcepath="/CTRE_Phoenix.sources"/>
<classpathentry kind="var" path="USERLIBS_DIR/navx_frc.jar"/>
<classpathentry kind="var" path="USERLIBS_DIR/jackson-databind-2.9.6.jar"/>
<classpathentry kind="var" path="USERLIBS_DIR/jackson-annotations-2.9.6.jar"/>
<classpathentry kind="var" path="USERLIBS_DIR/annotations-12.0.jar"/>
<classpathentry kind="var" path="USERLIBS_DIR/jackson-core-2.9.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<!-- <property name="version" value=""/> -->

<!-- By default the system team number is used -->
<!-- <property name="team-number" value=""/> -->
<property name="team-number" value="492"/>

<!-- By default the target is set to 10.TE.AM.2 -->
<!-- <property name="target" value=""/> -->
Expand Down
Binary file added required_libraries/CTRE_Phoenix-sources.jar
Binary file not shown.
Binary file added required_libraries/CTRE_Phoenix.jar
Binary file not shown.
Binary file added required_libraries/annotations-12.0.jar
Binary file not shown.
Binary file added required_libraries/jackson-annotations-2.9.6.jar
Binary file not shown.
Binary file added required_libraries/jackson-core-2.9.6.jar
Binary file not shown.
Binary file added required_libraries/jackson-databind-2.9.6.jar
Binary file not shown.
Binary file added required_libraries/libCTRE_PhoenixCCI.so
Binary file not shown.
Binary file added required_libraries/navx_frc.jar
Binary file not shown.
152 changes: 152 additions & 0 deletions src/com/github/arteam/simplejsonrpc/client/JsonRpcClient.java
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 {

Copy link
Copy Markdown
Collaborator Author

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.


/**
* 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));
}

}
21 changes: 21 additions & 0 deletions src/com/github/arteam/simplejsonrpc/client/JsonRpcId.java
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 src/com/github/arteam/simplejsonrpc/client/JsonRpcParams.java
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 src/com/github/arteam/simplejsonrpc/client/ParamsType.java
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
}
26 changes: 26 additions & 0 deletions src/com/github/arteam/simplejsonrpc/client/Transport.java
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;
}
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;
}
}
Loading