server.pb.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * DO NOT EDIT.
  3. *
  4. * Generated by the protocol buffer compiler.
  5. * Source: {{ protoFile.name }}
  6. *
  7. */
  8. /*
  9. *
  10. * Copyright 2016, Google Inc.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are
  15. * met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following disclaimer
  21. * in the documentation and/or other materials provided with the
  22. * distribution.
  23. * * Neither the name of Google Inc. nor the names of its
  24. * contributors may be used to endorse or promote products derived from
  25. * this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  30. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  31. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  34. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  35. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. import Foundation
  41. import gRPC
  42. import Dispatch
  43. //-{% for service in protoFile.service %}
  44. /// Type for errors thrown from generated server code.
  45. public enum {{ .|servererror:protoFile,service }} : Error {
  46. case endOfStream
  47. }
  48. /// To build a server, implement a class that conforms to this protocol.
  49. public protocol {{ .|provider:protoFile,service }} {
  50. //-{% for method in service.method %}
  51. //-{% if not method.clientStreaming and not method.serverStreaming %}
  52. func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:protoFile,service,method }}) throws -> {{ method|output }}
  53. //-{% endif %}
  54. //-{% if not method.clientStreaming and method.serverStreaming %}
  55. func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:protoFile,service,method }}) throws
  56. //-{% endif %}
  57. //-{% if method.clientStreaming and not method.serverStreaming %}
  58. func {{ method.name|lowercase }}(session : {{ .|session:protoFile,service,method }}) throws
  59. //-{% endif %}
  60. //-{% if method.clientStreaming and method.serverStreaming %}
  61. func {{ method.name|lowercase }}(session : {{ .|session:protoFile,service,method }}) throws
  62. //-{% endif %}
  63. //-{% endfor %}
  64. }
  65. /// Common properties available in each service session.
  66. public class {{ .|service:protoFile,service }}Session {
  67. fileprivate var handler : gRPC.Handler
  68. public var requestMetadata : Metadata { return handler.requestMetadata }
  69. public var statusCode : Int = 0
  70. public var statusMessage : String = "OK"
  71. public var initialMetadata : Metadata = Metadata()
  72. public var trailingMetadata : Metadata = Metadata()
  73. fileprivate init(handler:gRPC.Handler) {
  74. self.handler = handler
  75. }
  76. }
  77. //-{% for method in service.method %}
  78. //-{% if not method.clientStreaming and not method.serverStreaming %}
  79. //-{% include "server-session-unary.swift" %}
  80. //-{% endif %}
  81. //-{% if not method.clientStreaming and method.serverStreaming %}
  82. //-{% include "server-session-serverstreaming.swift" %}
  83. //-{% endif %}
  84. //-{% if method.clientStreaming and not method.serverStreaming %}
  85. //-{% include "server-session-clientstreaming.swift" %}
  86. //-{% endif %}
  87. //-{% if method.clientStreaming and method.serverStreaming %}
  88. //-{% include "server-session-bidistreaming.swift" %}
  89. //-{% endif %}
  90. //-{% endfor %}
  91. /// Main server for generated service
  92. public class {{ .|server:protoFile,service }} {
  93. private var address: String
  94. private var server: gRPC.Server
  95. private var provider: {{ .|provider:protoFile,service }}?
  96. /// Create a server that accepts insecure connections.
  97. public init(address:String,
  98. provider:{{ .|provider:protoFile,service }}) {
  99. gRPC.initialize()
  100. self.address = address
  101. self.provider = provider
  102. self.server = gRPC.Server(address:address)
  103. }
  104. /// Create a server that accepts secure connections.
  105. public init?(address:String,
  106. certificateURL:URL,
  107. keyURL:URL,
  108. provider:{{ .|provider:protoFile,service }}) {
  109. gRPC.initialize()
  110. self.address = address
  111. self.provider = provider
  112. guard
  113. let certificate = try? String(contentsOf: certificateURL),
  114. let key = try? String(contentsOf: keyURL)
  115. else {
  116. return nil
  117. }
  118. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  119. }
  120. /// Start the server.
  121. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  122. guard let provider = self.provider else {
  123. assert(false) // the server requires a provider
  124. }
  125. server.run {(handler) in
  126. print("Server received request to " + handler.host
  127. + " calling " + handler.method
  128. + " from " + handler.caller
  129. + " with " + String(describing:handler.requestMetadata) )
  130. do {
  131. switch handler.method {
  132. //-{% for method in service.method %}
  133. case "{{ .|path:protoFile,service,method }}":
  134. try {{ .|session:protoFile,service,method }}(handler:handler, provider:provider).run(queue:queue)
  135. //-{% endfor %}
  136. default:
  137. break // handle unknown requests
  138. }
  139. } catch (let error) {
  140. print("Server error: \(error)")
  141. }
  142. }
  143. }
  144. }
  145. //-{% endfor %}