...
1/*
2 * Copyright 2023 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
17import FlatBuffers
18import GRPC
19import Logging
20import Model
21import NIO
22
23// Quieten the logs.
24LoggingSystem.bootstrap {
25 var handler = StreamLogHandler.standardOutput(label: $0)
26 handler.logLevel = .critical
27 return handler
28}
29
30func 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
41 .SayHello(Message<models_HelloRequest>(builder: &builder))
42
43 // wait() on the response to stop the program from exiting before the response is received.
44 do {
45 let response = try sayHello.response.wait()
46 print("Greeter SayHello received: \(response.object.message ?? "Unknown")")
47 } catch {
48 print("Greeter failed: \(error)")
49 }
50
51 let surname = builder.create(string: name)
52 let manyRoot = models_HelloRequest.createHelloRequest(
53 &builder,
54 nameOffset: surname)
55 builder.finish(offset: manyRoot)
56
57 let call = greeter.SayManyHellos(Message(builder: &builder)) { message in
58 print(
59 "Greeter SayManyHellos received: \(message.object.message ?? "Unknown")")
60 }
61
62 let status = try! call.status.recover { _ in .processingError }.wait()
63 if status.code != .ok {
64 print("RPC failed: \(status)")
65 }
66}
67
68func main(args: [String]) {
69 // arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
70 // the name sent in the request.
71 let arg1 = args.dropFirst(1).first
72 let arg2 = args.dropFirst(2).first
73
74 switch (arg1.flatMap(Int.init), arg2) {
75 case (.none, _):
76 print("Usage: PORT [NAME]")
77 exit(1)
78
79 case let (.some(port), name):
80 // Setup an `EventLoopGroup` for the connection to run on.
81 //
82 // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
83 let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
84
85 // Make sure the group is shutdown when we're done with it.
86 defer {
87 try! group.syncShutdownGracefully()
88 }
89
90 // Configure the channel, we're not using TLS so the connection is `insecure`.
91 let channel = ClientConnection.insecure(group: group)
92 .connect(host: "localhost", port: port)
93
94 // Close the connection when we're done with it.
95 defer {
96 try! channel.close().wait()
97 }
98
99 // Provide the connection to the generated client.
100 let greeter = models_GreeterServiceClient(channel: channel)
101
102 // Do the greeting.
103 greet(name: name ?? "FlatBuffers!", client: greeter)
104 }
105}
106
107main(args: CommandLine.arguments)
View as plain text