Skip to content

Commit e3b1e3b

Browse files
committed
RPC: add ServiceDescriptors to transcode RPC / Protocol Buffers calls into another representation (e.g. http/json)
1 parent d493964 commit e3b1e3b

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

rsocket-messages/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ plugins {
1010

1111
dependencies {
1212
api "io.netty:netty-buffer"
13+
14+
compileOnly "com.google.protobuf:protobuf-java"
1315
compileOnly "com.google.code.findbugs:jsr305"
1416
}
1517

rsocket-messages/gradle.lockfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ com.google.errorprone:javac-shaded:9+181-r4173-1=googleJavaFormat1.6
77
com.google.googlejavaformat:google-java-format:1.6=googleJavaFormat1.6
88
com.google.guava:guava:22.0=googleJavaFormat1.6
99
com.google.j2objc:j2objc-annotations:1.1=googleJavaFormat1.6
10+
com.google.protobuf:protobuf-java:3.25.0=compileClasspath
1011
io.netty:netty-buffer:4.1.101.Final=compileClasspath,runtimeClasspath
1112
io.netty:netty-common:4.1.101.Final=compileClasspath,runtimeClasspath
1213
org.codehaus.mojo:animal-sniffer-annotations:1.14=googleJavaFormat1.6

rsocket-messages/src/main/java/com/jauntsdn/rsocket/Rpc.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
package com.jauntsdn.rsocket;
1818

19+
import com.google.protobuf.CodedInputStream;
1920
import com.jauntsdn.rsocket.exceptions.ApplicationErrorException;
21+
import com.jauntsdn.rsocket.exceptions.SerializationException;
2022
import io.netty.buffer.ByteBuf;
2123
import io.netty.buffer.ByteBufAllocator;
2224
import io.netty.buffer.ByteBufUtil;
2325
import io.netty.buffer.Unpooled;
2426
import io.netty.buffer.UnpooledByteBufAllocator;
2527
import io.netty.buffer.UnpooledHeapByteBuf;
28+
import java.io.IOException;
2629
import java.lang.annotation.ElementType;
2730
import java.lang.annotation.Retention;
2831
import java.lang.annotation.RetentionPolicy;
@@ -31,6 +34,8 @@
3134
import java.util.ArrayList;
3235
import java.util.List;
3336
import java.util.Objects;
37+
import java.util.function.Function;
38+
import java.util.function.Supplier;
3439

3540
public final class Rpc {
3641

@@ -423,4 +428,59 @@ public static Headers decodeHeaders(ByteBuf metadata) {
423428
return Headers.create(headers);
424429
}
425430
}
431+
432+
/**
433+
* Service descriptor used to transcode RPC / Protocol Buffers calls into another representation
434+
* (e.g. http/json)
435+
*/
436+
public static class ServiceDescriptor {
437+
private final List<Call> serviceCalls;
438+
439+
public ServiceDescriptor(List<Call> serviceCalls) {
440+
this.serviceCalls = Objects.requireNonNull(serviceCalls, "serviceCalls");
441+
}
442+
443+
public final List<Call> serviceCalls() {
444+
return serviceCalls;
445+
}
446+
447+
public static class Call {
448+
final String name;
449+
final InboundMessageFactory inMessageFactory;
450+
final OutboundMessageFactory outMessageFactory;
451+
452+
private Call(
453+
String name,
454+
InboundMessageFactory inMessageFactory,
455+
OutboundMessageFactory outMessageFactory) {
456+
this.name = Objects.requireNonNull(name, "name");
457+
this.inMessageFactory = Objects.requireNonNull(inMessageFactory, "inMessageFactory");
458+
this.outMessageFactory = Objects.requireNonNull(outMessageFactory, "outMessageFactory");
459+
}
460+
461+
public static Call of(
462+
String name,
463+
InboundMessageFactory inMessageFactory,
464+
OutboundMessageFactory outMessageFactory) {
465+
return new Call(name, inMessageFactory, outMessageFactory);
466+
}
467+
}
468+
469+
public interface InboundMessageFactory extends Supplier<com.google.protobuf.Message.Builder> {}
470+
471+
public interface OutboundMessageFactory
472+
extends Function<CodedInputStream, com.google.protobuf.Message> {
473+
474+
@Override
475+
default com.google.protobuf.Message apply(CodedInputStream codedInputStream) {
476+
try {
477+
return create(codedInputStream);
478+
} catch (IOException e) {
479+
throw new SerializationException("Protobuf deserialization error", e);
480+
}
481+
}
482+
483+
com.google.protobuf.Message create(CodedInputStream codedInputStream) throws IOException;
484+
}
485+
}
426486
}

0 commit comments

Comments
 (0)