FooHelloWorldAdopter.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2024, 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 GRPCCore
  17. import GRPCInProcessTransport
  18. import GRPCProtobuf
  19. @main
  20. struct PluginAdopter {
  21. static func main() async throws {
  22. let inProcess = InProcessTransport()
  23. try await withGRPCServer(transport: inProcess.server, services: [Greeter()]) { server in
  24. try await withGRPCClient(transport: inProcess.client) { client in
  25. try await Self.doRPC(Helloworld_Greeter.Client(wrapping: client))
  26. }
  27. }
  28. try await withGRPCServer(transport: inProcess.server, services: [FooService1()]) { server in
  29. try await withGRPCClient(transport: inProcess.client) { client in
  30. try await Self.doRPC(Foo_FooService1.Client(wrapping: client))
  31. }
  32. }
  33. }
  34. static func doRPC<Transport>(_ greeter: Helloworld_Greeter.Client<Transport>) async throws {
  35. do {
  36. let reply = try await greeter.sayHello(.with { $0.name = "(ignored)" })
  37. print("Reply: \(reply.message)")
  38. } catch {
  39. print("Error: \(error)")
  40. }
  41. }
  42. static func doRPC<Transport>(_ fooService1: Foo_FooService1.Client<Transport>) async throws {
  43. do {
  44. let reply = try await fooService1.foo(.with { _ in () })
  45. print("Reply: \(reply.hashValue)")
  46. } catch {
  47. print("Error: \(error)")
  48. }
  49. }
  50. }
  51. struct Greeter: Helloworld_Greeter.SimpleServiceProtocol {
  52. func sayHello(
  53. request: Helloworld_HelloRequest,
  54. context: ServerContext
  55. ) async throws -> Helloworld_HelloReply {
  56. return .with { reply in
  57. reply.message = "Hello, world!"
  58. }
  59. }
  60. }
  61. struct FooService1: Foo_FooService1.SimpleServiceProtocol {
  62. func foo(request: Foo_FooInput, context: GRPCCore.ServerContext) async throws -> Foo_FooOutput {
  63. return .with { _ in
  64. ()
  65. }
  66. }
  67. }