Browse Source

small interface refinements and README edits

Tim Burks 9 years ago
parent
commit
8e930354ac

+ 11 - 7
Packages/gRPC/Sources/Call.swift

@@ -38,7 +38,7 @@ import Foundation
 /// Singleton class that provides a mutex for synchronizing calls to cgrpc_call_perform()
 private class CallLock {
   var mutex : Mutex
-  init() {
+  private init() {
     mutex = Mutex()
   }
   static let sharedInstance = CallLock()
@@ -122,8 +122,8 @@ public class Call {
                                            operation_receiveInitialMetadata,
                                            operation_receiveStatusOnClient,
                                            operation_receiveMessage])
-    { (event) in
-      if (event.type == GRPC_OP_COMPLETE) {
+    { (success) in
+      if success {
         let response = CallResponse(status:operation_receiveStatusOnClient.status(),
                                     statusDetails:operation_receiveStatusOnClient.statusDetails(),
                                     message:operation_receiveMessage.message(),
@@ -131,7 +131,7 @@ public class Call {
                                     trailingMetadata:operation_receiveStatusOnClient.metadata())
         completion(response)
       } else {
-        completion(CallResponse(completion: event.type))
+        completion(CallResponse())
       }
     }
     let call_error = self.perform(call: self, operations: group)
@@ -172,6 +172,10 @@ public class Call {
     let operation_sendMessage = Operation_SendMessage(message:messageBuffer)
     let operations = OperationGroup(call:self, operations:[operation_sendMessage])
     { (event) in
+
+      // if the event failed, shut down
+
+
       DispatchQueue.main.async {
       if self.pendingMessages.count > 0 {
         let nextMessage = self.pendingMessages.first!
@@ -208,9 +212,9 @@ public class Call {
   private func sendInitialMetadata(metadata: Metadata) {
     let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:metadata);
     let operations = OperationGroup(call:self, operations:[operation_sendInitialMetadata])
-    { (event) in
-      if (event.type == GRPC_OP_COMPLETE) {
-        print("call status \(event.type) \(event.tag)")
+    { (success) in
+      if (success) {
+        print("call successful")
       } else {
         return
       }

+ 9 - 0
Packages/gRPC/Sources/CallResponse.swift

@@ -79,6 +79,15 @@ public class CallResponse {
     self.statusDetails = ""
   }
 
+
+  public init() {
+    self.error = GRPC_CALL_ERROR
+    self.completion = GRPC_OP_COMPLETE
+    self.status = 0
+    self.statusDetails = ""
+  }
+
+
   /// Initializes a response when error == GRPC_CALL_OK and completion == GRPC_OP_COMPLETE
   ///
   /// - Parameter status: a status code returned from the server

+ 5 - 2
Packages/gRPC/Sources/CompletionQueue.swift

@@ -73,8 +73,11 @@ class CompletionQueue {
           if let operations = self.operationGroups[tag] {
 
             print("[\(self.name)] event success=\(event.success)")
-
-            operations.completion(event)
+            if event.success == 0 {
+              print("something bad happened")
+            } else {
+              operations.completion(event.success == 1)
+            }
             self.operationGroups[tag] = nil
           }
           break

+ 6 - 6
Packages/gRPC/Sources/Handler.swift

@@ -112,8 +112,8 @@ public class Handler {
       operations:[
         operation_sendInitialMetadata,
         operation_receiveMessage])
-    {(event) in
-      if (event.type == GRPC_OP_COMPLETE) {
+    {(success) in
+      if (success) {
         completion(operation_receiveMessage.message()!.data())
       } else {
         completion(nil)
@@ -160,8 +160,8 @@ public class Handler {
     let call = self.call()
     let operation_sendInitialMetadata = Operation_SendInitialMetadata(metadata:initialMetadata);
     let operations = OperationGroup(call:call, operations:[operation_sendInitialMetadata])
-    {(event) in
-      if (event.type == GRPC_OP_COMPLETE) {
+    {(success) in
+      if (success) {
         completion()
       } else {
         completion()
@@ -178,8 +178,8 @@ public class Handler {
     let call = self.call()
     let operation_receiveMessage = Operation_ReceiveMessage()
     let operations = OperationGroup(call:call, operations:[operation_receiveMessage])
-    {(event) in
-      if (event.type == GRPC_OP_COMPLETE) {
+    {(success) in
+      if (success) {
         print("server receiveMessage complete")
         if let message = operation_receiveMessage.message() {
           completion(message.data())

+ 3 - 4
Packages/gRPC/Sources/OperationGroup.swift

@@ -37,7 +37,7 @@
 /// Singleton class that provides a mutex for synchronizing tag generation
 private class OperationGroupTagLock {
   var mutex : Mutex
-  init() {
+  private init() {
     mutex = Mutex()
   }
   static let sharedInstance = OperationGroupTagLock()
@@ -62,14 +62,14 @@ class OperationGroup {
   var operations : UnsafeMutableRawPointer!
 
   /// Completion handler that is called when the group completes
-  var completion : ((grpc_event) -> Void)
+  var completion : ((Bool) -> Void)
 
   /// Initializes a Operations representation
   ///
   /// - Parameter operations: an array of operations
   init(call: Call,
        operations: [Operation],
-       completion: @escaping ((grpc_event) -> Void)) {
+       completion: @escaping ((Bool) -> Void)) {
     self.call = call
     self.operationsArray = operations
     self.operations = cgrpc_operations_create()
@@ -78,7 +78,6 @@ class OperationGroup {
       cgrpc_operations_add_operation(self.operations, operation.observer)
     }
     self.completion = completion
-
     let mutex = OperationGroupTagLock.sharedInstance.mutex
     mutex.lock()
     self.tag = OperationGroup.nextTag

+ 8 - 6
README.md

@@ -13,12 +13,14 @@ parsing protocol buffers with no generated code.
 
 Code is provided for both gRPC clients and servers,
 and it can be built either with Xcode or the Swift Package Manager.
-The Xcode build is demonstrated with [Sessions](Examples/Sessions), 
-a sample Mac app that can be used to create and run multiple
-concurrent servers and clients. 
-
-Other examples include [StickyNotes](Examples/StickyNotes) and 
-[Echo](Examples/Echo).
+The Xcode build is demonstrated with [Echo](Examples/Echo), 
+a sample Mac app that can be used to run echo clients and
+servers with streaming and nonstreaming interfaces over secure (TLS)
+and insecure channels.
+
+Other examples include [Sessions](Examples/Sessions), 
+[StickyNotes](Examples/StickyNotes), and 
+[Speech](Examples/Speech).
 
 Swift Package Manager builds are demonstrated on Linux using 
 the instructions in the [Packages](Packages) directory.