server.pb.swift 4.8 KB

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