• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class Greeter: models_GreeterProvider {
24 
25   var interceptors: models_GreeterServerInterceptorFactoryProtocol?
26 
27   let greetings: [String]
28 
29   init() {
30     greetings = ["Hi", "Hallo", "Ciao"]
31   }
32 
33   func SayHello(
34     request: Message<models_HelloRequest>,
35     context: StatusOnlyCallContext) -> EventLoopFuture<Message<models_HelloReply>>
36   {
37     let recipient = request.object.name ?? "Stranger"
38 
39     var builder = FlatBufferBuilder()
40     let off = builder.create(string: "Hello \(recipient)")
41     let root = models_HelloReply.createHelloReply(&builder, messageOffset: off)
42     builder.finish(offset: root)
43     return context.eventLoop.makeSucceededFuture(Message<models_HelloReply>(builder: &builder))
44   }
45 
46   func SayManyHellos(
47     request: Message<models_HelloRequest>,
48     context: StreamingResponseCallContext<Message<models_HelloReply>>) -> EventLoopFuture<GRPCStatus>
49   {
50     for name in greetings {
51       var builder = FlatBufferBuilder()
52       let off = builder.create(string: "\(name) \(request.object.name ?? "Unknown")")
53       let root = models_HelloReply.createHelloReply(&builder, messageOffset: off)
54       builder.finish(offset: root)
55       _ = context.sendResponse(Message<models_HelloReply>(builder: &builder))
56     }
57     return context.eventLoop.makeSucceededFuture(.ok)
58   }
59 }
60 
61 // Quieten the logs.
62 LoggingSystem.bootstrap {
63   var handler = StreamLogHandler.standardOutput(label: $0)
64   handler.logLevel = .critical
65   return handler
66 }
67 
68 let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
69 defer {
70   try! group.syncShutdownGracefully()
71 }
72 
73 // Create some configuration for the server:
74 let configuration = Server.Configuration(
75   target: .hostAndPort("localhost", 0),
76   eventLoopGroup: group,
77   serviceProviders: [Greeter()])
78 
79 // Start the server and print its address once it has started.
80 let server = Server.start(configuration: configuration)
81 server.map {
82   $0.channel.localAddress
83 }.whenSuccess { address in
84   print("server started on port \(address!.port!)")
85 }
86 
87 // Wait on the server's `onClose` future to stop the program from exiting.
88 _ = try server.flatMap {
89   $0.onClose
90 }.wait()
91