WorkerService.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 NIOConcurrencyHelpers
  18. import NIOCore
  19. @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
  20. final class WorkerService: Grpc_Testing_WorkerService.ServiceProtocol, Sendable {
  21. private let state: NIOLockedValueBox<State>
  22. init() {
  23. let clientAndServer = State()
  24. self.state = NIOLockedValueBox(clientAndServer)
  25. }
  26. private struct State {
  27. var role: Role?
  28. enum Role {
  29. case client(GRPCClient)
  30. case server(GRPCServer)
  31. }
  32. init() {}
  33. init(role: Role) {
  34. self.role = role
  35. }
  36. init(server: GRPCServer) {
  37. self.role = .server(server)
  38. }
  39. init(client: GRPCClient) {
  40. self.role = .client(client)
  41. }
  42. }
  43. func quitWorker(
  44. request: ServerRequest.Single<Grpc_Testing_WorkerService.Method.QuitWorker.Input>
  45. ) async throws -> ServerResponse.Single<Grpc_Testing_WorkerService.Method.QuitWorker.Output> {
  46. let role = self.state.withLockedValue { state in
  47. defer { state.role = nil }
  48. return state.role
  49. }
  50. if let role = role {
  51. switch role {
  52. case .client(let client):
  53. client.close()
  54. case .server(let server):
  55. server.stopListening()
  56. }
  57. }
  58. return ServerResponse.Single(message: Grpc_Testing_WorkerService.Method.QuitWorker.Output())
  59. }
  60. func coreCount(
  61. request: ServerRequest.Single<Grpc_Testing_WorkerService.Method.CoreCount.Input>
  62. ) async throws -> ServerResponse.Single<Grpc_Testing_WorkerService.Method.CoreCount.Output> {
  63. let coreCount = System.coreCount
  64. return ServerResponse.Single(
  65. message: Grpc_Testing_WorkerService.Method.CoreCount.Output.with {
  66. $0.cores = Int32(coreCount)
  67. }
  68. )
  69. }
  70. func runServer(
  71. request: GRPCCore.ServerRequest.Stream<Grpc_Testing_WorkerService.Method.RunServer.Input>
  72. ) async throws
  73. -> GRPCCore.ServerResponse.Stream<Grpc_Testing_WorkerService.Method.RunServer.Output>
  74. {
  75. throw RPCError(code: .unimplemented, message: "This RPC has not been implemented yet.")
  76. }
  77. func runClient(
  78. request: GRPCCore.ServerRequest.Stream<Grpc_Testing_WorkerService.Method.RunClient.Input>
  79. ) async throws
  80. -> GRPCCore.ServerResponse.Stream<Grpc_Testing_WorkerService.Method.RunClient.Output>
  81. {
  82. throw RPCError(code: .unimplemented, message: "This RPC has not been implemented yet.")
  83. }
  84. }