make sequentially numbered ids, off drop car by timeout (worked incorrect)
This commit is contained in:
@@ -34,8 +34,8 @@ fun main(args: Array<String>) {
|
|||||||
val helpString = "available commands:\n" +
|
val helpString = "available commands:\n" +
|
||||||
"cars - get list of connected cars\n" +
|
"cars - get list of connected cars\n" +
|
||||||
"route [car_id] - setting a route for car with car id.\n" +
|
"route [car_id] - setting a route for car with car id.\n" +
|
||||||
"refloc - refresh all car locations\n" +
|
"refloc - refresh all car locations\n"
|
||||||
"stop - exit from this interface and stop all servers\n"
|
// "stop - exit from this interface and stop all servers\n"//todo sometimes server not completed. some threads dont stop
|
||||||
println(helpString)
|
println(helpString)
|
||||||
val routeRegex = Regex("route [0-9]{1,10}")
|
val routeRegex = Regex("route [0-9]{1,10}")
|
||||||
val environment = objects.Environment.instance
|
val environment = objects.Environment.instance
|
||||||
@@ -202,7 +202,8 @@ fun getCarsDestroyThread(): Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (key in keysToRemove) {
|
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 {
|
try {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import java.util.Random
|
|||||||
class Environment private constructor() {
|
class Environment private constructor() {
|
||||||
|
|
||||||
val map: MutableMap<Int, Car>
|
val map: MutableMap<Int, Car>
|
||||||
|
private var uid = 0
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
@@ -20,22 +21,8 @@ class Environment private constructor() {
|
|||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun connectCar(host: String, port: Int): Int {
|
fun connectCar(host: String, port: Int): Int {
|
||||||
val uid = getNewUid()
|
uid++
|
||||||
map.put(uid, Car(uid, host, port))
|
map.put(uid, Car(uid, host, port))
|
||||||
return uid
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user