Revert "Remove some unused fbs files"

This reverts commit 891036d89a.
This commit is contained in:
Siarhei Fedartsou 2024-07-09 20:08:14 +02:00
parent 8866f72340
commit 63b55e1820
73 changed files with 8681 additions and 0 deletions

View File

42
third_party/flatbuffers/grpc/README.md vendored Normal file
View File

@ -0,0 +1,42 @@
GRPC implementation and test
============================
NOTE: files in `src/` are shared with the GRPC project, and maintained there
(any changes should be submitted to GRPC instead). These files are copied
from GRPC, and work with both the Protobuf and FlatBuffers code generator.
`tests/` contains a GRPC specific test, you need to have built and installed
the GRPC libraries for this to compile. This test will build using the
`FLATBUFFERS_BUILD_GRPCTEST` option to the main FlatBuffers CMake project.
## Building Flatbuffers with gRPC
### Linux
1. Download, build and install gRPC. See [instructions](https://github.com/grpc/grpc/tree/master/src/cpp).
* Lets say your gRPC clone is at `/your/path/to/grpc_repo`.
* Install gRPC in a custom directory by running `make install prefix=/your/path/to/grpc_repo/install`.
2. `export GRPC_INSTALL_PATH=/your/path/to/grpc_repo/install`
3. `export PROTOBUF_DOWNLOAD_PATH=/your/path/to/grpc_repo/third_party/protobuf`
4. `mkdir build ; cd build`
5. `cmake -DFLATBUFFERS_BUILD_GRPCTEST=ON -DGRPC_INSTALL_PATH=${GRPC_INSTALL_PATH} -DPROTOBUF_DOWNLOAD_PATH=${PROTOBUF_DOWNLOAD_PATH} ..`
6. `make`
For Bazel users:
```shell
$bazel test src/compiler/...
```
## Running FlatBuffer gRPC tests
### Linux
1. `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${GRPC_INSTALL_PATH}/lib`
2. `make test ARGS=-V`
For Bazel users:
```shell
$bazel test tests/...
```

View File

@ -0,0 +1,13 @@
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1645a264a..12f8ca999 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -635,6 +635,8 @@ add_library(
src/ssl/tls_record.cc
)
+target_link_libraries(ssl crypto)
+
add_executable(
bssl

24
third_party/flatbuffers/grpc/build_grpc.sh vendored Executable file
View File

@ -0,0 +1,24 @@
#!/bin/bash
grpc_1_39_0_githash=58602e20a3f3e48f24a4114c757099b25b947f7b
function build_grpc () {
git clone https://github.com/grpc/grpc.git google/grpc
cd google/grpc
git checkout ${grpc_1_39_0_githash}
git submodule update --init
# Apply boringssl build patch
cd third_party/boringssl-with-bazel
git apply ../../../../grpc/boringssl.patch
cd ../..
mkdir ../grpc_build
cd ../grpc_build
cmake ../grpc -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DABSL_ENABLE_INSTALL=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=`pwd`/../grpc/install
cmake --build . --target install ${JOBS:+-j$JOBS}
cd ../..
}
GRPC_INSTALL_PATH=`pwd`/google/grpc/install
PROTOBUF_DOWNLOAD_PATH=`pwd`/google/grpc/third_party/protobuf
build_grpc

View File

@ -0,0 +1,10 @@
diff --git a/bazel/copts.bzl b/bazel/copts.bzl
index 10be944f25..879518b92f 100644
--- a/bazel/copts.bzl
+++ b/bazel/copts.bzl
@@ -59,4 +59,4 @@ GRPC_LLVM_WARNING_FLAGS = [
GRPC_DEFAULT_COPTS = select({
"//:use_strict_warning": GRPC_LLVM_WARNING_FLAGS + ["-DUSE_STRICT_WARNING=1"],
"//conditions:default": [],
-})
+}) + ["-std=c++14"]

View File

@ -0,0 +1,35 @@
## Languages known issues
### Python
- Assert the type required in your server/client since python is able to receive `Bytes array` or `utf8 strings`.
```python
def SayHello(self, request, context):
# request might be a byte array or a utf8 string
r = HelloRequest.HelloRequest().GetRootAs(request, 0)
reply = "Unknown"
if r.Name():
reply = r.Name()
# Issues might happen if type checking isnt present.
# thus encoding it as a `reply.decode('UTF-8')`
return build_reply("welcome " + reply.decode('UTF-8'))
```
This can be prevented by making sure all the requests coming to/from python are `Bytes array`
```python
def say_hello(stub, builder):
hello_request = bytes(builder.Output())
reply = stub.SayHello(hello_request)
r = HelloReply.HelloReply.GetRootAs(reply)
print(r.Message())
```
### Go
- Always requires the `content-type` of the payload to be set to `application/grpc+flatbuffers`
example: `.SayHello(ctx, b, grpc.CallContentSubtype("flatbuffers"))`

View File

@ -0,0 +1,36 @@
#!/bin/bash
#
# Copyright 2021 Google Inc. All rights reserved.
#
# 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.
set -e
format_greeter() {
cd greeter
# Format client
cd client
gofmt -w .
cd ..
# Format server
cd server
gofmt -w .
cd ..
cd ..
}
format_greeter

View File

@ -0,0 +1,2 @@
**/server
**/client

View File

@ -0,0 +1,25 @@
# Go Greeter example
## Project Structure
.
├── server # Server module
├── client # Client module
├── models # Flatbuffers models & main grpc code.
└── README.md
## How to run Server:
- `cd server`
- `go clean`
- `go run main.go`
## How to run Client:
- `cd client`
- `go clean`
- `go run main.go --name NAME`

View File

@ -0,0 +1,11 @@
module github.com/google/flatbuffers/grpc/examples/go/greeter/client
go 1.15
replace github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0 => ../models
require (
github.com/google/flatbuffers v2.0.8+incompatible
github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0
google.golang.org/grpc v1.56.3
)

View File

@ -0,0 +1,78 @@
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"time"
flatbuffers "github.com/google/flatbuffers/go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
models "github.com/google/flatbuffers/grpc/examples/go/greeter/models"
)
var (
addr = "3000"
name = flag.String("name", "Flatbuffers", "name to be sent to server :D")
)
func printSayHello(client models.GreeterClient, name string) {
log.Printf("Name to be sent (%s)", name)
b := flatbuffers.NewBuilder(0)
i := b.CreateString(name)
models.HelloRequestStart(b)
models.HelloRequestAddName(b, i)
b.Finish(models.HelloRequestEnd(b))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
request, err := client.SayHello(ctx, b, grpc.CallContentSubtype("flatbuffers"))
if err != nil {
log.Fatalf("%v.SayHello(_) = _, %v: ", client, err)
}
log.Printf("server said %q", request.Message())
}
func printSayManyHello(client models.GreeterClient, name string) {
log.Printf("Name to be sent (%s)", name)
b := flatbuffers.NewBuilder(0)
i := b.CreateString(name)
models.HelloRequestStart(b)
models.HelloRequestAddName(b, i)
b.Finish(models.HelloRequestEnd(b))
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
stream, err := client.SayManyHellos(ctx, b, grpc.CallContentSubtype("flatbuffers"))
if err != nil {
log.Fatalf("%v.SayManyHellos(_) = _, %v", client, err)
}
for {
request, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("%v.SayManyHellos(_) = _, %v", client, err)
}
log.Printf("server said %q", request.Message())
}
}
func main() {
flag.Parse()
conn, err := grpc.Dial(fmt.Sprintf("localhost:%d", 3000),
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(grpc.ForceCodec(flatbuffers.FlatbuffersCodec{})))
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
defer conn.Close()
client := models.NewGreeterClient(conn)
printSayHello(client, *name)
printSayManyHello(client, *name)
}

View File

@ -0,0 +1,158 @@
//Generated by gRPC Go plugin
//If you make any local changes, they will be lost
//source: greeter
package models
import (
context "context"
flatbuffers "github.com/google/flatbuffers/go"
grpc "google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Client API for Greeter service
type GreeterClient interface {
SayHello(ctx context.Context, in *flatbuffers.Builder,
opts ...grpc.CallOption) (*HelloReply, error)
SayManyHellos(ctx context.Context, in *flatbuffers.Builder,
opts ...grpc.CallOption) (Greeter_SayManyHellosClient, error)
}
type greeterClient struct {
cc grpc.ClientConnInterface
}
func NewGreeterClient(cc grpc.ClientConnInterface) GreeterClient {
return &greeterClient{cc}
}
func (c *greeterClient) SayHello(ctx context.Context, in *flatbuffers.Builder,
opts ...grpc.CallOption) (*HelloReply, error) {
out := new(HelloReply)
err := c.cc.Invoke(ctx, "/models.Greeter/SayHello", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *greeterClient) SayManyHellos(ctx context.Context, in *flatbuffers.Builder,
opts ...grpc.CallOption) (Greeter_SayManyHellosClient, error) {
stream, err := c.cc.NewStream(ctx, &_Greeter_serviceDesc.Streams[0], "/models.Greeter/SayManyHellos", opts...)
if err != nil {
return nil, err
}
x := &greeterSayManyHellosClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type Greeter_SayManyHellosClient interface {
Recv() (*HelloReply, error)
grpc.ClientStream
}
type greeterSayManyHellosClient struct {
grpc.ClientStream
}
func (x *greeterSayManyHellosClient) Recv() (*HelloReply, error) {
m := new(HelloReply)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// Server API for Greeter service
type GreeterServer interface {
SayHello(context.Context, *HelloRequest) (*flatbuffers.Builder, error)
SayManyHellos(*HelloRequest, Greeter_SayManyHellosServer) error
mustEmbedUnimplementedGreeterServer()
}
type UnimplementedGreeterServer struct {
}
func (UnimplementedGreeterServer) SayHello(context.Context, *HelloRequest) (*flatbuffers.Builder, error) {
return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented")
}
func (UnimplementedGreeterServer) SayManyHellos(*HelloRequest, Greeter_SayManyHellosServer) error {
return status.Errorf(codes.Unimplemented, "method SayManyHellos not implemented")
}
func (UnimplementedGreeterServer) mustEmbedUnimplementedGreeterServer() {}
type UnsafeGreeterServer interface {
mustEmbedUnimplementedGreeterServer()
}
func RegisterGreeterServer(s grpc.ServiceRegistrar, srv GreeterServer) {
s.RegisterService(&_Greeter_serviceDesc, srv)
}
func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context,
dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(HelloRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GreeterServer).SayHello(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/models.Greeter/SayHello",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest))
}
return interceptor(ctx, in, info, handler)
}
func _Greeter_SayManyHellos_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(HelloRequest)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(GreeterServer).SayManyHellos(m, &greeterSayManyHellosServer{stream})
}
type Greeter_SayManyHellosServer interface {
Send(*flatbuffers.Builder) error
grpc.ServerStream
}
type greeterSayManyHellosServer struct {
grpc.ServerStream
}
func (x *greeterSayManyHellosServer) Send(m *flatbuffers.Builder) error {
return x.ServerStream.SendMsg(m)
}
var _Greeter_serviceDesc = grpc.ServiceDesc{
ServiceName: "models.Greeter",
HandlerType: (*GreeterServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "SayHello",
Handler: _Greeter_SayHello_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "SayManyHellos",
Handler: _Greeter_SayManyHellos_Handler,
ServerStreams: true,
},
},
}

View File

@ -0,0 +1,60 @@
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
package models
import (
flatbuffers "github.com/google/flatbuffers/go"
)
type HelloReply struct {
_tab flatbuffers.Table
}
func GetRootAsHelloReply(buf []byte, offset flatbuffers.UOffsetT) *HelloReply {
n := flatbuffers.GetUOffsetT(buf[offset:])
x := &HelloReply{}
x.Init(buf, n+offset)
return x
}
func FinishHelloReplyBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
builder.Finish(offset)
}
func GetSizePrefixedRootAsHelloReply(buf []byte, offset flatbuffers.UOffsetT) *HelloReply {
n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
x := &HelloReply{}
x.Init(buf, n+offset+flatbuffers.SizeUint32)
return x
}
func FinishSizePrefixedHelloReplyBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
builder.FinishSizePrefixed(offset)
}
func (rcv *HelloReply) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *HelloReply) Table() flatbuffers.Table {
return rcv._tab
}
func (rcv *HelloReply) Message() []byte {
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
if o != 0 {
return rcv._tab.ByteVector(o + rcv._tab.Pos)
}
return nil
}
func HelloReplyStart(builder *flatbuffers.Builder) {
builder.StartObject(1)
}
func HelloReplyAddMessage(builder *flatbuffers.Builder, message flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(message), 0)
}
func HelloReplyEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return builder.EndObject()
}

View File

@ -0,0 +1,60 @@
// Code generated by the FlatBuffers compiler. DO NOT EDIT.
package models
import (
flatbuffers "github.com/google/flatbuffers/go"
)
type HelloRequest struct {
_tab flatbuffers.Table
}
func GetRootAsHelloRequest(buf []byte, offset flatbuffers.UOffsetT) *HelloRequest {
n := flatbuffers.GetUOffsetT(buf[offset:])
x := &HelloRequest{}
x.Init(buf, n+offset)
return x
}
func FinishHelloRequestBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
builder.Finish(offset)
}
func GetSizePrefixedRootAsHelloRequest(buf []byte, offset flatbuffers.UOffsetT) *HelloRequest {
n := flatbuffers.GetUOffsetT(buf[offset+flatbuffers.SizeUint32:])
x := &HelloRequest{}
x.Init(buf, n+offset+flatbuffers.SizeUint32)
return x
}
func FinishSizePrefixedHelloRequestBuffer(builder *flatbuffers.Builder, offset flatbuffers.UOffsetT) {
builder.FinishSizePrefixed(offset)
}
func (rcv *HelloRequest) Init(buf []byte, i flatbuffers.UOffsetT) {
rcv._tab.Bytes = buf
rcv._tab.Pos = i
}
func (rcv *HelloRequest) Table() flatbuffers.Table {
return rcv._tab
}
func (rcv *HelloRequest) Name() []byte {
o := flatbuffers.UOffsetT(rcv._tab.Offset(4))
if o != 0 {
return rcv._tab.ByteVector(o + rcv._tab.Pos)
}
return nil
}
func HelloRequestStart(builder *flatbuffers.Builder) {
builder.StartObject(1)
}
func HelloRequestAddName(builder *flatbuffers.Builder, name flatbuffers.UOffsetT) {
builder.PrependUOffsetTSlot(0, flatbuffers.UOffsetT(name), 0)
}
func HelloRequestEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
return builder.EndObject()
}

View File

@ -0,0 +1,8 @@
module github.com/google/flatbuffers/grpc/examples/go/greeter/models
go 1.15
require (
github.com/google/flatbuffers v2.0.8+incompatible
google.golang.org/grpc v1.56.3
)

View File

@ -0,0 +1,11 @@
module github.com/google/flatbuffers/grpc/examples/go/greeter/server
go 1.15
replace github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0 => ../models
require (
github.com/google/flatbuffers v2.0.8+incompatible
github.com/google/flatbuffers/grpc/examples/go/greeter/models v0.0.0
google.golang.org/grpc v1.56.3
)

View File

@ -0,0 +1,77 @@
package main
import (
"context"
"fmt"
"log"
"net"
flatbuffers "github.com/google/flatbuffers/go"
models "github.com/google/flatbuffers/grpc/examples/go/greeter/models"
"google.golang.org/grpc"
)
var (
greetings = [...]string{"Hi", "Hallo", "Ciao"}
)
type greeterServer struct {
models.UnimplementedGreeterServer
}
func (s *greeterServer) SayHello(ctx context.Context, request *models.HelloRequest) (*flatbuffers.Builder, error) {
v := request.Name()
var m string
if v == nil {
m = "Unknown"
} else {
m = string(v)
}
b := flatbuffers.NewBuilder(0)
idx := b.CreateString("welcome " + m)
models.HelloReplyStart(b)
models.HelloReplyAddMessage(b, idx)
b.Finish(models.HelloReplyEnd(b))
return b, nil
}
func (s *greeterServer) SayManyHellos(request *models.HelloRequest, stream models.Greeter_SayManyHellosServer) error {
v := request.Name()
var m string
if v == nil {
m = "Unknown"
} else {
m = string(v)
}
b := flatbuffers.NewBuilder(0)
for _, greeting := range greetings {
idx := b.CreateString(greeting + " " + m)
models.HelloReplyStart(b)
models.HelloReplyAddMessage(b, idx)
b.Finish(models.HelloReplyEnd(b))
if err := stream.Send(b); err != nil {
return err
}
}
return nil
}
func newServer() *greeterServer {
s := &greeterServer{}
return s
}
func main() {
lis, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", 3000))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
codec := &flatbuffers.FlatbuffersCodec{}
grpcServer := grpc.NewServer(grpc.ForceServerCodec(codec))
models.RegisterGreeterServer(grpcServer, newServer())
if err := grpcServer.Serve(lis); err != nil {
fmt.Print(err)
panic(err)
}
}

View File

@ -0,0 +1,14 @@
namespace models;
table HelloReply {
message:string;
}
table HelloRequest {
name:string;
}
rpc_service Greeter {
SayHello(HelloRequest):HelloReply;
SayManyHellos(HelloRequest):HelloReply (streaming: "server");
}

View File

@ -0,0 +1,12 @@
# Python Greeter example
## Prerequisite
- You need to have grpc python installed on your device `pip install grpcio`
## How to run Server:
- `python server.py ${PORT}`
## How to run Client:
- `python client.py ${PORT} ${NAME}`

View File

@ -0,0 +1,40 @@
import sys
import argparse
import grpc
sys.path.insert(0, '../../../../../flatbuffers/python')
import flatbuffers
from models import HelloReply, HelloRequest, greeter_grpc_fb
parser = argparse.ArgumentParser()
parser.add_argument("port", help="server port to connect to", default=3000)
parser.add_argument("name", help="name to be sent to server", default="flatbuffers")
def say_hello(stub, hello_request):
reply = stub.SayHello(hello_request)
r = HelloReply.HelloReply.GetRootAs(reply)
print(r.Message())
def say_many_hellos(stub, hello_request):
greetings = stub.SayManyHellos(hello_request)
for greeting in greetings:
r = HelloReply.HelloReply.GetRootAs(greeting)
print(r.Message())
def main():
args = parser.parse_args()
with grpc.insecure_channel('localhost:' + args.port) as channel:
builder = flatbuffers.Builder()
ind = builder.CreateString(args.name)
HelloRequest.HelloRequestStart(builder)
HelloRequest.HelloRequestAddName(builder, ind)
root = HelloRequest.HelloRequestEnd(builder)
builder.Finish(root)
output = bytes(builder.Output())
stub = greeter_grpc_fb.GreeterStub(channel)
say_hello(stub, output)
say_many_hellos(stub, output)
main()

View File

@ -0,0 +1,50 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: models
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class HelloReply(object):
__slots__ = ['_tab']
@classmethod
def GetRootAs(cls, buf, offset=0):
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
x = HelloReply()
x.Init(buf, n + offset)
return x
@classmethod
def GetRootAsHelloReply(cls, buf, offset=0):
"""This method is deprecated. Please switch to GetRootAs."""
return cls.GetRootAs(buf, offset)
# HelloReply
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# HelloReply
def Message(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
return self._tab.String(o + self._tab.Pos)
return None
def HelloReplyStart(builder):
builder.StartObject(1)
def Start(builder):
HelloReplyStart(builder)
def HelloReplyAddMessage(builder, message):
builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(message), 0)
def AddMessage(builder, message):
HelloReplyAddMessage(builder, message)
def HelloReplyEnd(builder):
return builder.EndObject()
def End(builder):
return HelloReplyEnd(builder)

View File

@ -0,0 +1,50 @@
# automatically generated by the FlatBuffers compiler, do not modify
# namespace: models
import flatbuffers
from flatbuffers.compat import import_numpy
np = import_numpy()
class HelloRequest(object):
__slots__ = ['_tab']
@classmethod
def GetRootAs(cls, buf, offset=0):
n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
x = HelloRequest()
x.Init(buf, n + offset)
return x
@classmethod
def GetRootAsHelloRequest(cls, buf, offset=0):
"""This method is deprecated. Please switch to GetRootAs."""
return cls.GetRootAs(buf, offset)
# HelloRequest
def Init(self, buf, pos):
self._tab = flatbuffers.table.Table(buf, pos)
# HelloRequest
def Name(self):
o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
if o != 0:
return self._tab.String(o + self._tab.Pos)
return None
def HelloRequestStart(builder):
builder.StartObject(1)
def Start(builder):
HelloRequestStart(builder)
def HelloRequestAddName(builder, name):
builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(name), 0)
def AddName(builder, name):
HelloRequestAddName(builder, name)
def HelloRequestEnd(builder):
return builder.EndObject()
def End(builder):
return HelloRequestEnd(builder)

View File

@ -0,0 +1,52 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
import grpc
class GreeterStub(object):
""" Interface exported by the server. """
def __init__(self, channel):
""" Constructor.
Args:
channel: A grpc.Channel.
"""
self.SayHello = channel.unary_unary(
"/models.Greeter/SayHello"
)
self.SayManyHellos = channel.unary_stream(
"/models.Greeter/SayManyHellos"
)
class GreeterServicer(object):
""" Interface exported by the server. """
def SayHello(self, request, context):
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def SayManyHellos(self, request, context):
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_GreeterServicer_to_server(servicer, server):
rpc_method_handlers = {
'SayHello': grpc.unary_unary_rpc_method_handler(
servicer.SayHello
),
'SayManyHellos': grpc.unary_stream_rpc_method_handler(
servicer.SayManyHellos
),
}
generic_handler = grpc.method_handlers_generic_handler(
'models.Greeter', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))

View File

@ -0,0 +1,57 @@
from concurrent import futures
import sys
import argparse
import grpc
sys.path.insert(0, '../../../../../flatbuffers/python')
import flatbuffers
from models import HelloReply, HelloRequest, greeter_grpc_fb
parser = argparse.ArgumentParser()
parser.add_argument("port", help="server on port", default=3000)
def build_reply(message):
builder = flatbuffers.Builder()
ind = builder.CreateString(message)
HelloReply.HelloReplyStart(builder)
HelloReply.HelloReplyAddMessage(builder, ind)
root = HelloReply.HelloReplyEnd(builder)
builder.Finish(root)
return bytes(builder.Output())
class GreeterServicer(greeter_grpc_fb.GreeterServicer):
def __init__(self):
self.greetings = ["Hi", "Hallo", "Ciao"]
def SayHello(self, request, context):
r = HelloRequest.HelloRequest().GetRootAs(request, 0)
reply = "Unknown"
if r.Name():
reply = r.Name()
return build_reply("welcome " + reply.decode('UTF-8'))
def SayManyHellos(self, request, context):
r = HelloRequest.HelloRequest().GetRootAs(request, 0)
reply = "Unknown"
if r.Name():
reply = r.Name()
for greeting in self.greetings:
print(type(reply))
yield build_reply(greeting + " " + reply.decode('UTF-8'))
def serve():
args = parser.parse_args()
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
greeter_grpc_fb.add_GreeterServicer_to_server(
GreeterServicer(), server
)
server.add_insecure_port('[::]:' + args.port)
server.start()
server.wait_for_termination()
if __name__ == '__main__':
serve()

View File

@ -0,0 +1,58 @@
// swift-tools-version:5.1
/*
* Copyright 2020 Google Inc. All rights reserved.
*
* 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.
*/
import PackageDescription
let package = Package(
name: "Greeter",
platforms: [
.iOS(.v11),
.macOS(.v10_14),
],
dependencies: [
.package(path: "../../../../swift"),
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Model",
dependencies: [
"GRPC",
"FlatBuffers",
],
path: "Sources/Model"),
// Client for the Greeter example
.target(
name: "Client",
dependencies: [
"GRPC",
"Model",
],
path: "Sources/client"),
// Server for the Greeter example
.target(
name: "Server",
dependencies: [
"GRPC",
"Model",
],
path: "Sources/server"),
])

View File

@ -0,0 +1,7 @@
# FlatBuffers.GRPC.Swift
The following is Swift example on how GRPC would be with Swift Flatbuffers, you can simply run the following commands:
`swift run Server`
`swift run Client {port} {name}`

View File

@ -0,0 +1,145 @@
// Generated GRPC code for FlatBuffers swift!
/// The following code is generated by the Flatbuffers library which might not be in sync with grpc-swift
/// in case of an issue please open github issue, though it would be maintained
// swiftlint:disable all
// swiftformat:disable all
import Foundation
import GRPC
import NIO
import NIOHTTP1
import FlatBuffers
public protocol GRPCFlatBufPayload: GRPCPayload, FlatBufferGRPCMessage {}
public extension GRPCFlatBufPayload {
init(serializedByteBuffer: inout NIO.ByteBuffer) throws {
self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: serializedByteBuffer.readableBytesView, count: serializedByteBuffer.readableBytes))
}
func serialize(into buffer: inout NIO.ByteBuffer) throws {
let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: Int(self.size))
buffer.writeBytes(buf)
}
}
extension Message: GRPCFlatBufPayload {}
/// Usage: instantiate models_GreeterServiceClient, then call methods of this protocol to make API calls.
public protocol models_GreeterClientProtocol: GRPCClient {
var serviceName: String { get }
var interceptors: models_GreeterClientInterceptorFactoryProtocol? { get }
func SayHello(
_ request: Message<models_HelloRequest>
, callOptions: CallOptions?
) -> UnaryCall<Message<models_HelloRequest>, Message<models_HelloReply>>
func SayManyHellos(
_ request: Message<models_HelloRequest>
, callOptions: CallOptions?,
handler: @escaping (Message<models_HelloReply>) -> Void
) -> ServerStreamingCall<Message<models_HelloRequest>, Message<models_HelloReply>>
}
extension models_GreeterClientProtocol {
public var serviceName: String { "models.Greeter" }
public func SayHello(
_ request: Message<models_HelloRequest>
, callOptions: CallOptions? = nil
) -> UnaryCall<Message<models_HelloRequest>, Message<models_HelloReply>> {
return self.makeUnaryCall(
path: "/models.Greeter/SayHello",
request: request,
callOptions: callOptions ?? self.defaultCallOptions,
interceptors: self.interceptors?.makeSayHelloInterceptors() ?? []
)
}
public func SayManyHellos(
_ request: Message<models_HelloRequest>
, callOptions: CallOptions? = nil,
handler: @escaping (Message<models_HelloReply>) -> Void
) -> ServerStreamingCall<Message<models_HelloRequest>, Message<models_HelloReply>> {
return self.makeServerStreamingCall(
path: "/models.Greeter/SayManyHellos",
request: request,
callOptions: callOptions ?? self.defaultCallOptions,
interceptors: self.interceptors?.makeSayManyHellosInterceptors() ?? [],
handler: handler
)
}
}
public protocol models_GreeterClientInterceptorFactoryProtocol {
/// - Returns: Interceptors to use when invoking 'SayHello'.
func makeSayHelloInterceptors() -> [ClientInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
/// - Returns: Interceptors to use when invoking 'SayManyHellos'.
func makeSayManyHellosInterceptors() -> [ClientInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
}
public final class models_GreeterServiceClient: models_GreeterClientProtocol {
public let channel: GRPCChannel
public var defaultCallOptions: CallOptions
public var interceptors: models_GreeterClientInterceptorFactoryProtocol?
public init(
channel: GRPCChannel,
defaultCallOptions: CallOptions = CallOptions(),
interceptors: models_GreeterClientInterceptorFactoryProtocol? = nil
) {
self.channel = channel
self.defaultCallOptions = defaultCallOptions
self.interceptors = interceptors
}
}
public protocol models_GreeterProvider: CallHandlerProvider {
var interceptors: models_GreeterServerInterceptorFactoryProtocol? { get }
func SayHello(request: Message<models_HelloRequest>, context: StatusOnlyCallContext) -> EventLoopFuture<Message<models_HelloReply>>
func SayManyHellos(request: Message<models_HelloRequest>, context: StreamingResponseCallContext<Message<models_HelloReply>>) -> EventLoopFuture<GRPCStatus>
}
public extension models_GreeterProvider {
var serviceName: Substring { return "models.Greeter" }
func handle(method name: Substring, context: CallHandlerContext) -> GRPCServerHandlerProtocol? {
switch name {
case "SayHello":
return UnaryServerHandler(
context: context,
requestDeserializer: GRPCPayloadDeserializer<Message<models_HelloRequest>>(),
responseSerializer: GRPCPayloadSerializer<Message<models_HelloReply>>(),
interceptors: self.interceptors?.makeSayHelloInterceptors() ?? [],
userFunction: self.SayHello(request:context:))
case "SayManyHellos":
return ServerStreamingServerHandler(
context: context,
requestDeserializer: GRPCPayloadDeserializer<Message<models_HelloRequest>>(),
responseSerializer: GRPCPayloadSerializer<Message<models_HelloReply>>(),
interceptors: self.interceptors?.makeSayManyHellosInterceptors() ?? [],
userFunction: self.SayManyHellos(request:context:))
default: return nil;
}
}
}
public protocol models_GreeterServerInterceptorFactoryProtocol {
/// - Returns: Interceptors to use when handling 'SayHello'.
/// Defaults to calling `self.makeInterceptors()`.
func makeSayHelloInterceptors() -> [ServerInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
/// - Returns: Interceptors to use when handling 'SayManyHellos'.
/// Defaults to calling `self.makeInterceptors()`.
func makeSayManyHellosInterceptors() -> [ServerInterceptor<Message<models_HelloRequest>, Message<models_HelloReply>>]
}

View File

@ -0,0 +1,100 @@
// automatically generated by the FlatBuffers compiler, do not modify
// swiftlint:disable all
// swiftformat:disable all
import FlatBuffers
public struct models_HelloReply: FlatBufferObject, Verifiable {
static func validateVersion() { FlatBuffersVersion_24_3_25() }
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Table
private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
private enum VTOFFSET: VOffset {
case message = 4
var v: Int32 { Int32(self.rawValue) }
var p: VOffset { self.rawValue }
}
public var message: String? { let o = _accessor.offset(VTOFFSET.message.v); return o == 0 ? nil : _accessor.string(at: o) }
public var messageSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.message.v) }
public static func startHelloReply(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
public static func add(message: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: message, at: VTOFFSET.message.p) }
public static func endHelloReply(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
public static func createHelloReply(
_ fbb: inout FlatBufferBuilder,
messageOffset message: Offset = Offset()
) -> Offset {
let __start = models_HelloReply.startHelloReply(&fbb)
models_HelloReply.add(message: message, &fbb)
return models_HelloReply.endHelloReply(&fbb, start: __start)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.message.p, fieldName: "message", required: false, type: ForwardOffset<String>.self)
_v.finish()
}
}
extension models_HelloReply: Encodable {
enum CodingKeys: String, CodingKey {
case message = "message"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(message, forKey: .message)
}
}
public struct models_HelloRequest: FlatBufferObject, Verifiable {
static func validateVersion() { FlatBuffersVersion_24_3_25() }
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Table
private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
private enum VTOFFSET: VOffset {
case name = 4
var v: Int32 { Int32(self.rawValue) }
var p: VOffset { self.rawValue }
}
public var name: String? { let o = _accessor.offset(VTOFFSET.name.v); return o == 0 ? nil : _accessor.string(at: o) }
public var nameSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.name.v) }
public static func startHelloRequest(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
public static func add(name: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: name, at: VTOFFSET.name.p) }
public static func endHelloRequest(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end }
public static func createHelloRequest(
_ fbb: inout FlatBufferBuilder,
nameOffset name: Offset = Offset()
) -> Offset {
let __start = models_HelloRequest.startHelloRequest(&fbb)
models_HelloRequest.add(name: name, &fbb)
return models_HelloRequest.endHelloRequest(&fbb, start: __start)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: false, type: ForwardOffset<String>.self)
_v.finish()
}
}
extension models_HelloRequest: Encodable {
enum CodingKeys: String, CodingKey {
case name = "name"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(name, forKey: .name)
}
}

View File

@ -0,0 +1,107 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* 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.
*/
import FlatBuffers
import GRPC
import Logging
import Model
import NIO
// Quieten the logs.
LoggingSystem.bootstrap {
var handler = StreamLogHandler.standardOutput(label: $0)
handler.logLevel = .critical
return handler
}
func greet(name: String, client greeter: models_GreeterServiceClient) {
// Form the request with the name, if one was provided.
var builder = FlatBufferBuilder()
let nameOff = builder.create(string: name)
let root = models_HelloRequest.createHelloRequest(
&builder,
nameOffset: nameOff)
builder.finish(offset: root)
// Make the RPC call to the server.
let sayHello = greeter
.SayHello(Message<models_HelloRequest>(builder: &builder))
// wait() on the response to stop the program from exiting before the response is received.
do {
let response = try sayHello.response.wait()
print("Greeter SayHello received: \(response.object.message ?? "Unknown")")
} catch {
print("Greeter failed: \(error)")
}
let surname = builder.create(string: name)
let manyRoot = models_HelloRequest.createHelloRequest(
&builder,
nameOffset: surname)
builder.finish(offset: manyRoot)
let call = greeter.SayManyHellos(Message(builder: &builder)) { message in
print(
"Greeter SayManyHellos received: \(message.object.message ?? "Unknown")")
}
let status = try! call.status.recover { _ in .processingError }.wait()
if status.code != .ok {
print("RPC failed: \(status)")
}
}
func main(args: [String]) {
// arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
// the name sent in the request.
let arg1 = args.dropFirst(1).first
let arg2 = args.dropFirst(2).first
switch (arg1.flatMap(Int.init), arg2) {
case (.none, _):
print("Usage: PORT [NAME]")
exit(1)
case let (.some(port), name):
// Setup an `EventLoopGroup` for the connection to run on.
//
// See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
// Make sure the group is shutdown when we're done with it.
defer {
try! group.syncShutdownGracefully()
}
// Configure the channel, we're not using TLS so the connection is `insecure`.
let channel = ClientConnection.insecure(group: group)
.connect(host: "localhost", port: port)
// Close the connection when we're done with it.
defer {
try! channel.close().wait()
}
// Provide the connection to the generated client.
let greeter = models_GreeterServiceClient(channel: channel)
// Do the greeting.
greet(name: name ?? "FlatBuffers!", client: greeter)
}
}
main(args: CommandLine.arguments)

View File

@ -0,0 +1,96 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* 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.
*/
import FlatBuffers
import GRPC
import Logging
import Model
import NIO
class Greeter: models_GreeterProvider {
var interceptors: models_GreeterServerInterceptorFactoryProtocol?
let greetings: [String]
init() {
greetings = ["Hi", "Hallo", "Ciao"]
}
func SayHello(
request: Message<models_HelloRequest>,
context: StatusOnlyCallContext)
-> EventLoopFuture<Message<models_HelloReply>>
{
let recipient = request.object.name ?? "Stranger"
var builder = FlatBufferBuilder()
let off = builder.create(string: "Hello \(recipient)")
let root = models_HelloReply.createHelloReply(&builder, messageOffset: off)
builder.finish(offset: root)
return context.eventLoop
.makeSucceededFuture(Message<models_HelloReply>(builder: &builder))
}
func SayManyHellos(
request: Message<models_HelloRequest>,
context: StreamingResponseCallContext<Message<models_HelloReply>>)
-> EventLoopFuture<GRPCStatus>
{
for name in greetings {
var builder = FlatBufferBuilder()
let off = builder
.create(string: "\(name) \(request.object.name ?? "Unknown")")
let root = models_HelloReply.createHelloReply(
&builder,
messageOffset: off)
builder.finish(offset: root)
_ = context.sendResponse(Message<models_HelloReply>(builder: &builder))
}
return context.eventLoop.makeSucceededFuture(.ok)
}
}
// Quieten the logs.
LoggingSystem.bootstrap {
var handler = StreamLogHandler.standardOutput(label: $0)
handler.logLevel = .critical
return handler
}
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
try! group.syncShutdownGracefully()
}
// Create some configuration for the server:
let configuration = Server.Configuration(
target: .hostAndPort("localhost", 0),
eventLoopGroup: group,
serviceProviders: [Greeter()])
// Start the server and print its address once it has started.
let server = Server.start(configuration: configuration)
server.map {
$0.channel.localAddress
}.whenSuccess { address in
print("server started on port \(address!.port!)")
}
// Wait on the server's `onClose` future to stop the program from exiting.
_ = try server.flatMap {
$0.onClose
}.wait()

View File

@ -0,0 +1,13 @@
# TS Greeter example
The following is an example on how to run the TS grpc server. Make sure that you have `Typescript` installed
you would need to run `npm run build` or simply use `npm install && tsc`
## How to run Server:
- `npm run server`
## How to run Client:
- `npm run client 3000`

View File

@ -0,0 +1,14 @@
{
"name": "flatbuffers-js-grpc",
"version": "1.0.0",
"author": "mustii@mmk.one",
"scripts": {
"build": "npm install && tsc",
"client": "node dist/client.js",
"server": "node dist/server.js"
},
"dependencies": {
"@grpc/grpc-js": "^1.3.2",
"flatbuffers": "^2.0.0"
}
}

View File

@ -0,0 +1,34 @@
import * as grpc from '@grpc/grpc-js';
import * as flatbuffers from 'flatbuffers';
import { HelloReply } from './models/hello-reply';
import { HelloRequest } from './models/hello-request';
import { GreeterClient } from './greeter_grpc';
async function main(PORT: Number, name: string) {
const client = new GreeterClient(`localhost:${PORT}`, grpc.credentials.createInsecure());
const builder = new flatbuffers.Builder();
const offset = builder.createString(name);
const root = HelloRequest.createHelloRequest(builder, offset);
builder.finish(root);
const buffer = HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(builder.asUint8Array()));
client.SayHello(buffer, (err, response) => {
console.log(response.message());
});
const data = client.SayManyHellos(buffer, null);
data.on('data', (data) => {
console.log(data.message());
});
}
const args = process.argv.slice(2)
const PORT = Number(args[0]);
const name: string = args[1] ?? "flatbuffers";
if (PORT) {
main(PORT, name);
} else {
throw new Error("Requires a valid port number.")
}

View File

@ -0,0 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
export * as models from './models.js';

View File

@ -0,0 +1,4 @@
// automatically generated by the FlatBuffers compiler, do not modify
export { HelloReply } from './models/hello-reply.js';
export { HelloRequest } from './models/hello-request.js';

View File

@ -0,0 +1,56 @@
// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***
import * as flatbuffers from 'flatbuffers';
import { HelloReply as models_HelloReply } from './models/hello-reply';
import { HelloRequest as models_HelloRequest } from './models/hello-request';
import * as grpc from '@grpc/grpc-js';
interface IGreeterService extends grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {
SayHello: IGreeterService_ISayHello;
SayManyHellos: IGreeterService_ISayManyHellos;
}
interface IGreeterService_ISayHello extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> {
path: string; // /models.Greeter/SayHello
requestStream: boolean; // false
responseStream: boolean; // false
requestSerialize: grpc.serialize<models_HelloRequest>;
requestDeserialize: grpc.deserialize<models_HelloRequest>;
responseSerialize: grpc.serialize<models_HelloReply>;
responseDeserialize: grpc.deserialize<models_HelloReply>;
}
interface IGreeterService_ISayManyHellos extends grpc.MethodDefinition<models_HelloRequest, models_HelloReply> {
path: string; // /models.Greeter/SayManyHellos
requestStream: boolean; // false
responseStream: boolean; // true
requestSerialize: grpc.serialize<models_HelloRequest>;
requestDeserialize: grpc.deserialize<models_HelloRequest>;
responseSerialize: grpc.serialize<models_HelloReply>;
responseDeserialize: grpc.deserialize<models_HelloReply>;
}
export const GreeterService: IGreeterService;
export interface IGreeterServer extends grpc.UntypedServiceImplementation {
SayHello: grpc.handleUnaryCall<models_HelloRequest, models_HelloReply>;
SayManyHellos: grpc.handleServerStreamingCall<models_HelloRequest, models_HelloReply>;
}
export interface IGreeterClient {
SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>;
SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>;
}
export class GreeterClient extends grpc.Client implements IGreeterClient {
constructor(address: string, credentials: grpc.ChannelCredentials, options?: object);
public SayHello(request: models_HelloRequest, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
public SayHello(request: models_HelloRequest, metadata: grpc.Metadata, options: Partial<grpc.CallOptions>, callback: (error: grpc.ServiceError | null, response: models_HelloReply) => void): grpc.ClientUnaryCall;
public SayManyHellos(request: models_HelloRequest, metadata: grpc.Metadata): grpc.ClientReadableStream<models_HelloReply>;
public SayManyHellos(request: models_HelloRequest, options: Partial<grpc.CallOptions>): grpc.ClientReadableStream<models_HelloReply>;
}

View File

@ -0,0 +1,56 @@
// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***
import * as flatbuffers from 'flatbuffers';
import { HelloReply as models_HelloReply } from './models/hello-reply';
import { HelloRequest as models_HelloRequest } from './models/hello-request';
var grpc = require('@grpc/grpc-js');
function serialize_models_HelloReply(buffer_args) {
if (!(buffer_args instanceof models_HelloReply)) {
throw new Error('Expected argument of type HelloReply');
}
return Buffer.from(buffer_args.serialize());
}
function deserialize_models_HelloReply(buffer) {
return models_HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
}
function serialize_models_HelloRequest(buffer_args) {
if (!(buffer_args instanceof models_HelloRequest)) {
throw new Error('Expected argument of type HelloRequest');
}
return Buffer.from(buffer_args.serialize());
}
function deserialize_models_HelloRequest(buffer) {
return models_HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
}
var GreeterService = exports.GreeterService = {
SayHello: {
path: '/models.Greeter/SayHello',
requestStream: false,
responseStream: false,
requestType: flatbuffers.ByteBuffer,
responseType: models_HelloReply,
requestSerialize: serialize_models_HelloRequest,
requestDeserialize: deserialize_models_HelloRequest,
responseSerialize: serialize_models_HelloReply,
responseDeserialize: deserialize_models_HelloReply,
},
SayManyHellos: {
path: '/models.Greeter/SayManyHellos',
requestStream: false,
responseStream: true,
requestType: flatbuffers.ByteBuffer,
responseType: models_HelloReply,
requestSerialize: serialize_models_HelloRequest,
requestDeserialize: deserialize_models_HelloRequest,
responseSerialize: serialize_models_HelloReply,
responseDeserialize: deserialize_models_HelloReply,
},
};
exports.GreeterClient = grpc.makeGenericClientConstructor(GreeterService);

View File

@ -0,0 +1,6 @@
// automatically generated by the FlatBuffers compiler, do not modify
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
export { HelloReply } from './models/hello-reply.js';
export { HelloRequest } from './models/hello-request.js';

View File

@ -0,0 +1,60 @@
// automatically generated by the FlatBuffers compiler, do not modify
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
import * as flatbuffers from 'flatbuffers';
export class HelloReply {
bb: flatbuffers.ByteBuffer|null = null;
bb_pos = 0;
__init(i:number, bb:flatbuffers.ByteBuffer):HelloReply {
this.bb_pos = i;
this.bb = bb;
return this;
}
static getRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}
static getSizePrefixedRootAsHelloReply(bb:flatbuffers.ByteBuffer, obj?:HelloReply):HelloReply {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new HelloReply()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}
message():string|null
message(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
message(optionalEncoding?:any):string|Uint8Array|null {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
}
static startHelloReply(builder:flatbuffers.Builder) {
builder.startObject(1);
}
static addMessage(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset) {
builder.addFieldOffset(0, messageOffset, 0);
}
static endHelloReply(builder:flatbuffers.Builder):flatbuffers.Offset {
const offset = builder.endObject();
return offset;
}
static createHelloReply(builder:flatbuffers.Builder, messageOffset:flatbuffers.Offset):flatbuffers.Offset {
HelloReply.startHelloReply(builder);
HelloReply.addMessage(builder, messageOffset);
return HelloReply.endHelloReply(builder);
}
serialize():Uint8Array {
return this.bb!.bytes();
}
static deserialize(buffer: Uint8Array):HelloReply {
return HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(buffer))
}
}

View File

@ -0,0 +1,60 @@
// automatically generated by the FlatBuffers compiler, do not modify
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any, @typescript-eslint/no-non-null-assertion */
import * as flatbuffers from 'flatbuffers';
export class HelloRequest {
bb: flatbuffers.ByteBuffer|null = null;
bb_pos = 0;
__init(i:number, bb:flatbuffers.ByteBuffer):HelloRequest {
this.bb_pos = i;
this.bb = bb;
return this;
}
static getRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}
static getSizePrefixedRootAsHelloRequest(bb:flatbuffers.ByteBuffer, obj?:HelloRequest):HelloRequest {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new HelloRequest()).__init(bb.readInt32(bb.position()) + bb.position(), bb);
}
name():string|null
name(optionalEncoding:flatbuffers.Encoding):string|Uint8Array|null
name(optionalEncoding?:any):string|Uint8Array|null {
const offset = this.bb!.__offset(this.bb_pos, 4);
return offset ? this.bb!.__string(this.bb_pos + offset, optionalEncoding) : null;
}
static startHelloRequest(builder:flatbuffers.Builder) {
builder.startObject(1);
}
static addName(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset) {
builder.addFieldOffset(0, nameOffset, 0);
}
static endHelloRequest(builder:flatbuffers.Builder):flatbuffers.Offset {
const offset = builder.endObject();
return offset;
}
static createHelloRequest(builder:flatbuffers.Builder, nameOffset:flatbuffers.Offset):flatbuffers.Offset {
HelloRequest.startHelloRequest(builder);
HelloRequest.addName(builder, nameOffset);
return HelloRequest.endHelloRequest(builder);
}
serialize():Uint8Array {
return this.bb!.bytes();
}
static deserialize(buffer: Uint8Array):HelloRequest {
return HelloRequest.getRootAsHelloRequest(new flatbuffers.ByteBuffer(buffer))
}
}

View File

@ -0,0 +1,49 @@
import * as grpc from '@grpc/grpc-js';
import * as flatbuffers from 'flatbuffers';
import { HelloReply } from './models/hello-reply';
import { HelloRequest } from './models/hello-request';
import { IGreeterServer, GreeterService } from './greeter_grpc';
const greeter: IGreeterServer = {
SayHello(call: grpc.ServerUnaryCall<HelloRequest, HelloReply>, callback: grpc.sendUnaryData<HelloReply>): void {
console.log(`SayHello ${call.request.name()}`);
const builder = new flatbuffers.Builder();
const offset = builder.createString(`welcome ${call.request.name()}`);
const root = HelloReply.createHelloReply(builder, offset);
builder.finish(root);
callback(null, HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(builder.asUint8Array())));
},
async SayManyHellos(call: grpc.ServerWritableStream<HelloRequest, HelloReply>): Promise<void> {
const name = call.request.name();
console.log(`${call.request.name()} saying hi in different langagues`);
['Hi', 'Hallo', 'Ciao'].forEach(element => {
const builder = new flatbuffers.Builder();
const offset = builder.createString(`${element} ${name}`);
const root = HelloReply.createHelloReply(builder, offset);
builder.finish(root);
call.write(HelloReply.getRootAsHelloReply(new flatbuffers.ByteBuffer(builder.asUint8Array())))
});
call.end();
}
}
function serve(): void {
const PORT = 3000;
const server = new grpc.Server();
server.addService(GreeterService, greeter);
console.log(`Listening on ${PORT}`);
server.bindAsync(
`localhost:${PORT}`,
grpc.ServerCredentials.createInsecure(),
(err: Error | null, port: number) => {
if (err) {
console.error(`Server error: ${err.message}`);
} else {
console.log(`Server bound on port: ${port}`);
server.start();
}
}
);
}
serve();

View File

@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist",
"allowJs": true,
"sourceMap": true,
"strict": true,
"noImplicitAny": false,
"strictNullChecks": false,
"esModuleInterop": true,
"baseUrl": "./",
"typeRoots": ["node_modules/@types"],
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-parent</artifactId>
<version>2.0.3</version>
</parent>
<artifactId>flatbuffers-java-grpc</artifactId>
<name>${project.artifactId}</name>
<packaging>bundle</packaging>
<description>
Utilities supporting generated code for GRPC
</description>
<developers>
<developer>
<name>Wouter van Oortmerssen</name>
</developer>
<developer>
<name>Yuri Finkelstein</name>
<url>https://github.com/yfinkelstein</url>
</developer>
</developers>
<properties>
<gRPC.version>1.36.0</gRPC.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-core</artifactId>
<version>${gRPC.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,117 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* 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 com.google.flatbuffers.grpc;
import com.google.flatbuffers.Table;
import io.grpc.Drainable;
import io.grpc.KnownLength;
import io.grpc.MethodDescriptor;
import javax.annotation.Nullable;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
public class FlatbuffersUtils {
abstract public static class FBExtactor <T extends Table> {
T extract (InputStream stream) throws IOException {
if (stream instanceof KnownLength) {
int size = stream.available();
ByteBuffer buffer = ByteBuffer.allocate(size);
stream.read(buffer.array());
return extract(buffer);
} else
throw new RuntimeException("The class " + stream.getClass().getCanonicalName() + " does not extend from KnownLength ");
}
public abstract T extract(ByteBuffer buffer);
}
static class FBInputStream extends InputStream implements Drainable, KnownLength {
private final ByteBuffer buffer;
private final int size;
@Nullable private ByteArrayInputStream inputStream;
FBInputStream(ByteBuffer buffer) {
this.buffer = buffer;
this.size = buffer.remaining();
}
private void makeStreamIfNotAlready() {
if (inputStream == null)
inputStream = new ByteArrayInputStream(buffer.array(), buffer.position(), size);
}
@Override
public int drainTo(OutputStream target) throws IOException {
target.write(buffer.array(), buffer.position(), size);
return size;
}
@Override
public int read() throws IOException {
makeStreamIfNotAlready();
return inputStream.read();
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
makeStreamIfNotAlready();
if (inputStream == null) {
if (len >= size) {
System.arraycopy(buffer.array(), buffer.position(), b, off, size);
return size;
} else {
makeStreamIfNotAlready();
return inputStream.read(b, off, len);
}
} else
return inputStream.read(b, off, len);
}
@Override
public int available() throws IOException {
return inputStream == null ? size : inputStream.available();
}
}
public static <T extends Table> MethodDescriptor.Marshaller<T> marshaller(final Class<T> clazz, final FBExtactor<T> extractor) {
return new MethodDescriptor.ReflectableMarshaller<T>() {
@Override
public Class<T> getMessageClass() {
return clazz;
}
@Override
public InputStream stream(T value) {
return new FBInputStream (value.getByteBuffer());
}
@Override
public T parse(InputStream stream) {
try {
return extractor.extract(stream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
}

219
third_party/flatbuffers/grpc/pom.xml vendored Normal file
View File

@ -0,0 +1,219 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-parent</artifactId>
<packaging>pom</packaging>
<version>2.0.3</version>
<name>flatbuffers-parent</name>
<description>parent pom for flatbuffers java artifacts</description>
<properties>
<scm.url>https://github.com/google/flatbuffers</scm.url>
<scm.connection>scm:git:${scm.url}.git</scm.connection>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/google/flatbuffers/issues</url>
</issueManagement>
<developers>
<developer>
<name>Wouter van Oortmerssen</name>
</developer>
</developers>
<url>${scm.url}</url>
<scm>
<connection>${scm.connection}</connection>
<developerConnection>${scm.connection}</developerConnection>
<url>${scm.url}</url>
<tag>HEAD</tag>
</scm>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<!--
os-maven-plugin is a Maven extension/plugin that generates various useful platform-dependent
project properties normalized from ${os.detected.name} and ${os.detected.arch}.
-->
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.12</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
</plugin>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.0.1</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<artifactId>maven-gpg-plugin</artifactId>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<gpgArguments>
<arg>--pinentry-mode</arg>
<arg>loopback</arg>
</gpgArguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
</plugins>
</build>
<modules>
<!-- consider the benefits of publishing all maven artifacts in this project
<module>flatbuffers-compiler</module>
<module>flatbuffers-java</module>
-->
<module>flatbuffers-java-grpc</module>
</modules>
</project>

View File

@ -0,0 +1,14 @@
CXXFLAGS ?= -I../../../include
LDFLAGS ?=
.PHONY: all
all: server client
greeter_generated: greeter.fbs
flatc --grpc --cpp $<
server: greeter_generated server.cpp greeter.grpc.fb.cc greeter.grpc.fb.h
g++ -std=c++11 -O2 $(CXXFLAGS) $(LDFLAGS) -lgpr -lgrpc -lgrpc++ server.cpp greeter.grpc.fb.cc -o $@
client: greeter_generated client.cpp greeter.grpc.fb.cc greeter.grpc.fb.h
g++ -std=c++11 -O2 $(CXXFLAGS) $(LDFLAGS) -lgpr -lgrpc -lgrpc++ client.cpp greeter.grpc.fb.cc -o $@

View File

@ -0,0 +1,85 @@
#include "greeter.grpc.fb.h"
#include "greeter_generated.h"
#include <grpcpp/grpcpp.h>
#include <iostream>
#include <memory>
#include <string>
class GreeterClient {
public:
GreeterClient(std::shared_ptr<grpc::Channel> channel)
: stub_(Greeter::NewStub(channel)) {}
std::string SayHello(const std::string &name) {
flatbuffers::grpc::MessageBuilder mb;
auto name_offset = mb.CreateString(name);
auto request_offset = CreateHelloRequest(mb, name_offset);
mb.Finish(request_offset);
auto request_msg = mb.ReleaseMessage<HelloRequest>();
flatbuffers::grpc::Message<HelloReply> response_msg;
grpc::ClientContext context;
auto status = stub_->SayHello(&context, request_msg, &response_msg);
if (status.ok()) {
const HelloReply *response = response_msg.GetRoot();
return response->message()->str();
} else {
std::cerr << status.error_code() << ": " << status.error_message()
<< std::endl;
return "RPC failed";
}
}
void SayManyHellos(const std::string &name, int num_greetings,
std::function<void(const std::string &)> callback) {
flatbuffers::grpc::MessageBuilder mb;
auto name_offset = mb.CreateString(name);
auto request_offset =
CreateManyHellosRequest(mb, name_offset, num_greetings);
mb.Finish(request_offset);
auto request_msg = mb.ReleaseMessage<ManyHellosRequest>();
flatbuffers::grpc::Message<HelloReply> response_msg;
grpc::ClientContext context;
auto stream = stub_->SayManyHellos(&context, request_msg);
while (stream->Read(&response_msg)) {
const HelloReply *response = response_msg.GetRoot();
callback(response->message()->str());
}
auto status = stream->Finish();
if (!status.ok()) {
std::cerr << status.error_code() << ": " << status.error_message()
<< std::endl;
callback("RPC failed");
}
}
private:
std::unique_ptr<Greeter::Stub> stub_;
};
int main(int argc, char **argv) {
std::string server_address("localhost:50051");
auto channel =
grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials());
GreeterClient greeter(channel);
std::string name("world");
std::string message = greeter.SayHello(name);
std::cerr << "Greeter received: " << message << std::endl;
int num_greetings = 10;
greeter.SayManyHellos(name, num_greetings, [](const std::string &message) {
std::cerr << "Greeter received: " << message << std::endl;
});
return 0;
}

View File

@ -0,0 +1,17 @@
table HelloReply {
message:string;
}
table HelloRequest {
name:string;
}
table ManyHellosRequest {
name:string;
num_greetings:int;
}
rpc_service Greeter {
SayHello(HelloRequest):HelloReply;
SayManyHellos(ManyHellosRequest):HelloReply (streaming: "server");
}

View File

@ -0,0 +1,81 @@
#include "greeter.grpc.fb.h"
#include "greeter_generated.h"
#include <grpcpp/grpcpp.h>
#include <iostream>
#include <memory>
#include <string>
class GreeterServiceImpl final : public Greeter::Service {
virtual grpc::Status SayHello(
grpc::ServerContext *context,
const flatbuffers::grpc::Message<HelloRequest> *request_msg,
flatbuffers::grpc::Message<HelloReply> *response_msg) override {
flatbuffers::grpc::MessageBuilder mb_;
// We call GetRoot to "parse" the message. Verification is already
// performed by default. See the notes below for more details.
const HelloRequest *request = request_msg->GetRoot();
// Fields are retrieved as usual with FlatBuffers
const std::string &name = request->name()->str();
// `flatbuffers::grpc::MessageBuilder` is a `FlatBufferBuilder` with a
// special allocator for efficient gRPC buffer transfer, but otherwise
// usage is the same as usual.
auto msg_offset = mb_.CreateString("Hello, " + name);
auto hello_offset = CreateHelloReply(mb_, msg_offset);
mb_.Finish(hello_offset);
// The `ReleaseMessage<T>()` function detaches the message from the
// builder, so we can transfer the resopnse to gRPC while simultaneously
// detaching that memory buffer from the builer.
*response_msg = mb_.ReleaseMessage<HelloReply>();
assert(response_msg->Verify());
// Return an OK status.
return grpc::Status::OK;
}
virtual grpc::Status SayManyHellos(
grpc::ServerContext *context,
const flatbuffers::grpc::Message<ManyHellosRequest> *request_msg,
grpc::ServerWriter<flatbuffers::grpc::Message<HelloReply>> *writer)
override {
// The streaming usage below is simply a combination of standard gRPC
// streaming with the FlatBuffers usage shown above.
const ManyHellosRequest *request = request_msg->GetRoot();
const std::string &name = request->name()->str();
int num_greetings = request->num_greetings();
for (int i = 0; i < num_greetings; i++) {
auto msg_offset = mb_.CreateString("Many hellos, " + name);
auto hello_offset = CreateHelloReply(mb_, msg_offset);
mb_.Finish(hello_offset);
writer->Write(mb_.ReleaseMessage<HelloReply>());
}
return grpc::Status::OK;
}
flatbuffers::grpc::MessageBuilder mb_;
};
void RunServer() {
std::string server_address("0.0.0.0:50051");
GreeterServiceImpl service;
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
std::cerr << "Server listening on " << server_address << std::endl;
server->Wait();
}
int main(int argc, const char *argv[]) {
RunServer();
return 0;
}

View File

@ -0,0 +1,131 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
package(
default_visibility = ["//visibility:public"],
)
filegroup(
name = "distribution",
srcs = [
"BUILD.bazel",
] + glob([
"*.cc",
"*.h",
]),
)
filegroup(
name = "common_headers",
srcs = [
"schema_interface.h",
],
)
cc_library(
name = "cpp_generator",
srcs = [
"cpp_generator.cc",
],
hdrs = [
"cpp_generator.h",
":common_headers",
],
include_prefix = "src/compiler",
strip_include_prefix = "/grpc/src/compiler",
deps = [
"//:flatbuffers",
],
)
cc_library(
name = "go_generator",
srcs = [
"go_generator.cc",
],
hdrs = [
"go_generator.h",
":common_headers",
],
include_prefix = "src/compiler",
strip_include_prefix = "/grpc/src/compiler",
deps = [
"//:flatbuffers",
],
)
cc_library(
name = "java_generator",
srcs = [
"java_generator.cc",
],
hdrs = [
"java_generator.h",
":common_headers",
],
include_prefix = "src/compiler",
strip_include_prefix = "/grpc/src/compiler",
deps = [
"//:flatbuffers",
],
)
cc_library(
name = "python_generator",
hdrs = [
"python_generator.h",
],
include_prefix = "src/compiler",
strip_include_prefix = "/grpc/src/compiler",
deps = [
":python_generator_private",
],
)
cc_library(
name = "python_generator_private",
srcs = [
"python_generator.cc",
],
hdrs = [
"python_generator.h",
":common_headers",
],
include_prefix = "src/compiler",
strip_include_prefix = "/grpc/src/compiler",
visibility = ["//visibility:private"],
deps = [
"//:flatbuffers",
],
)
cc_library(
name = "swift_generator",
srcs = [
"swift_generator.cc",
],
hdrs = [
"swift_generator.h",
":common_headers",
],
include_prefix = "src/compiler",
strip_include_prefix = "/grpc/src/compiler",
deps = [
"//:flatbuffers",
],
)
cc_library(
name = "ts_generator",
srcs = [
"ts_generator.cc",
],
hdrs = [
"ts_generator.h",
":common_headers",
],
include_prefix = "src/compiler",
strip_include_prefix = "/grpc/src/compiler",
deps = [
"//:flatbuffers",
],
)

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,106 @@
#ifndef GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H
#define GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H
// cpp_generator.h/.cc do not directly depend on GRPC/ProtoBuf, such that they
// can be used to generate code for other serialization systems, such as
// FlatBuffers.
#include <memory>
#include <vector>
#include "src/compiler/schema_interface.h"
#ifndef GRPC_CUSTOM_STRING
# include <string>
# define GRPC_CUSTOM_STRING std::string
#endif
namespace grpc {
typedef GRPC_CUSTOM_STRING string;
} // namespace grpc
namespace grpc_cpp_generator {
// Contains all the parameters that are parsed from the command line.
struct Parameters {
// Puts the service into a namespace
grpc::string services_namespace;
// Use system includes (<>) or local includes ("")
bool use_system_headers;
// Prefix to any grpc include
grpc::string grpc_search_path;
// Generate GMOCK code to facilitate unit testing.
bool generate_mock_code;
// By default, use "_generated.h"
std::string message_header_extension;
};
// Return the prologue of the generated header file.
grpc::string GetHeaderPrologue(grpc_generator::File *file,
const Parameters &params);
// Return the includes needed for generated header file.
grpc::string GetHeaderIncludes(grpc_generator::File *file,
const Parameters &params);
// Return the includes needed for generated source file.
grpc::string GetSourceIncludes(grpc_generator::File *file,
const Parameters &params);
// Return the epilogue of the generated header file.
grpc::string GetHeaderEpilogue(grpc_generator::File *file,
const Parameters &params);
// Return the prologue of the generated source file.
grpc::string GetSourcePrologue(grpc_generator::File *file,
const Parameters &params);
// Return the services for generated header file.
grpc::string GetHeaderServices(grpc_generator::File *file,
const Parameters &params);
// Return the services for generated source file.
grpc::string GetSourceServices(grpc_generator::File *file,
const Parameters &params);
// Return the epilogue of the generated source file.
grpc::string GetSourceEpilogue(grpc_generator::File *file,
const Parameters &params);
// Return the prologue of the generated mock file.
grpc::string GetMockPrologue(grpc_generator::File *file,
const Parameters &params);
// Return the includes needed for generated mock file.
grpc::string GetMockIncludes(grpc_generator::File *file,
const Parameters &params);
// Return the services for generated mock file.
grpc::string GetMockServices(grpc_generator::File *file,
const Parameters &params);
// Return the epilogue of generated mock file.
grpc::string GetMockEpilogue(grpc_generator::File *file,
const Parameters &params);
// Return the prologue of the generated mock file.
grpc::string GetMockPrologue(grpc_generator::File *file,
const Parameters &params);
// Return the includes needed for generated mock file.
grpc::string GetMockIncludes(grpc_generator::File *file,
const Parameters &params);
// Return the services for generated mock file.
grpc::string GetMockServices(grpc_generator::File *file,
const Parameters &params);
// Return the epilogue of generated mock file.
grpc::string GetMockEpilogue(grpc_generator::File *file,
const Parameters &params);
} // namespace grpc_cpp_generator
#endif // GRPC_INTERNAL_COMPILER_CPP_GENERATOR_H

View File

@ -0,0 +1,507 @@
#include "src/compiler/go_generator.h"
#include <cctype>
#include <map>
#include <sstream>
template<class T> grpc::string as_string(T x) {
std::ostringstream out;
out << x;
return out.str();
}
inline bool ClientOnlyStreaming(const grpc_generator::Method *method) {
return method->ClientStreaming() && !method->ServerStreaming();
}
inline bool ServerOnlyStreaming(const grpc_generator::Method *method) {
return !method->ClientStreaming() && method->ServerStreaming();
}
namespace grpc_go_generator {
namespace {
// Returns string with first letter to lowerCase
static grpc::string unexportName(grpc::string s) {
if (s.empty()) return s;
s[0] = static_cast<char>(std::tolower(s[0]));
return s;
}
// Returns string with first letter to uppercase
static grpc::string exportName(grpc::string s) {
if (s.empty()) return s;
s[0] = static_cast<char>(std::toupper(s[0]));
return s;
}
static void GenerateError(grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> vars,
const bool multiple_return = true) {
printer->Print(vars, "if $Error_Check$ {\n");
printer->Indent();
vars["Return"] = multiple_return ? "nil, err" : "err";
printer->Print(vars, "return $Return$\n");
printer->Outdent();
printer->Print("}\n");
}
// Generates imports for the service
static void GenerateImports(grpc_generator::File *file,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> vars) {
vars["filename"] = file->filename();
printer->Print("//Generated by gRPC Go plugin\n");
printer->Print("//If you make any local changes, they will be lost\n");
printer->Print(vars, "//source: $filename$\n\n");
printer->Print(vars, "package $Package$\n\n");
printer->Print("import (\n");
printer->Indent();
printer->Print(vars, "$context$ \"context\"\n");
printer->Print("flatbuffers \"github.com/google/flatbuffers/go\"\n");
printer->Print(vars, "$grpc$ \"google.golang.org/grpc\"\n");
printer->Print("\"google.golang.org/grpc/codes\"\n");
printer->Print("\"google.golang.org/grpc/status\"\n");
printer->Outdent();
printer->Print(")\n\n");
}
// Generates Server method signature source
static void GenerateServerMethodSignature(const grpc_generator::Method *method,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> vars) {
vars["Method"] = exportName(method->name());
vars["Request"] = method->get_input_type_name();
vars["Response"] = (vars["CustomMethodIO"] == "")
? method->get_output_type_name()
: vars["CustomMethodIO"];
if (method->NoStreaming()) {
printer->Print(
vars,
"$Method$($context$.Context, *$Request$) (*$Response$, error)$Ending$");
} else if (ServerOnlyStreaming(method)) {
printer->Print(
vars, "$Method$(*$Request$, $Service$_$Method$Server) error$Ending$");
} else {
printer->Print(vars, "$Method$($Service$_$Method$Server) error$Ending$");
}
}
static void GenerateServerMethod(const grpc_generator::Method *method,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> vars) {
vars["Method"] = exportName(method->name());
vars["Request"] = method->get_input_type_name();
vars["Response"] = (vars["CustomMethodIO"] == "")
? method->get_output_type_name()
: vars["CustomMethodIO"];
vars["FullMethodName"] =
"/" + vars["ServicePrefix"] + vars["Service"] + "/" + vars["Method"];
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
if (method->NoStreaming()) {
printer->Print(
vars,
"func $Handler$(srv interface{}, ctx $context$.Context,\n\tdec "
"func(interface{}) error, interceptor $grpc$.UnaryServerInterceptor) "
"(interface{}, error) {\n");
printer->Indent();
printer->Print(vars, "in := new($Request$)\n");
vars["Error_Check"] = "err := dec(in); err != nil";
GenerateError(printer, vars);
printer->Print("if interceptor == nil {\n");
printer->Indent();
printer->Print(vars, "return srv.($Service$Server).$Method$(ctx, in)\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(vars, "info := &$grpc$.UnaryServerInfo{\n");
printer->Indent();
printer->Print("Server: srv,\n");
printer->Print(vars, "FullMethod: \"$FullMethodName$\",\n");
printer->Outdent();
printer->Print("}\n");
printer->Outdent();
printer->Print("\n");
printer->Indent();
printer->Print(vars,
"handler := func(ctx $context$.Context, req interface{}) "
"(interface{}, error) {\n");
printer->Indent();
printer->Print(
vars, "return srv.($Service$Server).$Method$(ctx, req.(*$Request$))\n");
printer->Outdent();
printer->Print("}\n");
printer->Print("return interceptor(ctx, in, info, handler)\n");
printer->Outdent();
printer->Print("}\n");
return;
}
vars["StreamType"] = vars["ServiceUnexported"] + vars["Method"] + "Server";
printer->Print(
vars,
"func $Handler$(srv interface{}, stream $grpc$.ServerStream) error {\n");
printer->Indent();
if (ServerOnlyStreaming(method)) {
printer->Print(vars, "m := new($Request$)\n");
vars["Error_Check"] = "err := stream.RecvMsg(m); err != nil";
GenerateError(printer, vars, false);
printer->Print(
vars,
"return srv.($Service$Server).$Method$(m, &$StreamType${stream})\n");
} else {
printer->Print(
vars, "return srv.($Service$Server).$Method$(&$StreamType${stream})\n");
}
printer->Outdent();
printer->Print("}\n\n");
bool genSend = method->BidiStreaming() || ServerOnlyStreaming(method);
bool genRecv = method->BidiStreaming() || ClientOnlyStreaming(method);
bool genSendAndClose = ClientOnlyStreaming(method);
printer->Print(vars, "type $Service$_$Method$Server interface {\n");
printer->Indent();
if (genSend) { printer->Print(vars, "Send(*$Response$) error\n"); }
if (genRecv) { printer->Print(vars, "Recv() (*$Request$, error)\n"); }
if (genSendAndClose) {
printer->Print(vars, "SendAndClose(*$Response$) error\n");
}
printer->Print(vars, "$grpc$.ServerStream\n");
printer->Outdent();
printer->Print("}\n\n");
printer->Print(vars, "type $StreamType$ struct {\n");
printer->Indent();
printer->Print(vars, "$grpc$.ServerStream\n");
printer->Outdent();
printer->Print("}\n\n");
if (genSend) {
printer->Print(vars,
"func (x *$StreamType$) Send(m *$Response$) error {\n");
printer->Indent();
printer->Print("return x.ServerStream.SendMsg(m)\n");
printer->Outdent();
printer->Print("}\n\n");
}
if (genRecv) {
printer->Print(vars,
"func (x *$StreamType$) Recv() (*$Request$, error) {\n");
printer->Indent();
printer->Print(vars, "m := new($Request$)\n");
vars["Error_Check"] = "err := x.ServerStream.RecvMsg(m); err != nil";
GenerateError(printer, vars);
printer->Print("return m, nil\n");
printer->Outdent();
printer->Print("}\n\n");
}
if (genSendAndClose) {
printer->Print(
vars, "func (x *$StreamType$) SendAndClose(m *$Response$) error {\n");
printer->Indent();
printer->Print("return x.ServerStream.SendMsg(m)\n");
printer->Outdent();
printer->Print("}\n\n");
}
}
// Generates Client method signature source
static void GenerateClientMethodSignature(const grpc_generator::Method *method,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> vars) {
vars["Method"] = exportName(method->name());
vars["Request"] =
", in *" + ((vars["CustomMethodIO"] == "") ? method->get_input_type_name()
: vars["CustomMethodIO"]);
if (ClientOnlyStreaming(method) || method->BidiStreaming()) {
vars["Request"] = "";
}
vars["Response"] = "*" + method->get_output_type_name();
if (ClientOnlyStreaming(method) || method->BidiStreaming() ||
ServerOnlyStreaming(method)) {
vars["Response"] = vars["Service"] + "_" + vars["Method"] + "Client";
}
printer->Print(vars,
"$Method$(ctx $context$.Context$Request$,\n\topts "
"...$grpc$.CallOption) ($Response$, error)$Ending$");
}
// Generates Client method source
static void GenerateClientMethod(const grpc_generator::Method *method,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> vars) {
printer->Print(vars, "func (c *$ServiceUnexported$Client) ");
vars["Ending"] = " {\n";
GenerateClientMethodSignature(method, printer, vars);
printer->Indent();
vars["Method"] = exportName(method->name());
vars["Request"] = (vars["CustomMethodIO"] == "")
? method->get_input_type_name()
: vars["CustomMethodIO"];
vars["Response"] = method->get_output_type_name();
vars["FullMethodName"] =
"/" + vars["ServicePrefix"] + vars["Service"] + "/" + vars["Method"];
if (method->NoStreaming()) {
printer->Print(vars, "out := new($Response$)\n");
printer->Print(
vars,
"err := c.cc.Invoke(ctx, \"$FullMethodName$\", in, out, opts...)\n");
vars["Error_Check"] = "err != nil";
GenerateError(printer, vars);
printer->Print("return out, nil\n");
printer->Outdent();
printer->Print("}\n\n");
return;
}
vars["StreamType"] = vars["ServiceUnexported"] + vars["Method"] + "Client";
printer->Print(vars,
"stream, err := c.cc.NewStream(ctx, &$MethodDesc$, "
"\"$FullMethodName$\", opts...)\n");
vars["Error_Check"] = "err != nil";
GenerateError(printer, vars);
printer->Print(vars, "x := &$StreamType${stream}\n");
if (ServerOnlyStreaming(method)) {
vars["Error_Check"] = "err := x.ClientStream.SendMsg(in); err != nil";
GenerateError(printer, vars);
vars["Error_Check"] = "err := x.ClientStream.CloseSend(); err != nil";
GenerateError(printer, vars);
}
printer->Print("return x, nil\n");
printer->Outdent();
printer->Print("}\n\n");
bool genSend = method->BidiStreaming() || ClientOnlyStreaming(method);
bool genRecv = method->BidiStreaming() || ServerOnlyStreaming(method);
bool genCloseAndRecv = ClientOnlyStreaming(method);
// Stream interface
printer->Print(vars, "type $Service$_$Method$Client interface {\n");
printer->Indent();
if (genSend) { printer->Print(vars, "Send(*$Request$) error\n"); }
if (genRecv) { printer->Print(vars, "Recv() (*$Response$, error)\n"); }
if (genCloseAndRecv) {
printer->Print(vars, "CloseAndRecv() (*$Response$, error)\n");
}
printer->Print(vars, "$grpc$.ClientStream\n");
printer->Outdent();
printer->Print("}\n\n");
// Stream Client
printer->Print(vars, "type $StreamType$ struct {\n");
printer->Indent();
printer->Print(vars, "$grpc$.ClientStream\n");
printer->Outdent();
printer->Print("}\n\n");
if (genSend) {
printer->Print(vars, "func (x *$StreamType$) Send(m *$Request$) error {\n");
printer->Indent();
printer->Print("return x.ClientStream.SendMsg(m)\n");
printer->Outdent();
printer->Print("}\n\n");
}
if (genRecv) {
printer->Print(vars,
"func (x *$StreamType$) Recv() (*$Response$, error) {\n");
printer->Indent();
printer->Print(vars, "m := new($Response$)\n");
vars["Error_Check"] = "err := x.ClientStream.RecvMsg(m); err != nil";
GenerateError(printer, vars);
printer->Print("return m, nil\n");
printer->Outdent();
printer->Print("}\n\n");
}
if (genCloseAndRecv) {
printer->Print(
vars, "func (x *$StreamType$) CloseAndRecv() (*$Response$, error) {\n");
printer->Indent();
vars["Error_Check"] = "err := x.ClientStream.CloseSend(); err != nil";
GenerateError(printer, vars);
printer->Print(vars, "m := new($Response$)\n");
vars["Error_Check"] = "err := x.ClientStream.RecvMsg(m); err != nil";
GenerateError(printer, vars);
printer->Print("return m, nil\n");
printer->Outdent();
printer->Print("}\n\n");
}
}
// Generates client API for the service
void GenerateService(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> vars) {
vars["Service"] = exportName(service->name());
// Client Interface
printer->Print(vars, "// Client API for $Service$ service\n");
printer->Print(vars, "type $Service$Client interface {\n");
printer->Indent();
vars["Ending"] = "\n";
for (int i = 0; i < service->method_count(); i++) {
GenerateClientMethodSignature(service->method(i).get(), printer, vars);
}
printer->Outdent();
printer->Print("}\n\n");
// Client structure
vars["ServiceUnexported"] = unexportName(vars["Service"]);
printer->Print(vars, "type $ServiceUnexported$Client struct {\n");
printer->Indent();
printer->Print(vars, "cc $grpc$.ClientConnInterface\n");
printer->Outdent();
printer->Print("}\n\n");
// NewClient
printer->Print(vars,
"func New$Service$Client(cc $grpc$.ClientConnInterface) "
"$Service$Client {\n");
printer->Indent();
printer->Print(vars, "return &$ServiceUnexported$Client{cc}");
printer->Outdent();
printer->Print("\n}\n\n");
int unary_methods = 0, streaming_methods = 0;
vars["ServiceDesc"] = "_" + vars["Service"] + "_serviceDesc";
for (int i = 0; i < service->method_count(); i++) {
auto method = service->method(i);
if (method->NoStreaming()) {
vars["MethodDesc"] =
vars["ServiceDesc"] + ".Method[" + as_string(unary_methods) + "]";
unary_methods++;
} else {
vars["MethodDesc"] = vars["ServiceDesc"] + ".Streams[" +
as_string(streaming_methods) + "]";
streaming_methods++;
}
GenerateClientMethod(method.get(), printer, vars);
}
// Server Interface
printer->Print(vars, "// Server API for $Service$ service\n");
printer->Print(vars, "type $Service$Server interface {\n");
printer->Indent();
vars["Ending"] = "\n";
for (int i = 0; i < service->method_count(); i++) {
GenerateServerMethodSignature(service->method(i).get(), printer, vars);
}
printer->Print(vars, "mustEmbedUnimplemented$Service$Server()\n");
printer->Outdent();
printer->Print("}\n\n");
printer->Print(vars, "type Unimplemented$Service$Server struct {\n");
printer->Print("}\n\n");
vars["Ending"] = " {\n";
for (int i = 0; i < service->method_count(); i++) {
auto method = service->method(i);
vars["Method"] = exportName(method->name());
vars["Nil"] = method->NoStreaming() ? "nil, " : "";
printer->Print(vars, "func (Unimplemented$Service$Server) ");
GenerateServerMethodSignature(method.get(), printer, vars);
printer->Indent();
printer->Print(vars,
"return $Nil$status.Errorf(codes.Unimplemented, \"method "
"$Method$ not implemented\")\n");
printer->Outdent();
printer->Print("}\n");
printer->Print("\n");
}
printer->Print(vars,
"func (Unimplemented$Service$Server) "
"mustEmbedUnimplemented$Service$Server() {}");
printer->Print("\n\n");
printer->Print(vars, "type Unsafe$Service$Server interface {\n");
printer->Indent();
printer->Print(vars, "mustEmbedUnimplemented$Service$Server()\n");
printer->Outdent();
printer->Print("}\n\n");
// Server registration.
printer->Print(vars,
"func Register$Service$Server(s $grpc$.ServiceRegistrar, srv "
"$Service$Server) {\n");
printer->Indent();
printer->Print(vars, "s.RegisterService(&$ServiceDesc$, srv)\n");
printer->Outdent();
printer->Print("}\n\n");
for (int i = 0; i < service->method_count(); i++) {
GenerateServerMethod(service->method(i).get(), printer, vars);
}
// Service Descriptor
printer->Print(vars, "var $ServiceDesc$ = $grpc$.ServiceDesc{\n");
printer->Indent();
printer->Print(vars, "ServiceName: \"$ServicePrefix$$Service$\",\n");
printer->Print(vars, "HandlerType: (*$Service$Server)(nil),\n");
printer->Print(vars, "Methods: []$grpc$.MethodDesc{\n");
printer->Indent();
for (int i = 0; i < service->method_count(); i++) {
auto method = service->method(i);
vars["Method"] = exportName(method->name());
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
if (method->NoStreaming()) {
printer->Print("{\n");
printer->Indent();
printer->Print(vars, "MethodName: \"$Method$\",\n");
printer->Print(vars, "Handler: $Handler$,\n");
printer->Outdent();
printer->Print("},\n");
}
}
printer->Outdent();
printer->Print("},\n");
printer->Print(vars, "Streams: []$grpc$.StreamDesc{\n");
printer->Indent();
for (int i = 0; i < service->method_count(); i++) {
auto method = service->method(i);
vars["Method"] = exportName(method->name());
vars["Handler"] = "_" + vars["Service"] + "_" + vars["Method"] + "_Handler";
if (!method->NoStreaming()) {
printer->Print("{\n");
printer->Indent();
printer->Print(vars, "StreamName: \"$Method$\",\n");
printer->Print(vars, "Handler: $Handler$,\n");
if (ClientOnlyStreaming(method.get())) {
printer->Print("ClientStreams: true,\n");
} else if (ServerOnlyStreaming(method.get())) {
printer->Print("ServerStreams: true,\n");
} else {
printer->Print("ServerStreams: true,\n");
printer->Print("ClientStreams: true,\n");
}
printer->Outdent();
printer->Print("},\n");
}
}
printer->Outdent();
printer->Print("},\n");
printer->Outdent();
printer->Print("}\n");
}
} // namespace
// Returns source for the service
grpc::string GenerateServiceSource(grpc_generator::File *file,
const grpc_generator::Service *service,
grpc_go_generator::Parameters *parameters) {
grpc::string out;
auto p = file->CreatePrinter(&out, '\t');
p->SetIndentationSize(1);
auto printer = p.get();
std::map<grpc::string, grpc::string> vars;
vars["Package"] = parameters->package_name;
vars["ServicePrefix"] = parameters->service_prefix;
if (!parameters->service_prefix.empty()) vars["ServicePrefix"].append(".");
vars["grpc"] = "grpc";
vars["context"] = "context";
GenerateImports(file, printer, vars);
if (parameters->custom_method_io_type != "") {
vars["CustomMethodIO"] = parameters->custom_method_io_type;
}
GenerateService(service, printer, vars);
return out;
}
} // Namespace grpc_go_generator

View File

@ -0,0 +1,33 @@
#ifndef GRPC_INTERNAL_COMPILER_GO_GENERATOR_H
#define GRPC_INTERNAL_COMPILER_GO_GENERATOR_H
// go generator is used to generate GRPC code for serialization system, such as
// flatbuffers
#include <memory>
#include <vector>
#include "src/compiler/schema_interface.h"
namespace grpc_go_generator {
struct Parameters {
// Defines the custom parameter types for methods
// eg: flatbuffers uses flatbuffers.Builder as input for the client and output
// for the server
grpc::string custom_method_io_type;
// Package name for the service
grpc::string package_name;
// Prefix for RPC Calls
grpc::string service_prefix;
};
// Return the source of the generated service file.
grpc::string GenerateServiceSource(grpc_generator::File *file,
const grpc_generator::Service *service,
grpc_go_generator::Parameters *parameters);
} // namespace grpc_go_generator
#endif // GRPC_INTERNAL_COMPILER_GO_GENERATOR_H

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,85 @@
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* 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.
*/
#ifndef NET_GRPC_COMPILER_JAVA_GENERATOR_H_
#define NET_GRPC_COMPILER_JAVA_GENERATOR_H_
#include <stdlib.h> // for abort()
#include <iostream>
#include <map>
#include <string>
#include "src/compiler/schema_interface.h"
class LogMessageVoidify {
public:
LogMessageVoidify() {}
// This has to be an operator with a precedence lower than << but
// higher than ?:
void operator&(std::ostream&) {}
};
class LogHelper {
std::ostream* os_;
public:
LogHelper(std::ostream* os) : os_(os) {}
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning( \
disable : 4722) // the flow of control terminates in a destructor
// (needed to compile ~LogHelper where destructor emits abort intentionally -
// inherited from grpc/java code generator).
#endif
~LogHelper() {
*os_ << std::endl;
::abort();
}
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
std::ostream& get_os() const { return *os_; }
};
// Abort the program after logging the mesage if the given condition is not
// true. Otherwise, do nothing.
#define GRPC_CODEGEN_CHECK(x) \
(x) ? (void)0 \
: LogMessageVoidify() & LogHelper(&std::cerr).get_os() \
<< "CHECK FAILED: " << __FILE__ << ":" \
<< __LINE__ << ": "
// Abort the program after logging the mesage.
#define GRPC_CODEGEN_FAIL GRPC_CODEGEN_CHECK(false)
namespace grpc_java_generator {
struct Parameters {
// //Defines the custom parameter types for methods
// //eg: flatbuffers uses flatbuffers.Builder as input for the client
// and output for the server grpc::string custom_method_io_type;
// Package name for the service
grpc::string package_name;
};
// Return the source of the generated service file.
grpc::string GenerateServiceSource(grpc_generator::File* file,
const grpc_generator::Service* service,
grpc_java_generator::Parameters* parameters);
} // namespace grpc_java_generator
#endif // NET_GRPC_COMPILER_JAVA_GENERATOR_H_

View File

@ -0,0 +1,151 @@
/*
*
* Copyright 2015 gRPC authors.
*
* 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.
*
*/
#include <map>
#include <sstream>
#include "flatbuffers/util.h"
#include "src/compiler/python_generator.h"
namespace grpc_python_generator {
namespace {
static grpc::string GenerateMethodType(const grpc_generator::Method *method) {
if (method->NoStreaming())
return "unary_unary";
if (method->ServerStreaming())
return "unary_stream";
if (method->ClientStreaming())
return "stream_unary";
return "stream_stream";
}
grpc::string GenerateMethodInput(const grpc_generator::Method *method) {
if (method->NoStreaming() || method->ServerStreaming())
return "self, request, context";
return "self, request_iterator, context";
}
void GenerateStub(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars, "class $ServiceName$Stub(object):\n");
printer->Indent();
printer->Print("\"\"\" Interface exported by the server. \"\"\"");
printer->Print("\n\n");
printer->Print("def __init__(self, channel):\n");
printer->Indent();
printer->Print("\"\"\" Constructor. \n\n");
printer->Print("Args: \n");
printer->Print("channel: A grpc.Channel. \n");
printer->Print("\"\"\"\n\n");
for (int j = 0; j < service->method_count(); j++) {
auto method = service->method(j);
vars["MethodName"] = method->name();
vars["MethodType"] = GenerateMethodType(&*method);
printer->Print(vars, "self.$MethodName$ = channel.$MethodType$(\n");
printer->Indent();
printer->Print(vars, "\"/$PATH$$ServiceName$/$MethodName$\"\n");
printer->Print(")\n");
printer->Outdent();
printer->Print("\n");
}
printer->Outdent();
printer->Outdent();
printer->Print("\n");
}
void GenerateServicer(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars, "class $ServiceName$Servicer(object):\n");
printer->Indent();
printer->Print("\"\"\" Interface exported by the server. \"\"\"");
printer->Print("\n\n");
for (int j = 0; j < service->method_count(); j++) {
auto method = service->method(j);
vars["MethodName"] = method->name();
vars["MethodInput"] = GenerateMethodInput(&*method);
printer->Print(vars, "def $MethodName$($MethodInput$):\n");
printer->Indent();
printer->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n");
printer->Print("context.set_details('Method not implemented!')\n");
printer->Print("raise NotImplementedError('Method not implemented!')\n");
printer->Outdent();
printer->Print("\n\n");
}
printer->Outdent();
printer->Print("\n");
}
void GenerateRegister(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars, "def add_$ServiceName$Servicer_to_server(servicer, server):\n");
printer->Indent();
printer->Print("rpc_method_handlers = {\n");
printer->Indent();
for (int j = 0; j < service->method_count(); j++) {
auto method = service->method(j);
vars["MethodName"] = method->name();
vars["MethodType"] = GenerateMethodType(&*method);
printer->Print(vars, "'$MethodName$': grpc.$MethodType$_rpc_method_handler(\n");
printer->Indent();
printer->Print(vars, "servicer.$MethodName$\n");
printer->Outdent();
printer->Print("),\n");
}
printer->Outdent();
printer->Print("}\n");
printer->Print(vars, "generic_handler = grpc.method_handlers_generic_handler(\n");
printer->Indent();
printer->Print(vars, "'$PATH$$ServiceName$', rpc_method_handlers)\n");
printer->Outdent();
printer->Print("server.add_generic_rpc_handlers((generic_handler,))");
printer->Outdent();
printer->Print("\n");
}
} // namespace
grpc::string Generate(grpc_generator::File *file,
const grpc_generator::Service *service) {
grpc::string output;
std::map<grpc::string, grpc::string> vars;
vars["PATH"] = file->package();
if (!file->package().empty()) { vars["PATH"].append("."); }
vars["ServiceName"] = service->name();
auto printer = file->CreatePrinter(&output);
GenerateStub(service, &*printer, &vars);
GenerateServicer(service, &*printer, &vars);
GenerateRegister(service, &*printer, &vars);
return output;
}
} // namespace grpc_python_generator

View File

@ -0,0 +1,32 @@
/*
*
* Copyright 2015 gRPC authors.
*
* 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.
*
*/
#ifndef GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_H
#define GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_H
#include <utility>
#include "src/compiler/schema_interface.h"
namespace grpc_python_generator {
grpc::string Generate(grpc_generator::File *file,
const grpc_generator::Service *service);
} // namespace grpc_python_generator
#endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_H

View File

@ -0,0 +1,119 @@
/*
*
* Copyright 2015 gRPC authors.
*
* 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.
*
*/
#ifndef GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H
#define GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H
#include <map>
#include <memory>
#include <vector>
#ifndef GRPC_CUSTOM_STRING
# include <string>
# define GRPC_CUSTOM_STRING std::string
#endif
namespace grpc {
typedef GRPC_CUSTOM_STRING string;
} // namespace grpc
namespace grpc_generator {
// A common interface for objects having comments in the source.
// Return formatted comments to be inserted in generated code.
struct CommentHolder {
virtual ~CommentHolder() {}
virtual grpc::string GetLeadingComments(const grpc::string prefix) const = 0;
virtual grpc::string GetTrailingComments(const grpc::string prefix) const = 0;
virtual std::vector<grpc::string> GetAllComments() const = 0;
};
// An abstract interface representing a method.
struct Method : public CommentHolder {
virtual ~Method() {}
virtual grpc::string name() const = 0;
virtual grpc::string input_type_name() const = 0;
virtual grpc::string output_type_name() const = 0;
virtual bool get_module_and_message_path_input(
grpc::string *str, grpc::string generator_file_name,
bool generate_in_pb2_grpc, grpc::string import_prefix) const = 0;
virtual bool get_module_and_message_path_output(
grpc::string *str, grpc::string generator_file_name,
bool generate_in_pb2_grpc, grpc::string import_prefix) const = 0;
virtual std::vector<grpc::string> get_input_namespace_parts() const = 0;
virtual grpc::string get_input_type_name() const = 0;
virtual std::vector<grpc::string> get_output_namespace_parts() const = 0;
virtual grpc::string get_output_type_name() const = 0;
virtual grpc::string get_fb_builder() const = 0;
virtual bool NoStreaming() const = 0;
virtual bool ClientStreaming() const = 0;
virtual bool ServerStreaming() const = 0;
virtual bool BidiStreaming() const = 0;
};
// An abstract interface representing a service.
struct Service : public CommentHolder {
virtual ~Service() {}
virtual std::vector<grpc::string> namespace_parts() const = 0;
virtual grpc::string name() const = 0;
virtual bool is_internal() const = 0;
virtual int method_count() const = 0;
virtual std::unique_ptr<const Method> method(int i) const = 0;
};
struct Printer {
virtual ~Printer() {}
virtual void Print(const std::map<grpc::string, grpc::string> &vars,
const char *template_string) = 0;
virtual void Print(const char *string) = 0;
virtual void SetIndentationSize(const size_t size) = 0;
virtual void Indent() = 0;
virtual void Outdent() = 0;
};
// An interface that allows the source generated to be output using various
// libraries/idls/serializers.
struct File : public CommentHolder {
virtual ~File() {}
virtual grpc::string filename() const = 0;
virtual grpc::string filename_without_ext() const = 0;
virtual grpc::string package() const = 0;
virtual std::vector<grpc::string> package_parts() const = 0;
virtual grpc::string additional_headers() const = 0;
virtual int service_count() const = 0;
virtual std::unique_ptr<const Service> service(int i) const = 0;
virtual std::unique_ptr<Printer> CreatePrinter(
grpc::string *str, const char indentation_type = ' ') const = 0;
};
} // namespace grpc_generator
#endif // GRPC_INTERNAL_COMPILER_SCHEMA_INTERFACE_H

View File

@ -0,0 +1,440 @@
/*
* Copyright 2020 Google Inc. All rights reserved.
*
* 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.
*/
/*
* NOTE: The following implementation is a translation for the Swift-grpc
* generator since flatbuffers doesnt allow plugins for now. if an issue arises
* please open an issue in the flatbuffers repository. This file should always
* be maintained according to the Swift-grpc repository
*/
#include <map>
#include <sstream>
#include "flatbuffers/util.h"
#include "src/compiler/schema_interface.h"
#include "src/compiler/swift_generator.h"
namespace grpc_swift_generator {
namespace {
static std::string WrapInNameSpace(const std::vector<std::string> &components,
const grpc::string &name) {
std::string qualified_name;
for (auto it = components.begin(); it != components.end(); ++it)
qualified_name += *it + "_";
return qualified_name + name;
}
static grpc::string GenerateMessage(const std::vector<std::string> &components,
const grpc::string &name) {
return "Message<" + WrapInNameSpace(components, name) + ">";
}
// MARK: - Client
static void GenerateClientFuncName(const grpc_generator::Method *method,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
if (method->NoStreaming()) {
printer->Print(vars,
" $GenAccess$func $MethodName$(\n"
" _ request: $Input$\n"
" , callOptions: CallOptions?$isNil$\n"
" ) -> UnaryCall<$Input$, $Output$>");
return;
}
if (method->ServerStreaming()) {
printer->Print(vars,
" $GenAccess$func $MethodName$(\n"
" _ request: $Input$\n"
" , callOptions: CallOptions?$isNil$,\n"
" handler: @escaping ($Output$) -> Void\n"
" ) -> ServerStreamingCall<$Input$, $Output$>");
return;
}
if (method->ClientStreaming()) {
printer->Print(vars,
" $GenAccess$func $MethodName$(\n"
" callOptions: CallOptions?$isNil$\n"
" ) -> ClientStreamingCall<$Input$, $Output$>");
return;
}
printer->Print(vars,
" $GenAccess$func $MethodName$(\n"
" callOptions: CallOptions?$isNil$,\n"
" handler: @escaping ($Output$ ) -> Void\n"
" ) -> BidirectionalStreamingCall<$Input$, $Output$>");
}
static void GenerateClientFuncBody(const grpc_generator::Method *method,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
vars["Interceptor"] =
"interceptors: self.interceptors?.make$MethodName$Interceptors() ?? []";
if (method->NoStreaming()) {
printer->Print(
vars,
" return self.makeUnaryCall(\n"
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
" request: request,\n"
" callOptions: callOptions ?? self.defaultCallOptions,\n"
" $Interceptor$\n"
" )\n");
return;
}
if (method->ServerStreaming()) {
printer->Print(
vars,
" return self.makeServerStreamingCall(\n"
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
" request: request,\n"
" callOptions: callOptions ?? self.defaultCallOptions,\n"
" $Interceptor$,\n"
" handler: handler\n"
" )\n");
return;
}
if (method->ClientStreaming()) {
printer->Print(
vars,
" return self.makeClientStreamingCall(\n"
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
" callOptions: callOptions ?? self.defaultCallOptions,\n"
" $Interceptor$\n"
" )\n");
return;
}
printer->Print(vars,
" return self.makeBidirectionalStreamingCall(\n"
" path: \"/$PATH$$ServiceName$/$MethodName$\",\n"
" callOptions: callOptions ?? self.defaultCallOptions,\n"
" $Interceptor$,\n"
" handler: handler\n"
" )\n");
}
void GenerateClientProtocol(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(
vars,
"$ACCESS$ protocol $ServiceQualifiedName$ClientProtocol: GRPCClient {");
printer->Print("\n\n");
printer->Print(" var serviceName: String { get }");
printer->Print("\n\n");
printer->Print(
vars,
" var interceptors: "
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol? { get }");
printer->Print("\n\n");
vars["GenAccess"] = "";
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
method->get_input_type_name());
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
method->get_output_type_name());
vars["MethodName"] = method->name();
vars["isNil"] = "";
GenerateClientFuncName(method.get(), &*printer, &vars);
printer->Print("\n\n");
}
printer->Print("}\n\n");
printer->Print(vars, "extension $ServiceQualifiedName$ClientProtocol {");
printer->Print("\n\n");
printer->Print(vars,
" $ACCESS$ var serviceName: String { "
"\"$PATH$$ServiceName$\" }\n");
vars["GenAccess"] = service->is_internal() ? "internal " : "public ";
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
method->get_input_type_name());
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
method->get_output_type_name());
vars["MethodName"] = method->name();
vars["isNil"] = " = nil";
printer->Print("\n");
GenerateClientFuncName(method.get(), &*printer, &vars);
printer->Print(" {\n");
GenerateClientFuncBody(method.get(), &*printer, &vars);
printer->Print(" }\n");
}
printer->Print("}\n\n");
printer->Print(vars,
"$ACCESS$ protocol "
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol {\n");
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
method->get_input_type_name());
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
method->get_output_type_name());
vars["MethodName"] = method->name();
printer->Print(
vars,
" /// - Returns: Interceptors to use when invoking '$MethodName$'.\n");
printer->Print(vars,
" func make$MethodName$Interceptors() -> "
"[ClientInterceptor<$Input$, $Output$>]\n\n");
}
printer->Print("}\n\n");
}
void GenerateClientClass(grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars,
"$ACCESS$ final class $ServiceQualifiedName$ServiceClient: "
"$ServiceQualifiedName$ClientProtocol {\n");
printer->Print(vars, " $ACCESS$ let channel: GRPCChannel\n");
printer->Print(vars, " $ACCESS$ var defaultCallOptions: CallOptions\n");
printer->Print(vars,
" $ACCESS$ var interceptors: "
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol?\n");
printer->Print("\n");
printer->Print(
vars,
" $ACCESS$ init(\n"
" channel: GRPCChannel,\n"
" defaultCallOptions: CallOptions = CallOptions(),\n"
" interceptors: "
"$ServiceQualifiedName$ClientInterceptorFactoryProtocol? = nil\n"
" ) {\n");
printer->Print(" self.channel = channel\n");
printer->Print(" self.defaultCallOptions = defaultCallOptions\n");
printer->Print(" self.interceptors = interceptors\n");
printer->Print(" }");
printer->Print("\n");
printer->Print("}\n");
}
// MARK: - Server
grpc::string GenerateServerFuncName(const grpc_generator::Method *method) {
if (method->NoStreaming()) {
return "func $MethodName$(request: $Input$"
", context: StatusOnlyCallContext) -> EventLoopFuture<$Output$>";
}
if (method->ClientStreaming()) {
return "func $MethodName$(context: UnaryResponseCallContext<$Output$>) -> "
"EventLoopFuture<(StreamEvent<$Input$"
">) -> Void>";
}
if (method->ServerStreaming()) {
return "func $MethodName$(request: $Input$"
", context: StreamingResponseCallContext<$Output$>) -> "
"EventLoopFuture<GRPCStatus>";
}
return "func $MethodName$(context: StreamingResponseCallContext<$Output$>) "
"-> EventLoopFuture<(StreamEvent<$Input$>) -> Void>";
}
grpc::string GenerateServerExtensionBody(const grpc_generator::Method *method) {
grpc::string start = " case \"$MethodName$\":\n ";
grpc::string interceptors =
" interceptors: self.interceptors?.make$MethodName$Interceptors() "
"?? [],\n";
if (method->NoStreaming()) {
return start +
"return UnaryServerHandler(\n"
" context: context,\n"
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
interceptors +
" userFunction: self.$MethodName$(request:context:))\n";
}
if (method->ServerStreaming()) {
return start +
"return ServerStreamingServerHandler(\n"
" context: context,\n"
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
interceptors +
" userFunction: self.$MethodName$(request:context:))\n";
}
if (method->ClientStreaming()) {
return start +
"return ClientStreamingServerHandler(\n"
" context: context,\n"
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
interceptors +
" observerFactory: self.$MethodName$(context:))\n";
}
if (method->BidiStreaming()) {
return start +
"return BidirectionalStreamingServerHandler(\n"
" context: context,\n"
" requestDeserializer: GRPCPayloadDeserializer<$Input$>(),\n"
" responseSerializer: GRPCPayloadSerializer<$Output$>(),\n" +
interceptors +
" observerFactory: self.$MethodName$(context:))\n";
}
return "";
}
void GenerateServerProtocol(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars,
"$ACCESS$ protocol $ServiceQualifiedName$Provider: "
"CallHandlerProvider {\n");
printer->Print(
vars,
" var interceptors: "
"$ServiceQualifiedName$ServerInterceptorFactoryProtocol? { get }\n");
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
method->get_input_type_name());
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
method->get_output_type_name());
vars["MethodName"] = method->name();
printer->Print(" ");
auto func = GenerateServerFuncName(method.get());
printer->Print(vars, func.c_str());
printer->Print("\n");
}
printer->Print("}\n\n");
printer->Print(vars, "$ACCESS$ extension $ServiceQualifiedName$Provider {\n");
printer->Print("\n");
printer->Print(vars,
" var serviceName: Substring { return "
"\"$PATH$$ServiceName$\" }\n");
printer->Print("\n");
printer->Print(
" func handle(method name: Substring, context: "
"CallHandlerContext) -> GRPCServerHandlerProtocol? {\n");
printer->Print(" switch name {\n");
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
method->get_input_type_name());
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
method->get_output_type_name());
vars["MethodName"] = method->name();
auto body = GenerateServerExtensionBody(method.get());
printer->Print(vars, body.c_str());
printer->Print("\n");
}
printer->Print(" default: return nil;\n");
printer->Print(" }\n");
printer->Print(" }\n\n");
printer->Print("}\n\n");
printer->Print(vars,
"$ACCESS$ protocol "
"$ServiceQualifiedName$ServerInterceptorFactoryProtocol {\n");
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["Input"] = GenerateMessage(method->get_input_namespace_parts(),
method->get_input_type_name());
vars["Output"] = GenerateMessage(method->get_output_namespace_parts(),
method->get_output_type_name());
vars["MethodName"] = method->name();
printer->Print(
vars,
" /// - Returns: Interceptors to use when handling '$MethodName$'.\n"
" /// Defaults to calling `self.makeInterceptors()`.\n");
printer->Print(vars,
" func make$MethodName$Interceptors() -> "
"[ServerInterceptor<$Input$, $Output$>]\n\n");
}
printer->Print("}");
}
} // namespace
grpc::string Generate(grpc_generator::File *file,
const grpc_generator::Service *service) {
grpc::string output;
std::map<grpc::string, grpc::string> vars;
vars["PATH"] = file->package();
if (!file->package().empty()) { vars["PATH"].append("."); }
vars["ServiceQualifiedName"] =
WrapInNameSpace(service->namespace_parts(), service->name());
vars["ServiceName"] = service->name();
vars["ACCESS"] = service->is_internal() ? "internal" : "public";
auto printer = file->CreatePrinter(&output);
printer->Print(
vars,
"/// Usage: instantiate $ServiceQualifiedName$ServiceClient, then call "
"methods of this protocol to make API calls.\n");
GenerateClientProtocol(service, &*printer, &vars);
GenerateClientClass(&*printer, &vars);
printer->Print("\n");
GenerateServerProtocol(service, &*printer, &vars);
return output;
}
grpc::string GenerateHeader() {
grpc::string code;
code +=
"/// The following code is generated by the Flatbuffers library which "
"might not be in sync with grpc-swift\n";
code +=
"/// in case of an issue please open github issue, though it would be "
"maintained\n";
code += "\n";
code += "// swiftlint:disable all\n";
code += "// swiftformat:disable all\n";
code += "\n";
code += "import Foundation\n";
code += "import GRPC\n";
code += "import NIO\n";
code += "import NIOHTTP1\n";
code += "import FlatBuffers\n";
code += "\n";
code +=
"public protocol GRPCFlatBufPayload: GRPCPayload, FlatBufferGRPCMessage "
"{}\n";
code += "public extension GRPCFlatBufPayload {\n";
code += " init(serializedByteBuffer: inout NIO.ByteBuffer) throws {\n";
code +=
" self.init(byteBuffer: FlatBuffers.ByteBuffer(contiguousBytes: "
"serializedByteBuffer.readableBytesView, count: "
"serializedByteBuffer.readableBytes))\n";
code += " }\n";
code += " func serialize(into buffer: inout NIO.ByteBuffer) throws {\n";
code +=
" let buf = UnsafeRawBufferPointer(start: self.rawPointer, count: "
"Int(self.size))\n";
code += " buffer.writeBytes(buf)\n";
code += " }\n";
code += "}\n";
code += "extension Message: GRPCFlatBufPayload {}\n";
return code;
}
} // namespace grpc_swift_generator

View File

@ -0,0 +1,37 @@
/*
* Copyright 2020 Google Inc. All rights reserved.
*
* 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.
*/
#include <memory>
#include <vector>
#include "src/compiler/schema_interface.h"
#ifndef GRPC_CUSTOM_STRING
# include <string>
# define GRPC_CUSTOM_STRING std::string
#endif
namespace grpc {
typedef GRPC_CUSTOM_STRING string;
} // namespace grpc
namespace grpc_swift_generator {
grpc::string Generate(grpc_generator::File *file,
const grpc_generator::Service *service);
grpc::string GenerateHeader();
} // namespace grpc_swift_generator

View File

@ -0,0 +1,523 @@
/*
* Copyright 2020 Google Inc. All rights reserved.
*
* 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.
*/
/*
* NOTE: The following implementation is a translation for the Swift-grpc
* generator since flatbuffers doesnt allow plugins for now. if an issue arises
* please open an issue in the flatbuffers repository. This file should always
* be maintained according to the Swift-grpc repository
*/
#include "src/compiler/ts_generator.h"
#include <map>
#include <sstream>
#include "flatbuffers/util.h"
#include "src/compiler/schema_interface.h"
namespace grpc_ts_generator {
namespace {
static grpc::string GenerateNamespace(const std::vector<std::string> ns,
const std::string filename,
const bool include_separator) {
grpc::string path = "";
if (include_separator) path += ".";
for (auto it = ns.begin(); it < ns.end(); it++) {
if (include_separator) path += "/";
path += include_separator
? flatbuffers::ConvertCase(*it, flatbuffers::Case::kDasher,
flatbuffers::Case::kUpperCamel)
: *it + "_";
}
if (include_separator) path += "/";
path += include_separator
? flatbuffers::ConvertCase(filename, flatbuffers::Case::kDasher,
flatbuffers::Case::kUpperCamel)
: filename;
return path;
}
// MARK: - Shared code
static void GenerateImports(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary,
const bool grpc_var_import) {
auto vars = *dictonary;
printer->Print(
"// Generated GRPC code for FlatBuffers TS *** DO NOT EDIT ***\n");
printer->Print("import * as flatbuffers from 'flatbuffers';\n");
std::set<grpc::string> generated_imports;
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
auto output = method->get_output_type_name();
auto input = method->get_input_type_name();
auto input_namespace = method->get_input_namespace_parts();
vars["OUTPUT"] = output;
vars["INPUT"] = input;
if (generated_imports.find(output) == generated_imports.end()) {
generated_imports.insert(output);
vars["OUTPUT_DIR"] =
GenerateNamespace(method->get_output_namespace_parts(), output, true);
vars["Output_alias"] = GenerateNamespace(
method->get_output_namespace_parts(), output, false);
printer->Print(
vars, "import { $OUTPUT$ as $Output_alias$ } from '$OUTPUT_DIR$';\n");
}
if (generated_imports.find(input) == generated_imports.end()) {
generated_imports.insert(input);
vars["INPUT_DIR"] =
GenerateNamespace(method->get_output_namespace_parts(), input, true);
vars["Input_alias"] =
GenerateNamespace(method->get_output_namespace_parts(), input, false);
printer->Print(
vars, "import { $INPUT$ as $Input_alias$ } from '$INPUT_DIR$';\n");
}
}
printer->Print("\n");
if (grpc_var_import)
printer->Print("var grpc = require('@grpc/grpc-js');\n");
else
printer->Print("import * as grpc from '@grpc/grpc-js';\n");
printer->Print("\n");
}
// MARK: - Generate Main GRPC Code
static void GetStreamType(grpc_generator::Printer *printer,
const grpc_generator::Method *method,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
auto client_streaming = method->ClientStreaming() || method->BidiStreaming();
auto server_streaming = method->ServerStreaming() || method->BidiStreaming();
vars["ClientStreaming"] = client_streaming ? "true" : "false";
vars["ServerStreaming"] = server_streaming ? "true" : "false";
printer->Print(vars, "requestStream: $ClientStreaming$,\n");
printer->Print(vars, "responseStream: $ServerStreaming$,\n");
}
static void GenerateSerializeMethod(grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars, "function serialize_$Type$(buffer_args) {\n");
printer->Indent();
printer->Print(vars, "if (!(buffer_args instanceof $Type$)) {\n");
printer->Indent();
printer->Print(vars,
"throw new Error('Expected argument of type $VALUE$');\n");
printer->Outdent();
printer->Print("}\n");
printer->Print(vars, "return Buffer.from(buffer_args.serialize());\n");
printer->Outdent();
printer->Print("}\n\n");
}
static void GenerateDeserializeMethod(
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars, "function deserialize_$Type$(buffer) {\n");
printer->Indent();
printer->Print(vars,
"return $Type$.getRootAs$VALUE$(new "
"flatbuffers.ByteBuffer(buffer))\n");
printer->Outdent();
printer->Print("}\n\n");
}
static void GenerateMethods(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
std::set<grpc::string> generated_functions;
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
auto output = method->get_output_type_name();
auto input = method->get_input_type_name();
if (generated_functions.find(output) == generated_functions.end()) {
generated_functions.insert(output);
vars["VALUE"] = output;
vars["Type"] = GenerateNamespace(method->get_output_namespace_parts(),
output, false);
GenerateSerializeMethod(printer, &vars);
GenerateDeserializeMethod(printer, &vars);
}
printer->Print("\n");
if (generated_functions.find(input) == generated_functions.end()) {
generated_functions.insert(input);
vars["VALUE"] = input;
vars["Type"] =
GenerateNamespace(method->get_input_namespace_parts(), input, false);
GenerateSerializeMethod(printer, &vars);
GenerateDeserializeMethod(printer, &vars);
}
}
}
static void GenerateService(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
vars["NAME"] = service->name() + "Service";
printer->Print(vars, "var $NAME$ = exports.$NAME$ = {\n");
printer->Indent();
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["MethodName"] = method->name();
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
method->get_output_type_name(), false);
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
method->get_input_type_name(), false);
printer->Print(vars, "$MethodName$: {\n");
printer->Indent();
printer->Print(vars, "path: '/$PATH$$ServiceName$/$MethodName$',\n");
GetStreamType(printer, &*method, &vars);
printer->Print(vars, "requestType: flatbuffers.ByteBuffer,\n");
printer->Print(vars, "responseType: $OUTPUT$,\n");
printer->Print(vars, "requestSerialize: serialize_$INPUT$,\n");
printer->Print(vars, "requestDeserialize: deserialize_$INPUT$,\n");
printer->Print(vars, "responseSerialize: serialize_$OUTPUT$,\n");
printer->Print(vars, "responseDeserialize: deserialize_$OUTPUT$,\n");
printer->Outdent();
printer->Print("},\n");
}
printer->Outdent();
printer->Print("};\n");
printer->Print(vars,
"exports.$ServiceName$Client = "
"grpc.makeGenericClientConstructor($NAME$);");
}
} // namespace
grpc::string Generate(grpc_generator::File *file,
const grpc_generator::Service *service,
const grpc::string &filename) {
grpc::string output;
std::map<grpc::string, grpc::string> vars;
vars["PATH"] = file->package();
if (!file->package().empty()) { vars["PATH"].append("."); }
vars["ServiceName"] = service->name();
vars["FBSFile"] = service->name() + "_fbs";
vars["Filename"] = filename;
auto printer = file->CreatePrinter(&output);
GenerateImports(service, &*printer, &vars, true);
GenerateMethods(service, &*printer, &vars);
GenerateService(service, &*printer, &vars);
return output;
}
namespace {
// MARK: - Generate Interface
static void FillInterface(grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars,
"interface I$ServiceName$Service_I$MethodName$ extends "
"grpc.MethodDefinition<$INPUT$, $OUTPUT$> {\n");
printer->Indent();
printer->Print(vars, "path: string; // /$PATH$$ServiceName$/$MethodName$\n");
printer->Print(vars, "requestStream: boolean; // $ClientStreaming$\n");
printer->Print(vars, "responseStream: boolean; // $ServerStreaming$\n");
printer->Print(vars, "requestSerialize: grpc.serialize<$INPUT$>;\n");
printer->Print(vars, "requestDeserialize: grpc.deserialize<$INPUT$>;\n");
printer->Print(vars, "responseSerialize: grpc.serialize<$OUTPUT$>;\n");
printer->Print(vars, "responseDeserialize: grpc.deserialize<$OUTPUT$>;\n");
printer->Outdent();
printer->Print("}\n");
}
static void GenerateInterfaces(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
auto client_streaming =
method->ClientStreaming() || method->BidiStreaming();
auto server_streaming =
method->ServerStreaming() || method->BidiStreaming();
vars["ClientStreaming"] = client_streaming ? "true" : "false";
vars["ServerStreaming"] = server_streaming ? "true" : "false";
vars["MethodName"] = method->name();
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
method->get_output_type_name(), false);
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
method->get_input_type_name(), false);
FillInterface(printer, &vars);
printer->Print("\n");
}
}
static void GenerateExportedInterface(
const grpc_generator::Service *service, grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars,
"export interface I$ServiceName$Server extends "
"grpc.UntypedServiceImplementation {\n");
printer->Indent();
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["Name"] = method->name();
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
method->get_output_type_name(), false);
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
method->get_input_type_name(), false);
if (method->BidiStreaming()) {
printer->Print(vars,
"$Name$: grpc.handleBidiStreamingCall<$INPUT$, "
"$OUTPUT$>;\n");
continue;
}
if (method->NoStreaming()) {
printer->Print(vars,
"$Name$: grpc.handleUnaryCall<$INPUT$, "
"$OUTPUT$>;\n");
continue;
}
if (method->ClientStreaming()) {
printer->Print(vars,
"$Name$: grpc.handleClientStreamingCall<$INPUT$, "
"$OUTPUT$>;\n");
continue;
}
if (method->ServerStreaming()) {
printer->Print(vars,
"$Name$: grpc.handleServerStreamingCall<$INPUT$, "
"$OUTPUT$>;\n");
continue;
}
}
printer->Outdent();
printer->Print("}\n");
}
static void GenerateMainInterface(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(
vars,
"interface I$ServiceName$Service extends "
"grpc.ServiceDefinition<grpc.UntypedServiceImplementation> {\n");
printer->Indent();
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["MethodName"] = method->name();
printer->Print(vars,
"$MethodName$: I$ServiceName$Service_I$MethodName$;\n");
}
printer->Outdent();
printer->Print("}\n");
GenerateInterfaces(service, printer, &vars);
printer->Print("\n");
printer->Print(vars,
"export const $ServiceName$Service: I$ServiceName$Service;\n");
printer->Print("\n");
GenerateExportedInterface(service, printer, &vars);
}
static grpc::string GenerateMetaData() { return "metadata: grpc.Metadata"; }
static grpc::string GenerateOptions() { return "options: Partial<grpc.CallOptions>"; }
static void GenerateUnaryClientInterface(
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, ";
grpc::string callback =
"callback: (error: grpc.ServiceError | null, response: "
"$OUTPUT$) => void): grpc.ClientUnaryCall;\n";
auto meta_data = GenerateMetaData() + ", ";
auto options = GenerateOptions() + ", ";
printer->Print(vars, (main + callback).c_str());
printer->Print(vars, (main + meta_data + callback).c_str());
printer->Print(vars, (main + meta_data + options + callback).c_str());
}
static void GenerateClientWriteStreamInterface(
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
grpc::string main = "$ISPUBLIC$$MethodName$(";
grpc::string callback =
"callback: (error: grpc.ServiceError | null, response: "
"$INPUT$) => void): "
"grpc.ClientWritableStream<$OUTPUT$>;\n";
auto meta_data = GenerateMetaData() + ", ";
auto options = GenerateOptions() + ", ";
printer->Print(vars, (main + callback).c_str());
printer->Print(vars, (main + meta_data + callback).c_str());
printer->Print(vars, (main + options + callback).c_str());
printer->Print(vars, (main + meta_data + options + callback).c_str());
}
static void GenerateClientReadableStreamInterface(
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
grpc::string main = "$ISPUBLIC$$MethodName$(request: $INPUT$, ";
grpc::string end_function = "): grpc.ClientReadableStream<$OUTPUT$>;\n";
auto meta_data = GenerateMetaData();
auto options = GenerateOptions();
printer->Print(vars, (main + meta_data + end_function).c_str());
printer->Print(vars, (main + options + end_function).c_str());
}
static void GenerateDepluxStreamInterface(
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
grpc::string main = "$ISPUBLIC$$MethodName$(";
grpc::string end_function =
"): grpc.ClientDuplexStream<$INPUT$, $OUTPUT$>;\n";
auto meta_data = GenerateMetaData();
auto options = GenerateOptions();
printer->Print(vars, (main + end_function).c_str());
printer->Print(vars, (main + options + end_function).c_str());
printer->Print(vars, (main + meta_data +
", options?: Partial<grpc.CallOptions>" + end_function)
.c_str());
}
static void GenerateClientInterface(const grpc_generator::Service *service,
grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars, "export interface I$ServiceName$Client {\n");
printer->Indent();
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["MethodName"] = method->name();
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
method->get_output_type_name(), false);
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
method->get_input_type_name(), false);
vars["ISPUBLIC"] = "";
if (method->NoStreaming()) {
GenerateUnaryClientInterface(printer, &vars);
continue;
}
if (method->BidiStreaming()) {
GenerateDepluxStreamInterface(printer, &vars);
continue;
}
if (method->ClientStreaming()) {
GenerateClientWriteStreamInterface(printer, &vars);
continue;
}
if (method->ServerStreaming()) {
GenerateClientReadableStreamInterface(printer, &vars);
continue;
}
}
printer->Outdent();
printer->Print("}\n");
}
static void GenerateClientClassInterface(
const grpc_generator::Service *service, grpc_generator::Printer *printer,
std::map<grpc::string, grpc::string> *dictonary) {
auto vars = *dictonary;
printer->Print(vars,
"export class $ServiceName$Client extends grpc.Client "
"implements I$ServiceName$Client {\n");
printer->Indent();
printer->Print(
"constructor(address: string, credentials: grpc.ChannelCredentials, "
"options?: object);\n");
for (auto it = 0; it < service->method_count(); it++) {
auto method = service->method(it);
vars["MethodName"] = method->name();
vars["OUTPUT"] = GenerateNamespace(method->get_output_namespace_parts(),
method->get_output_type_name(), false);
vars["INPUT"] = GenerateNamespace(method->get_input_namespace_parts(),
method->get_input_type_name(), false);
vars["ISPUBLIC"] = "public ";
if (method->NoStreaming()) {
GenerateUnaryClientInterface(printer, &vars);
continue;
}
if (method->BidiStreaming()) {
GenerateDepluxStreamInterface(printer, &vars);
continue;
}
if (method->ClientStreaming()) {
GenerateClientWriteStreamInterface(printer, &vars);
continue;
}
if (method->ServerStreaming()) {
GenerateClientReadableStreamInterface(printer, &vars);
continue;
}
}
printer->Outdent();
printer->Print("}\n");
}
} // namespace
grpc::string GenerateInterface(grpc_generator::File *file,
const grpc_generator::Service *service,
const grpc::string &filename) {
grpc::string output;
std::set<grpc::string> generated_functions;
std::map<grpc::string, grpc::string> vars;
vars["PATH"] = file->package();
if (!file->package().empty()) { vars["PATH"].append("."); }
vars["ServiceName"] = service->name();
vars["FBSFile"] = service->name() + "_fbs";
vars["Filename"] = filename;
auto printer = file->CreatePrinter(&output);
GenerateImports(service, &*printer, &vars, false);
GenerateMainInterface(service, &*printer, &vars);
printer->Print("\n");
GenerateClientInterface(service, &*printer, &vars);
printer->Print("\n");
GenerateClientClassInterface(service, &*printer, &vars);
return output;
}
} // namespace grpc_ts_generator

View File

@ -0,0 +1,26 @@
#include <memory>
#include <set>
#include <vector>
#include "src/compiler/schema_interface.h"
#ifndef GRPC_CUSTOM_STRING
# include <string>
# define GRPC_CUSTOM_STRING std::string
#endif
namespace grpc {
typedef GRPC_CUSTOM_STRING string;
} // namespace grpc
namespace grpc_ts_generator {
grpc::string Generate(grpc_generator::File *file,
const grpc_generator::Service *service,
const grpc::string &filename);
grpc::string GenerateInterface(grpc_generator::File *file,
const grpc_generator::Service *service,
const grpc::string &filename);
} // namespace grpc_ts_generator

View File

@ -0,0 +1,17 @@
cc_test(
name = "grpc_test",
srcs = [
"grpctest.cpp",
"message_builder_test.cpp",
],
copts = ["-Itests"],
# This is required.
linkstatic = 1,
deps = [
"//tests:monster_test_cc_fbs",
"//tests:monster_test_grpc",
"//tests:test_assert",
"//tests:test_builder",
"@com_github_grpc_grpc//:grpc++",
],
)

View File

@ -0,0 +1,42 @@
import java.nio.ByteBuffer;
import MyGame.Example.Monster;
import MyGame.Example.Stat;
import com.google.flatbuffers.FlatBufferBuilder;
class GameFactory {
public static Monster createMonster(String monsterName, short nestedMonsterHp, short nestedMonsterMana) {
FlatBufferBuilder builder = new FlatBufferBuilder();
int name_offset = builder.createString(monsterName);
Monster.startMonster(builder);
Monster.addName(builder, name_offset);
Monster.addHp(builder, nestedMonsterHp);
Monster.addMana(builder, nestedMonsterMana);
int monster_offset = Monster.endMonster(builder);
Monster.finishMonsterBuffer(builder, monster_offset);
ByteBuffer buffer = builder.dataBuffer();
Monster monster = Monster.getRootAsMonster(buffer);
return monster;
}
public static Monster createMonsterFromStat(Stat stat, int seqNo) {
FlatBufferBuilder builder = new FlatBufferBuilder();
int name_offset = builder.createString(stat.id() + " No." + seqNo);
Monster.startMonster(builder);
Monster.addName(builder, name_offset);
int monster_offset = Monster.endMonster(builder);
Monster.finishMonsterBuffer(builder, monster_offset);
Monster monster = Monster.getRootAsMonster(builder.dataBuffer());
return monster;
}
public static Stat createStat(String greeting, long val, int count) {
FlatBufferBuilder builder = new FlatBufferBuilder();
int statOffset = Stat.createStat(builder, builder.createString(greeting), val, count);
builder.finish(statOffset);
Stat stat = Stat.getRootAsStat(builder.dataBuffer());
return stat;
}
}

View File

@ -0,0 +1,242 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* 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.
*/
import MyGame.Example.Monster;
import MyGame.Example.MonsterStorageGrpc;
import MyGame.Example.Stat;
import com.google.flatbuffers.FlatBufferBuilder;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import org.junit.Assert;
import java.io.IOException;
import java.lang.InterruptedException;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.CountDownLatch;
/**
* Demonstrates basic client-server interaction using grpc-java over netty.
*/
public class JavaGrpcTest {
static final String BIG_MONSTER_NAME = "Cyberdemon";
static final short nestedMonsterHp = 600;
static final short nestedMonsterMana = 1024;
static final int numStreamedMsgs = 10;
static final int timeoutMs = 3000;
static Server server;
static ManagedChannel channel;
static MonsterStorageGrpc.MonsterStorageBlockingStub blockingStub;
static MonsterStorageGrpc.MonsterStorageStub asyncStub;
static class MyService extends MonsterStorageGrpc.MonsterStorageImplBase {
@Override
public void store(Monster request, io.grpc.stub.StreamObserver<Stat> responseObserver) {
Assert.assertEquals(request.name(), BIG_MONSTER_NAME);
Assert.assertEquals(request.hp(), nestedMonsterHp);
Assert.assertEquals(request.mana(), nestedMonsterMana);
System.out.println("Received store request from " + request.name());
// Create a response from the incoming request name.
Stat stat = GameFactory.createStat("Hello " + request.name(), 100, 10);
responseObserver.onNext(stat);
responseObserver.onCompleted();
}
@Override
public void retrieve(Stat request, io.grpc.stub.StreamObserver<Monster> responseObserver) {
// Create 10 monsters for streaming response.
for (int i=0; i<numStreamedMsgs; i++) {
Monster monster = GameFactory.createMonsterFromStat(request, i);
responseObserver.onNext(monster);
}
responseObserver.onCompleted();
}
@Override
public StreamObserver<Monster> getMaxHitPoint(final StreamObserver<Stat> responseObserver) {
return computeMinMax(responseObserver, false);
}
@Override
public StreamObserver<Monster> getMinMaxHitPoints(final StreamObserver<Stat> responseObserver) {
return computeMinMax(responseObserver, true);
}
private StreamObserver<Monster> computeMinMax(final StreamObserver<Stat> responseObserver, final boolean includeMin) {
final AtomicInteger maxHp = new AtomicInteger(Integer.MIN_VALUE);
final AtomicReference<String> maxHpMonsterName = new AtomicReference<String>();
final AtomicInteger maxHpCount = new AtomicInteger();
final AtomicInteger minHp = new AtomicInteger(Integer.MAX_VALUE);
final AtomicReference<String> minHpMonsterName = new AtomicReference<String>();
final AtomicInteger minHpCount = new AtomicInteger();
return new StreamObserver<Monster>() {
public void onNext(Monster monster) {
if (monster.hp() > maxHp.get()) {
// Found a monster of higher hit points.
maxHp.set(monster.hp());
maxHpMonsterName.set(monster.name());
maxHpCount.set(1);
}
else if (monster.hp() == maxHp.get()) {
// Count how many times we saw a monster of current max hit points.
maxHpCount.getAndIncrement();
}
if (monster.hp() < minHp.get()) {
// Found a monster of a lower hit points.
minHp.set(monster.hp());
minHpMonsterName.set(monster.name());
minHpCount.set(1);
}
else if (monster.hp() == minHp.get()) {
// Count how many times we saw a monster of current min hit points.
minHpCount.getAndIncrement();
}
}
public void onCompleted() {
Stat maxHpStat = GameFactory.createStat(maxHpMonsterName.get(), maxHp.get(), maxHpCount.get());
// Send max hit points first.
responseObserver.onNext(maxHpStat);
if (includeMin) {
// Send min hit points.
Stat minHpStat = GameFactory.createStat(minHpMonsterName.get(), minHp.get(), minHpCount.get());
responseObserver.onNext(minHpStat);
}
responseObserver.onCompleted();
}
public void onError(Throwable t) {
// Not expected
Assert.fail();
};
};
}
}
@org.junit.BeforeClass
public static void startServer() throws IOException {
server = ServerBuilder.forPort(0).addService(new MyService()).build().start();
int port = server.getPort();
channel = ManagedChannelBuilder.forAddress("localhost", port)
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
// needing certificates.
.usePlaintext()
.directExecutor()
.build();
blockingStub = MonsterStorageGrpc.newBlockingStub(channel);
asyncStub = MonsterStorageGrpc.newStub(channel);
}
@org.junit.Test
public void testUnary() throws IOException {
Monster monsterRequest = GameFactory.createMonster(BIG_MONSTER_NAME, nestedMonsterHp, nestedMonsterMana);
Stat stat = blockingStub.store(monsterRequest);
Assert.assertEquals(stat.id(), "Hello " + BIG_MONSTER_NAME);
System.out.println("Received stat response from service: " + stat.id());
}
@org.junit.Test
public void testServerStreaming() throws IOException {
Monster monsterRequest = GameFactory.createMonster(BIG_MONSTER_NAME, nestedMonsterHp, nestedMonsterMana);
Stat stat = blockingStub.store(monsterRequest);
Iterator<Monster> iterator = blockingStub.retrieve(stat);
int counter = 0;
while(iterator.hasNext()) {
Monster m = iterator.next();
System.out.println("Received monster " + m.name());
counter ++;
}
Assert.assertEquals(counter, numStreamedMsgs);
System.out.println("FlatBuffers GRPC client/server test: completed successfully");
}
@org.junit.Test
public void testClientStreaming() throws IOException, InterruptedException {
final AtomicReference<Stat> maxHitStat = new AtomicReference<Stat>();
final CountDownLatch streamAlive = new CountDownLatch(1);
StreamObserver<Stat> statObserver = new StreamObserver<Stat>() {
public void onCompleted() {
streamAlive.countDown();
}
public void onError(Throwable ex) { }
public void onNext(Stat stat) {
maxHitStat.set(stat);
}
};
StreamObserver<Monster> monsterStream = asyncStub.getMaxHitPoint(statObserver);
short count = 10;
for (short i = 0;i < count; ++i) {
Monster monster = GameFactory.createMonster(BIG_MONSTER_NAME + i, (short) (nestedMonsterHp * i), nestedMonsterMana);
monsterStream.onNext(monster);
}
monsterStream.onCompleted();
// Wait a little bit for the server to send the stats of the monster with the max hit-points.
streamAlive.await(timeoutMs, TimeUnit.MILLISECONDS);
Assert.assertEquals(maxHitStat.get().id(), BIG_MONSTER_NAME + (count - 1));
Assert.assertEquals(maxHitStat.get().val(), nestedMonsterHp * (count - 1));
Assert.assertEquals(maxHitStat.get().count(), 1);
}
@org.junit.Test
public void testBiDiStreaming() throws IOException, InterruptedException {
final AtomicReference<Stat> maxHitStat = new AtomicReference<Stat>();
final AtomicReference<Stat> minHitStat = new AtomicReference<Stat>();
final CountDownLatch streamAlive = new CountDownLatch(1);
StreamObserver<Stat> statObserver = new StreamObserver<Stat>() {
public void onCompleted() {
streamAlive.countDown();
}
public void onError(Throwable ex) { }
public void onNext(Stat stat) {
// We expect the server to send the max stat first and then the min stat.
if (maxHitStat.get() == null) {
maxHitStat.set(stat);
}
else {
minHitStat.set(stat);
}
}
};
StreamObserver<Monster> monsterStream = asyncStub.getMinMaxHitPoints(statObserver);
short count = 10;
for (short i = 0;i < count; ++i) {
Monster monster = GameFactory.createMonster(BIG_MONSTER_NAME + i, (short) (nestedMonsterHp * i), nestedMonsterMana);
monsterStream.onNext(monster);
}
monsterStream.onCompleted();
// Wait a little bit for the server to send the stats of the monster with the max and min hit-points.
streamAlive.await(timeoutMs, TimeUnit.MILLISECONDS);
Assert.assertEquals(maxHitStat.get().id(), BIG_MONSTER_NAME + (count - 1));
Assert.assertEquals(maxHitStat.get().val(), nestedMonsterHp * (count - 1));
Assert.assertEquals(maxHitStat.get().count(), 1);
Assert.assertEquals(minHitStat.get().id(), BIG_MONSTER_NAME + 0);
Assert.assertEquals(minHitStat.get().val(), nestedMonsterHp * 0);
Assert.assertEquals(minHitStat.get().count(), 1);
}
}

View File

@ -0,0 +1,102 @@
package testing
import (
flatbuffers "github.com/google/flatbuffers/go"
"github.com/google/flatbuffers/tests/MyGame/Example"
"context"
"net"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/encoding"
)
type server struct {
Example.UnimplementedMonsterStorageServer
}
// test used to send and receive in grpc methods
var test = "Flatbuffers"
var addr = "0.0.0.0:50051"
// gRPC server store method
func (s *server) Store(context context.Context, in *Example.Monster) (*flatbuffers.Builder, error) {
b := flatbuffers.NewBuilder(0)
i := b.CreateString(test)
Example.StatStart(b)
Example.StatAddId(b, i)
b.Finish(Example.StatEnd(b))
return b, nil
}
// gRPC server retrieve method
func (s *server) Retrieve(context context.Context, in *Example.Stat) (*flatbuffers.Builder, error) {
b := flatbuffers.NewBuilder(0)
i := b.CreateString(test)
Example.MonsterStart(b)
Example.MonsterAddName(b, i)
b.Finish(Example.MonsterEnd(b))
return b, nil
}
func StoreClient(c Example.MonsterStorageClient, t *testing.T) {
b := flatbuffers.NewBuilder(0)
i := b.CreateString(test)
Example.MonsterStart(b)
Example.MonsterAddName(b, i)
b.Finish(Example.MonsterEnd(b))
out, err := c.Store(context.Background(), b)
if err != nil {
t.Fatalf("Store client failed: %v", err)
}
if string(out.Id()) != test {
t.Errorf("StoreClient failed: expected=%s, got=%s\n", test, out.Id())
t.Fail()
}
}
func RetrieveClient(c Example.MonsterStorageClient, t *testing.T) {
b := flatbuffers.NewBuilder(0)
i := b.CreateString(test)
Example.StatStart(b)
Example.StatAddId(b, i)
b.Finish(Example.StatEnd(b))
out, err := c.Retrieve(context.Background(), b)
if err != nil {
t.Fatalf("Retrieve client failed: %v", err)
}
monster, err := out.Recv()
if err != nil {
t.Fatalf("Recv failed: %v", err)
}
if string(monster.Name()) != test {
t.Errorf("RetrieveClient failed: expected=%s, got=%s\n", test, monster.Name())
t.Fail()
}
}
func TestGRPC(t *testing.T) {
lis, err := net.Listen("tcp", addr)
if err != nil {
t.Fatalf("Failed to listen: %v", err)
}
ser := grpc.NewServer()
encoding.RegisterCodec(flatbuffers.FlatbuffersCodec{})
Example.RegisterMonsterStorageServer(ser, &server{})
go func() {
if err := ser.Serve(lis); err != nil {
t.Fatalf("Failed to serve: %v", err)
t.FailNow()
}
}()
conn, err := grpc.Dial(addr, grpc.WithInsecure(), grpc.WithCodec(flatbuffers.FlatbuffersCodec{}))
if err != nil {
t.Fatalf("Failed to connect: %v", err)
}
defer conn.Close()
client := Example.NewMonsterStorageClient(conn)
StoreClient(client, t)
RetrieveClient(client, t)
}

View File

@ -0,0 +1,193 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* 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.
*/
#include <grpcpp/grpcpp.h>
#include <condition_variable>
#include <thread>
#include "monster_test.grpc.fb.h"
#include "monster_test_generated.h"
#include "test_assert.h"
using namespace MyGame::Example;
using flatbuffers::FlatBufferBuilder;
using flatbuffers::grpc::MessageBuilder;
void message_builder_tests();
// The callback implementation of our server, that derives from the generated
// code. It implements all rpcs specified in the FlatBuffers schema.
class ServiceImpl final : public MyGame::Example::MonsterStorage::Service {
virtual ::grpc::Status Store(
::grpc::ServerContext *context,
const flatbuffers::grpc::Message<Monster> *request,
flatbuffers::grpc::Message<Stat> *response) override {
// Create a response from the incoming request name.
fbb_.Clear();
auto stat_offset = CreateStat(
fbb_, fbb_.CreateString("Hello, " + request->GetRoot()->name()->str()));
fbb_.Finish(stat_offset);
// Transfer ownership of the message to gRPC
*response = fbb_.ReleaseMessage<Stat>();
return grpc::Status::OK;
}
virtual ::grpc::Status Retrieve(
::grpc::ServerContext *context,
const flatbuffers::grpc::Message<Stat> *request,
::grpc::ServerWriter<flatbuffers::grpc::Message<Monster>> *writer)
override {
for (int i = 0; i < 5; i++) {
fbb_.Clear();
// Create 5 monsters for resposne.
auto monster_offset =
CreateMonster(fbb_, 0, 0, 0,
fbb_.CreateString(request->GetRoot()->id()->str() +
" No." + std::to_string(i)));
fbb_.Finish(monster_offset);
flatbuffers::grpc::Message<Monster> monster =
fbb_.ReleaseMessage<Monster>();
// Send monster to client using streaming.
writer->Write(monster);
}
return grpc::Status::OK;
}
private:
flatbuffers::grpc::MessageBuilder fbb_;
};
// Track the server instance, so we can terminate it later.
grpc::Server *server_instance = nullptr;
// Mutex to protec this variable.
std::mutex wait_for_server;
std::condition_variable server_instance_cv;
// This function implements the server thread.
void RunServer() {
auto server_address = "0.0.0.0:50051";
// Callback interface we implemented above.
ServiceImpl service;
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
// Start the server. Lock to change the variable we're changing.
wait_for_server.lock();
server_instance = builder.BuildAndStart().release();
wait_for_server.unlock();
server_instance_cv.notify_one();
std::cout << "Server listening on " << server_address << std::endl;
// This will block the thread and serve requests.
server_instance->Wait();
}
template<class Builder> void StoreRPC(MonsterStorage::Stub *stub) {
Builder fbb;
grpc::ClientContext context;
// Build a request with the name set.
auto monster_offset = CreateMonster(fbb, 0, 0, 0, fbb.CreateString("Fred"));
MessageBuilder mb(std::move(fbb));
mb.Finish(monster_offset);
auto request = mb.ReleaseMessage<Monster>();
flatbuffers::grpc::Message<Stat> response;
// The actual RPC.
auto status = stub->Store(&context, request, &response);
if (status.ok()) {
auto resp = response.GetRoot()->id();
std::cout << "RPC response: " << resp->str() << std::endl;
} else {
std::cout << "RPC failed" << std::endl;
}
}
template<class Builder> void RetrieveRPC(MonsterStorage::Stub *stub) {
Builder fbb;
grpc::ClientContext context;
fbb.Clear();
auto stat_offset = CreateStat(fbb, fbb.CreateString("Fred"));
fbb.Finish(stat_offset);
auto request = MessageBuilder(std::move(fbb)).ReleaseMessage<Stat>();
flatbuffers::grpc::Message<Monster> response;
auto stream = stub->Retrieve(&context, request);
while (stream->Read(&response)) {
auto resp = response.GetRoot()->name();
std::cout << "RPC Streaming response: " << resp->str() << std::endl;
}
}
int grpc_server_test() {
// Launch server.
std::thread server_thread(RunServer);
// wait for server to spin up.
std::unique_lock<std::mutex> lock(wait_for_server);
while (!server_instance) server_instance_cv.wait(lock);
// Now connect the client.
auto channel = grpc::CreateChannel("localhost:50051",
grpc::InsecureChannelCredentials());
auto stub = MyGame::Example::MonsterStorage::NewStub(channel);
StoreRPC<MessageBuilder>(stub.get());
StoreRPC<FlatBufferBuilder>(stub.get());
RetrieveRPC<MessageBuilder>(stub.get());
RetrieveRPC<FlatBufferBuilder>(stub.get());
#if !FLATBUFFERS_GRPC_DISABLE_AUTO_VERIFICATION
{
// Test that an invalid request errors out correctly
grpc::ClientContext context;
flatbuffers::grpc::Message<Monster> request; // simulate invalid message
flatbuffers::grpc::Message<Stat> response;
auto status = stub->Store(&context, request, &response);
// The rpc status should be INTERNAL to indicate a verification error. This
// matches the protobuf gRPC status code for an unparseable message.
assert(!status.ok());
assert(status.error_code() == ::grpc::StatusCode::INTERNAL);
assert(strcmp(status.error_message().c_str(),
"Message verification failed") == 0);
}
#endif
server_instance->Shutdown();
server_thread.join();
delete server_instance;
return 0;
}
int main(int /*argc*/, const char * /*argv*/[]) {
message_builder_tests();
grpc_server_test();
if (!testing_fails) {
TEST_OUTPUT_LINE("ALL TESTS PASSED");
return 0;
} else {
TEST_OUTPUT_LINE("%d FAILED TESTS", testing_fails);
return 1;
}
}

View File

@ -0,0 +1,174 @@
from __future__ import print_function
import os
import sys
import grpc
import flatbuffers
from concurrent import futures
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'tests'))
import MyGame.Example.Monster as Monster
import MyGame.Example.Stat as Stat
import MyGame.Example.Vec3 as Vec3
import MyGame.Example.Test as Test
import MyGame.Example.monster_test_grpc_fb as monster_grpc_fb
test_stat_id = "test_stat_id"
test_stat_val = 8
test_stat_count = 1
test_monster_name1 = "test_monster_name1"
test_monster_name2 = "test_monster_name2"
test_string = "test_string"
test_color = 2
test_X = 3.0
test_Y = 2.0
test_Z = 6.0
test_test1 = 4.0
test_a = 8
test_b = 5
test_hp = 67
test_inventory = [1, 1, 2, 3, 5, 8]
test_testtype = 4
test_monsters_name_retrieve = ["big_monster", "small_monster"]
test_no_of_monsters = 2
class MonsterStorage(monster_grpc_fb.MonsterStorageServicer):
def Store(self, request, context):
m = Monster.Monster().GetRootAsMonster(request, 0)
assert m.Name().decode("utf-8") == test_monster_name1
assert m.Pos().X() == test_X
assert m.Pos().Y() == test_Y
assert m.Pos().Z() == test_Z
assert m.Pos().Test1() == test_test1
assert m.Pos().Test2() == test_color
test3 = Test.Test()
assert m.Pos().Test3(test3).A() == test_a
assert m.Pos().Test3(test3).B() == test_b
assert m.Hp() == test_hp
assert m.Color() == test_color
assert m.InventoryLength() == len(test_inventory)
for i in range(0, len(test_inventory)):
assert m.Inventory(i) == test_inventory[len(test_inventory)-i -1]
assert m.TestType() == test_testtype
assert m.Test() is not None
table = m.Test()
m2 = Monster.Monster()
m2.Init(table.Bytes, table.Pos)
assert m2.Name().decode("utf-8") == test_monster_name2
m3 = m.Enemy()
assert m3.Name().decode("utf-8") == test_monster_name2
assert m.Testarrayofstring(0).decode("utf-8") == test_string
b = flatbuffers.Builder(0)
i = b.CreateString(test_stat_id)
Stat.StatStart(b)
Stat.StatAddId(b, i)
Stat.StatAddVal(b, test_stat_val)
Stat.StatAddCount(b, test_stat_count)
b.Finish(Stat.StatEnd(b))
return bytes(b.Output())
def Retrieve(self, request, context):
s = Stat.Stat().GetRootAsStat(request, 0)
no_of_monsters = test_no_of_monsters
for i in range(0, no_of_monsters):
b = flatbuffers.Builder(0)
i = b.CreateString(test_monsters_name_retrieve[i])
Monster.MonsterStart(b)
Monster.MonsterAddName(b, i)
b.Finish(Monster.MonsterEnd(b))
yield bytes(b.Output())
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
monster_grpc_fb.add_MonsterStorageServicer_to_server(MonsterStorage(), server)
server.add_insecure_port('[::]:50051')
server.start()
run()
def run():
channel = grpc.insecure_channel('127.0.0.1:50051')
stub = monster_grpc_fb.MonsterStorageStub(channel)
b = flatbuffers.Builder(0)
name2 = b.CreateString(test_monster_name2)
name1 = b.CreateString(test_monster_name1)
Monster.MonsterStart(b)
Monster.MonsterAddName(b, name2)
monster2 = Monster.MonsterEnd(b)
test1 = b.CreateString(test_string)
Monster.MonsterStartInventoryVector(b, len(test_inventory))
for i in range(0, len(test_inventory)):
b.PrependByte(test_inventory[i])
inv = b.EndVector()
Monster.MonsterStartTest4Vector(b, 2)
Test.CreateTest(b, 10, 20)
Test.CreateTest(b, 30, 40)
test4 = b.EndVector()
Monster.MonsterStartTestarrayofstringVector(b, 1)
b.PrependUOffsetTRelative(test1)
test_array_of_string = b.EndVector()
Monster.MonsterStart(b)
Monster.MonsterAddHp(b, test_hp)
Monster.MonsterAddName(b, name1)
Monster.MonsterAddColor(b, test_color)
pos = Vec3.CreateVec3(b, test_X, test_Y, test_Z, test_test1, test_color, test_a, test_b)
Monster.MonsterAddPos(b, pos)
Monster.MonsterAddInventory(b, inv)
Monster.MonsterAddTestType(b, test_testtype)
Monster.MonsterAddTest(b, monster2)
Monster.MonsterAddTest4(b, test4)
Monster.MonsterAddEnemy(b, monster2)
Monster.MonsterAddTestarrayofstring(b, test_array_of_string)
monster = Monster.MonsterEnd(b)
b.Finish(monster)
stat_response = stub.Store(bytes(b.Output()))
s = Stat.Stat().GetRootAsStat(stat_response, 0)
assert s.Id().decode("utf-8") == test_stat_id
assert s.Val() == test_stat_val
assert s.Count() == test_stat_count
monster_reponses = stub.Retrieve(stat_response)
count = 0
for monster_reponse in monster_reponses:
m = Monster.Monster().GetRootAsMonster(monster_reponse, 0)
assert m.Name().decode("utf-8") == test_monsters_name_retrieve[count]
count = count + 1
if __name__ == '__main__':
serve()

View File

@ -0,0 +1,4 @@
#!/bin/sh
# NOTE: make sure `mvn install` in /gprc is executed before running this test
mvn test

View File

@ -0,0 +1,368 @@
#include "flatbuffers/grpc.h"
#include "monster_test_generated.h"
#include "test_assert.h"
#include "test_builder.h"
using MyGame::Example::Any_NONE;
using MyGame::Example::CreateStat;
using MyGame::Example::Vec3;
bool verify(flatbuffers::grpc::Message<Monster> &msg,
const std::string &expected_name, Color expected_color) {
const Monster *monster = msg.GetRoot();
const auto name = monster->name()->str();
const auto color = monster->color();
TEST_EQ(name, expected_name);
TEST_EQ(color, expected_color);
return (name == expected_name) && (color == expected_color);
}
bool release_n_verify(flatbuffers::grpc::MessageBuilder &mbb,
const std::string &expected_name, Color expected_color) {
flatbuffers::grpc::Message<Monster> msg = mbb.ReleaseMessage<Monster>();
return verify(msg, expected_name, expected_color);
}
void builder_move_assign_after_releaseraw_test(
flatbuffers::grpc::MessageBuilder dst) {
auto root_offset1 = populate1(dst);
dst.Finish(root_offset1);
size_t size, offset;
::grpc::Slice slice;
dst.ReleaseRaw(size, offset, slice);
flatbuffers::FlatBufferBuilder src;
auto root_offset2 = populate2(src);
src.Finish(root_offset2);
auto src_size = src.GetSize();
// Move into a released builder.
dst = std::move(src);
TEST_EQ(dst.GetSize(), src_size);
TEST_ASSERT(release_n_verify(dst, m2_name(), m2_color()));
TEST_EQ(src.GetSize(), 0);
}
template<class SrcBuilder>
struct BuilderReuseTests<flatbuffers::grpc::MessageBuilder, SrcBuilder> {
static void builder_reusable_after_release_message_test(
TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_MESSAGE)) { return; }
flatbuffers::grpc::MessageBuilder mb;
std::vector<flatbuffers::grpc::Message<Monster>> buffers;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(mb);
mb.Finish(root_offset1);
buffers.push_back(mb.ReleaseMessage<Monster>());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
}
}
static void builder_reusable_after_release_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE)) { return; }
// FIXME: Populate-Release loop fails assert(GRPC_SLICE_IS_EMPTY(slice_)) in
// SliceAllocator::allocate in the second iteration.
flatbuffers::grpc::MessageBuilder mb;
std::vector<flatbuffers::DetachedBuffer> buffers;
for (int i = 0; i < 2; ++i) {
auto root_offset1 = populate1(mb);
mb.Finish(root_offset1);
buffers.push_back(mb.Release());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
}
}
static void builder_reusable_after_releaseraw_test(TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW)) { return; }
flatbuffers::grpc::MessageBuilder mb;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(mb);
mb.Finish(root_offset1);
size_t size, offset;
::grpc::Slice slice;
const uint8_t *buf = mb.ReleaseRaw(size, offset, slice);
TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color()));
}
}
static void builder_reusable_after_release_and_move_assign_test(
TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN)) { return; }
// FIXME: Release-move_assign loop fails assert(p ==
// GRPC_SLICE_START_PTR(slice_)) in DetachedBuffer destructor after all the
// iterations
flatbuffers::grpc::MessageBuilder dst;
std::vector<flatbuffers::DetachedBuffer> buffers;
for (int i = 0; i < 2; ++i) {
auto root_offset1 = populate1(dst);
dst.Finish(root_offset1);
buffers.push_back(dst.Release());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
// bring dst back to life.
SrcBuilder src;
dst = std::move(src);
TEST_EQ_FUNC(dst.GetSize(), 0);
TEST_EQ_FUNC(src.GetSize(), 0);
}
}
static void builder_reusable_after_release_message_and_move_assign_test(
TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_MESSAGE_AND_MOVE_ASSIGN)) {
return;
}
flatbuffers::grpc::MessageBuilder dst;
std::vector<flatbuffers::grpc::Message<Monster>> buffers;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(dst);
dst.Finish(root_offset1);
buffers.push_back(dst.ReleaseMessage<Monster>());
TEST_ASSERT_FUNC(verify(buffers[i], m1_name(), m1_color()));
// bring dst back to life.
SrcBuilder src;
dst = std::move(src);
TEST_EQ_FUNC(dst.GetSize(), 0);
TEST_EQ_FUNC(src.GetSize(), 0);
}
}
static void builder_reusable_after_releaseraw_and_move_assign_test(
TestSelector selector) {
if (!selector.count(REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN)) { return; }
flatbuffers::grpc::MessageBuilder dst;
for (int i = 0; i < 5; ++i) {
auto root_offset1 = populate1(dst);
dst.Finish(root_offset1);
size_t size, offset;
::grpc::Slice slice;
const uint8_t *buf = dst.ReleaseRaw(size, offset, slice);
TEST_ASSERT_FUNC(verify(buf, offset, m1_name(), m1_color()));
SrcBuilder src;
dst = std::move(src);
TEST_EQ_FUNC(dst.GetSize(), 0);
TEST_EQ_FUNC(src.GetSize(), 0);
}
}
static void run_tests(TestSelector selector) {
builder_reusable_after_release_test(selector);
builder_reusable_after_release_message_test(selector);
builder_reusable_after_releaseraw_test(selector);
builder_reusable_after_release_and_move_assign_test(selector);
builder_reusable_after_releaseraw_and_move_assign_test(selector);
builder_reusable_after_release_message_and_move_assign_test(selector);
}
};
void slice_allocator_tests() {
// move-construct no-delete test
{
size_t size = 2048;
flatbuffers::grpc::SliceAllocator sa1;
uint8_t *buf = sa1.allocate(size);
TEST_ASSERT_FUNC(buf != 0);
buf[0] = 100;
buf[size - 1] = 200;
flatbuffers::grpc::SliceAllocator sa2(std::move(sa1));
// buf should not be deleted after move-construct
TEST_EQ_FUNC(buf[0], 100);
TEST_EQ_FUNC(buf[size - 1], 200);
// buf is freed here
}
// move-assign test
{
flatbuffers::grpc::SliceAllocator sa1, sa2;
uint8_t *buf = sa1.allocate(2048);
sa1 = std::move(sa2);
// sa1 deletes previously allocated memory in move-assign.
// So buf is no longer usable here.
TEST_ASSERT_FUNC(buf != 0);
}
}
/// This function does not populate exactly the first half of the table. But it
/// could.
void populate_first_half(MyGame::Example::MonsterBuilder &wrapper,
flatbuffers::Offset<flatbuffers::String> name_offset) {
wrapper.add_name(name_offset);
wrapper.add_color(m1_color());
}
/// This function does not populate exactly the second half of the table. But it
/// could.
void populate_second_half(MyGame::Example::MonsterBuilder &wrapper) {
wrapper.add_hp(77);
wrapper.add_mana(88);
Vec3 vec3;
wrapper.add_pos(&vec3);
}
/// This function is a hack to update the FlatBufferBuilder reference (fbb_) in
/// the MonsterBuilder object. This function will break if fbb_ is not the first
/// member in MonsterBuilder. In that case, some offset must be added. This
/// function is used exclusively for testing correctness of move operations
/// between FlatBufferBuilders. If MonsterBuilder had a fbb_ pointer, this hack
/// would be unnecessary. That involves a code-generator change though.
void test_only_hack_update_fbb_reference(
MyGame::Example::MonsterBuilder &monsterBuilder,
flatbuffers::grpc::MessageBuilder &mb) {
*reinterpret_cast<flatbuffers::FlatBufferBuilder **>(&monsterBuilder) = &mb;
}
/// This test validates correctness of move conversion of FlatBufferBuilder to a
/// MessageBuilder DURING a table construction. Half of the table is constructed
/// using FlatBufferBuilder and the other half of the table is constructed using
/// a MessageBuilder.
void builder_move_ctor_conversion_before_finish_half_n_half_table_test() {
for (size_t initial_size = 4; initial_size <= 2048; initial_size *= 2) {
flatbuffers::FlatBufferBuilder fbb(initial_size);
auto name_offset = fbb.CreateString(m1_name());
MyGame::Example::MonsterBuilder monsterBuilder(
fbb); // starts a table in FlatBufferBuilder
populate_first_half(monsterBuilder, name_offset);
flatbuffers::grpc::MessageBuilder mb(std::move(fbb));
test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack
populate_second_half(monsterBuilder);
mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
TEST_EQ_FUNC(fbb.GetSize(), 0);
}
}
/// This test populates a COMPLETE inner table before move conversion and later
/// populates more members in the outer table.
void builder_move_ctor_conversion_before_finish_test() {
for (size_t initial_size = 1; initial_size <= 2048; initial_size += 1) {
flatbuffers::FlatBufferBuilder fbb(initial_size);
auto stat_offset = CreateStat(fbb, fbb.CreateString("SomeId"), 0, 0);
flatbuffers::grpc::MessageBuilder mb(std::move(fbb));
auto monster_offset =
CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name()), 0,
m1_color(), Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset);
mb.Finish(monster_offset);
{
auto mon = flatbuffers::GetRoot<Monster>(mb.GetBufferPointer());
TEST_NOTNULL(mon);
TEST_NOTNULL(mon->name());
TEST_EQ_STR(mon->name()->c_str(), m1_name().c_str());
TEST_EQ(mon->color(), m1_color());
}
TEST_EQ(1, MyGame::Example::Color_Red);
TEST_EQ(1, m1_color());
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
TEST_EQ_FUNC(fbb.GetSize(), 0);
}
}
/// This test validates correctness of move conversion of FlatBufferBuilder to a
/// MessageBuilder DURING a table construction. Half of the table is constructed
/// using FlatBufferBuilder and the other half of the table is constructed using
/// a MessageBuilder.
void builder_move_assign_conversion_before_finish_half_n_half_table_test() {
flatbuffers::FlatBufferBuilder fbb;
flatbuffers::grpc::MessageBuilder mb;
for (int i = 0; i < 5; ++i) {
flatbuffers::FlatBufferBuilder fbb;
auto name_offset = fbb.CreateString(m1_name());
MyGame::Example::MonsterBuilder monsterBuilder(
fbb); // starts a table in FlatBufferBuilder
populate_first_half(monsterBuilder, name_offset);
mb = std::move(fbb);
test_only_hack_update_fbb_reference(monsterBuilder, mb); // hack
populate_second_half(monsterBuilder);
mb.Finish(monsterBuilder.Finish()); // ends the table in MessageBuilder
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
TEST_EQ_FUNC(fbb.GetSize(), 0);
}
}
/// This test populates a COMPLETE inner table before move conversion and later
/// populates more members in the outer table.
void builder_move_assign_conversion_before_finish_test() {
flatbuffers::FlatBufferBuilder fbb;
flatbuffers::grpc::MessageBuilder mb;
for (int i = 0; i < 5; ++i) {
auto stat_offset = CreateStat(fbb, fbb.CreateString("SomeId"), 0, 0);
mb = std::move(fbb);
auto monster_offset =
CreateMonster(mb, 0, 150, 100, mb.CreateString(m1_name()), 0,
m1_color(), Any_NONE, 0, 0, 0, 0, 0, 0, stat_offset);
mb.Finish(monster_offset);
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
TEST_EQ_FUNC(fbb.GetSize(), 0);
}
}
/// This test populates data, finishes the buffer, and does move conversion
/// after.
void builder_move_ctor_conversion_after_finish_test() {
flatbuffers::FlatBufferBuilder fbb;
fbb.Finish(populate1(fbb));
flatbuffers::grpc::MessageBuilder mb(std::move(fbb));
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
TEST_EQ_FUNC(fbb.GetSize(), 0);
}
/// This test populates data, finishes the buffer, and does move conversion
/// after.
void builder_move_assign_conversion_after_finish_test() {
flatbuffers::FlatBufferBuilder fbb;
flatbuffers::grpc::MessageBuilder mb;
for (int i = 0; i < 5; ++i) {
fbb.Finish(populate1(fbb));
mb = std::move(fbb);
TEST_ASSERT_FUNC(release_n_verify(mb, m1_name(), m1_color()));
TEST_EQ_FUNC(fbb.GetSize(), 0);
}
}
void message_builder_tests() {
using flatbuffers::FlatBufferBuilder;
using flatbuffers::grpc::MessageBuilder;
slice_allocator_tests();
#ifndef __APPLE__
builder_move_ctor_conversion_before_finish_half_n_half_table_test();
builder_move_assign_conversion_before_finish_half_n_half_table_test();
#endif // __APPLE__
builder_move_ctor_conversion_before_finish_test();
builder_move_assign_conversion_before_finish_test();
builder_move_ctor_conversion_after_finish_test();
builder_move_assign_conversion_after_finish_test();
BuilderTests<MessageBuilder, MessageBuilder>::all_tests();
BuilderTests<MessageBuilder, FlatBufferBuilder>::all_tests();
BuilderReuseTestSelector tests[6] = {
// REUSABLE_AFTER_RELEASE, // Assertion failed:
// (GRPC_SLICE_IS_EMPTY(slice_))
// REUSABLE_AFTER_RELEASE_AND_MOVE_ASSIGN, // Assertion failed: (p ==
// GRPC_SLICE_START_PTR(slice_)
REUSABLE_AFTER_RELEASE_RAW, REUSABLE_AFTER_RELEASE_MESSAGE,
REUSABLE_AFTER_RELEASE_MESSAGE_AND_MOVE_ASSIGN,
REUSABLE_AFTER_RELEASE_RAW_AND_MOVE_ASSIGN
};
BuilderReuseTests<MessageBuilder, MessageBuilder>::run_tests(
TestSelector(tests, tests + 6));
BuilderReuseTests<MessageBuilder, FlatBufferBuilder>::run_tests(
TestSelector(tests, tests + 6));
}

View File

@ -0,0 +1,73 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-parent</artifactId>
<version>2.0.3</version>
</parent>
<artifactId>grpc-test</artifactId>
<description>Example/Test project demonstrating usage of flatbuffers with GRPC-Java instead of protobufs
</description>
<properties>
<gRPC.version>2.0.3</gRPC.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.flatbuffers</groupId>
<artifactId>flatbuffers-java-grpc</artifactId>
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${gRPC.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${gRPC.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}</source>
<source>${project.basedir}/../../tests</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!--<testSourceDirectory>${project.basedir}</testSourceDirectory>-->
</build>
</project>