server.pb.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 }}) 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. //-{% for method in service.method %}
  66. //-{% if not method.clientStreaming and not method.serverStreaming %}
  67. //-{% include "server-session-unary.swift" %}
  68. //-{% endif %}
  69. //-{% if not method.clientStreaming and method.serverStreaming %}
  70. //-{% include "server-session-serverstreaming.swift" %}
  71. //-{% endif %}
  72. //-{% if method.clientStreaming and not method.serverStreaming %}
  73. //-{% include "server-session-clientstreaming.swift" %}
  74. //-{% endif %}
  75. //-{% if method.clientStreaming and method.serverStreaming %}
  76. //-{% include "server-session-bidistreaming.swift" %}
  77. //-{% endif %}
  78. //-{% endfor %}
  79. /// Main server for generated service
  80. public class {{ .|server:protoFile,service }} {
  81. private var address: String
  82. private var server: gRPC.Server
  83. private var provider: {{ .|provider:protoFile,service }}?
  84. /// Create a server that accepts insecure connections.
  85. public init(address:String,
  86. provider:{{ .|provider:protoFile,service }}) {
  87. gRPC.initialize()
  88. self.address = address
  89. self.provider = provider
  90. self.server = gRPC.Server(address:address)
  91. }
  92. /// Create a server that accepts secure connections.
  93. public init?(address:String,
  94. certificateURL:URL,
  95. keyURL:URL,
  96. provider:{{ .|provider:protoFile,service }}) {
  97. gRPC.initialize()
  98. self.address = address
  99. self.provider = provider
  100. guard
  101. let certificate = try? String(contentsOf: certificateURL),
  102. let key = try? String(contentsOf: keyURL)
  103. else {
  104. return nil
  105. }
  106. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  107. }
  108. /// Start the server.
  109. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  110. guard let provider = self.provider else {
  111. assert(false) // the server requires a provider
  112. }
  113. server.run {(handler) in
  114. print("Server received request to " + handler.host
  115. + " calling " + handler.method
  116. + " from " + handler.caller)
  117. do {
  118. switch handler.method {
  119. //-{% for method in service.method %}
  120. case "{{ .|path:protoFile,service,method }}":
  121. try {{ .|session:protoFile,service,method }}(handler:handler, provider:provider).run(queue:queue)
  122. //-{% endfor %}
  123. default:
  124. break // handle unknown requests
  125. }
  126. } catch (let error) {
  127. print("Server error: \(error)")
  128. }
  129. }
  130. }
  131. }
  132. //-{% endfor %}