Browse Source

Convert Handler properties to lazy vars (jconverse@)

Tim Burks 9 years ago
parent
commit
df50ba7a1a

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

@@ -58,12 +58,12 @@ class EchoServer {
     print("GRPC version " + gRPC.version())
 
      server.run {(requestHandler) in
-      print("Received request to " + requestHandler.host()
-        + " calling " + requestHandler.method()
-        + " from " + requestHandler.caller())
+      print("Received request to " + requestHandler.host
+        + " calling " + requestHandler.method
+        + " from " + requestHandler.caller)
 
       // NONSTREAMING
-      if (requestHandler.method() == "/echo.Echo/Get") {
+      if (requestHandler.method == "/echo.Echo/Get") {
         requestHandler.receiveMessage(initialMetadata:Metadata())
         {(requestData) in
           if let requestData = requestData,
@@ -80,7 +80,7 @@ class EchoServer {
       }
 
       // STREAMING
-      if (requestHandler.method() == "/echo.Echo/Update") {
+      if (requestHandler.method == "/echo.Echo/Update") {
         requestHandler.sendMetadata(
           initialMetadata: Metadata(),
           completion: {

+ 4 - 4
Examples/Sessions/Sessions/Document.swift

@@ -219,9 +219,9 @@ class Document: NSDocument {
 
       requestCount += 1
 
-      self.log("\(requestCount): Received request " + requestHandler.host()
-        + " " + requestHandler.method()
-        + " from " + requestHandler.caller())
+      self.log("\(requestCount): Received request " + requestHandler.host
+        + " " + requestHandler.method
+        + " from " + requestHandler.caller)
 
       let initialMetadata = requestHandler.requestMetadata
 
@@ -239,7 +239,7 @@ class Document: NSDocument {
         self.log("\(requestCount): Received message: " + messageString!)
       }
 
-      if requestHandler.method() == "/quit" {
+      if requestHandler.method == "/quit" {
         self.stop()
       }
 

+ 4 - 4
Examples/StickyNotes/StickyNotes/StickyServer.swift

@@ -57,16 +57,16 @@ class StickyServer {
       let server = gRPC.Server(address:self.address)
       server.run {(requestHandler) in
 
-        self.log("Received request to " + requestHandler.host()
-          + " calling " + requestHandler.method()
-          + " from " + requestHandler.caller())
+        self.log("Received request to " + requestHandler.host
+          + " calling " + requestHandler.method
+          + " from " + requestHandler.caller)
         let initialMetadata = requestHandler.requestMetadata
         for i in 0..<initialMetadata.count() {
           self.log("Received initial metadata -> " + initialMetadata.key(index:i)
             + ":" + initialMetadata.value(index:i))
         }
 
-        if (requestHandler.method() == "/messagepb.StickyNote/Get") {
+        if (requestHandler.method == "/messagepb.StickyNote/Get") {
           requestHandler.receiveMessage(initialMetadata:Metadata())
           {(requestData) in
             if let requestData = requestData,

+ 46 - 38
Packages/gRPC/Sources/Handler.swift

@@ -47,7 +47,29 @@ public class Handler {
   public var requestMetadata: Metadata
 
   /// A Call object that can be used to respond to the request
-  var call: Call!
+  lazy var call: Call = {
+    return Call(call: cgrpc_handler_get_call(self.underlyingHandler),
+                owned: false,
+                completionQueue: self.completionQueue)
+    }()
+
+  /// The host name sent with the request
+  public lazy var host: String = {
+    return String(cString:cgrpc_handler_host(self.underlyingHandler),
+                  encoding:.utf8)!;
+  }()
+
+  /// The method name sent with the request
+  public lazy var method: String = {
+    return String(cString:cgrpc_handler_method(self.underlyingHandler),
+                  encoding:.utf8)!;
+  }()
+
+  /// The caller address associated with the request
+  public lazy var caller: String = {
+    return String(cString:cgrpc_handler_call_peer(self.underlyingHandler),
+                  encoding:.utf8)!;
+  }()
 
   /// Initializes a Handler
   ///
@@ -55,7 +77,8 @@ public class Handler {
   init(underlyingHandler:UnsafeMutableRawPointer!) {
     self.underlyingHandler = underlyingHandler;
     self.requestMetadata = Metadata()
-    self.completionQueue = CompletionQueue(underlyingCompletionQueue:cgrpc_handler_get_completion_queue(underlyingHandler))
+    self.completionQueue = CompletionQueue(
+      underlyingCompletionQueue:cgrpc_handler_get_completion_queue(underlyingHandler))
     self.completionQueue.name = "Handler"
   }
 
@@ -63,27 +86,6 @@ public class Handler {
     cgrpc_handler_destroy(self.underlyingHandler)
   }
 
-  /// Gets the host name sent with the request
-  ///
-  /// - Returns: the host name sent with the request
-  public func host() -> String {
-    return String(cString:cgrpc_handler_host(underlyingHandler), encoding:String.Encoding.utf8)!;
-  }
-
-  /// Gets the method name sent with the request
-  ///
-  /// - Returns: the method name sent with the request
-  public func method() -> String {
-    return String(cString:cgrpc_handler_method(underlyingHandler), encoding:String.Encoding.utf8)!;
-  }
-
-  /// Gets the caller identity associated with the request
-  ///
-  /// - Returns: a string representing the caller address
-  public func caller() -> String {
-    return String(cString:cgrpc_handler_call_peer(underlyingHandler), encoding:String.Encoding.utf8)!;
-  }
-
   /// Requests a call for the handler
   ///
   /// Fills the handler properties with information about the received request
@@ -94,14 +96,6 @@ public class Handler {
     return CallError.callError(grpcCallError: error)
   }
 
-  /// Prepares the handler's call object for response handling
-  ///
-  func prepareCall() -> Void {
-    self.call = Call(call: cgrpc_handler_get_call(underlyingHandler),
-                     owned: false,
-                     completionQueue: self.completionQueue)
-  }
-
   /// Receive the message sent with a call
   ///
   /// - Returns: a tuple containing status codes and a message (if available)
@@ -122,7 +116,9 @@ public class Handler {
       }
     }
     self.completionQueue.operationGroups[operations.tag] = operations
-    _ = call.performOperations(operations:operations, tag:operations.tag, completionQueue: self.completionQueue)
+    _ = call.performOperations(operations:operations,
+                               tag:operations.tag,
+                               completionQueue: self.completionQueue)
   }
 
   /// Sends the response to a request
@@ -147,7 +143,9 @@ public class Handler {
       self.shutdown()
     }
     self.completionQueue.operationGroups[operations.tag] = operations
-    _ = call.performOperations(operations:operations, tag:operations.tag, completionQueue: self.completionQueue)
+    _ = call.performOperations(operations:operations,
+                               tag:operations.tag,
+                               completionQueue: self.completionQueue)
   }
 
   /// shutdown the handler's completion queue
@@ -168,7 +166,9 @@ public class Handler {
       }
     }
     self.completionQueue.operationGroups[operations.tag] = operations
-    _ = call.performOperations(operations:operations, tag:operations.tag, completionQueue: self.completionQueue)
+    _ = call.performOperations(operations:operations,
+                               tag:operations.tag,
+                               completionQueue: self.completionQueue)
   }
 
   /// Receive the message sent with a call
@@ -190,7 +190,9 @@ public class Handler {
       }
     }
     self.completionQueue.operationGroups[operations.tag] = operations
-    let call_error = call.performOperations(operations:operations, tag:operations.tag, completionQueue: self.completionQueue)
+    let call_error = call.performOperations(operations:operations,
+                                            tag:operations.tag,
+                                            completionQueue: self.completionQueue)
     print("perform receiveMessage \(call_error)")
   }
 
@@ -207,7 +209,9 @@ public class Handler {
       completion()
     }
     self.completionQueue.operationGroups[operations.tag] = operations
-    _ = call.performOperations(operations:operations, tag:operations.tag, completionQueue: self.completionQueue)
+    _ = call.performOperations(operations:operations,
+                               tag:operations.tag,
+                               completionQueue: self.completionQueue)
   }
 
   /// Recognize when the client has closed a request
@@ -219,7 +223,9 @@ public class Handler {
       completion()
     }
     self.completionQueue.operationGroups[operations.tag] = operations
-    let call_error = call.performOperations(operations:operations, tag:operations.tag, completionQueue: self.completionQueue)
+    let call_error = call.performOperations(operations:operations,
+                                            tag:operations.tag,
+                                            completionQueue: self.completionQueue)
     print("perform receiveClose \(call_error)")
   }
 
@@ -235,6 +241,8 @@ public class Handler {
       completion()
     }
     self.completionQueue.operationGroups[operations.tag] = operations
-    _ = call.performOperations(operations:operations, tag:operations.tag, completionQueue: self.completionQueue)
+    _ = call.performOperations(operations:operations,
+                               tag:operations.tag,
+                               completionQueue: self.completionQueue)
   }
 }

+ 0 - 2
Packages/gRPC/Sources/Server.swift

@@ -87,7 +87,6 @@ public class Server {
         let event = self.completionQueue.waitForCompletion(timeout:600)
         if (event.type == .complete) {
           if event.tag == 101 {
-            handler.prepareCall()
             // run the handler and remove it when it finishes
             if event.success != 0 {
               self.handlers.add(handler)
@@ -134,7 +133,6 @@ public class Server {
         handler.completionQueue.run() {
           self.handlers.remove(handler)
         }
-        handler.prepareCall()
         self.handlers.add(handler)
         return (.ok, .complete, handler)
       } else {