From 9c185b1f9a7616174133598659cee4d1f30dbcfc Mon Sep 17 00:00:00 2001 From: MaximZaitsev Date: Wed, 17 Aug 2016 16:36:33 +0300 Subject: [PATCH] make sequentially numbered ids, off drop car by timeout (worked incorrect) --- server/src/main/java/ServerMain.kt | 7 ++++--- server/src/main/java/objects/Environment.kt | 17 ++--------------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/ServerMain.kt b/server/src/main/java/ServerMain.kt index 2be83f7d386..19295bf38e2 100644 --- a/server/src/main/java/ServerMain.kt +++ b/server/src/main/java/ServerMain.kt @@ -34,8 +34,8 @@ fun main(args: Array) { 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" + "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 @@ -202,7 +202,8 @@ fun getCarsDestroyThread(): Thread { } } for (key in keysToRemove) { - environment.map.remove(key) + //todo this car is MAYBE disconnect. need ping this car and if dont have answer - drop +// environment.map.remove(key) } }) try { diff --git a/server/src/main/java/objects/Environment.kt b/server/src/main/java/objects/Environment.kt index a64f8c81c19..89283b90963 100644 --- a/server/src/main/java/objects/Environment.kt +++ b/server/src/main/java/objects/Environment.kt @@ -8,6 +8,7 @@ import java.util.Random class Environment private constructor() { val map: MutableMap + private var uid = 0 companion object { @@ -20,22 +21,8 @@ class Environment private constructor() { @Synchronized fun connectCar(host: String, port: Int): Int { - val uid = getNewUid() + uid++ map.put(uid, Car(uid, host, port)) return uid } - - fun getNewUid(): Int { - var unique = false - val random = Random() - var uid: Int = 0 - while (!unique) { - uid = random.nextInt(1000000) - if (map.get(uid) == null) { - unique = true; - } - } - return uid; - } - }