blob: 74729e7e3f0e8df405f11dfd8944d92d056ea9ee [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001/*
2 * Copyright 2020, gRPC Authors 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 GRPC
18import Model
19import NIO
20import Logging
21import FlatBuffers
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: GreeterServiceClient) {
31 // Form the request with the name, if one was provided.
32 var builder = FlatBufferBuilder()
33 let name = builder.create(string: name)
34 let root = HelloRequest.createHelloRequest(&builder, offsetOfName: name)
35 builder.finish(offset: root)
36
37 // Make the RPC call to the server.
38 let sayHello = greeter.SayHello(Message<HelloRequest>(builder: &builder))
39
40 // wait() on the response to stop the program from exiting before the response is received.
41 do {
42 let response = try sayHello.response.wait()
43 print("Greeter received: \(response.object.message)")
44 } catch {
45 print("Greeter failed: \(error)")
46 }
47
48 let surname = builder.create(string: "Name")
49 let manyRoot = ManyHellosRequest.createManyHellosRequest(&builder, offsetOfName: surname, numGreetings: 2)
50 builder.finish(offset: manyRoot)
51
52 let call = greeter.SayManyHellos(Message(builder: &builder)) { message in
53 print(message.object.message)
54 }
55
56 let status = try! call.status.recover { _ in .processingError }.wait()
57 if status.code != .ok {
58 print("RPC failed: \(status)")
59 }
60}
61
62func main(args: [String]) {
63 // arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
64 // the name sent in the request.
65 let arg1 = args.dropFirst(1).first
66 let arg2 = args.dropFirst(2).first
67
68 switch (arg1.flatMap(Int.init), arg2) {
69 case (.none, _):
70 print("Usage: PORT [NAME]")
71 exit(1)
72
73 case let (.some(port), name):
74 // Setup an `EventLoopGroup` for the connection to run on.
75 //
76 // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
77 let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
78
79 // Make sure the group is shutdown when we're done with it.
80 defer {
81 try! group.syncShutdownGracefully()
82 }
83
84 // Configure the channel, we're not using TLS so the connection is `insecure`.
85 let channel = ClientConnection.insecure(group: group)
86 .connect(host: "localhost", port: port)
87
88 // Close the connection when we're done with it.
89 defer {
90 try! channel.close().wait()
91 }
92
93 // Provide the connection to the generated client.
94 let greeter = GreeterServiceClient(channel: channel)
95
96 // Do the greeting.
97 greet(name: name ?? "Hello FlatBuffers!", client: greeter)
98 }
99}
100
101main(args: CommandLine.arguments)