diff options
author | mustiikhalil <mustii@mmk.one> | 2021-02-20 23:07:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-20 23:07:48 +0300 |
commit | b5da526e6d44dfab9b03398af229eebbe835cc1e (patch) | |
tree | b1426b4a2da85f427ca59b528913dda9d3e86360 /grpc | |
parent | 3b5365762d1913abb0078b88b35213ff971dfdc8 (diff) | |
download | flatbuffers-b5da526e6d44dfab9b03398af229eebbe835cc1e.tar.gz flatbuffers-b5da526e6d44dfab9b03398af229eebbe835cc1e.tar.bz2 flatbuffers-b5da526e6d44dfab9b03398af229eebbe835cc1e.zip |
[Swift] Moves grpc example to grpc/examples (#6479)
Updates generated Code
Removes grpc generation code from tests dir
Small fix to generate.sh
Diffstat (limited to 'grpc')
-rw-r--r-- | grpc/examples/generate.sh | 12 | ||||
-rw-r--r-- | grpc/examples/swift/Greeter/Package.swift | 58 | ||||
-rw-r--r-- | grpc/examples/swift/Greeter/README.md | 7 | ||||
-rw-r--r-- | grpc/examples/swift/Greeter/Sources/Model/greeter.grpc.swift | 145 | ||||
-rw-r--r-- | grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift | 70 | ||||
-rw-r--r-- | grpc/examples/swift/Greeter/Sources/client/main.swift | 105 | ||||
-rw-r--r-- | grpc/examples/swift/Greeter/Sources/server/main.swift | 90 |
7 files changed, 484 insertions, 3 deletions
diff --git a/grpc/examples/generate.sh b/grpc/examples/generate.sh index e70f28d6..0d2a900d 100644 --- a/grpc/examples/generate.sh +++ b/grpc/examples/generate.sh @@ -15,12 +15,18 @@ cd go/ cd greeter fbc --go ${generator} -cd ../.. +cd ${current_dir} + +cd swift/ + +cd Greeter/Sources/Model +fbc --swift ${generator} + +cd ${current_dir} cd ts/ cd greeter/src fbc --ts ${generator} -cd .. -cd ../..
\ No newline at end of file +cd ${current_dir}
\ No newline at end of file diff --git a/grpc/examples/swift/Greeter/Package.swift b/grpc/examples/swift/Greeter/Package.swift new file mode 100644 index 00000000..385b56fc --- /dev/null +++ b/grpc/examples/swift/Greeter/Package.swift @@ -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", .exact("1.0.0-alpha.24")), + ], + 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"), + ]) diff --git a/grpc/examples/swift/Greeter/README.md b/grpc/examples/swift/Greeter/README.md new file mode 100644 index 00000000..1632b783 --- /dev/null +++ b/grpc/examples/swift/Greeter/README.md @@ -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}` diff --git a/grpc/examples/swift/Greeter/Sources/Model/greeter.grpc.swift b/grpc/examples/swift/Greeter/Sources/Model/greeter.grpc.swift new file mode 100644 index 00000000..0f29f195 --- /dev/null +++ b/grpc/examples/swift/Greeter/Sources/Model/greeter.grpc.swift @@ -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>>] + +} diff --git a/grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift b/grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift new file mode 100644 index 00000000..4021e958 --- /dev/null +++ b/grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift @@ -0,0 +1,70 @@ +// automatically generated by the FlatBuffers compiler, do not modify +// swiftlint:disable all +// swiftformat:disable all + +import FlatBuffers + +public struct models_HelloReply: FlatBufferObject { + + static func validateVersion() { FlatBuffersVersion_1_12_0() } + public var __buffer: ByteBuffer! { return _accessor.bb } + private var _accessor: Table + + public static func getRootAsHelloReply(bb: ByteBuffer) -> models_HelloReply { return models_HelloReply(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) } + + 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<String>, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: message, at: VTOFFSET.message.p) } + public static func endHelloReply(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset<UOffset> { let end = Offset<UOffset>(offset: fbb.endTable(at: start)); return end } + public static func createHelloReply( + _ fbb: inout FlatBufferBuilder, + messageOffset message: Offset<String> = Offset() + ) -> Offset<UOffset> { + let __start = models_HelloReply.startHelloReply(&fbb) + models_HelloReply.add(message: message, &fbb) + return models_HelloReply.endHelloReply(&fbb, start: __start) + } +} + +public struct models_HelloRequest: FlatBufferObject { + + static func validateVersion() { FlatBuffersVersion_1_12_0() } + public var __buffer: ByteBuffer! { return _accessor.bb } + private var _accessor: Table + + public static func getRootAsHelloRequest(bb: ByteBuffer) -> models_HelloRequest { return models_HelloRequest(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) } + + 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<String>, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: name, at: VTOFFSET.name.p) } + public static func endHelloRequest(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset<UOffset> { let end = Offset<UOffset>(offset: fbb.endTable(at: start)); return end } + public static func createHelloRequest( + _ fbb: inout FlatBufferBuilder, + nameOffset name: Offset<String> = Offset() + ) -> Offset<UOffset> { + let __start = models_HelloRequest.startHelloRequest(&fbb) + models_HelloRequest.add(name: name, &fbb) + return models_HelloRequest.endHelloRequest(&fbb, start: __start) + } +} + diff --git a/grpc/examples/swift/Greeter/Sources/client/main.swift b/grpc/examples/swift/Greeter/Sources/client/main.swift new file mode 100644 index 00000000..a6b11300 --- /dev/null +++ b/grpc/examples/swift/Greeter/Sources/client/main.swift @@ -0,0 +1,105 @@ +/* + * 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. + */ + +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) diff --git a/grpc/examples/swift/Greeter/Sources/server/main.swift b/grpc/examples/swift/Greeter/Sources/server/main.swift new file mode 100644 index 00000000..af1c5557 --- /dev/null +++ b/grpc/examples/swift/Greeter/Sources/server/main.swift @@ -0,0 +1,90 @@ +/* + * 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. + */ + +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() |