Browse Source

Cleanup: Replace as many methods on `Handler` with their corresponding `Call` equivalents as possible, so that only server-specific methods are left in `Handler`.

Daniel Alm 7 years ago
parent
commit
ab21b2c464

+ 21 - 68
Sources/SwiftGRPC/Core/Handler.swift

@@ -83,6 +83,25 @@ public class Handler {
       throw CallError.callError(grpcCallError: error)
     }
   }
+  
+  /// Shuts down the handler's completion queue
+  public func shutdown() {
+    completionQueue.shutdown()
+  }
+  
+  /// Send initial metadata in response to a connection
+  ///
+  /// - Parameter initialMetadata: initial metadata to send
+  /// - Parameter completion: a completion handler to call after the metadata has been sent
+  public func sendMetadata(initialMetadata: Metadata,
+                           completion: ((Bool) -> Void)? = nil) throws {
+    let operations = OperationGroup(call: call,
+                                    operations: [.sendInitialMetadata(initialMetadata)],
+                                    completion: completion != nil
+                                      ? { operationGroup in completion?(operationGroup.success) }
+                                      : nil)
+    try call.perform(operations)
+  }
 
   /// Receive the message sent with a call
   ///
@@ -124,71 +143,6 @@ public class Handler {
     try call.perform(operations)
   }
 
-  /// Sends the response to a request
-  ///
-  /// - Parameter statusCode: status code to send
-  /// - Parameter statusMessage: status message to send
-  /// - Parameter trailingMetadata: trailing metadata to send
-  public func sendResponse(statusCode: StatusCode,
-                           statusMessage: String,
-                           trailingMetadata: Metadata) throws {
-    let operations = OperationGroup(call: call,
-                                    operations: [
-                                      .receiveCloseOnServer,
-                                      .sendStatusFromServer(statusCode, statusMessage, trailingMetadata)
-    ]) { operationGroup in
-      self.shutdown()
-    }
-    try call.perform(operations)
-  }
-
-  /// Shuts down the handler's completion queue
-  public func shutdown() {
-    completionQueue.shutdown()
-  }
-
-  /// Send initial metadata in response to a connection
-  ///
-  /// - Parameter initialMetadata: initial metadata to send
-  /// - Parameter completion: a completion handler to call after the metadata has been sent
-  public func sendMetadata(initialMetadata: Metadata,
-                           completion: ((Bool) -> Void)? = nil) throws {
-    let operations = OperationGroup(call: call,
-                                    operations: [.sendInitialMetadata(initialMetadata)],
-                                    completion: completion != nil
-                                      ? { operationGroup in completion?(operationGroup.success) }
-                                      : nil)
-    try call.perform(operations)
-  }
-  
-  /// Receive the message sent with a call
-  ///
-  /// - Parameter completion: a completion handler to call after the message has been received
-  /// - Returns: a tuple containing status codes and a message (if available)
-  public func receiveMessage(completion: @escaping (CallResult) -> Void) throws {
-    try call.receiveMessage(completion: completion)
-  }
-  
-  /// Sends the response to a request
-  ///
-  /// - Parameter message: the message to send
-  /// - Parameter completion: a completion handler to call after the response has been sent
-  public func sendResponse(message: Data,
-                           completion: ((Error?) -> Void)? = nil) throws {
-    try call.sendMessage(data: message, completion: completion)
-  }
-  
-  /// Recognize when the client has closed a request
-  ///
-  /// - Parameter completion: a completion handler to call after request has been closed
-  public func receiveClose(completion: @escaping (Bool) -> Void) throws {
-    let operations = OperationGroup(call: call,
-                                    operations: [.receiveCloseOnServer]) { operationGroup in
-                                      completion(operationGroup.success)
-    }
-    try call.perform(operations)
-  }
-
   /// Send final status to the client
   ///
   /// - Parameter statusCode: status code to send
@@ -201,9 +155,8 @@ public class Handler {
                          completion: ((Bool) -> Void)? = nil) throws {
     let operations = OperationGroup(call: call,
                                     operations: [
-                                      .sendStatusFromServer(statusCode,
-                                                            statusMessage,
-                                                            trailingMetadata)
+                                      .receiveCloseOnServer,
+                                      .sendStatusFromServer(statusCode, statusMessage, trailingMetadata)
     ]) { operationGroup in
       completion?(operationGroup.success)
       self.shutdown()

+ 3 - 3
Sources/SwiftGRPC/Runtime/ServiceServer.swift

@@ -69,9 +69,9 @@ open class ServiceServer {
       do {
         if try !strongSelf.handleMethod(unwrappedMethod, handler: handler, queue: queue) {
           // handle unknown requests
-          try handler.sendResponse(statusCode: .unimplemented,
-                                   statusMessage: "unknown method " + unwrappedMethod,
-                                   trailingMetadata: Metadata())
+          try handler.sendStatus(statusCode: .unimplemented,
+                                 statusMessage: "unknown method " + unwrappedMethod,
+                                 trailingMetadata: Metadata())
         }
       } catch (let error) {
         print("Server error: \(error)")

+ 13 - 13
Tests/SwiftGRPCTests/GRPCTests.swift

@@ -209,7 +209,7 @@ func callServerStream(channel: Channel) throws {
 
   for _ in 0..<steps {
     let messageSem = DispatchSemaphore(value: 0)
-    try call.receiveMessage(completion: { callResult in
+    try call.receiveMessage { callResult in
       if let data = callResult.resultData {
         let messageString = String(data: data, encoding: .utf8)
         XCTAssertEqual(messageString, serverText)
@@ -217,7 +217,7 @@ func callServerStream(channel: Channel) throws {
         print("callServerStream unexpected result: \(callResult)")
       }
       messageSem.signal()
-    })
+    }
 
     _ = messageSem.wait()
   }
@@ -266,7 +266,7 @@ func callBiDiStream(channel: Channel) throws {
   // Receive pongs
   for _ in 0..<steps {
     let pongSem = DispatchSemaphore(value: 0)
-    try call.receiveMessage(completion: { callResult in
+    try call.receiveMessage { callResult in
       if let data = callResult.resultData {
         let messageString = String(data: data, encoding: .utf8)
         XCTAssertEqual(messageString, serverPong)
@@ -274,7 +274,7 @@ func callBiDiStream(channel: Channel) throws {
         print("callBiDiStream unexpected result: \(callResult)")
       }
       pongSem.signal()
-    })
+    }
     _ = pongSem.wait()
   }
 
@@ -332,9 +332,9 @@ func handleUnary(requestHandler: Handler, requestCount: Int) throws {
                                     trailingMetadata: trailingMetadataToSend)
   } else {
     let trailingMetadataToSend = Metadata(trailingServerMetadata)
-    try requestHandler.sendResponse(statusCode: oddStatusCode,
-                                    statusMessage: oddStatusMessage,
-                                    trailingMetadata: trailingMetadataToSend)
+    try requestHandler.sendStatus(statusCode: oddStatusCode,
+                                  statusMessage: oddStatusMessage,
+                                  trailingMetadata: trailingMetadataToSend)
   }
 }
 
@@ -352,9 +352,9 @@ func handleServerStream(requestHandler: Handler) throws {
 
   let replyMessage = serverText
   for _ in 0..<steps {
-    try requestHandler.sendResponse(message: replyMessage.data(using: .utf8)!, completion: { (error) in
+    try requestHandler.call.sendMessage(data: replyMessage.data(using: .utf8)!) { error in
       XCTAssertNil(error)
-    })
+    }
     requestHandler.call.messageQueueEmpty.wait()
   }
 
@@ -380,20 +380,20 @@ func handleBiDiStream(requestHandler: Handler) throws {
   // Receive remaining pings
   for _ in 0..<steps {
     let receiveSem = DispatchSemaphore(value: 0)
-    try requestHandler.receiveMessage(completion: { callStatus in
+    try requestHandler.call.receiveMessage { callStatus in
       let messageString = String(data: callStatus.resultData!, encoding: .utf8)
       XCTAssertEqual(messageString, clientPing)
       receiveSem.signal()
-    })
+    }
     _ = receiveSem.wait()
   }
 
   // Send back pongs
   let replyMessage = serverPong.data(using: .utf8)!
   for _ in 0..<steps {
-    try requestHandler.sendResponse(message: replyMessage, completion: { (error) in
+    try requestHandler.call.sendMessage(data: replyMessage) { error in
       XCTAssertNil(error)
-    })
+    }
     requestHandler.call.messageQueueEmpty.wait()
   }