Compare commits
No commits in common. "master" and "network" have entirely different histories.
@ -21,35 +21,26 @@ class AverageNode(
|
|||||||
if (packet.isFeedback)
|
if (packet.isFeedback)
|
||||||
changeValue(payload)
|
changeValue(payload)
|
||||||
else {
|
else {
|
||||||
computeAverage(payload)
|
val newValue = (nodeValue + payload) / 2
|
||||||
sendFeedback(packet.senderAddress)
|
|
||||||
spreadNewValue()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun computeAverage(payload: Double) {
|
changeValue(newValue)
|
||||||
val newValue = (nodeValue + payload) / 2
|
|
||||||
changeValue(newValue)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun sendFeedback(receiverAddress: Address) {
|
send(
|
||||||
send(
|
FeedbackEpidemicPacket(
|
||||||
FeedbackEpidemicPacket(
|
address,
|
||||||
address,
|
packet.senderAddress,
|
||||||
receiverAddress,
|
newValue,
|
||||||
nodeValue,
|
if (infectStrategy == InfectStrategy.PUSH)
|
||||||
if (infectStrategy == InfectStrategy.PUSH)
|
EpidemicPacket.Type.PUSH else EpidemicPacket.Type.REPLY,
|
||||||
EpidemicPacket.Type.PUSH else EpidemicPacket.Type.REPLY,
|
true
|
||||||
true
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun spreadNewValue() {
|
when (infectStrategy) {
|
||||||
when (infectStrategy) {
|
InfectStrategy.PUSH -> sendPush()
|
||||||
InfectStrategy.PUSH -> sendPushToRandom()
|
InfectStrategy.PULL -> sendPull()
|
||||||
InfectStrategy.PULL -> sendPullToRandom()
|
InfectStrategy.PUSHPULL -> sendPushPull()
|
||||||
InfectStrategy.PUSHPULL -> sendPushPullToRandom()
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,4 +95,4 @@ class AverageNode(
|
|||||||
bufferSize: Int = 1
|
bufferSize: Int = 1
|
||||||
) = AverageNode(address, network, initialValue, InfectStrategy.PUSHPULL, bufferSize)
|
) = AverageNode(address, network, initialValue, InfectStrategy.PUSHPULL, bufferSize)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -32,4 +32,4 @@ fun main() {
|
|||||||
network.forEach {
|
network.forEach {
|
||||||
println(1.0 / (it as AverageNode).nodeValue)
|
println(1.0 / (it as AverageNode).nodeValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -32,4 +32,4 @@ fun main() {
|
|||||||
network.forEach {
|
network.forEach {
|
||||||
println(1.0 / (it as AverageNode).nodeValue)
|
println(1.0 / (it as AverageNode).nodeValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -32,4 +32,4 @@ fun main() {
|
|||||||
network.forEach {
|
network.forEach {
|
||||||
println(1.0 / (it as AverageNode).nodeValue)
|
println(1.0 / (it as AverageNode).nodeValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -37,9 +37,9 @@ class DbNode<T>(
|
|||||||
|
|
||||||
override fun infect() {
|
override fun infect() {
|
||||||
when (infectStrategy) {
|
when (infectStrategy) {
|
||||||
InfectStrategy.PUSH -> sendPushToRandom()
|
InfectStrategy.PUSH -> sendPush()
|
||||||
InfectStrategy.PULL -> sendPullToRandom()
|
InfectStrategy.PULL -> sendPull()
|
||||||
InfectStrategy.PUSHPULL -> sendPushPullToRandom()
|
InfectStrategy.PUSHPULL -> sendPushPull()
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread.sleep(DELTA)
|
Thread.sleep(DELTA)
|
||||||
@ -81,4 +81,4 @@ class DbNode<T>(
|
|||||||
bufferSize: Int = 1
|
bufferSize: Int = 1
|
||||||
) = DbNode(address, network, initialValue, InfectStrategy.PUSHPULL, bufferSize)
|
) = DbNode(address, network, initialValue, InfectStrategy.PUSHPULL, bufferSize)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,6 +4,7 @@ import util.network.epidemic.TimedPayload
|
|||||||
import util.network.simulator.Address
|
import util.network.simulator.Address
|
||||||
import util.network.simulator.Network
|
import util.network.simulator.Network
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val network = Network()
|
val network = Network()
|
||||||
|
|
||||||
@ -28,3 +29,4 @@ fun main() {
|
|||||||
println((it as DbNode<Int>).nodeValue)
|
println((it as DbNode<Int>).nodeValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import util.network.epidemic.TimedPayload
|
|||||||
import util.network.simulator.Address
|
import util.network.simulator.Address
|
||||||
import util.network.simulator.Network
|
import util.network.simulator.Network
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val network = Network()
|
val network = Network()
|
||||||
|
|
||||||
@ -28,3 +29,4 @@ fun main() {
|
|||||||
println((it as DbNode<Int>).nodeValue)
|
println((it as DbNode<Int>).nodeValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ import util.network.epidemic.TimedPayload
|
|||||||
import util.network.simulator.Address
|
import util.network.simulator.Address
|
||||||
import util.network.simulator.Network
|
import util.network.simulator.Network
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val network = Network()
|
val network = Network()
|
||||||
|
|
||||||
@ -28,3 +29,4 @@ fun main() {
|
|||||||
println((it as DbNode<Int>).nodeValue)
|
println((it as DbNode<Int>).nodeValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ class QueryNode<T>(
|
|||||||
address: Address,
|
address: Address,
|
||||||
network: Network,
|
network: Network,
|
||||||
initialValue: T,
|
initialValue: T,
|
||||||
private val demotivatingFactor: Int = 10,
|
|
||||||
bufferSize: Int = 1
|
bufferSize: Int = 1
|
||||||
) : EpidemicNode<T, FeedbackEpidemicPacket<T>>(
|
) : EpidemicNode<T, FeedbackEpidemicPacket<T>>(
|
||||||
address, network, initialValue, bufferSize
|
address, network, initialValue, bufferSize
|
||||||
@ -33,25 +32,26 @@ class QueryNode<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun receivePushOrReply(packet: FeedbackEpidemicPacket<T>) {
|
override fun receivePushOrReply(packet: FeedbackEpidemicPacket<T>) {
|
||||||
when (nodeState) {
|
//println(packet)
|
||||||
State.REMOVED -> return
|
if (nodeState == State.REMOVED)
|
||||||
State.INFECTED -> whenInfected(packet)
|
return
|
||||||
State.SUSCEPTIBLE -> whenSusceptible(packet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun whenSusceptible(packet: FeedbackEpidemicPacket<T>) {
|
if (nodeState == State.SUSCEPTIBLE) {
|
||||||
nodeState = State.INFECTED
|
// println("susceptible")
|
||||||
queryValue = packet.payload
|
nodeState = State.INFECTED
|
||||||
|
|
||||||
sendPushToRandom(2)
|
queryValue = packet.payload
|
||||||
|
sendPush(1)
|
||||||
|
|
||||||
if (packet.payload == nodeValue)
|
if (packet.payload == nodeValue)
|
||||||
println("find ${packet.payload} on node $address")
|
println("find on node $address")
|
||||||
}
|
} else {
|
||||||
|
if (packet.isFeedback && random.nextBoolean()) {
|
||||||
|
nodeState = State.REMOVED
|
||||||
|
println("removed")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
private fun whenInfected(packet: FeedbackEpidemicPacket<T>) {
|
|
||||||
if (!packet.isFeedback)
|
|
||||||
send(
|
send(
|
||||||
FeedbackEpidemicPacket(
|
FeedbackEpidemicPacket(
|
||||||
address,
|
address,
|
||||||
@ -61,9 +61,7 @@ class QueryNode<T>(
|
|||||||
true
|
true
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
}
|
||||||
if (packet.isFeedback && random.nextDouble(1.0) < 1 / demotivatingFactor)
|
|
||||||
nodeState = State.REMOVED
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun receivePull(packet: FeedbackEpidemicPacket<T>) {
|
override fun receivePull(packet: FeedbackEpidemicPacket<T>) {
|
||||||
@ -73,4 +71,4 @@ class QueryNode<T>(
|
|||||||
override fun infect() {
|
override fun infect() {
|
||||||
Thread.interrupted()
|
Thread.interrupted()
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,11 +5,12 @@ import util.network.epidemic.packet.FeedbackEpidemicPacket
|
|||||||
import util.network.simulator.Address
|
import util.network.simulator.Address
|
||||||
import util.network.simulator.Network
|
import util.network.simulator.Network
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val network = Network()
|
val network = Network()
|
||||||
|
|
||||||
repeat(20) {
|
repeat(10) {
|
||||||
QueryNode(Address(it), network, it * 2)
|
QueryNode(Address(it), network, it*2)
|
||||||
}
|
}
|
||||||
|
|
||||||
val alpha = network.getRandomNode() as QueryNode<Int>
|
val alpha = network.getRandomNode() as QueryNode<Int>
|
||||||
@ -18,7 +19,7 @@ fun main() {
|
|||||||
val packet = FeedbackEpidemicPacket(
|
val packet = FeedbackEpidemicPacket(
|
||||||
alpha.address,
|
alpha.address,
|
||||||
beta.address,
|
beta.address,
|
||||||
24,
|
14,
|
||||||
EpidemicPacket.Type.PUSH,
|
EpidemicPacket.Type.PUSH,
|
||||||
false
|
false
|
||||||
)
|
)
|
||||||
@ -27,3 +28,4 @@ fun main() {
|
|||||||
|
|
||||||
network.start()
|
network.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import util.network.simulator.Address
|
|||||||
import util.network.simulator.Network
|
import util.network.simulator.Network
|
||||||
import util.network.simulator.Node
|
import util.network.simulator.Node
|
||||||
|
|
||||||
abstract class EpidemicNode<T, P : EpidemicPacket<T>>(
|
abstract class EpidemicNode<T, P: EpidemicPacket<T>>(
|
||||||
address: Address,
|
address: Address,
|
||||||
network: Network,
|
network: Network,
|
||||||
initialValue: T,
|
initialValue: T,
|
||||||
@ -41,21 +41,21 @@ abstract class EpidemicNode<T, P : EpidemicPacket<T>>(
|
|||||||
type: EpidemicPacket.Type
|
type: EpidemicPacket.Type
|
||||||
): EpidemicPacket<T>
|
): EpidemicPacket<T>
|
||||||
|
|
||||||
protected fun sendPushToRandom(numberOfReceiver: Int = 1) {
|
protected fun sendPush(numberOfReceiver: Int = 1) {
|
||||||
sendToRandom(
|
sendToRandom(
|
||||||
EpidemicPacket.Type.PUSH,
|
EpidemicPacket.Type.PUSH,
|
||||||
numberOfReceiver
|
numberOfReceiver
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun sendPullToRandom(numberOfReceiver: Int = 1) {
|
protected fun sendPull(numberOfReceiver: Int = 1) {
|
||||||
sendToRandom(
|
sendToRandom(
|
||||||
EpidemicPacket.Type.PULL,
|
EpidemicPacket.Type.PULL,
|
||||||
numberOfReceiver
|
numberOfReceiver
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
protected fun sendPushPullToRandom(numberOfReceiver: Int = 1) {
|
protected fun sendPushPull(numberOfReceiver: Int = 1) {
|
||||||
sendToRandom(
|
sendToRandom(
|
||||||
EpidemicPacket.Type.PUSHPULL,
|
EpidemicPacket.Type.PUSHPULL,
|
||||||
numberOfReceiver
|
numberOfReceiver
|
||||||
@ -69,6 +69,8 @@ abstract class EpidemicNode<T, P : EpidemicPacket<T>>(
|
|||||||
protected abstract fun infect()
|
protected abstract fun infect()
|
||||||
|
|
||||||
protected fun onReceive(packet: P) {
|
protected fun onReceive(packet: P) {
|
||||||
|
//println(packet.type)
|
||||||
|
|
||||||
when (packet.type) {
|
when (packet.type) {
|
||||||
EpidemicPacket.Type.PUSH -> receivePushOrReply(packet)
|
EpidemicPacket.Type.PUSH -> receivePushOrReply(packet)
|
||||||
EpidemicPacket.Type.PULL -> receivePull(packet)
|
EpidemicPacket.Type.PULL -> receivePull(packet)
|
||||||
@ -123,4 +125,5 @@ abstract class EpidemicNode<T, P : EpidemicPacket<T>>(
|
|||||||
return EpidemicSender(threadGroup)
|
return EpidemicSender(threadGroup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
}
|
@ -6,4 +6,4 @@ data class TimedPayload<T>(val value: T, val timestamp: Long) {
|
|||||||
return TimedPayload(value, System.nanoTime())
|
return TimedPayload(value, System.nanoTime())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,10 +2,12 @@ package util.network.epidemic.packet
|
|||||||
|
|
||||||
import util.network.simulator.packet.PayloadPacket
|
import util.network.simulator.packet.PayloadPacket
|
||||||
|
|
||||||
interface EpidemicPacket<T> : PayloadPacket<T> {
|
interface EpidemicPacket<T>: PayloadPacket<T> {
|
||||||
val type: Type
|
val type: Type
|
||||||
|
|
||||||
enum class Type {
|
enum class Type {
|
||||||
PUSH, PULL, PUSHPULL, REPLY
|
PUSH, PULL, PUSHPULL, REPLY
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,3 +10,4 @@ data class FeedbackEpidemicPacket<T>(
|
|||||||
override val type: EpidemicPacket.Type,
|
override val type: EpidemicPacket.Type,
|
||||||
override val isFeedback: Boolean
|
override val isFeedback: Boolean
|
||||||
) : EpidemicPacket<T>, FeedbackPacket
|
) : EpidemicPacket<T>, FeedbackPacket
|
||||||
|
|
||||||
|
@ -8,4 +8,4 @@ data class TimedEpidemicPacket<T>(
|
|||||||
override val receiverAddress: Address,
|
override val receiverAddress: Address,
|
||||||
override val payload: TimedPayload<T>,
|
override val payload: TimedPayload<T>,
|
||||||
override val type: EpidemicPacket.Type
|
override val type: EpidemicPacket.Type
|
||||||
) : EpidemicPacket<TimedPayload<T>>
|
): EpidemicPacket<TimedPayload<T>>
|
@ -17,7 +17,7 @@ class Network(threadGrupName: String = "network") {
|
|||||||
addresses.add(node.address)
|
addresses.add(node.address)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getRandomAddress(): Address {
|
fun getRandomAddress() : Address {
|
||||||
val index = random.nextInt(addresses.size)
|
val index = random.nextInt(addresses.size)
|
||||||
|
|
||||||
return addresses[index]
|
return addresses[index]
|
||||||
|
@ -12,12 +12,12 @@ fun main() {
|
|||||||
network.start()
|
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 {
|
override fun communicatorFactory(threadGroup: ThreadGroup): CommunicatorFactory {
|
||||||
return TestCommunicatorFactory(threadGroup)
|
return TestCommunicatorFactory(threadGroup)
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class TestReceiver(threadGroup: ThreadGroup) : Receiver(threadGroup) {
|
private inner class TestReceiver(threadGroup: ThreadGroup): Receiver(threadGroup) {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
if (address != Address(0))
|
if (address != Address(0))
|
||||||
return
|
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() {
|
override fun run() {
|
||||||
if (address != Address(1))
|
if (address != Address(1))
|
||||||
return
|
return
|
||||||
@ -39,8 +39,7 @@ class TestNode(address: Address, network: Network) : Node(address, network) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class TestCommunicatorFactory(threadGroup: ThreadGroup) :
|
private inner class TestCommunicatorFactory(threadGroup: ThreadGroup): CommunicatorFactory(threadGroup) {
|
||||||
CommunicatorFactory(threadGroup) {
|
|
||||||
override fun createReceiver(): Receiver {
|
override fun createReceiver(): Receiver {
|
||||||
return TestReceiver(threadGroup)
|
return TestReceiver(threadGroup)
|
||||||
}
|
}
|
||||||
@ -49,4 +48,4 @@ class TestNode(address: Address, network: Network) : Node(address, network) {
|
|||||||
return TestSender(threadGroup)
|
return TestSender(threadGroup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package util.network.simulator.packet
|
package util.network.simulator.packet
|
||||||
|
|
||||||
interface FeedbackPacket : Packet {
|
interface FeedbackPacket: Packet {
|
||||||
val isFeedback: Boolean
|
val isFeedback: Boolean
|
||||||
}
|
}
|
@ -6,3 +6,4 @@ data class NetworkPacket(
|
|||||||
override val senderAddress: Address,
|
override val senderAddress: Address,
|
||||||
override val receiverAddress: Address
|
override val receiverAddress: Address
|
||||||
) : Packet
|
) : Packet
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package util.network.simulator.packet
|
package util.network.simulator.packet
|
||||||
|
|
||||||
interface PayloadPacket<T> : Packet {
|
interface PayloadPacket<T>: Packet {
|
||||||
val payload: T
|
val payload: T
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user