diff --git a/car_srv/kotlinSrv/src/Main.kt b/car_srv/kotlinSrv/src/Main.kt index 58ab1384db0..4986ff5710d 100644 --- a/car_srv/kotlinSrv/src/Main.kt +++ b/car_srv/kotlinSrv/src/Main.kt @@ -1,4 +1,5 @@ import net.server.handlers.AbstractHandler +import net.server.handlers.debug.Memory import net.server.handlers.flash.LoadBin import net.server.handlers.main.GetLocation import net.server.handlers.main.SetRoute @@ -13,7 +14,7 @@ import net.server.handlers.rc.Heartbeat val deltaTimeLocationRefresh: Int = 100//ms -val serverPort: Int = 8888 +val carServerPort: Int = 8888 val mainServerPort = 7925 val config = Config("config.cfg") @@ -21,10 +22,11 @@ val fs = require("fs") fun main(args: Array) { if (!config.loadConfig()) { println("incorrect config format!") - return + js("process.exit(1)") } val handlers: MutableMap = mutableMapOf() + // TODO remove injection of all internal builders from here handlers.put("/rc/control", Control(DirectionRequest.BuilderDirectionRequest(DirectionRequest.Command.fromIntToCommand(0), 0), DirectionResponse.BuilderDirectionResponse(0))) handlers.put("/rc/connect", Connect(SessionUpResponse.BuilderSessionUpResponse(0, 0))) handlers.put("/rc/disconnect", Disconnect(SessionDownRequest.BuilderSessionDownRequest(0), SessionDownResponse.BuilderSessionDownResponse(0))) @@ -35,7 +37,10 @@ fun main(args: Array) { handlers.put("/route", SetRoute(RouteRequest.BuilderRouteRequest(IntArray(0), IntArray(0)), RouteResponse.BuilderRouteResponse(0))) handlers.put("/getLocation", GetLocation(LocationResponse.BuilderLocationResponse(LocationResponse.LocationData.BuilderLocationData(0, 0, 0).build(), 0))) - net.server.start(handlers, serverPort) + + handlers.put("/debug/memoty", Memory()) + + net.server.start(handlers, carServerPort) val udev = Udev() udev.start() @@ -43,24 +48,8 @@ fun main(args: Array) { } -fun encodeInt(i: Int): ByteArray { - val result = ByteArray(4) - result[0] = i.shr(24).toByte() - result[1] = i.shr(16).toByte() - result[2] = i.shr(8).toByte() - result[3] = i.toByte() - - return result -} - -fun decodeInt(bytes: ByteArray): Int { - var result = 0 - result += bytes[3] - result += bytes[2].toInt().shl(8) - result += bytes[1].toInt().shl(16) - result += bytes[0].toInt().shl(24) - return result -} +// TODO move this dump of arbitrary functions that don't have any relation to +// to Main.kt from Main.kt fun encodeProtoBuf(protoMessage: dynamic): ByteArray { val protoSize = protoMessage.getSizeNoTag() @@ -71,16 +60,8 @@ fun encodeProtoBuf(protoMessage: dynamic): ByteArray { } @native -fun require(name: String): dynamic { - return null -} - +fun require(name: String): dynamic = noImpl @native -fun setInterval(callBack: () -> Unit, ms: Int) { - -} - +fun setInterval(callBack: () -> Unit, ms: Int) : dynamic = noImpl @native -fun setTimeout(callBack: () -> Unit, ms: Int) { - -} \ No newline at end of file +fun setTimeout(callBack: () -> Unit, ms: Int) : dynamic = noImpl \ No newline at end of file diff --git a/car_srv/kotlinSrv/src/net/server/handlers/debug/Memory.kt b/car_srv/kotlinSrv/src/net/server/handlers/debug/Memory.kt new file mode 100644 index 00000000000..f6b23c88f2e --- /dev/null +++ b/car_srv/kotlinSrv/src/net/server/handlers/debug/Memory.kt @@ -0,0 +1,37 @@ +package net.server.handlers.debug + +import CodedInputStream +import DebugRequest +import DebugResponseMemoryStats +import MicroController +import encodeProtoBuf +import mcTransport +import net.server.handlers.AbstractHandler + +/** + * Created by user on 8/18/16. + */ +class Memory : AbstractHandler { + + val fromServerObjectBuilder: DebugRequest.BuilderDebugRequest + val toServerObjectBuilder: DebugResponseMemoryStats.BuilderDebugResponseMemoryStats + + constructor() : super() { + fromServerObjectBuilder = DebugRequest.BuilderDebugRequest(DebugRequest.TYPE.MEMORYSTATS) + toServerObjectBuilder = DebugResponseMemoryStats.BuilderDebugResponseMemoryStats(0, 0, 0, 0) + } + + override fun getBytesResponse(data: ByteArray, callback: (ByteArray) -> Unit) { + + if (!MicroController.instance.isConnected()) { + println("mc is disconnected!") + val responseMessage = toServerObjectBuilder.build() + callback.invoke(encodeProtoBuf(responseMessage)) + return + } + mcTransport.setCallBack { bytes -> + callback.invoke(bytes) + } + mcTransport.sendBytes(data) + } +} \ No newline at end of file