lm-sistemi-software-distrib.../src/main/kotlin/drills/drill09/average/AverageNode.kt

108 lines
3.0 KiB
Kotlin
Raw Normal View History

2020-05-03 09:04:28 +00:00
package drills.drill09.average
2020-05-02 20:27:43 +00:00
import util.network.epidemic.EpidemicNode
import util.network.epidemic.packet.EpidemicPacket
import util.network.epidemic.packet.FeedbackEpidemicPacket
2020-05-02 20:27:43 +00:00
import util.network.simulator.Address
import util.network.simulator.Network
class AverageNode(
address: Address,
network: Network,
initialValue: Double,
val infectStrategy: InfectStrategy = InfectStrategy.PUSH,
2020-05-02 20:27:43 +00:00
bufferSize: Int = 1
) : EpidemicNode<Double, FeedbackEpidemicPacket<Double>>(
address, network, initialValue, bufferSize
) {
override fun receivePushOrReply(packet: FeedbackEpidemicPacket<Double>) {
2020-05-02 20:27:43 +00:00
val payload = packet.payload
if (packet.isFeedback)
2020-05-02 20:27:43 +00:00
changeValue(payload)
else {
2020-05-03 17:38:55 +00:00
computeAverage(payload)
sendFeedback(packet.senderAddress)
spreadNewValue()
}
}
2020-05-02 20:27:43 +00:00
2020-05-03 17:38:55 +00:00
private fun computeAverage(payload: Double) {
val newValue = (nodeValue + payload) / 2
changeValue(newValue)
}
2020-05-02 20:27:43 +00:00
2020-05-03 17:38:55 +00:00
private fun sendFeedback(receiverAddress: Address) {
send(
FeedbackEpidemicPacket(
address,
receiverAddress,
nodeValue,
if (infectStrategy == InfectStrategy.PUSH)
EpidemicPacket.Type.PUSH else EpidemicPacket.Type.REPLY,
true
2020-05-02 20:27:43 +00:00
)
2020-05-03 17:38:55 +00:00
)
}
2020-05-02 20:27:43 +00:00
2020-05-03 17:38:55 +00:00
private fun spreadNewValue() {
when (infectStrategy) {
InfectStrategy.PUSH -> sendPushToRandom()
InfectStrategy.PULL -> sendPullToRandom()
InfectStrategy.PUSHPULL -> sendPushPullToRandom()
2020-05-02 20:27:43 +00:00
}
}
override fun receivePull(packet: FeedbackEpidemicPacket<Double>) {
if (packet.type == EpidemicPacket.Type.PUSHPULL)
return
send(
makeInfectionPacket(
packet.senderAddress,
EpidemicPacket.Type.REPLY
)
)
2020-05-02 20:27:43 +00:00
}
override fun infect() {
Thread.interrupted()
}
2020-05-03 09:04:28 +00:00
override fun makeInfectionPacket(
receiverAddress: Address,
2020-05-02 20:27:43 +00:00
type: EpidemicPacket.Type
2020-05-03 09:04:28 +00:00
): EpidemicPacket<Double> {
return FeedbackEpidemicPacket(
2020-05-02 20:27:43 +00:00
address,
2020-05-03 09:04:28 +00:00
receiverAddress,
2020-05-02 20:27:43 +00:00
nodeValue,
type,
false
)
}
companion object {
fun createPushNode(
address: Address,
network: Network,
initialValue: Double,
bufferSize: Int = 1
) = AverageNode(address, network, initialValue, InfectStrategy.PUSH, bufferSize)
fun createPullNode(
address: Address,
network: Network,
initialValue: Double,
bufferSize: Int = 1
) = AverageNode(address, network, initialValue, InfectStrategy.PULL, bufferSize)
fun createPushPullNode(
address: Address,
network: Network,
initialValue: Double,
bufferSize: Int = 1
) = AverageNode(address, network, initialValue, InfectStrategy.PUSHPULL, bufferSize)
}
2020-05-03 17:38:55 +00:00
}