blob: db3206c387a5674e15da130171fc0c2d67caf40d [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001/*
Austin Schuh58b9b472020-11-25 19:12:44 -08002 * Copyright 2020 Google Inc. All rights reserved.
Austin Schuh272c6132020-11-14 16:37:52 -08003 *
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
Austin Schuh58b9b472020-11-25 19:12:44 -080017import FlatBuffers
Austin Schuh272c6132020-11-14 16:37:52 -080018import GRPC
Austin Schuh58b9b472020-11-25 19:12:44 -080019import Logging
Austin Schuh272c6132020-11-14 16:37:52 -080020import Model
21import NIO
Austin Schuh272c6132020-11-14 16:37:52 -080022
23// Quieten the logs.
24LoggingSystem.bootstrap {
Austin Schuh58b9b472020-11-25 19:12:44 -080025 var handler = StreamLogHandler.standardOutput(label: $0)
26 handler.logLevel = .critical
27 return handler
Austin Schuh272c6132020-11-14 16:37:52 -080028}
29
30func greet(name: String, client greeter: GreeterServiceClient) {
Austin Schuh58b9b472020-11-25 19:12:44 -080031 // 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)
Austin Schuh272c6132020-11-14 16:37:52 -080036
Austin Schuh58b9b472020-11-25 19:12:44 -080037 // Make the RPC call to the server.
38 let sayHello = greeter.SayHello(Message<HelloRequest>(builder: &builder))
Austin Schuh272c6132020-11-14 16:37:52 -080039
Austin Schuh58b9b472020-11-25 19:12:44 -080040 // 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 }
Austin Schuh272c6132020-11-14 16:37:52 -080047
Austin Schuh58b9b472020-11-25 19:12:44 -080048 let surname = builder.create(string: "Name")
49 let manyRoot = ManyHellosRequest.createManyHellosRequest(
50 &builder,
51 offsetOfName: surname,
52 numGreetings: 2)
53 builder.finish(offset: manyRoot)
Austin Schuh272c6132020-11-14 16:37:52 -080054
Austin Schuh58b9b472020-11-25 19:12:44 -080055 let call = greeter.SayManyHellos(Message(builder: &builder)) { message in
56 print(message.object.message)
57 }
58
59 let status = try! call.status.recover { _ in .processingError }.wait()
60 if status.code != .ok {
61 print("RPC failed: \(status)")
62 }
Austin Schuh272c6132020-11-14 16:37:52 -080063}
64
65func main(args: [String]) {
Austin Schuh58b9b472020-11-25 19:12:44 -080066 // arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
67 // the name sent in the request.
68 let arg1 = args.dropFirst(1).first
69 let arg2 = args.dropFirst(2).first
Austin Schuh272c6132020-11-14 16:37:52 -080070
Austin Schuh58b9b472020-11-25 19:12:44 -080071 switch (arg1.flatMap(Int.init), arg2) {
72 case (.none, _):
73 print("Usage: PORT [NAME]")
74 exit(1)
Austin Schuh272c6132020-11-14 16:37:52 -080075
Austin Schuh58b9b472020-11-25 19:12:44 -080076 case let (.some(port), name):
77 // Setup an `EventLoopGroup` for the connection to run on.
78 //
79 // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
80 let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
Austin Schuh272c6132020-11-14 16:37:52 -080081
Austin Schuh58b9b472020-11-25 19:12:44 -080082 // Make sure the group is shutdown when we're done with it.
83 defer {
84 try! group.syncShutdownGracefully()
Austin Schuh272c6132020-11-14 16:37:52 -080085 }
Austin Schuh58b9b472020-11-25 19:12:44 -080086
87 // Configure the channel, we're not using TLS so the connection is `insecure`.
88 let channel = ClientConnection.insecure(group: group)
89 .connect(host: "localhost", port: port)
90
91 // Close the connection when we're done with it.
92 defer {
93 try! channel.close().wait()
94 }
95
96 // Provide the connection to the generated client.
97 let greeter = GreeterServiceClient(channel: channel)
98
99 // Do the greeting.
100 greet(name: name ?? "Hello FlatBuffers!", client: greeter)
101 }
Austin Schuh272c6132020-11-14 16:37:52 -0800102}
103
104main(args: CommandLine.arguments)