Browse Source

Update code generator to implement blocking API calls by calling nonblocking calls.

Tim Burks 9 years ago
parent
commit
38dd33ea1c

+ 26 - 49
Examples/Echo/Generated/echo.client.pb.swift

@@ -63,24 +63,18 @@ public class Echo_EchoGetCall {
   fileprivate func run(request: Echo_EchoRequest,
                        metadata: Metadata) throws -> Echo_EchoResponse {
     let sem = DispatchSemaphore(value: 0)
-    var callResult : CallResult!
-    var response : Echo_EchoResponse?
-    let requestData = try request.serializeProtobuf()
-    try call.start(.unary,
-                   metadata:metadata,
-                   message:requestData)
-    {(_callResult) in
-      callResult = _callResult
-      if let responseData = callResult.resultData {
-        response = try? Echo_EchoResponse(protobuf:responseData)
-      }
+    var returnCallResult : CallResult!
+    var returnResponse : Echo_EchoResponse?
+    try start(request:request, metadata:metadata) {response, callResult in
+      returnResponse = response
+      returnCallResult = callResult
       sem.signal()
     }
     _ = sem.wait(timeout: DispatchTime.distantFuture)
-    if let response = response {
-      return response
+    if let returnResponse = returnResponse {
+      return returnResponse
     } else {
-      throw Echo_EchoClientError.error(c: callResult)
+      throw Echo_EchoClientError.error(c: returnCallResult)
     }
   }
 
@@ -115,7 +109,7 @@ public class Echo_EchoExpandCall {
     self.call = channel.makeCall("/echo.Echo/Expand")
   }
 
-  /// Call this once with the message to send.
+  /// Call this once with the message to send. Nonblocking.
   fileprivate func start(request: Echo_EchoRequest,
                          metadata: Metadata,
                          completion: @escaping (CallResult) -> ())
@@ -131,18 +125,12 @@ public class Echo_EchoExpandCall {
   /// Call this to wait for a result. Blocking.
   public func receive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
-    var response : Echo_EchoResponse!
+    var returnResponse : Echo_EchoResponse!
     let sem = DispatchSemaphore(value: 0)
     do {
-      try call.receiveMessage() {(responseData) in
-        if let responseData = responseData {
-          response = try? Echo_EchoResponse(protobuf:responseData)
-          if response == nil {
-            returnError = Echo_EchoClientError.invalidMessageReceived
-          }
-        } else {
-          returnError = Echo_EchoClientError.endOfStream
-        }
+      try receive() {response, error in
+        returnResponse = response
+        returnError = error
         sem.signal()
       }
       _ = sem.wait(timeout: DispatchTime.distantFuture)
@@ -150,7 +138,7 @@ public class Echo_EchoExpandCall {
     if let returnError = returnError {
       throw returnError
     }
-    return response
+    return returnResponse
   }
 
   /// Call this to wait for a result. Nonblocking.
@@ -180,14 +168,14 @@ public class Echo_EchoCollectCall {
     self.call = channel.makeCall("/echo.Echo/Collect")
   }
 
-  /// Call this to start a call.
+  /// Call this to start a call. Nonblocking.
   fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
     throws -> Echo_EchoCollectCall {
       try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
       return self
   }
 
-  /// Call this to send each message in the request stream.
+  /// Call this to send each message in the request stream. Nonblocking.
   public func send(_ message: Echo_EchoRequest) throws {
     let messageData = try message.serializeProtobuf()
     try call.sendMessage(data:messageData)
@@ -199,16 +187,11 @@ public class Echo_EchoCollectCall {
     var returnResponse : Echo_EchoResponse!
     let sem = DispatchSemaphore(value: 0)
     do {
-      try call.receiveMessage() {(responseData) in
-        if let responseData = responseData,
-          let response = try? Echo_EchoResponse(protobuf:responseData) {
-          returnResponse = response
-        } else {
-          returnError = Echo_EchoClientError.invalidMessageReceived
-        }
+      try closeAndReceive() {response, error in
+        returnResponse = response
+        returnError = error
         sem.signal()
       }
-      try call.close(completion:{})
       _ = sem.wait(timeout: DispatchTime.distantFuture)
     } catch (let error) {
       throw error
@@ -254,21 +237,15 @@ public class Echo_EchoUpdateCall {
       return self
   }
 
-  /// Call this to wait for a result. Blocks.
+  /// Call this to wait for a result. Blocking.
   public func receive() throws -> Echo_EchoResponse {
     var returnError : Echo_EchoClientError?
     var returnMessage : Echo_EchoResponse!
     let sem = DispatchSemaphore(value: 0)
     do {
-      try call.receiveMessage() {(data) in
-        if let data = data {
-          returnMessage = try? Echo_EchoResponse(protobuf:data)
-          if returnMessage == nil {
-            returnError = Echo_EchoClientError.invalidMessageReceived
-          }
-        } else {
-          returnError = Echo_EchoClientError.endOfStream
-        }
+      try receive() {response, error in
+        returnMessage = response
+        returnError = error
         sem.signal()
       }
       _ = sem.wait(timeout: DispatchTime.distantFuture)
@@ -302,16 +279,16 @@ public class Echo_EchoUpdateCall {
     try call.sendMessage(data:messageData)
   }
 
-  /// Call this to close the sending connection. Blocking
+  /// Call this to close the sending connection. Blocking.
   public func closeSend() throws {
     let sem = DispatchSemaphore(value: 0)
-    try call.close() {
+    try closeSend() {
       sem.signal()
     }
     _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 
-  /// Call this to close the sending connection. Nonblocking
+  /// Call this to close the sending connection. Nonblocking.
   public func closeSend(completion:@escaping ()->()) throws {
     try call.close() {
       completion()

File diff suppressed because it is too large
+ 0 - 0
Plugin/Sources/protoc-gen-swiftgrpc/templates.swift


+ 7 - 13
Plugin/Templates/client-call-bidistreaming.swift

@@ -14,21 +14,15 @@ public class {{ .|call:protoFile,service,method }} {
       return self
   }
 
-  /// Call this to wait for a result. Blocks.
+  /// Call this to wait for a result. Blocking.
   public func receive() throws -> {{ method|output }} {
     var returnError : {{ .|clienterror:protoFile,service }}?
     var returnMessage : {{ method|output }}!
     let sem = DispatchSemaphore(value: 0)
     do {
-      try call.receiveMessage() {(data) in
-        if let data = data {
-          returnMessage = try? {{ method|output }}(protobuf:data)
-          if returnMessage == nil {
-            returnError = {{ .|clienterror:protoFile,service }}.invalidMessageReceived
-          }
-        } else {
-          returnError = {{ .|clienterror:protoFile,service }}.endOfStream
-        }
+      try receive() {response, error in
+        returnMessage = response
+        returnError = error
         sem.signal()
       }
       _ = sem.wait(timeout: DispatchTime.distantFuture)
@@ -62,16 +56,16 @@ public class {{ .|call:protoFile,service,method }} {
     try call.sendMessage(data:messageData)
   }
 
-  /// Call this to close the sending connection. Blocking
+  /// Call this to close the sending connection. Blocking.
   public func closeSend() throws {
     let sem = DispatchSemaphore(value: 0)
-    try call.close() {
+    try closeSend() {
       sem.signal()
     }
     _ = sem.wait(timeout: DispatchTime.distantFuture)
   }
 
-  /// Call this to close the sending connection. Nonblocking
+  /// Call this to close the sending connection. Nonblocking.
   public func closeSend(completion:@escaping ()->()) throws {
     try call.close() {
       completion()

+ 5 - 10
Plugin/Templates/client-call-clientstreaming.swift

@@ -7,14 +7,14 @@ public class {{ .|call:protoFile,service,method }} {
     self.call = channel.makeCall("{{ .|path:protoFile,service,method }}")
   }
 
-  /// Call this to start a call.
+  /// Call this to start a call. Nonblocking.
   fileprivate func start(metadata:Metadata, completion:@escaping (CallResult)->())
     throws -> {{ .|call:protoFile,service,method }} {
       try self.call.start(.clientStreaming, metadata:metadata, completion:completion)
       return self
   }
 
-  /// Call this to send each message in the request stream.
+  /// Call this to send each message in the request stream. Nonblocking.
   public func send(_ message: {{ method|input }}) throws {
     let messageData = try message.serializeProtobuf()
     try call.sendMessage(data:messageData)
@@ -26,16 +26,11 @@ public class {{ .|call:protoFile,service,method }} {
     var returnResponse : {{ method|output }}!
     let sem = DispatchSemaphore(value: 0)
     do {
-      try call.receiveMessage() {(responseData) in
-        if let responseData = responseData,
-          let response = try? {{ method|output }}(protobuf:responseData) {
-          returnResponse = response
-        } else {
-          returnError = {{ .|clienterror:protoFile,service }}.invalidMessageReceived
-        }
+      try closeAndReceive() {response, error in
+        returnResponse = response
+        returnError = error
         sem.signal()
       }
-      try call.close(completion:{})
       _ = sem.wait(timeout: DispatchTime.distantFuture)
     } catch (let error) {
       throw error

+ 6 - 12
Plugin/Templates/client-call-serverstreaming.swift

@@ -7,7 +7,7 @@ public class {{ .|call:protoFile,service,method }} {
     self.call = channel.makeCall("{{ .|path:protoFile,service,method }}")
   }
 
-  /// Call this once with the message to send.
+  /// Call this once with the message to send. Nonblocking.
   fileprivate func start(request: {{ method|input }},
                          metadata: Metadata,
                          completion: @escaping (CallResult) -> ())
@@ -23,18 +23,12 @@ public class {{ .|call:protoFile,service,method }} {
   /// Call this to wait for a result. Blocking.
   public func receive() throws -> {{ method|output }} {
     var returnError : {{ .|clienterror:protoFile,service }}?
-    var response : {{ method|output }}!
+    var returnResponse : {{ method|output }}!
     let sem = DispatchSemaphore(value: 0)
     do {
-      try call.receiveMessage() {(responseData) in
-        if let responseData = responseData {
-          response = try? {{ method|output }}(protobuf:responseData)
-          if response == nil {
-            returnError = {{ .|clienterror:protoFile,service }}.invalidMessageReceived
-          }
-        } else {
-          returnError = {{ .|clienterror:protoFile,service }}.endOfStream
-        }
+      try receive() {response, error in
+        returnResponse = response
+        returnError = error
         sem.signal()
       }
       _ = sem.wait(timeout: DispatchTime.distantFuture)
@@ -42,7 +36,7 @@ public class {{ .|call:protoFile,service,method }} {
     if let returnError = returnError {
       throw returnError
     }
-    return response
+    return returnResponse
   }
 
   /// Call this to wait for a result. Nonblocking.

+ 8 - 14
Plugin/Templates/client-call-unary.swift

@@ -11,24 +11,18 @@ public class {{ .|call:protoFile,service,method }} {
   fileprivate func run(request: {{ method|input }},
                        metadata: Metadata) throws -> {{ method|output }} {
     let sem = DispatchSemaphore(value: 0)
-    var callResult : CallResult!
-    var response : {{ method|output }}?
-    let requestData = try request.serializeProtobuf()
-    try call.start(.unary,
-                   metadata:metadata,
-                   message:requestData)
-    {(_callResult) in
-      callResult = _callResult
-      if let responseData = callResult.resultData {
-        response = try? {{ method|output }}(protobuf:responseData)
-      }
+    var returnCallResult : CallResult!
+    var returnResponse : {{ method|output }}?
+    try start(request:request, metadata:metadata) {response, callResult in
+      returnResponse = response
+      returnCallResult = callResult
       sem.signal()
     }
     _ = sem.wait(timeout: DispatchTime.distantFuture)
-    if let response = response {
-      return response
+    if let returnResponse = returnResponse {
+      return returnResponse
     } else {
-      throw {{ .|clienterror:protoFile,service }}.error(c: callResult)
+      throw {{ .|clienterror:protoFile,service }}.error(c: returnCallResult)
     }
   }
 

Some files were not shown because too many files changed in this diff