ServiceServer.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright 2018, 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 Dispatch
  17. import Foundation
  18. import SwiftProtobuf
  19. open class ServiceServer {
  20. public let address: String
  21. public let server: Server
  22. /// Create a server that accepts insecure connections.
  23. public init(address: String) {
  24. gRPC.initialize()
  25. self.address = address
  26. server = Server(address: address)
  27. }
  28. /// Create a server that accepts secure connections.
  29. public init?(address: String, certificateURL: URL, keyURL: URL) {
  30. gRPC.initialize()
  31. self.address = address
  32. guard let certificate = try? String(contentsOf: certificateURL, encoding: .utf8),
  33. let key = try? String(contentsOf: keyURL, encoding: .utf8)
  34. else { return nil }
  35. server = Server(address: address, key: key, certs: certificate)
  36. }
  37. /// Handle the given method. Needs to be overridden by actual implementations.
  38. /// Returns whether the method was actually handled.
  39. open func handleMethod(_ method: String, handler: Handler, queue: DispatchQueue) throws -> Bool { fatalError("needs to be overridden") }
  40. /// Start the server.
  41. public func start(queue: DispatchQueue = DispatchQueue.global()) {
  42. server.run { [weak self] handler in
  43. guard let strongSelf = self else {
  44. print("ERROR: ServiceServer has been asked to handle a request even though it has already been deallocated")
  45. return
  46. }
  47. let unwrappedHost = handler.host ?? "(nil)"
  48. let unwrappedMethod = handler.method ?? "(nil)"
  49. let unwrappedCaller = handler.caller ?? "(nil)"
  50. print("Server received request to " + unwrappedHost
  51. + " calling " + unwrappedMethod
  52. + " from " + unwrappedCaller
  53. + " with " + handler.requestMetadata.description)
  54. do {
  55. if try !strongSelf.handleMethod(unwrappedMethod, handler: handler, queue: queue) {
  56. // handle unknown requests
  57. try handler.receiveMessage(initialMetadata: Metadata()) { _ in
  58. try handler.sendResponse(statusCode: .unimplemented,
  59. statusMessage: "unknown method " + unwrappedMethod,
  60. trailingMetadata: Metadata())
  61. }
  62. }
  63. } catch (let error) {
  64. print("Server error: \(error)")
  65. }
  66. }
  67. }
  68. }