ServiceServer.swift 3.0 KB

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