1 /*
2 * Copyright 2021 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 import FlatBuffers
18 import GRPC
19 import Logging
20 import Model
21 import NIO
22
23 // Quieten the logs.
24 LoggingSystem.bootstrap {
25 var handler = StreamLogHandler.standardOutput(label: $0)
26 handler.logLevel = .critical
27 return handler
28 }
29
greetnull30 func greet(name: String, client greeter: models_GreeterServiceClient) {
31 // Form the request with the name, if one was provided.
32 var builder = FlatBufferBuilder()
33 let nameOff = builder.create(string: name)
34 let root = models_HelloRequest.createHelloRequest(
35 &builder,
36 nameOffset: nameOff)
37 builder.finish(offset: root)
38
39 // Make the RPC call to the server.
40 let sayHello = greeter.SayHello(Message<models_HelloRequest>(builder: &builder))
41
42 // wait() on the response to stop the program from exiting before the response is received.
43 do {
44 let response = try sayHello.response.wait()
45 print("Greeter SayHello received: \(response.object.message ?? "Unknown")")
46 } catch {
47 print("Greeter failed: \(error)")
48 }
49
50 let surname = builder.create(string: name)
51 let manyRoot = models_HelloRequest.createHelloRequest(
52 &builder,
53 nameOffset: surname)
54 builder.finish(offset: manyRoot)
55
56 let call = greeter.SayManyHellos(Message(builder: &builder)) { message in
57 print("Greeter SayManyHellos received: \(message.object.message ?? "Unknown")")
58 }
59
60 let status = try! call.status.recover { _ in .processingError }.wait()
61 if status.code != .ok {
62 print("RPC failed: \(status)")
63 }
64 }
65
mainnull66 func main(args: [String]) {
67 // arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
68 // the name sent in the request.
69 let arg1 = args.dropFirst(1).first
70 let arg2 = args.dropFirst(2).first
71
72 switch (arg1.flatMap(Int.init), arg2) {
73 case (.none, _):
74 print("Usage: PORT [NAME]")
75 exit(1)
76
77 case let (.some(port), name):
78 // Setup an `EventLoopGroup` for the connection to run on.
79 //
80 // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
81 let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
82
83 // Make sure the group is shutdown when we're done with it.
84 defer {
85 try! group.syncShutdownGracefully()
86 }
87
88 // Configure the channel, we're not using TLS so the connection is `insecure`.
89 let channel = ClientConnection.insecure(group: group)
90 .connect(host: "localhost", port: port)
91
92 // Close the connection when we're done with it.
93 defer {
94 try! channel.close().wait()
95 }
96
97 // Provide the connection to the generated client.
98 let greeter = models_GreeterServiceClient(channel: channel)
99
100 // Do the greeting.
101 greet(name: name ?? "FlatBuffers!", client: greeter)
102 }
103 }
104
105 main(args: CommandLine.arguments)
106