server.pb.swift 5.1 KB

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