-
Notifications
You must be signed in to change notification settings - Fork 6
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
Generate service stubs #255
Labels
New Feature
A new feature, service, or documentation. Major changes that are not backwards compatible.
Comments
rbair23
added
the
New Feature
A new feature, service, or documentation. Major changes that are not backwards compatible.
label
May 24, 2024
Here's an example implementation of public interface ConsensusService extends ServiceInterface {
enum ConsensusMethod implements Method {
createTopic,
updateTopic,
deleteTopic,
submitMessage,
getTopicInfo;
}
TransactionResponse createTopic(Transaction tx);
TransactionResponse updateTopic(Transaction tx);
TransactionResponse deleteTopic(Transaction tx);
TransactionResponse submitMessage(Transaction tx);
Response getTopicInfo(Query q);
default String serviceName() {
return "ConsensusService";
}
default String fullName() {
return "proto.ConsensusService";
}
default List<Method> methods() {
return List.of(
ConsensusMethod.createTopic,
ConsensusMethod.updateTopic,
ConsensusMethod.deleteTopic,
ConsensusMethod.submitMessage,
ConsensusMethod.getTopicInfo);
}
@Override
default void open(
final @NonNull Method method,
final @NonNull BlockingQueue<Bytes> messages,
final @NonNull ResponseCallback callback) {
final var m = (ConsensusMethod) method;
Thread.ofVirtual().start(() -> {
try {
switch (m) {
case ConsensusMethod.createTopic -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = Transaction.PROTOBUF.parse(message);
final var response = createTopic(messageBytes);
final var responseBytes = TransactionResponse.PROTOBUF.toBytes(response);
callback.send(responseBytes);
callback.close();
}
case ConsensusMethod.updateTopic -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = Transaction.PROTOBUF.parse(message);
final var response = updateTopic(messageBytes);
final var responseBytes = TransactionResponse.PROTOBUF.toBytes(response);
callback.send(responseBytes);
callback.close();
}
case ConsensusMethod.deleteTopic -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = Transaction.PROTOBUF.parse(message);
final var response = deleteTopic(messageBytes);
final var responseBytes = TransactionResponse.PROTOBUF.toBytes(response);
callback.send(responseBytes);
callback.close();
}
case ConsensusMethod.submitMessage -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = Transaction.PROTOBUF.parse(message);
final var response = submitMessage(messageBytes);
final var responseBytes = TransactionResponse.PROTOBUF.toBytes(response);
callback.send(responseBytes);
callback.close();
}
case ConsensusMethod.getTopicInfo -> {
// Unary method
final var message = messages.take();
callback.start();
final var messageBytes = Query.PROTOBUF.parse(message);
final var response = getTopicInfo(messageBytes);
final var responseBytes = Response.PROTOBUF.toBytes(response);
callback.send(responseBytes);
callback.close();
}
}
} catch (Exception e) {
e.printStackTrace();
callback.close();
}
});
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
New Feature
A new feature, service, or documentation. Major changes that are not backwards compatible.
Problem
PBJ generates Java objects for each
message
it encounters in the protobuf schema definitions, but it does not generate anything forservice
definitions. Right now the only way to use PBJ as a library in a web server is to use a very low-level API provided by the gRPC libraries that basically deliver you a byte array from which you can parse. An example of how this can be done can be found in the Hashgraph consensus node repo.Solution
Create two new files:
ServiceInterface
: this represents theservice
defined in the protobuf schema files. In addition to some helper methods, it contains the actual interface definition for the different methods defined on theservice
, such that a user can implement this interface in their code. It also contains default methods for various aspects of the actual handling of the call.ServiceMethod
: a simple definition of the method on theservice
-- what kind of method it is (unary, server streaming, client streaming, bidi streaming), and what it is named.Alternatives
We could generate the same types of stubs used by the normal gRPC libraries. The stubs we create would be different from those created by protoc, but they should interoperate with the normal gRPC libraries. This would increase interoperability, because it would just work with any existing servers with gRPC support. And maybe this is something we should consider doing as well.
However, generating our own solution has benefits:
The text was updated successfully, but these errors were encountered: