This repository has been archived by the owner on Apr 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a scuttlebutt-mux module for multiplexing RPC requests into futur…
…es and streams (#202) * Start adding code to multiplex RPC requests. * Fix typos. * Add helper classes. * Move mux module to the RPC module. * Add missing docs and handle an underlying connection being closed. * Fix mistakenly refactored gradle files. * Take concurrency into account. * Formatting * Remove redundant casting. * Fix formatting. * Handle stream end properly. * Marshall scuttlebutt error body. * Disable patchwork integration test by default. * Pass an ObjectMapper instance to functions which serialize and deserialize to allow for object mapper configuration.
- Loading branch information
Showing
20 changed files
with
1,045 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
58 changes: 58 additions & 0 deletions
58
scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/RPCAsyncRequest.java
This file contains 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,58 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package net.consensys.cava.scuttlebutt.rpc; | ||
|
||
import net.consensys.cava.bytes.Bytes; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class RPCAsyncRequest { | ||
|
||
|
||
private final RPCFunction function; | ||
private final List<Object> arguments; | ||
|
||
/** | ||
* | ||
* @param function the function to be in invoked. If the function is in a namespace, the first n-1 items in the array | ||
* are the namespace followed by the function name (e.g. 'blobs.get' becomes ['blobs', 'get']). | ||
* @param arguments The arguments passed to the function being invoked. Each item can be any arbitrary object which is | ||
* JSON serializable (e.g. String, Int, list, object.) | ||
* | ||
*/ | ||
public RPCAsyncRequest(RPCFunction function, List<Object> arguments) { | ||
this.function = function; | ||
this.arguments = arguments; | ||
} | ||
|
||
/** | ||
* Encode the RPC request as bytes. | ||
* | ||
* @param objectMapper the object mapper to serialize the request with | ||
* @return an RPC request serialized into bytes | ||
* @throws JsonProcessingException thrown if there is an error while serializing the request to bytes | ||
*/ | ||
public Bytes toEncodedRpcMessage(ObjectMapper objectMapper) throws JsonProcessingException { | ||
return RPCCodec.encodeRequest( | ||
new RPCRequestBody(function.asList(), RPCRequestType.ASYNC, arguments).asBytes(objectMapper), | ||
getRPCFlags()); | ||
} | ||
|
||
public RPCFlag[] getRPCFlags() { | ||
return new RPCFlag[] {RPCFlag.BodyType.JSON}; | ||
} | ||
|
||
} |
This file contains 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
64 changes: 64 additions & 0 deletions
64
scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/RPCErrorBody.java
This file contains 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,64 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package net.consensys.cava.scuttlebutt.rpc; | ||
|
||
/** | ||
* An RPC message response body which contains an error | ||
*/ | ||
public class RPCErrorBody { | ||
|
||
private String name; | ||
private String message; | ||
private String stack; | ||
|
||
public RPCErrorBody() { | ||
|
||
} | ||
|
||
/** | ||
* A description of an error that occurred while performing an RPC request. | ||
* | ||
* @param name the name of the error type | ||
* @param message the message describing the error | ||
* @param stack the stack trace from the error | ||
*/ | ||
public RPCErrorBody(String name, String message, String stack) { | ||
this.name = name; | ||
this.message = message; | ||
this.stack = stack; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public String getStack() { | ||
return stack; | ||
} | ||
|
||
public void setStack(String stack) { | ||
this.stack = stack; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
scuttlebutt-rpc/src/main/java/net/consensys/cava/scuttlebutt/rpc/RPCFunction.java
This file contains 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,52 @@ | ||
/* | ||
* Copyright 2019 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package net.consensys.cava.scuttlebutt.rpc; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* A scuttlebutt RPC function namespace and name representation. | ||
*/ | ||
public class RPCFunction { | ||
|
||
private final List<String> namespace; | ||
private final String functionName; | ||
|
||
/** | ||
* | ||
* @param namespace the namespace of the function (e.g. ['blobs']. May be empty if there is no namespace for the | ||
* function. | ||
* @param functionName the function (e.g. 'add'.) | ||
*/ | ||
public RPCFunction(List<String> namespace, String functionName) { | ||
this.namespace = namespace; | ||
this.functionName = functionName; | ||
} | ||
|
||
public RPCFunction(String functionName) { | ||
this.namespace = new ArrayList<>(); | ||
this.functionName = functionName; | ||
} | ||
|
||
/** | ||
* @return The list representation of the namespace and function call. | ||
*/ | ||
public List<String> asList() { | ||
List<String> list = new ArrayList<>(); | ||
list.addAll(namespace); | ||
list.add(functionName); | ||
return list; | ||
} | ||
|
||
} |
Oops, something went wrong.