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

@@ -5,7 +5,47 @@
// Created by Michael Pivato on 8/5/20.
//
// Entry-Point to the Swift Car Controller
var text = "Hello World!"
print(text)
import NIO
import SwiftyGPIO
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")
}