Skip to content

Commit

Permalink
JSON-RPC Requests: add timeout parameter (#1412)
Browse files Browse the repository at this point in the history
This non-standard extension of the JSON-RPC protocol adds a "timeout" parameter in seconds. This is the maximum time that we wait for a JSON-RPC Response. Timeout is defined as an `Optional`:

- timeout > 0: use the given timeout in seconds
- timeout <= 0: apply no timeout, i.e. wait forever
- no timeout: use default timeout of 60 seconds
  • Loading branch information
sfeilmeier authored Mar 11, 2021
1 parent 61f6132 commit 75ee849
Show file tree
Hide file tree
Showing 38 changed files with 935 additions and 326 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.openems.backend.b2bwebsocket.jsonrpc.request;

import java.util.TreeSet;
import java.util.UUID;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -36,7 +35,7 @@ public class SubscribeEdgesChannelsRequest extends JsonrpcRequest {
public static SubscribeEdgesChannelsRequest from(JsonrpcRequest r) throws OpenemsNamedException {
JsonObject p = r.getParams();
int count = JsonUtils.getAsInt(p, "count");
SubscribeEdgesChannelsRequest result = new SubscribeEdgesChannelsRequest(r.getId(), count);
SubscribeEdgesChannelsRequest result = new SubscribeEdgesChannelsRequest(r, count);
JsonArray edgeIds = JsonUtils.getAsJsonArray(p, "ids");
for (JsonElement edgeId : edgeIds) {
result.addEdgeId(JsonUtils.getAsString(edgeId));
Expand All @@ -57,13 +56,14 @@ public static SubscribeEdgesChannelsRequest from(JsonObject j) throws OpenemsNam
private final TreeSet<String> edgeIds = new TreeSet<>();
private final TreeSet<ChannelAddress> channels = new TreeSet<>();

public SubscribeEdgesChannelsRequest(UUID id, int count) {
super(id, METHOD);
private SubscribeEdgesChannelsRequest(JsonrpcRequest request, int count) {
super(request, METHOD);
this.count = count;
}

public SubscribeEdgesChannelsRequest(int count) {
this(UUID.randomUUID(), count);
super(METHOD);
this.count = count;
}

public void addEdgeId(String edgeId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package io.openems.backend.common.jsonrpc.request;

import java.util.TreeSet;
import java.util.UUID;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.jsonrpc.base.GenericJsonrpcRequest;
import io.openems.common.jsonrpc.base.JsonrpcRequest;
import io.openems.common.types.ChannelAddress;
import io.openems.common.utils.JsonUtils;
Expand All @@ -30,9 +28,19 @@
*/
public class GetEdgesChannelsValuesRequest extends JsonrpcRequest {

public static final String METHOD = "getEdgesChannelsValues";

/**
* Create {@link GetEdgesChannelsValuesRequest} from a template
* {@link JsonrpcRequest}.
*
* @param r the template {@link JsonrpcRequest}
* @return the {@link GetEdgesChannelsValuesRequest}
* @throws OpenemsNamedException on parse error
*/
public static GetEdgesChannelsValuesRequest from(JsonrpcRequest r) throws OpenemsNamedException {
JsonObject p = r.getParams();
GetEdgesChannelsValuesRequest result = new GetEdgesChannelsValuesRequest(r.getId());
GetEdgesChannelsValuesRequest result = new GetEdgesChannelsValuesRequest(r);
JsonArray edgeIds = JsonUtils.getAsJsonArray(p, "ids");
for (JsonElement edgeId : edgeIds) {
result.addEdgeId(JsonUtils.getAsString(edgeId));
Expand All @@ -45,37 +53,51 @@ public static GetEdgesChannelsValuesRequest from(JsonrpcRequest r) throws Openem
return result;
}

public static GetEdgesChannelsValuesRequest from(JsonObject j) throws OpenemsNamedException {
return from(GenericJsonrpcRequest.from(j));
}

public static final String METHOD = "getEdgesChannelsValues";

private final TreeSet<String> edgeIds = new TreeSet<>();
private final TreeSet<ChannelAddress> channels = new TreeSet<>();

public GetEdgesChannelsValuesRequest() {
this(UUID.randomUUID());
super(METHOD);
}

public GetEdgesChannelsValuesRequest(UUID id) {
super(id, METHOD);
private GetEdgesChannelsValuesRequest(JsonrpcRequest request) {
super(request, METHOD);
}

/**
* Adds a Edge-ID.
*
* @param edgeId the Edge-ID
*/
public void addEdgeId(String edgeId) {
this.edgeIds.add(edgeId);
}

/**
* Gets the Edge-IDs.
*
* @return set of Edge-IDs.
*/
public TreeSet<String> getEdgeIds() {
return edgeIds;
return this.edgeIds;
}

/**
* Adds a {@link ChannelAddress}.
*
* @param address the {@link ChannelAddress}
*/
public void addChannel(ChannelAddress address) {
this.channels.add(address);
}

/**
* Gets the {@link ChannelAddress}es.
*
* @return the {@link ChannelAddress}es
*/
public TreeSet<ChannelAddress> getChannels() {
return channels;
return this.channels;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.openems.backend.common.jsonrpc.request;

import java.util.UUID;

import com.google.gson.JsonObject;

import io.openems.common.exceptions.OpenemsError.OpenemsNamedException;
import io.openems.common.exceptions.OpenemsException;
import io.openems.common.jsonrpc.base.JsonrpcRequest;

Expand All @@ -21,18 +20,25 @@
*/
public class GetEdgesStatusRequest extends JsonrpcRequest {

public static final String METHOD = "getEdgesStatus";

/**
* Create {@link GetEdgesStatusRequest} from a template {@link JsonrpcRequest}.
*
* @param r the template {@link JsonrpcRequest}
* @return the {@link GetEdgesStatusRequest}
* @throws OpenemsNamedException on parse error
*/
public static GetEdgesStatusRequest from(JsonrpcRequest r) throws OpenemsException {
return new GetEdgesStatusRequest(r.getId());
return new GetEdgesStatusRequest(r);
}

public static final String METHOD = "getEdgesStatus";

public GetEdgesStatusRequest() {
this(UUID.randomUUID());
super(METHOD);
}

public GetEdgesStatusRequest(UUID id) {
super(id, METHOD);
private GetEdgesStatusRequest(JsonrpcRequest request) {
super(request, METHOD);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.openems.backend.metadata.odoo.odoo.jsonrpc;

import java.util.UUID;

import io.openems.common.jsonrpc.base.JsonrpcRequest;

/**
Expand All @@ -20,12 +18,8 @@ public abstract class OdooCallRequest extends JsonrpcRequest {

public static final String METHOD = "call";

public OdooCallRequest(UUID id) {
super(id, METHOD);
}

public OdooCallRequest() {
this(UUID.randomUUID());
protected OdooCallRequest() {
super(METHOD);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.openems.common.jsonrpc.base;

import java.util.Optional;
import java.util.UUID;

import com.google.gson.JsonObject;
Expand All @@ -15,7 +16,8 @@
* "jsonrpc": "2.0",
* "id": "UUID",
* "method": string,
* "params": {}
* "params": {},
* "timeout"?: number, defaults to 60 seconds; negative or zero to disable timeout
* }
* </pre>
*
Expand All @@ -27,7 +29,7 @@ public class GenericJsonrpcRequest extends JsonrpcRequest {
/**
* Parses the String to a {@link GenericJsonrpcRequest}.
*
* @param j the String
* @param json the String
* @return the {@link GenericJsonrpcRequest}
* @throws OpenemsNamedException on error
*/
Expand All @@ -36,29 +38,30 @@ public static GenericJsonrpcRequest from(String json) throws OpenemsNamedExcepti
}

/**
* Parses the String to a {@link GenericJsonrpcRequest}. If the request UUID is
* missing, it is replaced by a random UUID.
* Parses the {@link JsonObject} to a {@link GenericJsonrpcRequest}.
*
* @param j the String
* @param j the {@link JsonObject}
* @return the {@link GenericJsonrpcRequest}
* @throws OpenemsNamedException on error
*/
public static GenericJsonrpcRequest fromIgnoreId(String json) throws OpenemsNamedException {
return fromIgnoreId(JsonUtils.parseToJsonObject(json));
public static GenericJsonrpcRequest from(JsonObject j) throws OpenemsNamedException {
UUID id = JsonUtils.getAsUUID(j, "id");
String method = JsonUtils.getAsString(j, "method");
JsonObject params = JsonUtils.getAsJsonObject(j, "params");
Optional<Integer> timeout = JsonUtils.getAsOptionalInt(j, "timeout");
return new GenericJsonrpcRequest(id, method, params, timeout);
}

/**
* Parses the {@link JsonObject} to a {@link GenericJsonrpcRequest}.
* Parses the String to a {@link GenericJsonrpcRequest}. If the request UUID is
* missing, it is replaced by a random UUID.
*
* @param j the {@link JsonObject}
* @param json the String
* @return the {@link GenericJsonrpcRequest}
* @throws OpenemsNamedException on error
*/
public static GenericJsonrpcRequest from(JsonObject j) throws OpenemsNamedException {
UUID id = JsonUtils.getAsUUID(j, "id");
String method = JsonUtils.getAsString(j, "method");
JsonObject params = JsonUtils.getAsJsonObject(j, "params");
return new GenericJsonrpcRequest(id, method, params);
public static GenericJsonrpcRequest fromIgnoreId(String json) throws OpenemsNamedException {
return fromIgnoreId(JsonUtils.parseToJsonObject(json));
}

/**
Expand All @@ -73,13 +76,19 @@ public static GenericJsonrpcRequest fromIgnoreId(JsonObject j) throws OpenemsNam
UUID id = JsonUtils.getAsOptionalUUID(j, "id").orElse(new UUID(0L, 0L) /* dummy UUID */);
String method = JsonUtils.getAsString(j, "method");
JsonObject params = JsonUtils.getAsJsonObject(j, "params");
return new GenericJsonrpcRequest(id, method, params);
Optional<Integer> timeout = JsonUtils.getAsOptionalInt(j, "timeout");
return new GenericJsonrpcRequest(id, method, params, timeout);
}

private final JsonObject params;

public GenericJsonrpcRequest(UUID id, String method, JsonObject params) {
super(id, method);
public GenericJsonrpcRequest(UUID id, String method, JsonObject params, int timeout) {
super(id, method, timeout);
this.params = params;
}

public GenericJsonrpcRequest(UUID id, String method, JsonObject params, Optional<Integer> timeout) {
super(id, method, timeout);
this.params = params;
}

Expand Down
Loading

0 comments on commit 75ee849

Please sign in to comment.