Browse Source

Add common server session properties for response status and metadata.

Tim Burks 9 years ago
parent
commit
776056d566

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

@@ -36,7 +36,7 @@ import Foundation
 class EchoProvider : Echo_EchoProvider {
 
   // get returns requests as they were received.
-  func get(request : Echo_EchoRequest) throws -> Echo_EchoResponse {
+  func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse {
     return Echo_EchoResponse(text:"Swift echo get: " + request.text)
   }
 

+ 32 - 22
Examples/Echo/Swift/Generated/echo.server.pb.swift

@@ -50,13 +50,23 @@ public enum Echo_EchoServerError : Error {
 
 /// To build a server, implement a class that conforms to this protocol.
 public protocol Echo_EchoProvider {
-  func get(request : Echo_EchoRequest) throws -> Echo_EchoResponse
+  func get(request : Echo_EchoRequest, session : Echo_EchoGetSession) throws -> Echo_EchoResponse
   func expand(request : Echo_EchoRequest, session : Echo_EchoExpandSession) throws
   func collect(session : Echo_EchoCollectSession) throws
   func update(session : Echo_EchoUpdateSession) throws
 }
+
+/// Common properties available in each service session.
+public class Echo_EchoSession {
+  var statusCode : Int = 0
+  var statusMessage : String = "OK"
+  var initialMetadata : Metadata = Metadata()
+  var trailingMetadata : Metadata = Metadata()
+  var receivedMetadata : Metadata = Metadata()
+}
+
 // Get (Unary)
-public class Echo_EchoGetSession {
+public class Echo_EchoGetSession : Echo_EchoSession {
   private var handler : gRPC.Handler
   private var provider : Echo_EchoProvider
 
@@ -68,21 +78,21 @@ public class Echo_EchoGetSession {
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
+    try handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
       if let requestData = requestData {
         let requestMessage = try Echo_EchoRequest(protobuf:requestData)
-        let replyMessage = try self.provider.get(request:requestMessage)
+        let replyMessage = try self.provider.get(request:requestMessage, session: self)
         try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
-                                      statusCode: 0,
-                                      statusMessage: "OK",
-                                      trailingMetadata:Metadata())
+                                      statusCode:self.statusCode,
+                                      statusMessage:self.statusMessage,
+                                      trailingMetadata:self.trailingMetadata)
       }
     }
   }
 }
 
 // Expand (Server Streaming)
-public class Echo_EchoExpandSession {
+public class Echo_EchoExpandSession : Echo_EchoSession {
   private var handler : gRPC.Handler
   private var provider : Echo_EchoProvider
 
@@ -99,7 +109,7 @@ public class Echo_EchoExpandSession {
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try self.handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
+    try self.handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
       if let requestData = requestData {
         do {
           let requestMessage = try Echo_EchoRequest(protobuf:requestData)
@@ -108,9 +118,9 @@ public class Echo_EchoExpandSession {
           queue.async {
             do {
               try self.provider.expand(request:requestMessage, session: self)
-              try self.handler.sendStatus(statusCode:0,
-                                          statusMessage:"OK",
-                                          trailingMetadata:Metadata(),
+              try self.handler.sendStatus(statusCode:self.statusCode,
+                                          statusMessage:self.statusMessage,
+                                          trailingMetadata:self.trailingMetadata,
                                           completion:{})
             } catch (let error) {
               print("error: \(error)")
@@ -125,7 +135,7 @@ public class Echo_EchoExpandSession {
 }
 
 // Collect (Client Streaming)
-public class Echo_EchoCollectSession {
+public class Echo_EchoCollectSession : Echo_EchoSession {
   private var handler : gRPC.Handler
   private var provider : Echo_EchoProvider
 
@@ -159,14 +169,14 @@ public class Echo_EchoCollectSession {
   /// Send a response and close the connection.
   public func SendAndClose(_ response: Echo_EchoResponse) throws {
     try self.handler.sendResponse(message:response.serializeProtobuf(),
-                                  statusCode: 0,
-                                  statusMessage: "OK",
-                                  trailingMetadata: Metadata())
+                                  statusCode:self.statusCode,
+                                  statusMessage:self.statusMessage,
+                                  trailingMetadata:self.trailingMetadata)
   }
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try self.handler.sendMetadata(initialMetadata:Metadata()) {
+    try self.handler.sendMetadata(initialMetadata:initialMetadata) {
       queue.async {
         do {
           try self.provider.collect(session:self)
@@ -179,7 +189,7 @@ public class Echo_EchoCollectSession {
 }
 
 // Update (Bidirectional Streaming)
-public class Echo_EchoUpdateSession {
+public class Echo_EchoUpdateSession : Echo_EchoSession {
   private var handler : gRPC.Handler
   private var provider : Echo_EchoProvider
 
@@ -223,9 +233,9 @@ public class Echo_EchoUpdateSession {
   /// Close a connection. Blocks until the connection is closed.
   public func Close() throws {
     let done = NSCondition()
-    try self.handler.sendStatus(statusCode: 0,
-                                statusMessage: "OK",
-                                trailingMetadata: Metadata()) {
+    try self.handler.sendStatus(statusCode:self.statusCode,
+                                statusMessage:self.statusMessage,
+                                trailingMetadata:self.trailingMetadata) {
                                   done.lock()
                                   done.signal()
                                   done.unlock()
@@ -237,7 +247,7 @@ public class Echo_EchoUpdateSession {
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try self.handler.sendMetadata(initialMetadata:Metadata()) {
+    try self.handler.sendMetadata(initialMetadata:initialMetadata) {
       queue.async {
         do {
           try self.provider.update(session:self)

+ 7 - 10
Plugin/Makefile

@@ -1,12 +1,10 @@
 
-all:	test
+default:	build
 
-build:
-	rm -f echo.client.pb.swift echo.server.pb.swift
+build:  clear
 	swift build
 	cp .build/debug/protoc-gen-swiftgrpc .
 
-
 test:	build
 	protoc ../Examples/Echo/echo.proto --proto_path=../Examples/Echo --swiftgrpc_out=. 
 	diff echo.client.pb.swift ../Examples/Echo/Swift/Generated/echo.client.pb.swift
@@ -16,9 +14,8 @@ deploy:
 	cp echo.client.pb.swift ../Examples/Echo/Swift/Generated/echo.client.pb.swift 
 	cp echo.server.pb.swift ../Examples/Echo/Swift/Generated/echo.server.pb.swift 
 
-clean :
-	rm -f protoc-gen-swiftgrpc 
-	rm -f swiftgrpc.log
-	rm -f echo.client.pb.swift echo.server.pb.swift
-	rm -rf Packages
-	rm -rf .build
+clear : 
+	rm -f echo.client.pb.swift echo.server.pb.swift swiftgrpc.log
+
+clean : clear
+	rm -rf protoc-gen-swiftgrpc Packages .build

+ 3 - 0
Plugin/Sources/main.swift

@@ -150,6 +150,9 @@ func main() throws {
   ext.registerFilter("server") { (value: Any?, arguments: [Any?]) in
     return try packageServiceName(arguments) + "Server"
   }
+  ext.registerFilter("service") { (value: Any?, arguments: [Any?]) in
+    return try packageServiceName(arguments)
+  }
   ext.registerFilter("input") { (value: Any?) in
     if let value = value as? Google_Protobuf_MethodDescriptorProto {
       return protoMessageName(value.inputType)

+ 5 - 5
Plugin/swiftgrpc.templates/server-session-bidistreaming.swift

@@ -1,5 +1,5 @@
 // {{ method.name }} (Bidirectional Streaming)
-public class {{ .|session:protoFile,service,method }} {
+public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,service }}Session {
   private var handler : gRPC.Handler
   private var provider : {{ .|provider:protoFile,service }}
 
@@ -43,9 +43,9 @@ public class {{ .|session:protoFile,service,method }} {
   /// Close a connection. Blocks until the connection is closed.
   public func Close() throws {
     let done = NSCondition()
-    try self.handler.sendStatus(statusCode: 0,
-                                statusMessage: "OK",
-                                trailingMetadata: Metadata()) {
+    try self.handler.sendStatus(statusCode:self.statusCode,
+                                statusMessage:self.statusMessage,
+                                trailingMetadata:self.trailingMetadata) {
                                   done.lock()
                                   done.signal()
                                   done.unlock()
@@ -57,7 +57,7 @@ public class {{ .|session:protoFile,service,method }} {
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try self.handler.sendMetadata(initialMetadata:Metadata()) {
+    try self.handler.sendMetadata(initialMetadata:initialMetadata) {
       queue.async {
         do {
           try self.provider.{{ method.name|lowercase }}(session:self)

+ 5 - 5
Plugin/swiftgrpc.templates/server-session-clientstreaming.swift

@@ -1,5 +1,5 @@
 // {{ method.name }} (Client Streaming)
-public class {{ .|session:protoFile,service,method }} {
+public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,service }}Session {
   private var handler : gRPC.Handler
   private var provider : {{ .|provider:protoFile,service }}
 
@@ -33,14 +33,14 @@ public class {{ .|session:protoFile,service,method }} {
   /// Send a response and close the connection.
   public func SendAndClose(_ response: {{ method|output }}) throws {
     try self.handler.sendResponse(message:response.serializeProtobuf(),
-                                  statusCode: 0,
-                                  statusMessage: "OK",
-                                  trailingMetadata: Metadata())
+                                  statusCode:self.statusCode,
+                                  statusMessage:self.statusMessage,
+                                  trailingMetadata:self.trailingMetadata)
   }
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try self.handler.sendMetadata(initialMetadata:Metadata()) {
+    try self.handler.sendMetadata(initialMetadata:initialMetadata) {
       queue.async {
         do {
           try self.provider.{{ method.name|lowercase }}(session:self)

+ 5 - 5
Plugin/swiftgrpc.templates/server-session-serverstreaming.swift

@@ -1,5 +1,5 @@
 // {{ method.name }} (Server Streaming)
-public class {{ .|session:protoFile,service,method }} {
+public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,service }}Session {
   private var handler : gRPC.Handler
   private var provider : {{ .|provider:protoFile,service }}
 
@@ -16,7 +16,7 @@ public class {{ .|session:protoFile,service,method }} {
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try self.handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
+    try self.handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
       if let requestData = requestData {
         do {
           let requestMessage = try {{ method|input }}(protobuf:requestData)
@@ -25,9 +25,9 @@ public class {{ .|session:protoFile,service,method }} {
           queue.async {
             do {
               try self.provider.{{ method.name|lowercase }}(request:requestMessage, session: self)
-              try self.handler.sendStatus(statusCode:0,
-                                          statusMessage:"OK",
-                                          trailingMetadata:Metadata(),
+              try self.handler.sendStatus(statusCode:self.statusCode,
+                                          statusMessage:self.statusMessage,
+                                          trailingMetadata:self.trailingMetadata,
                                           completion:{})
             } catch (let error) {
               print("error: \(error)")

+ 6 - 6
Plugin/swiftgrpc.templates/server-session-unary.swift

@@ -1,5 +1,5 @@
 // {{ method.name }} (Unary)
-public class {{ .|session:protoFile,service,method }} {
+public class {{ .|session:protoFile,service,method }} : {{ .|service:protoFile,service }}Session {
   private var handler : gRPC.Handler
   private var provider : {{ .|provider:protoFile,service }}
 
@@ -11,14 +11,14 @@ public class {{ .|session:protoFile,service,method }} {
 
   /// Run the session. Internal.
   fileprivate func run(queue:DispatchQueue) throws {
-    try handler.receiveMessage(initialMetadata:Metadata()) {(requestData) in
+    try handler.receiveMessage(initialMetadata:initialMetadata) {(requestData) in
       if let requestData = requestData {
         let requestMessage = try {{ method|input }}(protobuf:requestData)
-        let replyMessage = try self.provider.{{ method.name|lowercase }}(request:requestMessage)
+        let replyMessage = try self.provider.{{ method.name|lowercase }}(request:requestMessage, session: self)
         try self.handler.sendResponse(message:replyMessage.serializeProtobuf(),
-                                      statusCode: 0,
-                                      statusMessage: "OK",
-                                      trailingMetadata:Metadata())
+                                      statusCode:self.statusCode,
+                                      statusMessage:self.statusMessage,
+                                      trailingMetadata:self.trailingMetadata)
       }
     }
   }

+ 11 - 1
Plugin/swiftgrpc.templates/server.pb.swift

@@ -53,7 +53,7 @@ public enum {{ .|servererror:protoFile,service }} : Error {
 public protocol {{ .|provider:protoFile,service }} {
   //-{% for method in service.method %}
   //-{% if not method.clientStreaming and not method.serverStreaming %}
-  func {{ method.name|lowercase }}(request : {{ method|input }}) throws -> {{ method|output }}
+  func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:protoFile,service,method }}) throws -> {{ method|output }}
   //-{% endif %}
   //-{% if not method.clientStreaming and method.serverStreaming %}
   func {{ method.name|lowercase }}(request : {{ method|input }}, session : {{ .|session:protoFile,service,method }}) throws
@@ -66,6 +66,16 @@ public protocol {{ .|provider:protoFile,service }} {
   //-{% endif %}
   //-{% endfor %}
 }
+
+/// Common properties available in each service session.
+public class {{ .|service:protoFile,service }}Session {
+  var statusCode : Int = 0
+  var statusMessage : String = "OK"
+  var initialMetadata : Metadata = Metadata()
+  var trailingMetadata : Metadata = Metadata()
+  var receivedMetadata : Metadata = Metadata()
+}
+
 //-{% for method in service.method %}
 //-{% if not method.clientStreaming and not method.serverStreaming %}
 //-{% include "server-session-unary.swift" %}