Add initial grpc server implementation

Still need to use a factory to get the correct vehicle, based on environment variables.
This commit is contained in:
Piv
2020-05-20 19:07:11 +09:30
parent de50b87d88
commit 4ad8342033
3 changed files with 47 additions and 7 deletions

View File

@@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/grpc/grpc-swift.git", "repositoryURL": "https://github.com/grpc/grpc-swift.git",
"state": { "state": {
"branch": null, "branch": null,
"revision": "373ffd54c1c1a319d0ddac9476d13be9023584bb", "revision": "b83ee1ee2caa0660eb02444977b9b6e353c2adbf",
"version": "1.0.0-alpha.11" "version": "1.0.0-alpha.12"
} }
}, },
{ {

View File

@@ -1,4 +1,4 @@
// swift-tools-version:5.2 // swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package. // The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription
@@ -11,7 +11,7 @@ let package = Package(
dependencies: [ dependencies: [
// Dependencies declare other packages that this package depends on. // Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"), // .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0-alpha.11"), .package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0-alpha.12"),
.package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0"), .package(url: "https://github.com/uraimo/SwiftyGPIO.git", from: "1.0.0"),
], ],
targets: [ targets: [

View File

@@ -5,7 +5,47 @@
// Created by Michael Pivato on 8/5/20. // Created by Michael Pivato on 8/5/20.
// //
// Entry-Point to the Swift Car Controller import NIO
var text = "Hello World!" import SwiftyGPIO
print(text) import GRPC
func doServer() throws {
// Copied from examples
// Create an event loop group for the server to run on.
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
defer {
try! group.syncShutdownGracefully()
}
let pwms = SwiftyGPIO.hardwarePWMs(for:.RaspberryPi3)!
// Read the feature database.
let vehicle = try RPiVehicle2D(withThrottlePin: Servo(forPin: (pwms[0]?[.P18])!)!, withSteeringPin:Servo(forPin: (pwms[1]?[.P19])!)!)
// Create a provider using the features we read.
let provider = MotorProvider(vehicle: vehicle)
// Start the server and print its address once it has started.
let server = Server.insecure(group: group)
.withServiceProviders([provider])
.bind(host: "localhost", port: 0)
server.map {
$0.channel.localAddress
}.whenSuccess { address in
print("server started on port \(address!.port!)")
}
// Wait on the server's `onClose` future to stop the program from exiting.
_ = try server.flatMap {
$0.onClose
}.wait()
}
// Entry-Point to the Swift Car Controller
print("Starting Server")
do{
try doServer()
}
catch{
print("Server failed")
}