Commit 3233ef90 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Re-add locking

parent 35793998
......@@ -11,7 +11,9 @@ import java.net.DatagramSocket
import java.net.Socket
import java.net.SocketException
import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.collections.ArrayList
import kotlin.concurrent.withLock
/*
* Copyright (C) 2019 Daniel Wolf (Ch4t4r)
......@@ -41,6 +43,7 @@ class ForwardedPacketStore(var logger: Logger?) {
private val _nonPollableForwardedPackets:LinkedList<FutureAnswer> = LinkedList()
private val newlyForwardedPackets: ArrayList<FutureAnswer> = ArrayList()
private val newlyForwardedNonPollablePackets: ArrayList<FutureAnswer> = ArrayList()
private val newPacketsLock = ReentrantLock()
val nonPollableForwardedPackets:List<FutureAnswer> = _nonPollableForwardedPackets
val forwardedPollablePackets:List<FutureAnswer> = _forwardedPollablePackets
var hasMixedAnswerType:Boolean = true
......@@ -63,27 +66,33 @@ class ForwardedPacketStore(var logger: Logger?) {
@Throws(IOException::class)
fun addForwardedQuestion(futureAnswer: FutureAnswer) {
if(futureAnswer.isPollable()) newlyForwardedPackets.add(futureAnswer)
else newlyForwardedNonPollablePackets.add(futureAnswer)
newPacketsLock.withLock {
if(futureAnswer.isPollable()) newlyForwardedPackets.add(futureAnswer)
else newlyForwardedNonPollablePackets.add(futureAnswer)
}
}
fun processNewAnswers(pollable:Boolean) {
if(pollable && newlyForwardedPackets.isNotEmpty()) {
hasMixedAnswerType = newlyForwardedNonPollablePackets.isNotEmpty()
for (packet in newlyForwardedPackets) {
packet.index = _forwardedPollablePackets.size
if(packet.isPollable()) _forwardedPollablePackets.add(packet)
else _forwardedPollablePackets.add(packet)
newPacketsLock.withLock {
for (packet in newlyForwardedPackets) {
packet.index = _forwardedPollablePackets.size
if(packet.isPollable()) _forwardedPollablePackets.add(packet)
else _forwardedPollablePackets.add(packet)
}
newlyForwardedPackets.clear()
}
newlyForwardedPackets.clear()
} else if(!pollable && newlyForwardedNonPollablePackets.isNotEmpty()) {
hasMixedAnswerType = newlyForwardedPackets.isNotEmpty()
for (packet in newlyForwardedNonPollablePackets) {
packet.index = _nonPollableForwardedPackets.size
if(packet.isPollable()) _nonPollableForwardedPackets.add(packet)
else _nonPollableForwardedPackets.add(packet)
newPacketsLock.withLock {
hasMixedAnswerType = newlyForwardedPackets.isNotEmpty()
for (packet in newlyForwardedNonPollablePackets) {
packet.index = _nonPollableForwardedPackets.size
if(packet.isPollable()) _nonPollableForwardedPackets.add(packet)
else _nonPollableForwardedPackets.add(packet)
}
newlyForwardedNonPollablePackets.clear()
}
newlyForwardedNonPollablePackets.clear()
}
}
......@@ -97,22 +106,24 @@ class ForwardedPacketStore(var logger: Logger?) {
if(clearing) return
clearing = true
logger?.fine("Clearing ForwardedPacketStore")
newPacketsLock.withLock {
newlyForwardedPackets.forEach {
cleanup(it)
}
newlyForwardedNonPollablePackets.forEach {
cleanup(it)
}
newlyForwardedPackets.clear()
newlyForwardedNonPollablePackets.clear()
}
forwardedPollablePackets.forEach {
cleanup(it)
}
_nonPollableForwardedPackets.forEach {
cleanup(it)
}
newlyForwardedPackets.forEach {
cleanup(it)
}
newlyForwardedNonPollablePackets.forEach {
cleanup(it)
}
_nonPollableForwardedPackets.clear()
_forwardedPollablePackets.clear()
newlyForwardedPackets.clear()
newlyForwardedNonPollablePackets.clear()
clearing = false
if(completely) logger = null
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment