add average and db push, pull and pushpull
This commit is contained in:
parent
32ca1dd9a0
commit
581d9365ee
@ -1,8 +1,8 @@
|
||||
package drills.drill09.average
|
||||
|
||||
import util.network.epidemic.EpidemicNode
|
||||
import util.network.epidemic.EpidemicPacket
|
||||
import util.network.epidemic.replay.ReplayPacket
|
||||
import util.network.epidemic.packet.EpidemicPacket
|
||||
import util.network.epidemic.packet.FeedbackEpidemicPacket
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
@ -10,12 +10,15 @@ class AverageNode(
|
||||
address: Address,
|
||||
network: Network,
|
||||
initialValue: Double,
|
||||
val infectStrategy: InfectStrategy = InfectStrategy.PUSH,
|
||||
bufferSize: Int = 1
|
||||
) : EpidemicNode<Double, ReplayPacket<Double>>(address, network, initialValue, bufferSize) {
|
||||
override fun receivePushOrReply(packet: ReplayPacket<Double>) {
|
||||
) : EpidemicNode<Double, FeedbackEpidemicPacket<Double>>(
|
||||
address, network, initialValue, bufferSize
|
||||
) {
|
||||
override fun receivePushOrReply(packet: FeedbackEpidemicPacket<Double>) {
|
||||
val payload = packet.payload
|
||||
|
||||
if (packet.isReplay)
|
||||
if (packet.isFeedback)
|
||||
changeValue(payload)
|
||||
else {
|
||||
val newValue = (nodeValue + payload) / 2
|
||||
@ -23,21 +26,34 @@ class AverageNode(
|
||||
changeValue(newValue)
|
||||
|
||||
send(
|
||||
ReplayPacket(
|
||||
FeedbackEpidemicPacket(
|
||||
address,
|
||||
packet.senderAddress,
|
||||
newValue,
|
||||
EpidemicPacket.Type.PUSH,
|
||||
if (infectStrategy == InfectStrategy.PUSH)
|
||||
EpidemicPacket.Type.PUSH else EpidemicPacket.Type.REPLY,
|
||||
true
|
||||
)
|
||||
)
|
||||
|
||||
sendPush()
|
||||
when (infectStrategy) {
|
||||
InfectStrategy.PUSH -> sendPush()
|
||||
InfectStrategy.PULL -> sendPull()
|
||||
InfectStrategy.PUSHPULL -> sendPushPull()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun receivePull(packet: ReplayPacket<Double>) {
|
||||
TODO("Not yet implemented")
|
||||
override fun receivePull(packet: FeedbackEpidemicPacket<Double>) {
|
||||
if (packet.type == EpidemicPacket.Type.PUSHPULL)
|
||||
return
|
||||
|
||||
send(
|
||||
makeInfectionPacket(
|
||||
packet.senderAddress,
|
||||
EpidemicPacket.Type.REPLY
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override fun infect() {
|
||||
@ -48,7 +64,7 @@ class AverageNode(
|
||||
receiverAddress: Address,
|
||||
type: EpidemicPacket.Type
|
||||
): EpidemicPacket<Double> {
|
||||
return ReplayPacket(
|
||||
return FeedbackEpidemicPacket(
|
||||
address,
|
||||
receiverAddress,
|
||||
nodeValue,
|
||||
@ -56,4 +72,27 @@ class AverageNode(
|
||||
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)
|
||||
}
|
||||
}
|
35
src/main/kotlin/drills/drill09/average/TestPull.kt
Normal file
35
src/main/kotlin/drills/drill09/average/TestPull.kt
Normal file
@ -0,0 +1,35 @@
|
||||
package drills.drill09.average
|
||||
|
||||
import util.network.epidemic.packet.EpidemicPacket
|
||||
import util.network.epidemic.packet.FeedbackEpidemicPacket
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
fun main() {
|
||||
val network = Network()
|
||||
|
||||
repeat(17) {
|
||||
AverageNode.createPullNode(Address(it), network, 0.0)
|
||||
}
|
||||
|
||||
val alpha = network.getRandomNode() as AverageNode
|
||||
val beta = network.getRandomNode() as AverageNode
|
||||
|
||||
val packet = FeedbackEpidemicPacket(
|
||||
alpha.address,
|
||||
beta.address,
|
||||
1,
|
||||
EpidemicPacket.Type.REPLY,
|
||||
false
|
||||
)
|
||||
|
||||
alpha.send(packet)
|
||||
|
||||
network.start()
|
||||
|
||||
Thread.sleep(2000)
|
||||
|
||||
network.forEach {
|
||||
println(1.0 / (it as AverageNode).nodeValue)
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package drills.drill09.average
|
||||
|
||||
import util.network.epidemic.EpidemicPacket
|
||||
import util.network.epidemic.replay.ReplayPacket
|
||||
import util.network.epidemic.packet.EpidemicPacket
|
||||
import util.network.epidemic.packet.FeedbackEpidemicPacket
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
@ -9,13 +9,13 @@ fun main() {
|
||||
val network = Network()
|
||||
|
||||
repeat(17) {
|
||||
AverageNode(Address(it), network, 0.0)
|
||||
AverageNode.createPushNode(Address(it), network, 0.0)
|
||||
}
|
||||
|
||||
val alpha = network.getRandomNode() as AverageNode
|
||||
val beta = network.getRandomNode() as AverageNode
|
||||
|
||||
val packet = ReplayPacket(
|
||||
val packet = FeedbackEpidemicPacket(
|
||||
alpha.address,
|
||||
beta.address,
|
||||
1,
|
35
src/main/kotlin/drills/drill09/average/TestPushPull.kt
Normal file
35
src/main/kotlin/drills/drill09/average/TestPushPull.kt
Normal file
@ -0,0 +1,35 @@
|
||||
package drills.drill09.average
|
||||
|
||||
import util.network.epidemic.packet.EpidemicPacket
|
||||
import util.network.epidemic.packet.FeedbackEpidemicPacket
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
fun main() {
|
||||
val network = Network()
|
||||
|
||||
repeat(17) {
|
||||
AverageNode.createPushPullNode(Address(it), network, 0.0)
|
||||
}
|
||||
|
||||
val alpha = network.getRandomNode() as AverageNode
|
||||
val beta = network.getRandomNode() as AverageNode
|
||||
|
||||
val packet = FeedbackEpidemicPacket(
|
||||
alpha.address,
|
||||
beta.address,
|
||||
1,
|
||||
EpidemicPacket.Type.REPLY,
|
||||
false
|
||||
)
|
||||
|
||||
alpha.send(packet)
|
||||
|
||||
network.start()
|
||||
|
||||
Thread.sleep(2000)
|
||||
|
||||
network.forEach {
|
||||
println(1.0 / (it as AverageNode).nodeValue)
|
||||
}
|
||||
}
|
@ -1,54 +1,84 @@
|
||||
package drills.drill09.dbreplica
|
||||
|
||||
import util.network.epidemic.TimePayload
|
||||
import util.network.epidemic.TimedPayload
|
||||
import util.network.epidemic.EpidemicNode
|
||||
import util.network.epidemic.EpidemicPacket
|
||||
import util.network.epidemic.packet.EpidemicPacket
|
||||
import util.network.epidemic.packet.TimedEpidemicPacket
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
class DbNode<T>(
|
||||
address: Address,
|
||||
network: Network,
|
||||
initialValue: TimePayload<T>,
|
||||
initialValue: TimedPayload<T>,
|
||||
val infectStrategy: InfectStrategy = InfectStrategy.PUSHPULL,
|
||||
bufferSize: Int = 1
|
||||
) : EpidemicNode<TimePayload<T>, EpidemicPacket<TimePayload<T>>>(
|
||||
) : EpidemicNode<TimedPayload<T>, TimedEpidemicPacket<T>>(
|
||||
address, network, initialValue, bufferSize
|
||||
) {
|
||||
override fun receivePushOrReply(packet: EpidemicPacket<TimePayload<T>>) {
|
||||
override fun receivePushOrReply(packet: TimedEpidemicPacket<T>) {
|
||||
val receivedValue = packet.payload
|
||||
|
||||
if (nodeValue.timestamp < receivedValue.timestamp)
|
||||
changeValue(receivedValue)
|
||||
}
|
||||
|
||||
override fun receivePull(packet: EpidemicPacket<TimePayload<T>>) {
|
||||
override fun receivePull(packet: TimedEpidemicPacket<T>) {
|
||||
val receivedValue = packet.payload
|
||||
|
||||
if (nodeValue.timestamp > receivedValue.timestamp)
|
||||
send(
|
||||
EpidemicPacket(
|
||||
address,
|
||||
makeInfectionPacket(
|
||||
packet.senderAddress,
|
||||
nodeValue,
|
||||
EpidemicPacket.Type.REPLY
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
override fun infect() {
|
||||
sendPushPull()
|
||||
Thread.sleep(100)
|
||||
when (infectStrategy) {
|
||||
InfectStrategy.PUSH -> sendPush()
|
||||
InfectStrategy.PULL -> sendPull()
|
||||
InfectStrategy.PUSHPULL -> sendPushPull()
|
||||
}
|
||||
|
||||
Thread.sleep(DELTA)
|
||||
}
|
||||
|
||||
override fun makeInfectionPacket(
|
||||
receiverAddress: Address,
|
||||
type: EpidemicPacket.Type
|
||||
): EpidemicPacket<TimePayload<T>> {
|
||||
return EpidemicPacket(
|
||||
): EpidemicPacket<TimedPayload<T>> {
|
||||
return TimedEpidemicPacket(
|
||||
address,
|
||||
receiverAddress,
|
||||
nodeValue,
|
||||
type
|
||||
)
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val DELTA = 100L
|
||||
|
||||
fun <T> createPushNode(
|
||||
address: Address,
|
||||
network: Network,
|
||||
initialValue: TimedPayload<T>,
|
||||
bufferSize: Int = 1
|
||||
) = DbNode(address, network, initialValue, InfectStrategy.PUSH, bufferSize)
|
||||
|
||||
fun <T> createPullNode(
|
||||
address: Address,
|
||||
network: Network,
|
||||
initialValue: TimedPayload<T>,
|
||||
bufferSize: Int = 1
|
||||
) = DbNode(address, network, initialValue, InfectStrategy.PULL, bufferSize)
|
||||
|
||||
fun <T> createPushPullNode(
|
||||
address: Address,
|
||||
network: Network,
|
||||
initialValue: TimedPayload<T>,
|
||||
bufferSize: Int = 1
|
||||
) = DbNode(address, network, initialValue, InfectStrategy.PUSHPULL, bufferSize)
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package drills.drill09.dbreplica
|
||||
|
||||
import util.network.epidemic.TimePayload
|
||||
import util.network.epidemic.TimedPayload
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
@ -9,7 +9,7 @@ fun main() {
|
||||
val network = Network()
|
||||
|
||||
repeat(10) {
|
||||
DbNode(Address(it), network, TimePayload(0, 0))
|
||||
DbNode.createPullNode(Address(it), network, TimedPayload(0, 0))
|
||||
}
|
||||
|
||||
val alpha = network.getRandomNode() as DbNode<Int>
|
||||
@ -22,7 +22,7 @@ fun main() {
|
||||
println((it as DbNode<Int>).nodeValue)
|
||||
}
|
||||
|
||||
alpha.changeValue(TimePayload(42, System.nanoTime()))
|
||||
alpha.changeValue(TimedPayload(42, System.nanoTime()))
|
||||
Thread.sleep(2000)
|
||||
|
||||
network.forEach {
|
32
src/main/kotlin/drills/drill09/dbreplica/TestPush.kt
Normal file
32
src/main/kotlin/drills/drill09/dbreplica/TestPush.kt
Normal file
@ -0,0 +1,32 @@
|
||||
package drills.drill09.dbreplica
|
||||
|
||||
import util.network.epidemic.TimedPayload
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
|
||||
fun main() {
|
||||
val network = Network()
|
||||
|
||||
repeat(10) {
|
||||
DbNode.createPushNode(Address(it), network, TimedPayload(0, 0))
|
||||
}
|
||||
|
||||
val alpha = network.getRandomNode() as DbNode<Int>
|
||||
|
||||
network.start()
|
||||
|
||||
Thread.sleep(1000)
|
||||
|
||||
network.forEach {
|
||||
println((it as DbNode<Int>).nodeValue)
|
||||
}
|
||||
|
||||
alpha.changeValue(TimedPayload(42, System.nanoTime()))
|
||||
Thread.sleep(2000)
|
||||
|
||||
network.forEach {
|
||||
println((it as DbNode<Int>).nodeValue)
|
||||
}
|
||||
}
|
||||
|
32
src/main/kotlin/drills/drill09/dbreplica/TestPushPull.kt
Normal file
32
src/main/kotlin/drills/drill09/dbreplica/TestPushPull.kt
Normal file
@ -0,0 +1,32 @@
|
||||
package drills.drill09.dbreplica
|
||||
|
||||
import util.network.epidemic.TimedPayload
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
|
||||
|
||||
fun main() {
|
||||
val network = Network()
|
||||
|
||||
repeat(10) {
|
||||
DbNode.createPushPullNode(Address(it), network, TimedPayload(0, 0))
|
||||
}
|
||||
|
||||
val alpha = network.getRandomNode() as DbNode<Int>
|
||||
|
||||
network.start()
|
||||
|
||||
Thread.sleep(1000)
|
||||
|
||||
network.forEach {
|
||||
println((it as DbNode<Int>).nodeValue)
|
||||
}
|
||||
|
||||
alpha.changeValue(TimedPayload(42, System.nanoTime()))
|
||||
Thread.sleep(2000)
|
||||
|
||||
network.forEach {
|
||||
println((it as DbNode<Int>).nodeValue)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package util.network.epidemic
|
||||
|
||||
import util.network.epidemic.packet.EpidemicPacket
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.Network
|
||||
import util.network.simulator.Node
|
||||
@ -75,6 +76,10 @@ abstract class EpidemicNode<T, P: EpidemicPacket<T>>(
|
||||
SUSCETIBLE, INFECTED, REMOVED
|
||||
}
|
||||
|
||||
enum class InfectStrategy {
|
||||
PUSH, PULL, PUSHPULL
|
||||
}
|
||||
|
||||
protected inner class EpidemicReceiver(
|
||||
threadGroup: ThreadGroup
|
||||
) : Node.Receiver(threadGroup) {
|
||||
|
@ -1,18 +0,0 @@
|
||||
package util.network.epidemic
|
||||
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.packet.PayloadPacket
|
||||
|
||||
open class EpidemicPacket<T>(
|
||||
override val senderAddress: Address,
|
||||
override val receiverAddress: Address,
|
||||
override val payload: T,
|
||||
val type: Type
|
||||
) : PayloadPacket<T> {
|
||||
|
||||
enum class Type {
|
||||
PUSH, PULL, PUSHPULL, REPLY
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +0,0 @@
|
||||
package util.network.epidemic
|
||||
|
||||
data class ReplayPayload<T>(
|
||||
val value: T,
|
||||
val isReplay: Boolean = true
|
||||
)
|
@ -1,9 +0,0 @@
|
||||
package util.network.epidemic
|
||||
|
||||
data class TimePayload<T>(val value: T, val timestamp: Long) {
|
||||
companion object {
|
||||
fun <T> createContent(value: T): TimePayload<T> {
|
||||
return TimePayload(value, System.nanoTime())
|
||||
}
|
||||
}
|
||||
}
|
9
src/main/kotlin/util/network/epidemic/TimedPayload.kt
Normal file
9
src/main/kotlin/util/network/epidemic/TimedPayload.kt
Normal file
@ -0,0 +1,9 @@
|
||||
package util.network.epidemic
|
||||
|
||||
data class TimedPayload<T>(val value: T, val timestamp: Long) {
|
||||
companion object {
|
||||
fun <T> createContent(value: T): TimedPayload<T> {
|
||||
return TimedPayload(value, System.nanoTime())
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package util.network.epidemic.packet
|
||||
|
||||
import util.network.simulator.packet.PayloadPacket
|
||||
|
||||
interface EpidemicPacket<T>: PayloadPacket<T> {
|
||||
val type: Type
|
||||
|
||||
enum class Type {
|
||||
PUSH, PULL, PUSHPULL, REPLY
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
package util.network.epidemic.packet
|
||||
|
||||
import util.network.simulator.Address
|
||||
import util.network.simulator.packet.FeedbackPacket
|
||||
|
||||
data class FeedbackEpidemicPacket<T>(
|
||||
override val senderAddress: Address,
|
||||
override val receiverAddress: Address,
|
||||
override val payload: T,
|
||||
override val type: EpidemicPacket.Type,
|
||||
override val isFeedback: Boolean
|
||||
) : EpidemicPacket<T>, FeedbackPacket
|
||||
|
@ -0,0 +1,11 @@
|
||||
package util.network.epidemic.packet
|
||||
|
||||
import util.network.epidemic.TimedPayload
|
||||
import util.network.simulator.Address
|
||||
|
||||
data class TimedEpidemicPacket<T>(
|
||||
override val senderAddress: Address,
|
||||
override val receiverAddress: Address,
|
||||
override val payload: TimedPayload<T>,
|
||||
override val type: EpidemicPacket.Type
|
||||
): EpidemicPacket<TimedPayload<T>>
|
@ -1,13 +0,0 @@
|
||||
package util.network.epidemic.replay
|
||||
|
||||
import util.network.epidemic.EpidemicPacket
|
||||
import util.network.simulator.Address
|
||||
|
||||
class ReplayPacket<T>(
|
||||
senderAddress: Address,
|
||||
receiverAddress: Address,
|
||||
payload: T,
|
||||
type: Type,
|
||||
val isReplay: Boolean
|
||||
) : EpidemicPacket<T>(senderAddress, receiverAddress, payload, type)
|
||||
|
@ -0,0 +1,5 @@
|
||||
package util.network.simulator.packet
|
||||
|
||||
interface FeedbackPacket: Packet {
|
||||
val isFeedback: Boolean
|
||||
}
|
Loading…
Reference in New Issue
Block a user