Skip to content

Commit

Permalink
RPC: add ServiceDescriptors to transcode RPC / Protocol Buffers calls…
Browse files Browse the repository at this point in the history
… into another representation (e.g. http/json)
  • Loading branch information
mostroverkhov committed Nov 15, 2023
1 parent d493964 commit e3b1e3b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions rsocket-messages/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ plugins {

dependencies {
api "io.netty:netty-buffer"

compileOnly "com.google.protobuf:protobuf-java"
compileOnly "com.google.code.findbugs:jsr305"
}

Expand Down
1 change: 1 addition & 0 deletions rsocket-messages/gradle.lockfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ com.google.errorprone:javac-shaded:9+181-r4173-1=googleJavaFormat1.6
com.google.googlejavaformat:google-java-format:1.6=googleJavaFormat1.6
com.google.guava:guava:22.0=googleJavaFormat1.6
com.google.j2objc:j2objc-annotations:1.1=googleJavaFormat1.6
com.google.protobuf:protobuf-java:3.25.0=compileClasspath
io.netty:netty-buffer:4.1.101.Final=compileClasspath,runtimeClasspath
io.netty:netty-common:4.1.101.Final=compileClasspath,runtimeClasspath
org.codehaus.mojo:animal-sniffer-annotations:1.14=googleJavaFormat1.6
Expand Down
60 changes: 60 additions & 0 deletions rsocket-messages/src/main/java/com/jauntsdn/rsocket/Rpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.jauntsdn.rsocket;

import com.google.protobuf.CodedInputStream;
import com.jauntsdn.rsocket.exceptions.ApplicationErrorException;
import com.jauntsdn.rsocket.exceptions.SerializationException;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.buffer.UnpooledHeapByteBuf;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -31,6 +34,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

public final class Rpc {

Expand Down Expand Up @@ -423,4 +428,59 @@ public static Headers decodeHeaders(ByteBuf metadata) {
return Headers.create(headers);
}
}

/**
* Service descriptor used to transcode RPC / Protocol Buffers calls into another representation
* (e.g. http/json)
*/
public static class ServiceDescriptor {
private final List<Call> serviceCalls;

public ServiceDescriptor(List<Call> serviceCalls) {
this.serviceCalls = Objects.requireNonNull(serviceCalls, "serviceCalls");
}

public final List<Call> serviceCalls() {
return serviceCalls;
}

public static class Call {
final String name;
final InboundMessageFactory inMessageFactory;
final OutboundMessageFactory outMessageFactory;

private Call(
String name,
InboundMessageFactory inMessageFactory,
OutboundMessageFactory outMessageFactory) {
this.name = Objects.requireNonNull(name, "name");
this.inMessageFactory = Objects.requireNonNull(inMessageFactory, "inMessageFactory");
this.outMessageFactory = Objects.requireNonNull(outMessageFactory, "outMessageFactory");
}

public static Call of(
String name,
InboundMessageFactory inMessageFactory,
OutboundMessageFactory outMessageFactory) {
return new Call(name, inMessageFactory, outMessageFactory);
}
}

public interface InboundMessageFactory extends Supplier<com.google.protobuf.Message.Builder> {}

public interface OutboundMessageFactory
extends Function<CodedInputStream, com.google.protobuf.Message> {

@Override
default com.google.protobuf.Message apply(CodedInputStream codedInputStream) {
try {
return create(codedInputStream);
} catch (IOException e) {
throw new SerializationException("Protobuf deserialization error", e);
}
}

com.google.protobuf.Message create(CodedInputStream codedInputStream) throws IOException;
}
}
}

0 comments on commit e3b1e3b

Please sign in to comment.