2
0
Эх сурвалжийг харах

Update the HelloWorld examples (#1424)

George Barnett 3 жил өмнө
parent
commit
32e290b954

+ 9 - 1
Package.swift

@@ -27,6 +27,14 @@ let cgrpcZlibTargetName = cgrpcZlibProductName
 
 let includeNIOSSL = ProcessInfo.processInfo.environment["GRPC_NO_NIO_SSL"] == nil
 
+#if swift(>=5.6)
+// swift-argument-parser raised its minimum Swift version in 1.1.0 but
+// also accidentally broke API. This was fixed in "1.1.1".
+let argumentParserMinimumVersion: Version = "1.1.1"
+#else
+let argumentParserMinimumVersion: Version = "1.0.0"
+#endif
+
 // MARK: - Package Dependencies
 
 let packageDependencies: [Package.Dependency] = [
@@ -57,7 +65,7 @@ let packageDependencies: [Package.Dependency] = [
   ),
   .package(
     url: "https://github.com/apple/swift-argument-parser.git",
-    from: "1.0.0"
+    from: argumentParserMinimumVersion
   ),
 ].appending(
   .package(

+ 17 - 24
Sources/Examples/HelloWorld/Client/main.swift

@@ -13,38 +13,22 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#if compiler(>=5.6)
 import ArgumentParser
 import GRPC
 import HelloWorldModel
 import NIOCore
 import NIOPosix
 
-func greet(name: String?, client greeter: Helloworld_GreeterNIOClient) {
-  // Form the request with the name, if one was provided.
-  let request = Helloworld_HelloRequest.with {
-    $0.name = name ?? ""
-  }
-
-  // Make the RPC call to the server.
-  let sayHello = greeter.sayHello(request)
-
-  // wait() on the response to stop the program from exiting before the response is received.
-  do {
-    let response = try sayHello.response.wait()
-    print("Greeter received: \(response.message)")
-  } catch {
-    print("Greeter failed: \(error)")
-  }
-}
-
-struct HelloWorld: ParsableCommand {
+@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
+struct HelloWorld: AsyncParsableCommand {
   @Option(help: "The port to connect to")
   var port: Int = 1234
 
   @Argument(help: "The name to greet")
   var name: String?
 
-  func run() throws {
+  func run() async throws {
     // Setup an `EventLoopGroup` for the connection to run on.
     //
     // See: https://github.com/apple/swift-nio#eventloops-and-eventloopgroups
@@ -68,11 +52,20 @@ struct HelloWorld: ParsableCommand {
     }
 
     // Provide the connection to the generated client.
-    let greeter = Helloworld_GreeterNIOClient(channel: channel)
+    let greeter = Helloworld_GreeterAsyncClient(channel: channel)
+
+    // Form the request with the name, if one was provided.
+    let request = Helloworld_HelloRequest.with {
+      $0.name = self.name ?? ""
+    }
 
-    // Do the greeting.
-    greet(name: self.name, client: greeter)
+    do {
+      let greeting = try await greeter.sayHello(request)
+      print("Greeter received: \(greeting.message)")
+    } catch {
+      print("Greeter failed: \(error)")
+    }
   }
 }
 
-HelloWorld.main()
+#endif // compiler(>=5.6)

+ 8 - 6
Sources/Examples/HelloWorld/Server/GreeterProvider.swift

@@ -13,21 +13,23 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#if compiler(>=5.6)
 import GRPC
 import HelloWorldModel
 import NIOCore
 
-class GreeterProvider: Helloworld_GreeterProvider {
-  var interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol?
+@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
+final class GreeterProvider: Helloworld_GreeterAsyncProvider {
+  let interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol? = nil
 
   func sayHello(
     request: Helloworld_HelloRequest,
-    context: StatusOnlyCallContext
-  ) -> EventLoopFuture<Helloworld_HelloReply> {
+    context: GRPCAsyncServerCallContext
+  ) async throws -> Helloworld_HelloReply {
     let recipient = request.name.isEmpty ? "stranger" : request.name
-    let response = Helloworld_HelloReply.with {
+    return Helloworld_HelloReply.with {
       $0.message = "Hello \(recipient)!"
     }
-    return context.eventLoop.makeSucceededFuture(response)
   }
 }
+#endif // compiler(>=5.6)

+ 9 - 12
Sources/Examples/HelloWorld/Server/main.swift

@@ -13,38 +13,35 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#if compiler(>=5.6)
 import ArgumentParser
 import GRPC
 import HelloWorldModel
 import NIOCore
 import NIOPosix
 
-struct HelloWorld: ParsableCommand {
+@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
+struct HelloWorld: AsyncParsableCommand {
   @Option(help: "The port to listen on for new connections")
   var port = 1234
 
-  func run() throws {
+  func run() async throws {
     let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
     defer {
       try! group.syncShutdownGracefully()
     }
 
     // Start the server and print its address once it has started.
-    let server = Server.insecure(group: group)
+    let server = try await Server.insecure(group: group)
       .withServiceProviders([GreeterProvider()])
       .bind(host: "localhost", port: self.port)
+      .get()
 
-    server.map {
-      $0.channel.localAddress
-    }.whenSuccess { address in
-      print("server started on port \(address!.port!)")
-    }
+    print("server started on port \(server.channel.localAddress!.port!)")
 
     // Wait on the server's `onClose` future to stop the program from exiting.
-    _ = try server.flatMap {
-      $0.onClose
-    }.wait()
+    try await server.onClose.get()
   }
 }
 
-HelloWorld.main()
+#endif // compiler(>=5.6)