Browse Source

Committing to new API structure in Echo samples.

Tim Burks 9 years ago
parent
commit
cb3e8e80fa

+ 1 - 2
Samples/CLEANUP.sh

@@ -2,5 +2,4 @@
 
 cd Simple/SimpleServer; make clean; cd ../..
 cd Simple/SimpleClient; make clean; cd ../..
-cd Echo-QuickProto/EchoServer; make clean; cd ../..
-cd Echo-QuickProto/EchoClient; make clean; cd ../..
+cd Echo; make clean; cd ../..

+ 0 - 9
Samples/Echo-v2/Makefile

@@ -1,9 +0,0 @@
-LIBDIR = /usr/local/lib
-INCDIR = /usr/local/include
-
-all:
-	swift build -Xlinker -L$(LIBDIR) -Xlinker -lgrpc -Xcc -I$(INCDIR)
-
-clean :
-	rm -rf Packages
-	rm -rf .build

+ 0 - 40
Samples/Echo-v2/Package.swift

@@ -1,40 +0,0 @@
-/*
- *
- * Copyright 2016, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *     * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- *     * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-import PackageDescription
-let package = Package (
-    name: "Echo",
-    dependencies: [
-        .Package(url: "https://github.com/grpc/grpc-swift.git", Version(0,1,0)),
-        .Package(url: "https://github.com/apple/swift-protobuf.git", Version(0,9,24)),
-    ]
-)

+ 0 - 113
Samples/Echo-v2/Sources/echo.pb.swift

@@ -1,113 +0,0 @@
-/*
- * DO NOT EDIT.
- *
- * Generated by the protocol buffer compiler.
- * Source: echo.proto
- *
- */
-
-//  Copyright (c) 2015, Google Inc.
-// 
-//  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 SwiftProtobuf
-
-
-public struct Echo_EchoRequest: ProtobufGeneratedMessage {
-  public var swiftClassName: String {return "Echo_EchoRequest"}
-  public var protoMessageName: String {return "EchoRequest"}
-  public var protoPackageName: String {return "echo"}
-  public var jsonFieldNames: [String: Int] {return [
-    "text": 1,
-  ]}
-  public var protoFieldNames: [String: Int] {return [
-    "text": 1,
-  ]}
-
-  public var text: String = ""
-
-  public init() {}
-
-  public init(text: String? = nil)
-  {
-    if let v = text {
-      self.text = v
-    }
-  }
-
-  public mutating func _protoc_generated_decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
-    let handled: Bool
-    switch protoFieldNumber {
-    case 1: handled = try setter.decodeSingularField(fieldType: ProtobufString.self, value: &text)
-    default:
-      handled = false
-    }
-    return handled
-  }
-
-  public func _protoc_generated_traverse(visitor: inout ProtobufVisitor) throws {
-    if text != "" {
-      try visitor.visitSingularField(fieldType: ProtobufString.self, value: text, protoFieldNumber: 1, protoFieldName: "text", jsonFieldName: "text", swiftFieldName: "text")
-    }
-  }
-
-  public func _protoc_generated_isEqualTo(other: Echo_EchoRequest) -> Bool {
-    if text != other.text {return false}
-    return true
-  }
-}
-
-public struct Echo_EchoResponse: ProtobufGeneratedMessage {
-  public var swiftClassName: String {return "Echo_EchoResponse"}
-  public var protoMessageName: String {return "EchoResponse"}
-  public var protoPackageName: String {return "echo"}
-  public var jsonFieldNames: [String: Int] {return [
-    "text": 1,
-  ]}
-  public var protoFieldNames: [String: Int] {return [
-    "text": 1,
-  ]}
-
-  public var text: String = ""
-
-  public init() {}
-
-  public init(text: String? = nil)
-  {
-    if let v = text {
-      self.text = v
-    }
-  }
-
-  public mutating func _protoc_generated_decodeField(setter: inout ProtobufFieldDecoder, protoFieldNumber: Int) throws -> Bool {
-    let handled: Bool
-    switch protoFieldNumber {
-    case 1: handled = try setter.decodeSingularField(fieldType: ProtobufString.self, value: &text)
-    default:
-      handled = false
-    }
-    return handled
-  }
-
-  public func _protoc_generated_traverse(visitor: inout ProtobufVisitor) throws {
-    if text != "" {
-      try visitor.visitSingularField(fieldType: ProtobufString.self, value: text, protoFieldNumber: 1, protoFieldName: "text", jsonFieldName: "text", swiftFieldName: "text")
-    }
-  }
-
-  public func _protoc_generated_isEqualTo(other: Echo_EchoResponse) -> Bool {
-    if text != other.text {return false}
-    return true
-  }
-}

+ 0 - 195
Samples/Echo-v2/Sources/main.swift

@@ -1,195 +0,0 @@
-/*
- *
- * Copyright 2016, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *     * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- *     * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-import Foundation
-import gRPC
-import QuickProto
-
-print("\(CommandLine.arguments)")
-
-// server options
-var server : Bool = false
-
-// client options
-var client : String = ""
-var message : String = "Testing 1 2 3"
-
-// general configuration
-var useSSL : Bool = false
-
-var i : Int = 0
-while i < Int(CommandLine.argc) {
-  let arg = CommandLine.arguments[i]
-  i = i + 1
-  if i == 1 {
-    continue // skip the first argument
-  }
-
-  if arg == "serve" {
-    server = true
-  } else if (arg == "get") || (arg == "expand") || (arg == "collect") || (arg == "update") {
-    client = arg
-  } else if arg == "-ssl" {
-    useSSL = true
-  } else if arg == "-m" && (i < Int(CommandLine.argc)) {
-    message = CommandLine.arguments[i]
-    i = i + 1
-  }
-}
-
-var done = NSCondition()
-
-gRPC.initialize()
-
-if server {
-  let echoProvider = EchoProvider()
-  var echoServer: Echo_EchoServer!
-
-  if useSSL {
-    print("Starting secure server")
-    let certificateURL = URL(fileURLWithPath:"ssl.crt")
-    let keyURL = URL(fileURLWithPath:"ssl.key")
-    echoServer = Echo_EchoServer(address:"localhost:8443",
-                                 certificateURL:certificateURL,
-                                 keyURL:keyURL,
-                                 provider:echoProvider)
-  } else {
-    print("Starting insecure server")
-    echoServer = Echo_EchoServer(address:"localhost:8081",
-                                 provider:echoProvider)
-  }
-  echoServer.start()
-  // Block to keep the main thread from finishing while the server runs.
-  // This server never exits. Kill the process to stop it.
-  done.lock()
-  done.wait()
-  done.unlock()
-}
-
-if client != "" {
-  print("Starting client")
-
-  var service : Echo_EchoService
-  if useSSL {
-    let certificateURL = URL(fileURLWithPath:"ssl.crt")
-    let certificates = try! String(contentsOf: certificateURL)
-    service = Echo_EchoService(address:"localhost:8443", certificates:certificates, host:"example.com")
-    service.host = "example.com" // sample override
-  } else {
-    service = Echo_EchoService(address:"localhost:8081")
-  }
-
-  let requestMetadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
-                                  "x-ios-bundle-identifier":"com.google.echo"])
-
-  // Unary
-  if client == "get" {
-    var requestMessage = Echo_EchoRequest(text:message)
-    print("Sending: " + requestMessage.text)
-    let responseMessage = try service.get(requestMessage)
-    print("get received: " + responseMessage.text)
-  }
-
-  // Server streaming
-  if client == "expand" {
-    let requestMessage = Echo_EchoRequest(text:message)
-    print("Sending: " + requestMessage.text)
-    let expandCall = try service.expand(requestMessage)
-    var running = true
-    while running {
-      do {
-        let responseMessage = try expandCall.Receive()
-        print("Received: \(responseMessage.text)")
-      } catch Echo_EchoClientError.endOfStream {
-        print("expand closed")
-        running = false
-      }
-    }
-  }
-
-  // Client streaming
-  if client == "collect" {
-    let collectCall = try service.collect()
-
-    let parts = message.components(separatedBy:" ")
-    for part in parts {
-      let requestMessage = Echo_EchoRequest(text:part)
-      print("Sending: " + part)
-      collectCall.Send(requestMessage)
-      sleep(1)
-    }
-
-    let responseMessage = try collectCall.CloseAndReceive()
-    print("Received: \(responseMessage.text)")
-  }
-
-  // Bidirectional streaming
-  if client == "update" {
-    let updateCall = try service.update()
-
-    DispatchQueue.global().async {
-      var running = true
-      while running {
-        do {
-          let responseMessage = try updateCall.Receive()
-          print("Received: \(responseMessage.text)")
-        } catch Echo_EchoClientError.endOfStream {
-          print("update closed")
-          done.lock()
-          done.signal()
-          done.unlock()
-          break
-        } catch (let error) {
-          print("error: \(error)")
-        }
-      }
-    }
-
-    let parts = message.components(separatedBy:" ")
-    for part in parts {
-      let requestMessage = Echo_EchoRequest(text:part)
-      print("Sending: " + requestMessage.text)
-      updateCall.Send(requestMessage)
-      sleep(1)
-    }
-    updateCall.CloseSend()
-
-    // Wait for the call to complete.
-    done.lock()
-    done.wait()
-    done.unlock()
-  }
-  
-}
-
-
-

+ 0 - 17
Samples/Echo-v2/ssl.crt

@@ -1,17 +0,0 @@
------BEGIN CERTIFICATE-----
-MIICqDCCAZACCQC7L1EYvLe1FDANBgkqhkiG9w0BAQsFADAWMRQwEgYDVQQDDAtl
-eGFtcGxlLmNvbTAeFw0xNjAzMzEyMTA3NDZaFw0xNzAzMzEyMTA3NDZaMBYxFDAS
-BgNVBAMMC2V4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC
-AQEAu2KXnItyilAByNapgqAlkOrLP7bcr4FJEuZH5LEu5yGkUT/swcZxKtuvLS5u
-5hU12gr27D9MbS7mSdBMFbayrWOf615vagf+D/ReTnPCvsn/4T+sQn3qdnSgIZJ5
-6dGFpLjk0aeEQX3kctUZxGEC8tUgkoP5W42zZvcEQR3EPyJr5plzzkw6t5ZEOQid
-i6xk0S7rNI2B6KoN78JoCJQr2JoYyrua1tjHB8OCHF+IXmXHhMOjfVgx1s0aqTdR
-BeYVUUrt9pKg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FJOir5EuNenpVZeMeQMktkg806
------END RSA PRIVATE KEY-----

+ 0 - 0
Samples/Echo-v2/Sources/EchoProvider.swift → Samples/Echo/Sources/EchoProvider.swift


+ 0 - 321
Samples/Echo/Sources/EchoServer.swift

@@ -1,321 +0,0 @@
-/*
- *
- * Copyright 2016, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *     * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- *     * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-import Foundation
-import gRPC
-import Darwin // for sleep()
-
-// This seemed like a nice idea but doesn't work because
-// specific message types are in the protocol signatures.
-// There are also functions in the Session classes that depend
-// on specific message types.
-
-protocol UnaryServer {
-  func handle(message:Echo_EchoRequest) -> Echo_EchoResponse?
-}
-
-protocol ServerStreamingServer {
-  func handle(session:ServerStreamingSession, message:Echo_EchoRequest) -> Void
-}
-
-protocol ClientStreamingServer {
-  func handle(session:ClientStreamingSession, message:Echo_EchoRequest) -> Void
-  func close(session:ClientStreamingSession)
-}
-
-protocol BidiStreamingServer {
-  func handle(session:BidiStreamingSession, message:Echo_EchoRequest) -> Void
-}
-
-// nonstreaming
-class UnarySession : Session {
-  var handler : Handler
-  var server : UnaryServer
-
-  init(handler:Handler, server: UnaryServer) {
-    self.handler = handler
-    self.server = server
-  }
-
-  func run() {
-    do {
-      try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
-        if let requestData = requestData {
-          let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
-          if let replyMessage = self.server.handle(message:requestMessage) { // calling stub
-            try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
-                                          statusCode: 0,
-                                          statusMessage: "OK",
-                                          trailingMetadata:Metadata())
-          }
-        }
-      }
-    } catch (let callError) {
-      print("grpc error: \(callError)")
-    }
-  }
-}
-
-// server streaming
-class ServerStreamingSession : Session {
-  var handler : Handler
-  var server : ServerStreamingServer
-
-  init(handler:Handler, server: ServerStreamingServer) {
-    self.handler = handler
-    self.server = server
-  }
-
-  func sendMessage(message:Echo_EchoResponse) -> Void {
-    try! handler.sendResponse(message:message.serializeProtobuf()) {}
-  }
-
-  func close() -> Void {
-    try! self.handler.sendStatus(statusCode:0,
-                                 statusMessage:"OK",
-                                 trailingMetadata:Metadata(),
-                                 completion:{
-
-    })
-  }
-
-  func run() {
-    do {
-      try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
-        if let requestData = requestData {
-          let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
-          self.server.handle(session: self, message:requestMessage)
-        }
-      }
-    } catch (let callError) {
-      print("grpc error: \(callError)")
-    }
-  }
-}
-
-// client streaming
-class ClientStreamingSession : Session {
-  var handler : Handler
-  var server : ClientStreamingServer
-
-  init(handler:Handler, server: ClientStreamingServer) {
-    self.handler = handler
-    self.server = server
-  }
-
-  func sendMessage(message:Echo_EchoResponse) -> Void {
-    try! self.handler.sendResponse(message:message.serializeProtobuf(),
-                                   statusCode: 0,
-                                   statusMessage: "OK",
-                                   trailingMetadata: Metadata())
-  }
-
-  func waitForMessage() {
-    do {
-      try handler.receiveMessage() {(requestData) in
-        if let requestData = requestData {
-
-          let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
-          self.waitForMessage()
-          self.server.handle(session:self, message:requestMessage)
-
-        } else {
-          // if we get an empty message (requestData == nil), we close the connection
-          self.server.close(session:self)
-        }
-      }
-    } catch (let error) {
-      print(error)
-    }
-  }
-
-  func run() {
-    do {
-      try self.handler.sendMetadata(initialMetadata:Metadata()) {
-        self.waitForMessage()
-      }
-    } catch (let callError) {
-      print("grpc error: \(callError)")
-    }
-  }
-}
-
-// fully streaming
-class BidiStreamingSession : Session {
-  var handler : Handler
-  var server : BidiStreamingServer
-
-  init(handler:Handler, server: BidiStreamingServer) {
-    self.handler = handler
-    self.server = server
-  }
-
-  func sendMessage(message:Echo_EchoResponse) -> Void {
-    try! handler.sendResponse(message:message.serializeProtobuf()) {}
-  }
-
-  func waitForMessage() {
-    do {
-      try handler.receiveMessage() {(requestData) in
-        if let requestData = requestData {
-
-          let requestMessage = try! Echo_EchoRequest(protobuf:requestData)
-          self.waitForMessage()
-          self.server.handle(session:self, message:requestMessage)
-
-        } else {
-          // if we get an empty message (requestData == nil), we close the connection
-          try self.handler.sendStatus(statusCode: 0,
-                                      statusMessage: "OK",
-                                      trailingMetadata: Metadata())
-          {
-          }
-        }
-      }
-    } catch (let error) {
-      print(error)
-    }
-  }
-
-  func run() {
-    do {
-      try self.handler.sendMetadata(initialMetadata:Metadata()) {
-        self.waitForMessage()
-      }
-    } catch (let callError) {
-      print("grpc error: \(callError)")
-    }
-  }
-}
-
-class EchoServer {
-  private var address: String
-  private var server: Server
-
-  init(address:String, secure:Bool) {
-    gRPC.initialize()
-    self.address = address
-    if secure {
-      let certificateURL = URL(fileURLWithPath:"ssl.crt")
-      let certificate = try! String(contentsOf: certificateURL)
-      let keyURL = URL(fileURLWithPath:"ssl.key")
-      let key = try! String(contentsOf: keyURL)
-      self.server = gRPC.Server(address:address, key:key, certs:certificate)
-    } else {
-      self.server = gRPC.Server(address:address)
-    }
-  }
-
-  func start() {
-    print("Server Starting")
-    print("GRPC version " + gRPC.version())
-
-    server.run {(handler) in
-      print("Server received request to " + handler.host
-        + " calling " + handler.method
-        + " from " + handler.caller)
-
-      if (handler.method == "/echo.Echo/Get") {
-        handler.session = UnarySession(handler:handler,
-                                       server:EchoGetServer())
-        handler.session.run()
-      }
-
-      else if (handler.method == "/echo.Echo/Expand") {
-        handler.session = ServerStreamingSession(handler:handler,
-                                                 server:EchoExpandServer())
-        handler.session.run()
-      }
-
-      else if (handler.method == "/echo.Echo/Collect") {
-        handler.session = ClientStreamingSession(handler:handler,
-                                                 server:EchoCollectServer())
-        handler.session.run()
-      }
-
-      else if (handler.method == "/echo.Echo/Update") {
-        handler.session = BidiStreamingSession(handler:handler,
-                                               server:EchoUpdateServer())
-        handler.session.run()
-      }
-    }
-  }
-}
-
-// The following code is for developer/users to edit.
-// Everything above these lines is intended to be preexisting or generated.
-
-class EchoGetServer : UnaryServer {
-
-  func handle(message:Echo_EchoRequest) -> Echo_EchoResponse? {
-    return Echo_EchoResponse(text: "Swift echo get: " + message.text)
-  }
-}
-
-class EchoExpandServer : ServerStreamingServer {
-
-  func handle(session:ServerStreamingSession, message:Echo_EchoRequest) -> Void {
-    let parts = message.text.components(separatedBy: " ")
-    var i = 0
-    for part in parts {
-      session.sendMessage(message:Echo_EchoResponse(text:"Swift echo expand (\(i)): \(part)"))
-      i += 1
-      sleep(1)
-    }
-    session.close()
-  }
-}
-
-class EchoCollectServer : ClientStreamingServer {
-  var result = ""
-
-  func handle(session:ClientStreamingSession, message:Echo_EchoRequest) -> Void {
-    if result != "" {
-      result += " "
-    }
-    result += message.text
-  }
-
-  func close(session:ClientStreamingSession) {
-    session.sendMessage(message:Echo_EchoResponse(text:"Swift echo collect: " + result))
-  }
-}
-
-class EchoUpdateServer : BidiStreamingServer {
-  var i = 0
-
-  func handle(session:BidiStreamingSession, message:Echo_EchoRequest) -> Void {
-    session.sendMessage(message:Echo_EchoResponse(text:"Swift echo update (\(i)): \(message.text)"))
-    i += 1
-  }
-}
-

+ 0 - 207
Samples/Echo/Sources/EchoService.swift

@@ -1,207 +0,0 @@
-/*
- *
- * Copyright 2016, Google Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *     * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *     * Redistributions in binary form must reproduce the above
- * copyright notice, this list of conditions and the following disclaimer
- * in the documentation and/or other materials provided with the
- * distribution.
- *     * Neither the name of Google Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
-import Foundation
-import gRPC
-
-// all code that follows is to-be-generated
-
-public class EchoGetCall {
-  var call : Call
-
-  init(_ call: Call) {
-    self.call = call
-  }
-
-  // Call this with the message to send,
-  // the callback will be called after the request is received.
-  func perform(request: Echo_EchoRequest,
-               callback:@escaping (CallResult, Echo_EchoResponse?) -> Void)
-    -> Void {
-      let requestMessageData = try! request.serializeProtobuf()
-      let requestMetadata = Metadata()
-      try! call.perform(message: requestMessageData,
-                        metadata: requestMetadata)
-      {(callResult) in
-        print("Client received status \(callResult.statusCode) \(callResult.statusMessage!)")
-
-        if let messageData = callResult.resultData {
-          let responseMessage = try! Echo_EchoResponse(protobuf:messageData)
-          callback(callResult, responseMessage)
-        } else {
-          callback(callResult, nil)
-        }
-      }
-  }
-}
-
-public class EchoExpandCall {
-  var call : Call
-
-  init(_ call: Call) {
-    self.call = call
-  }
-
-  // Call this once with the message to send,
-  // the callback will be called after the request is initiated.
-  func perform(request: Echo_EchoRequest,
-               callback:@escaping (CallResult, Echo_EchoResponse?) -> Void)
-    -> Void {
-      let requestMessageData = try! request.serializeProtobuf()
-      let requestMetadata = Metadata()
-      try! call.startServerStreaming(message: requestMessageData,
-                                     metadata: requestMetadata)
-      {(callResult) in
-        //print("Client received status \(callResult.statusCode): \(callResult.statusMessage!)")
-      }
-  }
-
-  // Call this to receive a message.
-  // The callback will be called when a message is received.
-  // call this again from the callback to wait for another message.
-  func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void) throws {
-    try call.receiveMessage() {(data) in
-      if let data = data {
-        if let responseMessage = try? Echo_EchoResponse(protobuf:data) {
-          try callback(responseMessage)
-        } else {
-          try callback(nil)
-        }
-      } else {
-        try callback(nil)
-      }
-    }
-  }
-}
-
-public class EchoCollectCall {
-  var call : Call
-
-  init(_ call: Call) {
-    self.call = call
-  }
-
-  // Call this to start a call.
-  func start(metadata:Metadata, completion:@escaping (() -> Void)) throws {
-    try self.call.start(metadata: metadata, completion:completion)
-  }
-
-  // Call this to send each message in the request stream.
-  func sendMessage(message: Echo_EchoRequest) {
-    let messageData = try! message.serializeProtobuf()
-    _ = call.sendMessage(data:messageData)
-  }
-
-  // Call this to receive a message.
-  // The callback will be called when a message is received.
-  // call this again from the callback to wait for another message.
-  func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void)
-    throws {
-      try call.receiveMessage() {(data) in
-        guard
-          let responseMessage = try? Echo_EchoResponse(protobuf:data)
-          else {
-            return
-        }
-        try callback(responseMessage)
-      }
-  }
-
-
-  func close(completion:@escaping (() -> Void)) throws {
-    try call.close(completion:completion)
-  }
-}
-
-public class EchoUpdateCall {
-  var call : Call
-
-  init(_ call: Call) {
-    self.call = call
-  }
-
-  func start(metadata:Metadata, completion:@escaping (() -> Void)) throws {
-    try self.call.start(metadata: metadata, completion:completion)
-  }
-
-  func receiveMessage(callback:@escaping (Echo_EchoResponse?) throws -> Void) throws {
-    try call.receiveMessage() {(data) in
-      guard let data = data
-        else {
-          return
-      }
-      guard
-        let responseMessage = try? Echo_EchoResponse(protobuf:data)
-        else {
-          return
-      }
-      try callback(responseMessage)
-    }
-  }
-
-  func sendMessage(message: Echo_EchoRequest) {
-    let messageData = try! message.serializeProtobuf()
-    _ = call.sendMessage(data:messageData)
-  }
-
-  func close(completion:@escaping (() -> Void)) throws {
-    try call.close(completion:completion)
-  }
-}
-
-public class EchoService {
-  public var channel: Channel
-
-  public init(address: String) {
-    channel = Channel(address:address)
-  }
-
-  public init(address: String, certificates: String?, host: String?) {
-    channel = Channel(address:address, certificates:certificates, host:host)
-  }
-
-  func get() -> EchoGetCall {
-    return EchoGetCall(channel.makeCall("/echo.Echo/Get"))
-  }
-
-  func expand() -> EchoExpandCall {
-    return EchoExpandCall(channel.makeCall("/echo.Echo/Expand"))
-  }
-
-  func collect() -> EchoCollectCall {
-    return EchoCollectCall(channel.makeCall("/echo.Echo/Collect"))
-  }
-
-  func update() -> EchoUpdateCall {
-    return EchoUpdateCall(channel.makeCall("/echo.Echo/Update"))
-  }
-}

+ 0 - 0
Samples/Echo-v2/Sources/echo.client.pb.swift → Samples/Echo/Sources/echo.client.pb.swift


+ 0 - 0
Samples/Echo-v2/Sources/echo.server.pb.swift → Samples/Echo/Sources/echo.server.pb.swift


+ 56 - 110
Samples/Echo/Sources/main.swift

@@ -71,13 +71,21 @@ var done = NSCondition()
 gRPC.initialize()
 
 if server {
-  var echoServer: EchoServer!
+  let echoProvider = EchoProvider()
+  var echoServer: Echo_EchoServer!
+
   if useSSL {
     print("Starting secure server")
-    echoServer = EchoServer(address:"localhost:8443", secure:true)
+    let certificateURL = URL(fileURLWithPath:"ssl.crt")
+    let keyURL = URL(fileURLWithPath:"ssl.key")
+    echoServer = Echo_EchoServer(address:"localhost:8443",
+                                 certificateURL:certificateURL,
+                                 keyURL:keyURL,
+                                 provider:echoProvider)
   } else {
     print("Starting insecure server")
-    echoServer = EchoServer(address:"localhost:8081", secure:false)
+    echoServer = Echo_EchoServer(address:"localhost:8081",
+                                 provider:echoProvider)
   }
   echoServer.start()
   // Block to keep the main thread from finishing while the server runs.
@@ -90,14 +98,14 @@ if server {
 if client != "" {
   print("Starting client")
 
-  var service : EchoService
+  var service : Echo_EchoService
   if useSSL {
     let certificateURL = URL(fileURLWithPath:"ssl.crt")
     let certificates = try! String(contentsOf: certificateURL)
-    service = EchoService(address:"localhost:8443", certificates:certificates, host:"example.com")
-    service.channel.host = "example.com" // sample override
+    service = Echo_EchoService(address:"localhost:8443", certificates:certificates, host:"example.com")
+    service.host = "example.com" // sample override
   } else {
-    service = EchoService(address:"localhost:8081")
+    service = Echo_EchoService(address:"localhost:8081")
   }
 
   let requestMetadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
@@ -105,144 +113,82 @@ if client != "" {
 
   // Unary
   if client == "get" {
-    let getCall = service.get()
-
     var requestMessage = Echo_EchoRequest(text:message)
     print("Sending: " + requestMessage.text)
-    getCall.perform(request:requestMessage) {(callResult, responseMessage) in
-      if let responseMessage = responseMessage {
-        print("Received: " + responseMessage.text)
-      } else {
-        print("No message received. gRPC Status \(callResult.statusCode): \(callResult.statusMessage)")
-      }
-      print("get closed")
-      done.lock()
-      done.signal()
-      done.unlock()
-    }
-    // Wait for the call to complete.
-    done.lock()
-    done.wait()
-    done.unlock()
+    let responseMessage = try service.get(requestMessage)
+    print("get received: " + responseMessage.text)
   }
 
   // Server streaming
   if client == "expand" {
-    let expandCall = service.expand()
-
-    func receiveExpandMessage() throws -> Void {
-      try expandCall.receiveMessage() {(responseMessage) in
-        if let responseMessage = responseMessage {
-          try receiveExpandMessage() // prepare to receive the next message in the stream
-          print("Received: " + responseMessage.text)
-        } else {
-          print("expand closed")
-          done.lock()
-          done.signal()
-          done.unlock()
-        }
-      }
-    }
-
     let requestMessage = Echo_EchoRequest(text:message)
     print("Sending: " + requestMessage.text)
-    expandCall.perform(request:requestMessage) {(callResult, response) in /* do nothing */}
-    try receiveExpandMessage()
-    // Wait for the call to complete.
-    done.lock()
-    done.wait()
-    done.unlock()
+    let expandCall = try service.expand(requestMessage)
+    var running = true
+    while running {
+      do {
+        let responseMessage = try expandCall.Receive()
+        print("Received: \(responseMessage.text)")
+      } catch Echo_EchoClientError.endOfStream {
+        print("expand closed")
+        running = false
+      }
+    }
   }
 
   // Client streaming
   if client == "collect" {
-    let collectCall = service.collect()
-
-    func sendCollectMessage(message: String) {
-      let requestMessage = Echo_EchoRequest(text:message)
-      print("Sending: " + requestMessage.text)
-      _ = collectCall.sendMessage(message:requestMessage)
-    }
-
-    func sendCollectClose() {
-      print("Closing")
-      _ = try! collectCall.close(completion:{})
-    }
-
-    func receiveCollectMessage() throws -> Void {
-      try collectCall.receiveMessage() {(responseMessage) in
-        if let responseMessage = responseMessage {
-          print("Received: " + responseMessage.text)
-        }
-      }
-    }
-
-    try collectCall.start(metadata:requestMetadata) {
-      // this is called when the server closes the connection
-      print("collect closed")
-      done.lock()
-      done.signal()
-      done.unlock()
-    }
-    try receiveCollectMessage()
+    let collectCall = try service.collect()
 
     let parts = message.components(separatedBy:" ")
     for part in parts {
-      sendCollectMessage(message:part)
+      let requestMessage = Echo_EchoRequest(text:part)
+      print("Sending: " + part)
+      collectCall.Send(requestMessage)
       sleep(1)
     }
-    sendCollectClose()
-    // Wait for the call to complete.
-    done.lock()
-    done.wait()
-    done.unlock()
+
+    let responseMessage = try collectCall.CloseAndReceive()
+    print("Received: \(responseMessage.text)")
   }
 
   // Bidirectional streaming
   if client == "update" {
-    let updateCall = service.update()
-
-    func sendUpdateMessage(message: String) {
-      let requestMessage = Echo_EchoRequest(text:message)
-      print("Sending: " + requestMessage.text)
-      _ = updateCall.sendMessage(message:requestMessage)
-    }
-
-    func sendUpdateClose() {
-      print("Closing")
-      _ = try! updateCall.close(completion:{})
-    }
-
-    func receiveUpdateMessage() throws -> Void {
-      try updateCall.receiveMessage() {(responseMessage) in
-        try receiveUpdateMessage() // prepare to receive the next message
-        if let responseMessage = responseMessage {
-          print("Received: " + responseMessage.text)
+    let updateCall = try service.update()
+
+    DispatchQueue.global().async {
+      var running = true
+      while running {
+        do {
+          let responseMessage = try updateCall.Receive()
+          print("Received: \(responseMessage.text)")
+        } catch Echo_EchoClientError.endOfStream {
+          print("update closed")
+          done.lock()
+          done.signal()
+          done.unlock()
+          break
+        } catch (let error) {
+          print("error: \(error)")
         }
       }
     }
 
-    try updateCall.start(metadata:requestMetadata) {
-      // this is called when the server closes the connection
-      print("update closed")
-      done.lock()
-      done.signal()
-      done.unlock()
-    }
-    try receiveUpdateMessage()
-
     let parts = message.components(separatedBy:" ")
     for part in parts {
-      sendUpdateMessage(message:part)
+      let requestMessage = Echo_EchoRequest(text:part)
+      print("Sending: " + requestMessage.text)
+      updateCall.Send(requestMessage)
       sleep(1)
     }
-    sendUpdateClose()
+    updateCall.CloseSend()
 
     // Wait for the call to complete.
     done.lock()
     done.wait()
     done.unlock()
   }
+  
 }