Browse Source

Minor updates to Echo sample.

Tim Burks 9 years ago
parent
commit
982c57dcf5
2 changed files with 37 additions and 2 deletions
  1. 10 2
      Samples/Echo/Sources/EchoServer.swift
  2. 27 0
      Samples/Echo/Sources/main.swift

+ 10 - 2
Samples/Echo/Sources/EchoServer.swift

@@ -99,6 +99,15 @@ class ServerStreamingSession : Session {
     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
@@ -287,8 +296,7 @@ class EchoExpandServer : ServerStreamingServer {
       i += 1
       sleep(1)
     }
-    var reply = Echo_EchoResponse()
-    session.sendMessage(message:reply)
+    session.close()
   }
 }
 

+ 27 - 0
Samples/Echo/Sources/main.swift

@@ -167,10 +167,18 @@ if client_get || client_expand || client_collect || client_update {
       _ = collectCall.sendMessage(message:requestMessage)
     }
 
+    func sendClose() {
+      print("Closing")
+      _ = try! collectCall.close(completion:{})
+    }
+
     func receiveCollectMessage() throws -> Void {
       try collectCall.receiveMessage() {(responseMessage) in
         if let responseMessage = responseMessage {
           print("Received: " + responseMessage.text)
+          done.lock()
+          done.signal()
+          done.unlock()
         } else {
           print("collect closed")
           done.lock()
@@ -182,6 +190,13 @@ if client_get || client_expand || client_collect || client_update {
     try collectCall.start(metadata:requestMetadata)
     try receiveCollectMessage()
     sendCollectMessage()
+    sleep(1)
+    sendCollectMessage()
+    sleep(1)
+    sendCollectMessage()
+    sleep(1)
+    sendClose()
+
     done.lock()
     done.wait()
     done.unlock()
@@ -197,6 +212,11 @@ if client_get || client_expand || client_collect || client_update {
       _ = updateCall.sendMessage(message:requestMessage)
     }
 
+    func sendClose() {
+      print("Closing")
+      _ = try! updateCall.close(completion:{})
+    }
+
     func receiveUpdateMessage() throws -> Void {
       try updateCall.receiveMessage() {(responseMessage) in
         try receiveUpdateMessage() // prepare to receive the next message
@@ -214,6 +234,13 @@ if client_get || client_expand || client_collect || client_update {
     try updateCall.start(metadata:requestMetadata)
     try receiveUpdateMessage()
     sendUpdateMessage()
+    sleep(1)
+    sendUpdateMessage()
+    sleep(1)
+    sendUpdateMessage()
+    sleep(1)
+    sendClose()
+    sleep(1)
     done.lock()
     done.wait()
     done.unlock()