fixed concurrent modification exception of car map
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
package Exceptions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by user on 7/28/16.
|
||||||
|
*/
|
||||||
|
class InactiveCarException : Exception() {
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import Exceptions.InactiveCarException
|
||||||
import car.client.Client
|
import car.client.Client
|
||||||
import io.netty.bootstrap.ServerBootstrap
|
import io.netty.bootstrap.ServerBootstrap
|
||||||
import io.netty.buffer.Unpooled
|
import io.netty.buffer.Unpooled
|
||||||
@@ -77,7 +78,13 @@ fun main(args: Array<String>) {
|
|||||||
request.headers().set(HttpHeaderNames.HOST, car.host)
|
request.headers().set(HttpHeaderNames.HOST, car.host)
|
||||||
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
|
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
|
||||||
request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes())
|
request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes())
|
||||||
Client.sendRequest(request, car.host, car.port, id)
|
try {
|
||||||
|
Client.sendRequest(request, car.host, car.port, id)
|
||||||
|
} catch (e: InactiveCarException) {
|
||||||
|
synchronized(environment, {
|
||||||
|
environment.map.remove(id)
|
||||||
|
})
|
||||||
|
}
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
val wayPointData = wayPointInputString.split(" ")
|
val wayPointData = wayPointInputString.split(" ")
|
||||||
@@ -99,13 +106,24 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
} else if (readedString.equals("refloc", true)) {
|
} else if (readedString.equals("refloc", true)) {
|
||||||
val cars = synchronized(environment, { environment.map.values })
|
val cars = synchronized(environment, { environment.map.values })
|
||||||
|
val inactiveCarUids = mutableListOf<Int>()
|
||||||
for (car in cars) {
|
for (car in cars) {
|
||||||
val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, getLocationUrl, Unpooled.EMPTY_BUFFER)
|
val request = DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, getLocationUrl, Unpooled.EMPTY_BUFFER)
|
||||||
request.headers().set(HttpHeaderNames.HOST, car.host)
|
request.headers().set(HttpHeaderNames.HOST, car.host)
|
||||||
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
|
request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE)
|
||||||
request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes())
|
request.headers().setInt(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes())
|
||||||
Client.sendRequest(request, car.host, car.port, car.uid)
|
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")) {
|
} else if (readedString.equals("stop")) {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package car.client
|
package car.client
|
||||||
|
|
||||||
|
import Exceptions.InactiveCarException
|
||||||
import io.netty.bootstrap.Bootstrap
|
import io.netty.bootstrap.Bootstrap
|
||||||
import io.netty.channel.nio.NioEventLoopGroup
|
import io.netty.channel.nio.NioEventLoopGroup
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel
|
import io.netty.channel.socket.nio.NioSocketChannel
|
||||||
import io.netty.handler.codec.http.HttpRequest
|
import io.netty.handler.codec.http.HttpRequest
|
||||||
import io.netty.util.AttributeKey
|
import io.netty.util.AttributeKey
|
||||||
import objects.Environment
|
|
||||||
import java.net.ConnectException
|
import java.net.ConnectException
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -34,10 +34,7 @@ object Client {
|
|||||||
} catch (e: InterruptedException) {
|
} catch (e: InterruptedException) {
|
||||||
|
|
||||||
} catch (e: ConnectException) {
|
} catch (e: ConnectException) {
|
||||||
val carMap = Environment.instance.map
|
throw InactiveCarException()
|
||||||
synchronized(carMap, {
|
|
||||||
carMap.remove(carUid)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
package car.client
|
package car.client
|
||||||
|
|
||||||
|
import CodedInputStream
|
||||||
|
import InvalidProtocolBufferException
|
||||||
|
import LocationResponse
|
||||||
import getLocationUrl
|
import getLocationUrl
|
||||||
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelHandlerContext
|
||||||
import io.netty.channel.SimpleChannelInboundHandler
|
import io.netty.channel.SimpleChannelInboundHandler
|
||||||
import io.netty.handler.codec.http.HttpContent
|
import io.netty.handler.codec.http.DefaultHttpContent
|
||||||
import io.netty.util.AttributeKey
|
import io.netty.util.AttributeKey
|
||||||
import objects.Environment
|
import objects.Environment
|
||||||
import LocationResponse
|
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import CodedInputStream
|
import java.util.*
|
||||||
import InvalidProtocolBufferException
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by user on 7/8/16.
|
* Created by user on 7/8/16.
|
||||||
@@ -41,6 +42,7 @@ class ClientHandler : SimpleChannelInboundHandler<Any> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
} catch (e: InvalidProtocolBufferException) {
|
} catch (e: InvalidProtocolBufferException) {
|
||||||
|
println("invalic proto format!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
@@ -51,7 +53,7 @@ class ClientHandler : SimpleChannelInboundHandler<Any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun channelRead0(ctx: ChannelHandlerContext?, msg: Any?) {
|
override fun channelRead0(ctx: ChannelHandlerContext?, msg: Any?) {
|
||||||
if (msg is HttpContent) {
|
if (msg is DefaultHttpContent) {
|
||||||
val contentsBytes = msg.content();
|
val contentsBytes = msg.content();
|
||||||
contentBytes = ByteArray(contentsBytes.capacity())
|
contentBytes = ByteArray(contentsBytes.capacity())
|
||||||
contentsBytes.readBytes(contentBytes)
|
contentsBytes.readBytes(contentBytes)
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
class DirectionRequest private constructor (command: DirectionRequest.Command = DirectionRequest.Command.fromIntToCommand(0)) {
|
class DirectionRequest private constructor (command: DirectionRequest.Command = DirectionRequest.Command.fromIntToCommand(0), sid: Int = 0) {
|
||||||
var command : DirectionRequest.Command
|
var command : DirectionRequest.Command
|
||||||
private set
|
private set
|
||||||
|
|
||||||
|
var sid : Int
|
||||||
|
private set
|
||||||
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
this.command = command
|
this.command = command
|
||||||
|
this.sid = sid
|
||||||
}
|
}
|
||||||
enum class Command(val ord: Int) {
|
enum class Command(val ord: Int) {
|
||||||
stop (0),
|
stop (0),
|
||||||
@@ -31,9 +35,12 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
||||||
output.writeEnum (1, command.ord)
|
output.writeEnum (1, command.ord)
|
||||||
}
|
}
|
||||||
|
if (sid != 0) {
|
||||||
|
output.writeInt32 (2, sid)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BuilderDirectionRequest constructor (command: DirectionRequest.Command = DirectionRequest.Command.fromIntToCommand(0)) {
|
class BuilderDirectionRequest constructor (command: DirectionRequest.Command = DirectionRequest.Command.fromIntToCommand(0), sid: Int = 0) {
|
||||||
var command : DirectionRequest.Command
|
var command : DirectionRequest.Command
|
||||||
private set
|
private set
|
||||||
fun setCommand(value: DirectionRequest.Command): DirectionRequest.BuilderDirectionRequest {
|
fun setCommand(value: DirectionRequest.Command): DirectionRequest.BuilderDirectionRequest {
|
||||||
@@ -41,19 +48,30 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sid : Int
|
||||||
|
private set
|
||||||
|
fun setSid(value: Int): DirectionRequest.BuilderDirectionRequest {
|
||||||
|
sid = value
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
this.command = command
|
this.command = command
|
||||||
|
this.sid = sid
|
||||||
}
|
}
|
||||||
|
|
||||||
fun writeTo (output: CodedOutputStream) {
|
fun writeTo (output: CodedOutputStream) {
|
||||||
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
||||||
output.writeEnum (1, command.ord)
|
output.writeEnum (1, command.ord)
|
||||||
}
|
}
|
||||||
|
if (sid != 0) {
|
||||||
|
output.writeInt32 (2, sid)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun build(): DirectionRequest {
|
fun build(): DirectionRequest {
|
||||||
return DirectionRequest(command)
|
return DirectionRequest(command, sid)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun parseFieldFrom(input: CodedInputStream): Boolean {
|
fun parseFieldFrom(input: CodedInputStream): Boolean {
|
||||||
@@ -68,6 +86,11 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
throw InvalidProtocolBufferException("Error: Field number 1 has wire type WireType.VARINT but read ${wireType.toString()}")}
|
throw InvalidProtocolBufferException("Error: Field number 1 has wire type WireType.VARINT but read ${wireType.toString()}")}
|
||||||
command = DirectionRequest.Command.fromIntToCommand(input.readEnumNoTag())
|
command = DirectionRequest.Command.fromIntToCommand(input.readEnumNoTag())
|
||||||
}
|
}
|
||||||
|
2 -> {
|
||||||
|
if (wireType != WireType.VARINT) {
|
||||||
|
throw InvalidProtocolBufferException("Error: Field number 2 has wire type WireType.VARINT but read ${wireType.toString()}")}
|
||||||
|
sid = input.readInt32NoTag()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true}
|
return true}
|
||||||
fun parseFromWithSize(input: CodedInputStream, expectedSize: Int): DirectionRequest.BuilderDirectionRequest {
|
fun parseFromWithSize(input: CodedInputStream, expectedSize: Int): DirectionRequest.BuilderDirectionRequest {
|
||||||
@@ -86,6 +109,9 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
||||||
size += WireFormat.getEnumSize(1, command.ord)
|
size += WireFormat.getEnumSize(1, command.ord)
|
||||||
}
|
}
|
||||||
|
if (sid != 0) {
|
||||||
|
size += WireFormat.getInt32Size(2, sid)
|
||||||
|
}
|
||||||
size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED)
|
size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED)
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
@@ -94,6 +120,9 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
||||||
size += WireFormat.getEnumSize(1, command.ord)
|
size += WireFormat.getEnumSize(1, command.ord)
|
||||||
}
|
}
|
||||||
|
if (sid != 0) {
|
||||||
|
size += WireFormat.getInt32Size(2, sid)
|
||||||
|
}
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,6 +130,7 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
|
|
||||||
fun mergeWith (other: DirectionRequest) {
|
fun mergeWith (other: DirectionRequest) {
|
||||||
command = other.command
|
command = other.command
|
||||||
|
sid = other.sid
|
||||||
}
|
}
|
||||||
|
|
||||||
fun mergeFromWithSize (input: CodedInputStream, expectedSize: Int) {
|
fun mergeFromWithSize (input: CodedInputStream, expectedSize: Int) {
|
||||||
@@ -115,6 +145,9 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
||||||
size += WireFormat.getEnumSize(1, command.ord)
|
size += WireFormat.getEnumSize(1, command.ord)
|
||||||
}
|
}
|
||||||
|
if (sid != 0) {
|
||||||
|
size += WireFormat.getInt32Size(2, sid)
|
||||||
|
}
|
||||||
size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED)
|
size += WireFormat.getVarint32Size(size) + WireFormat.getTagSize(fieldNumber, WireType.LENGTH_DELIMITED)
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
@@ -123,6 +156,9 @@ class DirectionRequest private constructor (command: DirectionRequest.Command =
|
|||||||
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
if (command != DirectionRequest.Command.fromIntToCommand(0)) {
|
||||||
size += WireFormat.getEnumSize(1, command.ord)
|
size += WireFormat.getEnumSize(1, command.ord)
|
||||||
}
|
}
|
||||||
|
if (sid != 0) {
|
||||||
|
size += WireFormat.getInt32Size(2, sid)
|
||||||
|
}
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user