• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 import GRPC
18 import Model
19 import NIO
20 import Logging
21 import FlatBuffers
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: 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     // wait() on the response to stop the program from exiting before the response is received.
40     do {
41         let response = try sayHello.response.wait()
42         print("Greeter received: \(response.object.message)")
43     } catch {
44         print("Greeter failed: \(error)")
45     }
46 
47     let surname = builder.create(string: "Name")
48     let manyRoot = ManyHellosRequest.createManyHellosRequest(builder, offsetOfName: surname, numGreetings: 2)
49     builder.finish(offset: manyRoot)
50 
51     let call = greeter.SayManyHellos(Message(builder: &builder)) { message in
52         print(message.object.message)
53     }
54 
55     let status = try! call.status.recover { _ in .processingError }.wait()
56     if status.code != .ok {
57       print("RPC failed: \(status)")
58     }
59 }
60 
mainnull61 func main(args: [String]) {
62     // arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
63     // the name sent in the request.
64     let arg1 = args.dropFirst(1).first
65     let arg2 = args.dropFirst(2).first
66 
67     switch (arg1.flatMap(Int.init), arg2) {
68     case (.none, _):
69         print("Usage: PORT [NAME]")
70         exit(1)
71 
72     case let (.some(port), name):
73         // Setup an `EventLoopGroup` for the connection to run on.
74         //
75         // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
76         let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
77 
78         // Make sure the group is shutdown when we're done with it.
79         defer {
80             try! group.syncShutdownGracefully()
81         }
82 
83         // Provide some basic configuration for the connection, in this case we connect to an endpoint on
84         // localhost at the given port.
85         let configuration = ClientConnection.Configuration(
86             target: .hostAndPort("localhost", port),
87             eventLoopGroup: group
88         )
89 
90         // Create a connection using the configuration.
91         let connection = ClientConnection(configuration: configuration)
92 
93         // Provide the connection to the generated client.
94         let greeter = GreeterServiceClient(connection: connection)
95 
96         // Do the greeting.
97         greet(name: "Hello FlatBuffers!", client: greeter)
98     }
99 }
100 
101 main(args: CommandLine.arguments)
102