Browse Source

minor API cleanup

Tim Burks 9 years ago
parent
commit
afbf1ed7fb

+ 1 - 1
Examples/Echo/Swift/Echo/EchoServer.swift

@@ -58,7 +58,7 @@ class EchoServer {
     print("GRPC version " + gRPC.version())
 
     server.run {(requestHandler) in
-      print("Received request to " + requestHandler.host
+      print("Server received request to " + requestHandler.host
         + " calling " + requestHandler.method
         + " from " + requestHandler.caller)
 

+ 7 - 5
Examples/Echo/Swift/Echo/EchoViewController.swift

@@ -55,7 +55,9 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
 
   @IBAction func messageReturnPressed(sender: NSTextField) {
     if enabled {
-      if let error = try? callServer(address:addressField.stringValue) {
+      do {
+        try callServer(address:addressField.stringValue)
+      } catch (let error) {
         print(error)
       }
     }
@@ -116,10 +118,10 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
         guard let call = call else {
           return
         }
-        try call.performNonStreamingCall(message: requestMessageData,
-                                         metadata: requestMetadata)
+        try call.perform(message: requestMessageData,
+                         metadata: requestMetadata)
         {(callResult) in
-          print("Received status: \(callResult.statusCode): \(callResult.statusMessage)")
+          print("Client received status \(callResult.statusCode): \(callResult.statusMessage!)")
           if let messageData = callResult.resultData,
             let responseMessage = self.fileDescriptorSet.readMessage("EchoResponse",
                                                                      data:messageData) {
@@ -160,7 +162,7 @@ class EchoViewController : NSViewController, NSTextFieldDelegate {
     requestMessage.addField("text", value:self.messageField.stringValue)
     let messageData = requestMessage.data()
     if let call = call {
-      call.sendMessage(data:messageData)
+      _ = call.sendMessage(data:messageData)
     }
   }
 

+ 3 - 3
Packages/CgRPC/Sources/client.c

@@ -93,9 +93,9 @@ void cgrpc_client_destroy(cgrpc_client *c) {
 }
 
 cgrpc_call *cgrpc_client_create_call(cgrpc_client *client,
-                                           const char *method,
-                                           const char *host,
-                                           double timeout) {
+                                     const char *method,
+                                     const char *host,
+                                     double timeout) {
   // create call
   gpr_timespec deadline = cgrpc_deadline_in_seconds_from_now(timeout);
   grpc_call *client_call = grpc_channel_create_call(client->client,

+ 31 - 19
Packages/gRPC/Sources/Call.swift

@@ -104,6 +104,9 @@ public class Call {
   /// Shared mutex for synchronizing calls to cgrpc_call_perform()
   static let callMutex = Mutex()
 
+  /// Maximum number of messages that can be queued
+  static var maximumQueuedMessages = 10
+
   /// Pointer to underlying C representation
   private var underlyingCall : UnsafeMutableRawPointer
 
@@ -148,7 +151,7 @@ public class Call {
   ///
   /// - Parameter operations: group of operations to be performed
   /// - Returns: the result of initiating the call
-  func perform(_ operations: OperationGroup) throws -> Void {
+  internal func perform(_ operations: OperationGroup) throws -> Void {
     completionQueue.register(operations)
     Call.callMutex.lock()
     let error = cgrpc_call_perform(underlyingCall, operations.underlyingOperations, operations.tag)
@@ -163,9 +166,9 @@ public class Call {
   /// - Parameter message: data containing the message to send
   /// - Parameter metadata: metadata to send with the call
   /// - Parameter callback: a blocko to call with a CallResponse object containing call results
-  public func performNonStreamingCall(message: Data,
-                                      metadata: Metadata,
-                                      completion: @escaping (CallResult) throws -> Void)
+  public func perform(message: Data,
+                      metadata: Metadata,
+                      completion: @escaping (CallResult) throws -> Void)
     throws -> Void {
       let messageBuffer = ByteBuffer(data:message)
       let operations = OperationGroup(call:self,
@@ -195,32 +198,41 @@ public class Call {
       try self.perform(operations)
   }
 
-  // start a streaming connection
+  /// start a streaming connection
+  ///
+  /// Parameter metadata: initial metadata to send
   public func start(metadata: Metadata) throws -> Void {
     try self.sendInitialMetadata(metadata: metadata)
     try self.receiveInitialMetadata()
     try self.receiveStatus()
   }
 
-  // send a message over a streaming connection
-  public func sendMessage(data: Data) -> Void {
-    self.sendMutex.synchronize {
-      if self.writing {
-        self.pendingMessages.append(data) // TODO: return something if we can't accept another message
-      } else {
-        self.writing = true
-        do {
-          try self.sendWithoutBlocking(data: data)
-        } catch (let callError) {
-          print("grpc error: \(callError)")
-        }
+  /// send a message over a streaming connection
+  ///
+  /// Parameter data: the message data to send
+  /// Returns: true if the message could be queued or sent, false if the queue is full
+  public func sendMessage(data: Data) -> Bool {
+    self.sendMutex.lock()
+    defer {self.sendMutex.unlock()}
+    if self.writing {
+      if self.pendingMessages.count == Call.maximumQueuedMessages {
+        return false
+      }
+      self.pendingMessages.append(data) // TODO: return something if we can't accept another message
+    } else {
+      self.writing = true
+      do {
+        try self.sendWithoutBlocking(data: data)
+      } catch (let callError) {
+        print("grpc error: \(callError)")
       }
     }
+    return true
   }
 
+  /// helper for sending queued messages
   private func sendWithoutBlocking(data: Data) throws -> Void {
-    let messageBuffer = ByteBuffer(data:data)
-    let operations = OperationGroup(call:self, operations:[.sendMessage(messageBuffer)])
+    let operations = OperationGroup(call:self, operations:[.sendMessage(ByteBuffer(data:data))])
     {(operationGroup) in
       if operationGroup.success {
         self.messageDispatchQueue.async {