/* * DO NOT EDIT. * * Generated by the protocol buffer compiler. * Source: {{ file.name }} * */ /* * Copyright 2017, gRPC Authors All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import Foundation import Dispatch import gRPC //-{% for service in file.service %} /// Type for errors thrown from generated server code. {{ access }} enum {{ .|servererror:file,service }} : Error { case endOfStream } /// To build a server, implement a class that conforms to this protocol. {{ access }} protocol {{ .|provider:file,service }} { //-{% for method in service.method %} //-{% if not method.clientStreaming and not method.serverStreaming %} func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:file,service,method }}) throws -> {{ method|output }} //-{% endif %} //-{% if not method.clientStreaming and method.serverStreaming %} func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:file,service,method }}) throws //-{% endif %} //-{% if method.clientStreaming and not method.serverStreaming %} func {{ method.name|lowercase }}(session : {{ .|session:file,service,method }}) throws //-{% endif %} //-{% if method.clientStreaming and method.serverStreaming %} func {{ method.name|lowercase }}(session : {{ .|session:file,service,method }}) throws //-{% endif %} //-{% endfor %} } /// Common properties available in each service session. {{ access }} class {{ .|service:file,service }}Session { fileprivate var handler : gRPC.Handler {{ access }} var requestMetadata : Metadata { return handler.requestMetadata } {{ access }} var statusCode : Int = 0 {{ access }} var statusMessage : String = "OK" {{ access }} var initialMetadata : Metadata = Metadata() {{ access }} var trailingMetadata : Metadata = Metadata() fileprivate init(handler:gRPC.Handler) { self.handler = handler } } //-{% for method in service.method %} //-{% if not method.clientStreaming and not method.serverStreaming %} //-{% include "server-session-unary.swift" %} //-{% endif %} //-{% if not method.clientStreaming and method.serverStreaming %} //-{% include "server-session-serverstreaming.swift" %} //-{% endif %} //-{% if method.clientStreaming and not method.serverStreaming %} //-{% include "server-session-clientstreaming.swift" %} //-{% endif %} //-{% if method.clientStreaming and method.serverStreaming %} //-{% include "server-session-bidistreaming.swift" %} //-{% endif %} //-{% endfor %} /// Main server for generated service {{ access }} class {{ .|server:file,service }} { private var address: String private var server: gRPC.Server private var provider: {{ .|provider:file,service }}? /// Create a server that accepts insecure connections. {{ access }} init(address:String, provider:{{ .|provider:file,service }}) { gRPC.initialize() self.address = address self.provider = provider self.server = gRPC.Server(address:address) } /// Create a server that accepts secure connections. {{ access }} init?(address:String, certificateURL:URL, keyURL:URL, provider:{{ .|provider:file,service }}) { gRPC.initialize() self.address = address self.provider = provider guard let certificate = try? String(contentsOf: certificateURL, encoding: .utf8), let key = try? String(contentsOf: keyURL, encoding: .utf8) else { return nil } self.server = gRPC.Server(address:address, key:key, certs:certificate) } /// Start the server. {{ access }} func start(queue:DispatchQueue = DispatchQueue.global()) { guard let provider = self.provider else { assert(false) // the server requires a provider } server.run {(handler) in print("Server received request to " + handler.host + " calling " + handler.method + " from " + handler.caller + " with " + String(describing:handler.requestMetadata) ) do { switch handler.method { //-{% for method in service.method %} case "{{ .|path:file,service,method }}": try {{ .|session:file,service,method }}(handler:handler, provider:provider).run(queue:queue) //-{% endfor %} default: break // handle unknown requests } } catch (let error) { print("Server error: \(error)") } } } } //-{% endfor %}