From 5540b7e98284d3e8a0d29bb8759e3b720305ff02 Mon Sep 17 00:00:00 2001 From: Oskar Thoren Date: Fri, 11 Jan 2019 00:08:49 -0500 Subject: [PATCH] Move networksim concerns --- data_sync/networksim.py | 45 ++++++++++++++++++++++++++++++++++++ data_sync/networksim.pyc | Bin 0 -> 1393 bytes data_sync/sync.py | 48 ++------------------------------------- 3 files changed, 47 insertions(+), 46 deletions(-) create mode 100644 data_sync/networksim.py create mode 100644 data_sync/networksim.pyc diff --git a/data_sync/networksim.py b/data_sync/networksim.py new file mode 100644 index 0000000..6ab709d --- /dev/null +++ b/data_sync/networksim.py @@ -0,0 +1,45 @@ +import random + +# NOTE: Inspired by github.com/ethereum/research networksim.py. +class NetworkSimulator(): + def __init__(self): + self.nodes = [] + self.time = 0 + self.queue = {} + self.peers = {} + # Global network reliability + self.reliability = 1 # 0.95? Dunno. + + def tick(self): + if self.time in self.queue: + # XXX: Should sender be here? + for sender, receiver, msg in self.queue[self.time]: + if random.random() < self.reliability: + #print "*** message ok", sender.name, "->", receiver.name + receiver.on_receive(sender, msg) + #else: + #print "*** message dropped", sender.name, "->", receiver.name + #print "" + print "tick", self.time + 1 + #print "-----------" + for n in self.nodes: + n.tick() + self.time += 1 + + # XXX: This should be normal distribution or Poisson + def latency_uniform_random(self): + # XXX: Hardcode for now, easier analyze + latency = 1 + #latency = random.randint(1,3) + return latency + + # NOTE: Direct message, no broadcast etc + def send_message(self, sender_id, receiver_id, message): + #print "*** (network) send_message", sender_id, receiver_id + # XXX: Assuming sender exists + sender = self.peers[sender_id] + receiver = self.peers[receiver_id] + recv_time = self.time + self.latency_uniform_random() + if recv_time not in self.queue: + self.queue[recv_time] = [] + self.queue[recv_time].append((sender, receiver, message)) diff --git a/data_sync/networksim.pyc b/data_sync/networksim.pyc new file mode 100644 index 0000000000000000000000000000000000000000..cd7a1d38b9f6b4e0179410e3d44fde0becaf520f GIT binary patch literal 1393 zcmah|QBM;=5T4y@3q`6`qXbQi37Yi5nD~Sb#26vL2hxY@1F?Z@&fY?mV1ZolxRU@q)HYPWx=Aa1xd+ANJ*$RLR244sF|}# z^`2WO%G5f~SNSZrVmh1Kd5~t-R?gqUVP&UT@->@g{<6mjB*NM0Ne>Fi<~opy0E5f& zx3f#X3X3sWk$GeIGPj0}R6G1>X^!oDy)^d=0;(Q|UD2dPRYDgBVpR3f=HBr(9YwTM zwA8eW$ZHyFCWk{KnxNb*TEN(#1%e{F(xlF|2;X5LpzViC0Q<)N=ZohO*rdJVE-ew$ zpmB@Ssk85N2+s!Uf$O$r#Bb27K^sj1qAMspW|m^c+PAP(Ns*R$%pH2}yev#*M>adR zF=H4rJi%w#Df3uKWGdTR$@|vXipPO#tBqP8`!rx}`@f zaLyavb0{_MiZ+>pFa#P(QalvDx~eCI@ZcCzB)K(4Cd-(-Oy^Umv6RaWv2??f u++HivzYjHrlaS#!u%+6o?T77^5GEtdU5L|vQ_1M=glax8$c_`NM}Gn0Kj?h` literal 0 HcmV?d00001 diff --git a/data_sync/sync.py b/data_sync/sync.py index df0d1eb..2032c10 100644 --- a/data_sync/sync.py +++ b/data_sync/sync.py @@ -1,6 +1,7 @@ # Sync protocol PoC import hashlib +import networksim import random import sync_pb2 import time @@ -17,51 +18,6 @@ GROUP_ID = "0xdeadbeef" def log(message): print message -# NOTE: Inspired by github.com/ethereum/research networksim.py. -# XXX: Break this out into separate namespace -class NetworkSimulator(): - def __init__(self): - self.nodes = [] - self.time = 0 - self.queue = {} - self.peers = {} - # Global network reliability - self.reliability = 1 # 0.95? Dunno. - - def tick(self): - if self.time in self.queue: - # XXX: Should sender be here? - for sender, receiver, msg in self.queue[self.time]: - if random.random() < self.reliability: - #print "*** message ok", sender.name, "->", receiver.name - receiver.on_receive(sender, msg) - #else: - #print "*** message dropped", sender.name, "->", receiver.name - #print "" - print "tick", self.time + 1 - #print "-----------" - for n in self.nodes: - n.tick() - self.time += 1 - - # XXX: This should be normal distribution or Poisson - def latency_uniform_random(self): - # XXX: Hardcode for now, easier analyze - latency = 1 - #latency = random.randint(1,3) - return latency - - # NOTE: Direct message, no broadcast etc - def send_message(self, sender_id, receiver_id, message): - #print "*** (network) send_message", sender_id, receiver_id - # XXX: Assuming sender exists - sender = self.peers[sender_id] - receiver = self.peers[receiver_id] - recv_time = self.time + self.latency_uniform_random() - if recv_time not in self.queue: - self.queue[recv_time] = [] - self.queue[recv_time].append((sender, receiver, message)) - class Node(): def __init__(self, name, network, profile): self.name = name @@ -280,7 +236,7 @@ def new_ack_record(id): ################################################################################ def run(steps=10): - n = NetworkSimulator() + n = networksim.NetworkSimulator() a = Node("A", n, 'burstyMobile') b = Node("B", n, 'burstyMobile')