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:
@@ -6,8 +6,8 @@
|
||||
"repositoryURL": "https://github.com/grpc/grpc-swift.git",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "373ffd54c1c1a319d0ddac9476d13be9023584bb",
|
||||
"version": "1.0.0-alpha.11"
|
||||
"revision": "b83ee1ee2caa0660eb02444977b9b6e353c2adbf",
|
||||
"version": "1.0.0-alpha.12"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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.
|
||||
|
||||
import PackageDescription
|
||||
@@ -11,7 +11,7 @@ let package = Package(
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
// .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"),
|
||||
],
|
||||
targets: [
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user