refactoring
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
Raffaele Mignone 2020-05-03 19:38:55 +02:00
parent 711da44f53
commit b50890b44f
Signed by: norangebit
GPG Key ID: F5255658CB220573
20 changed files with 74 additions and 77 deletions

View File

@ -21,26 +21,35 @@ class AverageNode(
if (packet.isFeedback)
changeValue(payload)
else {
computeAverage(payload)
sendFeedback(packet.senderAddress)
spreadNewValue()
}
}
private fun computeAverage(payload: Double) {
val newValue = (nodeValue + payload) / 2
changeValue(newValue)
}
private fun sendFeedback(receiverAddress: Address) {
send(
FeedbackEpidemicPacket(
address,
packet.senderAddress,
newValue,
receiverAddress,
nodeValue,
if (infectStrategy == InfectStrategy.PUSH)
EpidemicPacket.Type.PUSH else EpidemicPacket.Type.REPLY,
true
)
)
when (infectStrategy) {
InfectStrategy.PUSH -> sendPush()
InfectStrategy.PULL -> sendPull()
InfectStrategy.PUSHPULL -> sendPushPull()
}
private fun spreadNewValue() {
when (infectStrategy) {
InfectStrategy.PUSH -> sendPushToRandom()
InfectStrategy.PULL -> sendPullToRandom()
InfectStrategy.PUSHPULL -> sendPushPullToRandom()
}
}

View File

@ -37,9 +37,9 @@ class DbNode<T>(
override fun infect() {
when (infectStrategy) {
InfectStrategy.PUSH -> sendPush()
InfectStrategy.PULL -> sendPull()
InfectStrategy.PUSHPULL -> sendPushPull()
InfectStrategy.PUSH -> sendPushToRandom()
InfectStrategy.PULL -> sendPullToRandom()
InfectStrategy.PUSHPULL -> sendPushPullToRandom()
}
Thread.sleep(DELTA)

View File

@ -4,7 +4,6 @@ import util.network.epidemic.TimedPayload
import util.network.simulator.Address
import util.network.simulator.Network
fun main() {
val network = Network()
@ -29,4 +28,3 @@ fun main() {
println((it as DbNode<Int>).nodeValue)
}
}

View File

@ -4,7 +4,6 @@ import util.network.epidemic.TimedPayload
import util.network.simulator.Address
import util.network.simulator.Network
fun main() {
val network = Network()
@ -29,4 +28,3 @@ fun main() {
println((it as DbNode<Int>).nodeValue)
}
}

View File

@ -4,7 +4,6 @@ import util.network.epidemic.TimedPayload
import util.network.simulator.Address
import util.network.simulator.Network
fun main() {
val network = Network()
@ -29,4 +28,3 @@ fun main() {
println((it as DbNode<Int>).nodeValue)
}
}

View File

@ -11,6 +11,7 @@ class QueryNode<T>(
address: Address,
network: Network,
initialValue: T,
private val demotivatingFactor: Int = 10,
bufferSize: Int = 1
) : EpidemicNode<T, FeedbackEpidemicPacket<T>>(
address, network, initialValue, bufferSize
@ -32,26 +33,25 @@ class QueryNode<T>(
}
override fun receivePushOrReply(packet: FeedbackEpidemicPacket<T>) {
//println(packet)
if (nodeState == State.REMOVED)
return
if (nodeState == State.SUSCEPTIBLE) {
// println("susceptible")
nodeState = State.INFECTED
queryValue = packet.payload
sendPush(1)
if (packet.payload == nodeValue)
println("find on node $address")
} else {
if (packet.isFeedback && random.nextBoolean()) {
nodeState = State.REMOVED
println("removed")
when (nodeState) {
State.REMOVED -> return
State.INFECTED -> whenInfected(packet)
State.SUSCEPTIBLE -> whenSusceptible(packet)
}
}
private fun whenSusceptible(packet: FeedbackEpidemicPacket<T>) {
nodeState = State.INFECTED
queryValue = packet.payload
sendPushToRandom(2)
if (packet.payload == nodeValue)
println("find ${packet.payload} on node $address")
}
private fun whenInfected(packet: FeedbackEpidemicPacket<T>) {
if (!packet.isFeedback)
send(
FeedbackEpidemicPacket(
address,
@ -61,7 +61,9 @@ class QueryNode<T>(
true
)
)
}
if (packet.isFeedback && random.nextDouble(1.0) < 1 / demotivatingFactor)
nodeState = State.REMOVED
}
override fun receivePull(packet: FeedbackEpidemicPacket<T>) {

View File

@ -5,12 +5,11 @@ import util.network.epidemic.packet.FeedbackEpidemicPacket
import util.network.simulator.Address
import util.network.simulator.Network
fun main() {
val network = Network()
repeat(10) {
QueryNode(Address(it), network, it*2)
repeat(20) {
QueryNode(Address(it), network, it * 2)
}
val alpha = network.getRandomNode() as QueryNode<Int>
@ -19,7 +18,7 @@ fun main() {
val packet = FeedbackEpidemicPacket(
alpha.address,
beta.address,
14,
24,
EpidemicPacket.Type.PUSH,
false
)
@ -28,4 +27,3 @@ fun main() {
network.start()
}

View File

@ -5,7 +5,7 @@ import util.network.simulator.Address
import util.network.simulator.Network
import util.network.simulator.Node
abstract class EpidemicNode<T, P: EpidemicPacket<T>>(
abstract class EpidemicNode<T, P : EpidemicPacket<T>>(
address: Address,
network: Network,
initialValue: T,
@ -41,21 +41,21 @@ abstract class EpidemicNode<T, P: EpidemicPacket<T>>(
type: EpidemicPacket.Type
): EpidemicPacket<T>
protected fun sendPush(numberOfReceiver: Int = 1) {
protected fun sendPushToRandom(numberOfReceiver: Int = 1) {
sendToRandom(
EpidemicPacket.Type.PUSH,
numberOfReceiver
)
}
protected fun sendPull(numberOfReceiver: Int = 1) {
protected fun sendPullToRandom(numberOfReceiver: Int = 1) {
sendToRandom(
EpidemicPacket.Type.PULL,
numberOfReceiver
)
}
protected fun sendPushPull(numberOfReceiver: Int = 1) {
protected fun sendPushPullToRandom(numberOfReceiver: Int = 1) {
sendToRandom(
EpidemicPacket.Type.PUSHPULL,
numberOfReceiver
@ -69,8 +69,6 @@ abstract class EpidemicNode<T, P: EpidemicPacket<T>>(
protected abstract fun infect()
protected fun onReceive(packet: P) {
//println(packet.type)
when (packet.type) {
EpidemicPacket.Type.PUSH -> receivePushOrReply(packet)
EpidemicPacket.Type.PULL -> receivePull(packet)
@ -125,5 +123,4 @@ abstract class EpidemicNode<T, P: EpidemicPacket<T>>(
return EpidemicSender(threadGroup)
}
}
}

View File

@ -2,12 +2,10 @@ package util.network.epidemic.packet
import util.network.simulator.packet.PayloadPacket
interface EpidemicPacket<T>: PayloadPacket<T> {
interface EpidemicPacket<T> : PayloadPacket<T> {
val type: Type
enum class Type {
PUSH, PULL, PUSHPULL, REPLY
}
}

View File

@ -10,4 +10,3 @@ data class FeedbackEpidemicPacket<T>(
override val type: EpidemicPacket.Type,
override val isFeedback: Boolean
) : EpidemicPacket<T>, FeedbackPacket

View File

@ -8,4 +8,4 @@ data class TimedEpidemicPacket<T>(
override val receiverAddress: Address,
override val payload: TimedPayload<T>,
override val type: EpidemicPacket.Type
): EpidemicPacket<TimedPayload<T>>
) : EpidemicPacket<TimedPayload<T>>

View File

@ -17,7 +17,7 @@ class Network(threadGrupName: String = "network") {
addresses.add(node.address)
}
fun getRandomAddress() : Address {
fun getRandomAddress(): Address {
val index = random.nextInt(addresses.size)
return addresses[index]

View File

@ -12,12 +12,12 @@ fun main() {
network.start()
}
class TestNode(address: Address, network: Network): Node(address, network) {
class TestNode(address: Address, network: Network) : Node(address, network) {
override fun communicatorFactory(threadGroup: ThreadGroup): CommunicatorFactory {
return TestCommunicatorFactory(threadGroup)
}
private inner class TestReceiver(threadGroup: ThreadGroup): Receiver(threadGroup) {
private inner class TestReceiver(threadGroup: ThreadGroup) : Receiver(threadGroup) {
override fun run() {
if (address != Address(0))
return
@ -28,7 +28,7 @@ class TestNode(address: Address, network: Network): Node(address, network) {
}
}
private inner class TestSender(threadGroup: ThreadGroup): Sender(threadGroup) {
private inner class TestSender(threadGroup: ThreadGroup) : Sender(threadGroup) {
override fun run() {
if (address != Address(1))
return
@ -39,7 +39,8 @@ class TestNode(address: Address, network: Network): Node(address, network) {
}
}
private inner class TestCommunicatorFactory(threadGroup: ThreadGroup): CommunicatorFactory(threadGroup) {
private inner class TestCommunicatorFactory(threadGroup: ThreadGroup) :
CommunicatorFactory(threadGroup) {
override fun createReceiver(): Receiver {
return TestReceiver(threadGroup)
}

View File

@ -1,5 +1,5 @@
package util.network.simulator.packet
interface FeedbackPacket: Packet {
interface FeedbackPacket : Packet {
val isFeedback: Boolean
}

View File

@ -6,4 +6,3 @@ data class NetworkPacket(
override val senderAddress: Address,
override val receiverAddress: Address
) : Packet

View File

@ -1,5 +1,5 @@
package util.network.simulator.packet
interface PayloadPacket<T>: Packet {
interface PayloadPacket<T> : Packet {
val payload: T
}