server.pb.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. //-{% for service in protoFile.service %}
  43. public enum {{ .|servererror:protoFile,service }} : Error {
  44. case endOfStream
  45. }
  46. /// To build a server, implement a class that conforms to this protocol.
  47. public protocol {{ .|provider:protoFile,service }} {
  48. //-{% for method in service.method %}
  49. //-{% if not method.clientStreaming and not method.serverStreaming %}
  50. func {{ method.name|lowercase }}(request : {{ method|input }}) throws -> {{ method|output }}
  51. //-{% endif %}
  52. //-{% if not method.clientStreaming and method.serverStreaming %}
  53. func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:protoFile,service,method }}) throws
  54. //-{% endif %}
  55. //-{% if method.clientStreaming and not method.serverStreaming %}
  56. func {{ method.name|lowercase }}(session : {{ .|session:protoFile,service,method }}) throws
  57. //-{% endif %}
  58. //-{% if method.clientStreaming and method.serverStreaming %}
  59. func {{ method.name|lowercase }}(session : {{ .|session:protoFile,service,method }}) throws
  60. //-{% endif %}
  61. //-{% endfor %}
  62. }
  63. //-{% for method in service.method %}
  64. //-{% if not method.clientStreaming and not method.serverStreaming %}
  65. //-{% include "server-session-unary.swift" %}
  66. //-{% endif %}
  67. //-{% if not method.clientStreaming and method.serverStreaming %}
  68. //-{% include "server-session-serverstreaming.swift" %}
  69. //-{% endif %}
  70. //-{% if method.clientStreaming and not method.serverStreaming %}
  71. //-{% include "server-session-clientstreaming.swift" %}
  72. //-{% endif %}
  73. //-{% if method.clientStreaming and method.serverStreaming %}
  74. //-{% include "server-session-bidistreaming.swift" %}
  75. //-{% endif %}
  76. //-{% endfor %}
  77. /// Main server for generated service
  78. public class {{ .|server:protoFile,service }} {
  79. private var address: String
  80. private var server: gRPC.Server
  81. private var provider: {{ .|provider:protoFile,service }}?
  82. /// Create a server that accepts insecure connections.
  83. public init(address:String,
  84. provider:{{ .|provider:protoFile,service }}) {
  85. gRPC.initialize()
  86. self.address = address
  87. self.provider = provider
  88. self.server = gRPC.Server(address:address)
  89. }
  90. /// Create a server that accepts secure connections.
  91. public init?(address:String,
  92. certificateURL:URL,
  93. keyURL:URL,
  94. provider:{{ .|provider:protoFile,service }}) {
  95. gRPC.initialize()
  96. self.address = address
  97. self.provider = provider
  98. guard
  99. let certificate = try? String(contentsOf: certificateURL),
  100. let key = try? String(contentsOf: keyURL)
  101. else {
  102. return nil
  103. }
  104. self.server = gRPC.Server(address:address, key:key, certs:certificate)
  105. }
  106. /// Start the server.
  107. public func start(queue:DispatchQueue = DispatchQueue.global()) {
  108. guard let provider = self.provider else {
  109. assert(false) // the server requires a provider
  110. }
  111. server.run {(handler) in
  112. print("Server received request to " + handler.host
  113. + " calling " + handler.method
  114. + " from " + handler.caller)
  115. switch handler.method {
  116. //-{% for method in service.method %}
  117. case "{{ .|path:protoFile,service,method }}":
  118. {{ .|session:protoFile,service,method }}(handler:handler, provider:provider).run(queue:queue)
  119. //-{% endfor %}
  120. default:
  121. break // handle unknown requests
  122. }
  123. }
  124. }
  125. }
  126. //-{% endfor %}