make sending type message before protobuf request message
This commit is contained in:
@@ -1,2 +1,51 @@
|
||||
@native
|
||||
fun require(name: String): dynamic = noImpl
|
||||
|
||||
fun <T> encodeProtoBuf(protoMessage: T): ByteArray {
|
||||
val routeBytes: ByteArray
|
||||
if (protoMessage is LocationResponse) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is UploadResult) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is DebugResponseMemoryStats) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is DirectionResponse) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is RouteResponse) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is RouteRequest) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is TaskRequest) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is RouteDoneRequest) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else {
|
||||
println("PROTO MESSAGE DON'T ENCODE!")
|
||||
routeBytes = ByteArray(0)
|
||||
}
|
||||
return routeBytes
|
||||
}
|
||||
@@ -9,7 +9,7 @@ class McTransport() : MCConnectObserver<String> {
|
||||
McConditionMonitor.instance.addObserver(this)
|
||||
}
|
||||
|
||||
fun sendBytes(bytes: ByteArray) {
|
||||
private fun sendBytes(bytes: ByteArray) {
|
||||
val bytesHeader = encodeInt(bytes.size)
|
||||
println("write: $bytesHeader")
|
||||
writeStream.write(js("new Buffer(bytesHeader)"))
|
||||
@@ -17,6 +17,29 @@ class McTransport() : MCConnectObserver<String> {
|
||||
writeStream.write(js("new Buffer(bytes)"))
|
||||
}
|
||||
|
||||
|
||||
fun sendProtoBuf(message: DebugRequest) {
|
||||
val typeMessage = getProtoBufTypeMessage(TaskRequest.Type.DEBUG)
|
||||
sendProtoBufType(typeMessage)
|
||||
sendBytes(encodeProtoBuf(message))
|
||||
}
|
||||
|
||||
fun sendProtoBuf(message: RouteRequest) {
|
||||
val typeMessage = getProtoBufTypeMessage(TaskRequest.Type.ROUTE)
|
||||
sendProtoBufType(typeMessage)
|
||||
sendBytes(encodeProtoBuf(message))
|
||||
}
|
||||
|
||||
private fun sendProtoBufType(messageType: TaskRequest) {
|
||||
val typeBytes = encodeProtoBuf(messageType)
|
||||
sendBytes(typeBytes)
|
||||
}
|
||||
|
||||
private fun getProtoBufTypeMessage(type: TaskRequest.Type): TaskRequest {
|
||||
val result = TaskRequest.BuilderTaskRequest(type)
|
||||
return result.build()
|
||||
}
|
||||
|
||||
private fun initStreams(pathToFile: String) {
|
||||
writeStream = fs.createWriteStream(pathToFile)
|
||||
readStream = fs.createReadStream(pathToFile)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import control.car.ControllerToUsb
|
||||
import net.Client
|
||||
import net.server.handlers.AbstractHandler
|
||||
import net.server.handlers.ProtoType
|
||||
import net.server.handlers.debug.Memory
|
||||
import net.server.handlers.flash.LoadBin
|
||||
import net.server.handlers.main.GetLocation
|
||||
@@ -27,7 +26,6 @@ fun main(args: Array<String>) {
|
||||
handlers.put("/route", SetRoute(carController))
|
||||
handlers.put("/getLocation", GetLocation())
|
||||
handlers.put("/debug/memory", Memory())
|
||||
handlers.put("/protoType", ProtoType())
|
||||
|
||||
Client.instance.connectToServer(config.getCarIp(), carServerPort)
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package control.car
|
||||
|
||||
import CodedOutputStream
|
||||
import RouteRequest
|
||||
import control.Controller
|
||||
import mcTransport
|
||||
@@ -9,16 +8,12 @@ class ControllerToUsb : Controller {
|
||||
|
||||
override fun executeRoute(route: RouteRequest) {
|
||||
println("Execute Route:")
|
||||
val protoSize = route.getSizeNoTag()
|
||||
val routeBytes = ByteArray(protoSize)
|
||||
|
||||
route.writeTo(CodedOutputStream(routeBytes))
|
||||
|
||||
mcTransport.setCallBack { bytes ->
|
||||
println("Read $bytes;")
|
||||
}
|
||||
|
||||
mcTransport.sendBytes(routeBytes)
|
||||
mcTransport.sendProtoBuf(route)
|
||||
}
|
||||
|
||||
override fun getSensorData(degrees: IntArray): IntArray {
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
package net.server.handlers
|
||||
|
||||
import CodedOutputStream
|
||||
import DebugResponseMemoryStats
|
||||
import DirectionResponse
|
||||
import LocationResponse
|
||||
import RouteDoneRequest
|
||||
import RouteRequest
|
||||
import RouteResponse
|
||||
import UploadResult
|
||||
|
||||
abstract class AbstractHandler {
|
||||
|
||||
fun execute(data: List<Byte>, response: dynamic) {
|
||||
@@ -21,49 +12,4 @@ abstract class AbstractHandler {
|
||||
|
||||
|
||||
abstract fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit)
|
||||
|
||||
protected fun <T> encodeProtoBuf(protoMessage: T): ByteArray {
|
||||
val routeBytes: ByteArray
|
||||
if (protoMessage is LocationResponse) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is UploadResult) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is DebugResponseMemoryStats) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is DirectionResponse) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is RouteResponse) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is RouteRequest) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else if (protoMessage is RouteDoneRequest) {
|
||||
val protoSize = protoMessage.getSizeNoTag()
|
||||
routeBytes = ByteArray(protoSize)
|
||||
val codedOutput = CodedOutputStream(routeBytes)
|
||||
protoMessage.writeTo(codedOutput)
|
||||
} else {
|
||||
println("PROTO MESSAGE DON'T ENCODE!")
|
||||
routeBytes = ByteArray(0)
|
||||
}
|
||||
return routeBytes
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package net.server.handlers
|
||||
|
||||
import McState
|
||||
import TaskRequest
|
||||
import mcTransport
|
||||
|
||||
class ProtoType : AbstractHandler {
|
||||
|
||||
val fromServerObjectBuilder: TaskRequest.BuilderTaskRequest
|
||||
|
||||
constructor() : super() {
|
||||
fromServerObjectBuilder = TaskRequest.BuilderTaskRequest(TaskRequest.TYPE.DEBUG)
|
||||
}
|
||||
|
||||
override fun getBytesResponse(data: ByteArray, callback: (ByteArray) -> Unit) {
|
||||
if (!McState.instance.isConnected()) {
|
||||
println("mc is disconnected!")
|
||||
callback.invoke(ByteArray(0))
|
||||
return
|
||||
}
|
||||
mcTransport.sendBytes(data)
|
||||
callback.invoke(ByteArray(0))
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,10 @@ package net.server.handlers.debug
|
||||
import DebugRequest
|
||||
import DebugResponseMemoryStats
|
||||
import McState
|
||||
import encodeProtoBuf
|
||||
import mcTransport
|
||||
import net.server.handlers.AbstractHandler
|
||||
import CodedInputStream
|
||||
|
||||
class Memory : AbstractHandler {
|
||||
|
||||
@@ -27,6 +29,8 @@ class Memory : AbstractHandler {
|
||||
mcTransport.setCallBack { bytes ->
|
||||
callback.invoke(bytes)
|
||||
}
|
||||
mcTransport.sendBytes(data)
|
||||
val message = fromServerObjectBuilder.build()
|
||||
message.mergeFrom(CodedInputStream(data))
|
||||
mcTransport.sendProtoBuf(message)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,8 @@ package net.server.handlers.flash
|
||||
import CodedInputStream
|
||||
import Upload
|
||||
import UploadResult
|
||||
import mcTransport
|
||||
import encodeProtoBuf
|
||||
import fs
|
||||
import net.server.handlers.AbstractHandler
|
||||
import require
|
||||
|
||||
@@ -23,13 +24,18 @@ class LoadBin : AbstractHandler {
|
||||
override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) {
|
||||
val message = fromServerObjectBuilder.build()
|
||||
message.mergeFrom(CodedInputStream(data))
|
||||
val responseMessage = toServerObjectBuilder.build()
|
||||
mcTransport.sendBytes(message.data)
|
||||
val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000"
|
||||
exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
|
||||
val resultCode = if (err != null) 15 else 0
|
||||
responseMessage.resultCode = resultCode
|
||||
callback.invoke(encodeProtoBuf(responseMessage))
|
||||
fs.writeFile("./flash.bin", js("new Buffer(data)"), fun(err: dynamic) {
|
||||
if (err) {
|
||||
println("error in save flash.bin file\n $err")
|
||||
val responseMessage = toServerObjectBuilder.setResultCode(14).build()
|
||||
callback.invoke(encodeProtoBuf(responseMessage))
|
||||
}
|
||||
val stFlashCommand = "./st-flash write ./flash.bin " + "0x08000000"
|
||||
exec(stFlashCommand, { err, stdOutRes, stdErrRes ->
|
||||
val resultCode = if (err != null) 15 else 0
|
||||
val responseMessage = toServerObjectBuilder.setResultCode(resultCode).build()
|
||||
callback.invoke(encodeProtoBuf(responseMessage))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package net.server.handlers.main
|
||||
|
||||
import CarState
|
||||
import LocationResponse
|
||||
import encodeProtoBuf
|
||||
import net.server.handlers.AbstractHandler
|
||||
|
||||
class GetLocation : AbstractHandler {
|
||||
|
||||
@@ -5,6 +5,7 @@ import McState
|
||||
import RouteRequest
|
||||
import RouteResponse
|
||||
import control.Controller
|
||||
import encodeProtoBuf
|
||||
import net.server.handlers.AbstractHandler
|
||||
|
||||
class SetRoute : AbstractHandler {
|
||||
|
||||
@@ -4,6 +4,7 @@ import CodedInputStream
|
||||
import DirectionRequest
|
||||
import DirectionResponse
|
||||
import control.emulator.ControllerEmulator.MoveDirection
|
||||
import encodeProtoBuf
|
||||
import net.server.handlers.AbstractHandler
|
||||
|
||||
class Control : AbstractHandler {
|
||||
@@ -18,28 +19,28 @@ class Control : AbstractHandler {
|
||||
|
||||
override fun getBytesResponse(data: ByteArray, callback: (b: ByteArray) -> Unit) {
|
||||
//TODO now this handler don't make nothing. need fix:)
|
||||
val message = fromServerObjectBuilder.build()
|
||||
message.mergeFrom(CodedInputStream(data))
|
||||
val commandNumber = message.command
|
||||
val sid = message.sid
|
||||
val command = when (commandNumber) {
|
||||
DirectionRequest.Command.stop -> {
|
||||
MoveDirection.STOP
|
||||
}
|
||||
DirectionRequest.Command.forward -> {
|
||||
MoveDirection.FORWARD
|
||||
}
|
||||
DirectionRequest.Command.backward -> {
|
||||
MoveDirection.BACKWARD
|
||||
}
|
||||
DirectionRequest.Command.right -> {
|
||||
MoveDirection.RIGHT
|
||||
}
|
||||
DirectionRequest.Command.left -> {
|
||||
MoveDirection.LEFT
|
||||
}
|
||||
else -> MoveDirection.STOP
|
||||
}
|
||||
// val message = fromServerObjectBuilder.build()
|
||||
// message.mergeFrom(CodedInputStream(data))
|
||||
// val commandNumber = message.command
|
||||
// val sid = message.sid
|
||||
// val command = when (commandNumber) {
|
||||
// DirectionRequest.Command.stop -> {
|
||||
// MoveDirection.STOP
|
||||
// }
|
||||
// DirectionRequest.Command.forward -> {
|
||||
// MoveDirection.FORWARD
|
||||
// }
|
||||
// DirectionRequest.Command.backward -> {
|
||||
// MoveDirection.BACKWARD
|
||||
// }
|
||||
// DirectionRequest.Command.right -> {
|
||||
// MoveDirection.RIGHT
|
||||
// }
|
||||
// DirectionRequest.Command.left -> {
|
||||
// MoveDirection.LEFT
|
||||
// }
|
||||
// else -> MoveDirection.STOP
|
||||
// }
|
||||
val resultCode: Int = 0
|
||||
val resultMessage = toServerObjectBuilder.setCode(resultCode).build()
|
||||
callback.invoke(encodeProtoBuf(resultMessage))
|
||||
|
||||
Reference in New Issue
Block a user