main.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2019, 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. import GRPC
  17. import HelloWorldModel
  18. import NIO
  19. import Logging
  20. // Quieten the logs.
  21. LoggingSystem.bootstrap {
  22. var handler = StreamLogHandler.standardOutput(label: $0)
  23. handler.logLevel = .critical
  24. return handler
  25. }
  26. func greet(name: String?, client greeter: Helloworld_GreeterServiceClient) {
  27. // Form the request with the name, if one was provided.
  28. let request = Helloworld_HelloRequest.with {
  29. $0.name = name ?? ""
  30. }
  31. // Make the RPC call to the server.
  32. let sayHello = greeter.sayHello(request)
  33. // wait() on the response to stop the program from exiting before the response is received.
  34. do {
  35. let response = try sayHello.response.wait()
  36. print("Greeter received: \(response.message)")
  37. } catch {
  38. print("Greeter failed: \(error)")
  39. }
  40. }
  41. func main(args: [String]) {
  42. // arg0 (dropped) is the program name. We expect arg1 to be the port, and arg2 (optional) to be
  43. // the name sent in the request.
  44. let arg1 = args.dropFirst(1).first
  45. let arg2 = args.dropFirst(2).first
  46. switch (arg1.flatMap(Int.init), arg2) {
  47. case (.none, _):
  48. print("Usage: PORT [NAME]")
  49. exit(1)
  50. case let (.some(port), name):
  51. // Setup an `EventLoopGroup` for the connection to run on.
  52. //
  53. // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
  54. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  55. // Make sure the group is shutdown when we're done with it.
  56. defer {
  57. try! group.syncShutdownGracefully()
  58. }
  59. // Provide some basic configuration for the connection, in this case we connect to an endpoint on
  60. // localhost at the given port.
  61. let configuration = ClientConnection.Configuration(
  62. target: .hostAndPort("localhost", port),
  63. eventLoopGroup: group
  64. )
  65. // Create a connection using the configuration.
  66. let connection = ClientConnection(configuration: configuration)
  67. // Provide the connection to the generated client.
  68. let greeter = Helloworld_GreeterServiceClient(connection: connection)
  69. // Do the greeting.
  70. greet(name: name, client: greeter)
  71. }
  72. }
  73. main(args: CommandLine.arguments)