added handlers for main server requests

This commit is contained in:
MaximZaitsev
2016-07-28 17:43:44 +03:00
parent ce0d4e2912
commit 4752172b83
4 changed files with 57 additions and 6 deletions
+12 -1
View File
@@ -1,6 +1,8 @@
import exceptions.RcControlException
import net.server.handlers.AbstractHandler
import net.server.handlers.flash.LoadBin
import net.server.handlers.main.GetLocation
import net.server.handlers.main.SetRoute
import net.server.handlers.rc.Connect
import net.server.handlers.rc.Control
import net.server.handlers.rc.Disconnect
@@ -22,6 +24,9 @@ fun main(args: Array<String>) {
val rcSessionConstructor = protoBuf.loadProtoFile(getRelativePathToProto("rc_session.proto")).build("carkot")
val carkotConstructor = protoBuf.loadProtoFile(getRelativePathToProto("carkot.proto")).build("carkot")
val routeConstructor = protoBuf.loadProtoFile(getRelativePathToProto("route.proto")).build("carkot")
val locationConstructor = protoBuf.loadProtoFile(getRelativePathToProto("location.proto")).build("carkot")
val handlers: MutableMap<String, AbstractHandler> = mutableMapOf()
handlers.put("/rc/control", Control(controlConstructor.DirectionRequest, controlConstructor.DirectionResponse))
@@ -31,7 +36,8 @@ fun main(args: Array<String>) {
handlers.put("/loadBin", LoadBin(carkotConstructor.Upload, carkotConstructor.UploadResult))
//todo work with main server
handlers.put("/route", SetRoute(routeConstructor.RouteRequest, routeConstructor.RouteResponse))
handlers.put("/getLocation", GetLocation(null, locationConstructor.LocationResponse))
net.server.start(handlers, serverPort)
val udev = Udev()
@@ -61,4 +67,9 @@ fun require(name: String): dynamic {
@native
fun setInterval(callBack: () -> Unit, ms: Int) {
}
@native
fun setTimeout(callBack: () -> Unit, ms: Int) {
}
@@ -18,17 +18,16 @@ abstract class AbstractHandler(protoDecoder: dynamic, protoEncoder: dynamic) {
fun execute(data: List<Byte>, response: dynamic) {
val message = if (protoDecoder != null) protoDecoder.decode(data.toByteArray()) else null
val resultMessage: dynamic = {}
js("resultMessage = {}")//todo bad?
js("resultMessage = {}")
val afterExecute: () -> Unit = {
if (this.protoEncoder != null) {
val protoEn = this.protoEncoder//temporarily:)
val resultMsg = resultMessage
val resultObject = js("new protoEn(resultMsg)")
val resultBuffer = resultObject.encode()
response.write(trimBuffer(resultBuffer.buffer, resultBuffer.limit))
val resultBuffer = js("new protoEn(resultMsg)").encode()
val trimBuffer = trimBuffer(resultBuffer.buffer, resultBuffer.limit)
response.write(trimBuffer)
}
response.end()
}
@@ -0,0 +1,19 @@
package net.server.handlers.main
import net.server.handlers.AbstractHandler
/**
* Created by user on 7/28/16.
*/
class GetLocation : AbstractHandler {
constructor(protoDecoder: dynamic, protoEncoder: dynamic) : super(protoDecoder, protoEncoder)
override fun makeResponse(message: dynamic, responseMessage: dynamic, finalCallback: () -> Unit) {
val car = MicroController.instance.car
val locationResponseData = {}
js("locationResponseData = {x:car.x, y:car.y, angle:car.angle}")
responseMessage.locationResponseData = locationResponseData
finalCallback.invoke()
}
}
@@ -0,0 +1,22 @@
package net.server.handlers.main
import net.server.handlers.AbstractHandler
/**
* Created by user on 7/28/16.
*/
class SetRoute : AbstractHandler {
constructor(protoDecoder: dynamic, protoEncoder: dynamic) : super(protoDecoder, protoEncoder)
override fun makeResponse(message: dynamic, responseMessage: dynamic, finalCallback: () -> Unit) {
val car = MicroController.instance.car
car.routeExecutor.executeRoute(message)
responseMessage.code = 0
responseMessage.errorMsg = ""
finalCallback.invoke()
}
}