From 7adec721f3ffff00323aa5b539b51eead792ba69 Mon Sep 17 00:00:00 2001 From: MaximZaitsev Date: Thu, 18 Aug 2016 14:01:46 +0300 Subject: [PATCH] make proto file for debug, make work with debug message --- .../protofiles_sources/server_car/debug.proto | 18 ++ server/src/main/java/DebugClInterface.kt | 183 ++++++++++++++++++ server/src/main/java/ServerMain.kt | 106 +--------- .../src/main/java/car/client/ClientHandler.kt | 9 + 4 files changed, 213 insertions(+), 103 deletions(-) create mode 100644 proto/protofiles_sources/server_car/debug.proto create mode 100644 server/src/main/java/DebugClInterface.kt diff --git a/proto/protofiles_sources/server_car/debug.proto b/proto/protofiles_sources/server_car/debug.proto new file mode 100644 index 00000000000..eec51b80405 --- /dev/null +++ b/proto/protofiles_sources/server_car/debug.proto @@ -0,0 +1,18 @@ +syntax = "proto3"; +package carkot; + +message DebugRequest { + + TYPE type = 1; + + enum TYPE { + MEMORYSTATS = 0; + } +} + +message DebugResponseMemoryStats { + int32 heapDynamicTail = 1; + int32 heapStaticTail = 2; + int32 heapDynamicMaxBytes = 3; + int32 heapDynamicTotalBytes = 4; +} diff --git a/server/src/main/java/DebugClInterface.kt b/server/src/main/java/DebugClInterface.kt new file mode 100644 index 00000000000..a149ca49b35 --- /dev/null +++ b/server/src/main/java/DebugClInterface.kt @@ -0,0 +1,183 @@ +import Exceptions.InactiveCarException +import car.client.Client +import io.netty.buffer.Unpooled +import io.netty.handler.codec.http.* +import objects.Car + +/** + * Created by user on 8/18/16. + */ +class DebugClInterface { + + fun run() { + + DebugRequest.BuilderDebugRequest(DebugRequest.TYPE.valueOf("mem")) +// DebugRequest.TYPE. + + val helpString = "available commands:\n" + + "cars - get list of connected cars\n" + + "route [car_id] - setting a route for car with car id.\n" + + "refloc - refresh all car locations\n" + + "dbinfo [car_id] [type] - refresh all car locations\n." + + "type is string name of value or int value. available values: MEMORYSTATS - 0" + // "stop - exit from this interface and stop all servers\n"//todo sometimes server not completed. some threads dont stop + println(helpString) + val routeRegex = Regex("route [0-9]{1,10}") + val environment = objects.Environment.instance + while (true) { + val readedString = readLine() + if (readedString == null) { + break + } + if (readedString.equals("cars", true)) { + synchronized(environment, { + println(environment.map.values) + }) + } else if (routeRegex.matches(readedString)) { + + val id: Int + try { + id = readedString.split(" ")[1].toInt() + } catch (e: NumberFormatException) { + e.printStackTrace() + println("error in converting id to int type") + println(helpString) + continue + } + val car: Car? = + synchronized(environment, { + environment.map.get(id) + }) + if (car != null) { + println("print way points in polar coordinate als [distance] [rotation angle] in metres and degrees. after enter all points print \"done\". for reset route print \"reset\"") + println("e.g. for move from (x,y) to (x+1,y) and back to (x,y) u need enter: 1 0[press enter] 1 180[press enter] done") + + val distances = arrayListOf() + val angles = arrayListOf() + while (true) { + val wayPointInputString = readLine()!! + if (wayPointInputString.equals("reset", true)) { + break + } else if (wayPointInputString.equals("done", true)) { + val routeBuilder = RouteRequest.BuilderRouteRequest(distances.toIntArray(), angles.toIntArray()) + val requestObject = routeBuilder.build() + val requestBytes = ByteArray(requestObject.getSizeNoTag()) + requestObject.writeTo(CodedOutputStream(requestBytes)) + val request = getDefaultHttpRequest(car.host, setRouteUrl, requestBytes) + try { + Client.sendRequest(request, car.host, car.port, id) + } catch (e: InactiveCarException) { + synchronized(environment, { + environment.map.remove(id) + }) + } + break + } else { + val wayPointData = wayPointInputString.split(" ") + val distance: Int + val angle: Int + try { + distance = wayPointData[0].toInt() + angle = wayPointData[1].toInt() + } catch (e: NumberFormatException) { + println("error in convertion angle or distance to int. try again") + continue + } catch (e: IndexOutOfBoundsException) { + println("format error, u must print two number separated by spaces. Try again") + continue + } + distances.add(distance) + angles.add(angle) + } + } + } else { + println("car with id=$id not found") + } + } else if (readedString.equals("refloc", true)) { + val cars = synchronized(environment, { environment.map.values }) + val inactiveCarUids = mutableListOf() + for (car in cars) { + val request = getDefaultHttpRequest(car.host, getLocationUrl, ByteArray(0)) + try { + Client.sendRequest(request, car.host, car.port, car.uid) + } catch (e: InactiveCarException) { + inactiveCarUids.add(car.uid) + } + println("ref loc done") + } + synchronized(environment, { + for (id in inactiveCarUids) { + environment.map.remove(id) + } + }) + } else if (readedString.equals("stop")) { + break + } else if (readedString.contains("dbinfo")) { + val params = readedString.split(" ") + if (!params[0].equals("dbinfo")) { + println("not supported command: $readedString") + println(helpString) + continue + } + if (params.size != 3) { + println("incorrect args of command dbinfo.") + println(helpString) + continue + } + val id: Int + try { + id = params[1].toInt() + } catch (e: NumberFormatException) { + e.printStackTrace() + println("error in converting id to int type") + println(helpString) + continue + } + val car: Car? = + synchronized(environment, { + environment.map.get(id) + }) + if (car == null) { + println("car with id=$id not found") + continue + } + val paramType = params[2]//maybe string or int + val type: DebugRequest.TYPE + try { + type = DebugRequest.TYPE.fromIntToTYPE(paramType.toInt()) + } catch (e: NumberFormatException) { + try { + type = DebugRequest.TYPE.valueOf(paramType) + } catch (e: IllegalArgumentException) { + type = DebugRequest.TYPE.Unexpected + } + } + if (type == DebugRequest.TYPE.Unexpected) { + println("type with name/id $paramType not found") + continue + } + val requestObject = DebugRequest.BuilderDebugRequest(type).build() + val bytes = ByteArray(requestObject.getSizeNoTag()) + requestObject.writeTo(CodedOutputStream(bytes)) + val request = getDefaultHttpRequest(car.host, debugMemoryUrl, bytes) + try { + Client.sendRequest(request, car.host, car.port, car.uid) + } catch (e: InactiveCarException) { + println("this car is inactive") + } + } else { + println("not supported command: $readedString") + println(helpString) + } + } + } + + fun getDefaultHttpRequest(host: String, url: String, bytes: ByteArray): DefaultFullHttpRequest { + val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, url, Unpooled.copiedBuffer(bytes)); + request.headers().set(HttpHeaderNames.HOST, host) + request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE) + request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes()) + return request + } + +} \ No newline at end of file diff --git a/server/src/main/java/ServerMain.kt b/server/src/main/java/ServerMain.kt index 19295bf38e2..33761c10bf6 100644 --- a/server/src/main/java/ServerMain.kt +++ b/server/src/main/java/ServerMain.kt @@ -7,6 +7,7 @@ import io.netty.channel.socket.nio.NioServerSocketChannel import io.netty.handler.codec.http.* import objects.Car import java.io.ByteArrayOutputStream +import java.util.* import kotlin.concurrent.thread /** @@ -21,6 +22,7 @@ val getLocationUrl = "/getLocation" val routeDoneUrl = "/routeDone" val setRouteUrl = "/route" val connectUrl = "/connect" +val debugMemoryUrl = "/debug/memoty" fun main(args: Array) { @@ -31,110 +33,8 @@ fun main(args: Array) { carsDestroy.start() //CL user interface - val helpString = "available commands:\n" + - "cars - get list of connected cars\n" + - "route [car_id] - setting a route for car with car id.\n" + - "refloc - refresh all car locations\n" - // "stop - exit from this interface and stop all servers\n"//todo sometimes server not completed. some threads dont stop - println(helpString) - val routeRegex = Regex("route [0-9]{1,10}") - val environment = objects.Environment.instance - while (true) { - val readedString = readLine() - if (readedString == null) { - break - } - if (readedString.equals("cars", true)) { - synchronized(environment, { - println(environment.map.values) - }) - } else if (routeRegex.matches(readedString)) { + DebugClInterface().run() - val id: Int - try { - id = readedString.split(" ")[1].toInt() - } catch (e: NumberFormatException) { - e.printStackTrace() - println("error in converting id to int type") - println(helpString) - continue - } - val car: Car? = - synchronized(environment, { - environment.map.get(id) - }) - if (car != null) { - println("print way points in polar coordinate als [distance] [rotation angle] in metres and degrees. after enter all points print \"done\". for reset route print \"reset\"") - println("e.g. for move from (x,y) to (x+1,y) and back to (x,y) u need enter: 1 0[press enter] 1 180[press enter] done") - - val distances = arrayListOf() - val angles = arrayListOf() - while (true) { - val wayPointInputString = readLine()!! - if (wayPointInputString.equals("reset", true)) { - break - } else if (wayPointInputString.equals("done", true)) { - val routeBuilder = RouteRequest.BuilderRouteRequest(distances.toIntArray(), angles.toIntArray()) - val requestObject = routeBuilder.build() - val requestBytes = ByteArray(requestObject.getSizeNoTag()) - requestObject.writeTo(CodedOutputStream(requestBytes)) - val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, setRouteUrl, Unpooled.copiedBuffer(requestBytes)) - request.headers().set(HttpHeaderNames.HOST, car.host) - request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE) - request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes()) - try { - Client.sendRequest(request, car.host, car.port, id) - } catch (e: InactiveCarException) { - synchronized(environment, { - environment.map.remove(id) - }) - } - break - } else { - val wayPointData = wayPointInputString.split(" ") - val distance: Int - val angle: Int - try { - distance = wayPointData[0].toInt() - angle = wayPointData[1].toInt() - } catch (e: NumberFormatException) { - println("error in convertion angle or distance to double. try again") - continue - } - distances.add(distance) - angles.add(angle) - } - } - } else { - println("car with id=$id not found") - } - } else if (readedString.equals("refloc", true)) { - val cars = synchronized(environment, { environment.map.values }) - val inactiveCarUids = mutableListOf() - for (car in cars) { - val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, getLocationUrl, Unpooled.EMPTY_BUFFER) - request.headers().set(HttpHeaderNames.HOST, car.host) - request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE) - request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes()) - try { - Client.sendRequest(request, car.host, car.port, car.uid) - } catch (e: InactiveCarException) { - inactiveCarUids.add(car.uid) - } - println("ref loc done") - } - synchronized(environment, { - for (id in inactiveCarUids) { - environment.map.remove(id) - } - }) - } else if (readedString.equals("stop")) { - break - } else { - println("not supported command: $readedString") - println(helpString) - } - } carsDestroy.interrupt() carServer.interrupt() } diff --git a/server/src/main/java/car/client/ClientHandler.kt b/server/src/main/java/car/client/ClientHandler.kt index 88f52cfbc3f..f5d20bb64d6 100644 --- a/server/src/main/java/car/client/ClientHandler.kt +++ b/server/src/main/java/car/client/ClientHandler.kt @@ -2,6 +2,7 @@ package car.client import CodedInputStream import LocationResponse +import debugMemoryUrl import getLocationUrl import io.netty.channel.ChannelHandlerContext import io.netty.channel.SimpleChannelInboundHandler @@ -38,6 +39,14 @@ class ClientHandler : SimpleChannelInboundHandler { }) } } + debugMemoryUrl -> { + val response = DebugResponseMemoryStats.BuilderDebugResponseMemoryStats(0, 0, 0, 0).build() + response.mergeFrom(CodedInputStream(contentBytes)) + println("heapDynamicMaxBytes ${response.heapDynamicMaxBytes}") + println("heapDynamicTail ${response.heapDynamicTail}") + println("heapDynamicTotalBytes ${response.heapDynamicTotalBytes}") + println("heapStaticTail ${response.heapStaticTail}") + } else -> { }