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" +
|
||||
"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 {
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.Random
|
||||
class Environment private constructor() {
|
||||
|
||||
val map: MutableMap<Int, Car>
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user