server.pb.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: {{ file.name }}
  6. *
  7. */
  8. /*
  9. * Copyright 2017, gRPC Authors All rights reserved.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. import Foundation
  24. import Dispatch
  25. import gRPC
  26. //-{% for service in file.service %}
  27. /// Type for errors thrown from generated server code.
  28. {{ access }} enum {{ .|servererror:file,service }} : Error {
  29. case endOfStream
  30. }
  31. /// To build a server, implement a class that conforms to this protocol.
  32. {{ access }} protocol {{ .|provider:file,service }} {
  33. //-{% for method in service.method %}
  34. //-{% if not method.clientStreaming and not method.serverStreaming %}
  35. func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:file,service,method }}) throws -> {{ method|output }}
  36. //-{% endif %}
  37. //-{% if not method.clientStreaming and method.serverStreaming %}
  38. func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:file,service,method }}) throws
  39. //-{% endif %}
  40. //-{% if method.clientStreaming and not method.serverStreaming %}
  41. func {{ method.name|lowercase }}(session : {{ .|session:file,service,method }}) throws
  42. //-{% endif %}
  43. //-{% if method.clientStreaming and method.serverStreaming %}
  44. func {{ method.name|lowercase }}(session : {{ .|session:file,service,method }}) throws
  45. //-{% endif %}
  46. //-{% endfor %}
  47. }
  48. /// Common properties available in each service session.
  49. {{ access }} class {{ .|service:file,service }}Session {
  50. fileprivate var handler : gRPC.Handler
  51. {{ access }} var requestMetadata : Metadata { return handler.requestMetadata }
  52. {{ access }} var statusCode : Int = 0
  53. {{ access }} var statusMessage : String = "OK"
  54. {{ access }} var initialMetadata : Metadata = Metadata()
  55. {{ access }} var trailingMetadata : Metadata = Metadata()
  56. fileprivate init(handler:gRPC.Handler) {
  57. self.handler = handler
  58. }
  59. }
  60. //-{% for method in service.method %}
  61. //-{% if not method.clientStreaming and not method.serverStreaming %}
  62. //-{% include "server-session-unary.swift" %}
  63. //-{% endif %}
  64. //-{% if not method.clientStreaming and method.serverStreaming %}
  65. //-{% include "server-session-serverstreaming.swift" %}
  66. //-{% endif %}
  67. //-{% if method.clientStreaming and not method.serverStreaming %}
  68. //-{% include "server-session-clientstreaming.swift" %}
  69. //-{% endif %}
  70. //-{% if method.clientStreaming and method.serverStreaming %}
  71. //-{% include "server-session-bidistreaming.swift" %}
  72. //-{% endif %}
  73. //-{% endfor %}
  74. /// Main server for generated service
  75. {{ access }} class {{ .|server:file,service }} {
  76. private var address: String
  77. private var server: gRPC.Server
  78. private var provider: {{ .|provider:file,service }}?
  79. /// Create a server that accepts insecure connections.
  80. {{ access }} init(address:String,
  81. provider:{{ .|provider:file,service }}) {
  82. gRPC.initialize()
  83. self.address = address
  84. self.provider = provider
  85. self.server = gRPC.Server(address:address)
  86. }
  87. /// Create a server that accepts secure connections.
  88. {{ access }} init?(address:String,
  89. certificateURL:URL,
  90. keyURL:URL,
  91. provider:{{ .|provider:file,service }}) {
  92. gRPC.initialize()
  93. self.address = address
  94. self.provider = provider
  95. guard
  96. let certificate = try? String(contentsOf: certificateURL, encoding: .utf8),
  97. let key = try? String(contentsOf: keyURL, encoding: .utf8)
  98. else {
  99. return nil
  100. }
  101. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  102. }
  103. /// Start the server.
  104. {{ access }} func start(queue:DispatchQueue = DispatchQueue.global()) {
  105. guard let provider = self.provider else {
  106. assert(false) // the server requires a provider
  107. }
  108. server.run {(handler) in
  109. print("Server received request to " + handler.host
  110. + " calling " + handler.method
  111. + " from " + handler.caller
  112. + " with " + String(describing:handler.requestMetadata) )
  113. do {
  114. switch handler.method {
  115. //-{% for method in service.method %}
  116. case "{{ .|path:file,service,method }}":
  117. try {{ .|session:file,service,method }}(handler:handler, provider:provider).run(queue:queue)
  118. //-{% endfor %}
  119. default:
  120. break // handle unknown requests
  121. }
  122. } catch (let error) {
  123. print("Server error: \(error)")
  124. }
  125. }
  126. }
  127. }
  128. //-{% endfor %}