Commit 8bfb2ce1 authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Convert random traffic to generator classes

parent 5b3b63b1
...@@ -12,6 +12,7 @@ A simulation is made up from a Network which contains: ...@@ -12,6 +12,7 @@ A simulation is made up from a Network which contains:
__all__ = [ __all__ = [
'Network', 'Network',
'Buffer', 'Buffer',
'Node', 'Node',
'EpidemicNode', 'EpidemicNode',
...@@ -22,7 +23,11 @@ __all__ = [ ...@@ -22,7 +23,11 @@ __all__ = [
'random_trace', 'random_trace',
'Traffic', 'Traffic',
'random_traffic', 'RandomManyToManyTraffic',
] ]
__version__ = '0.2' __version__ = '0.2'
__author__ = 'Jarrod Pas <>' __author__ = 'Jarrod Pas <>'
...@@ -174,36 +179,45 @@ class Network: ...@@ -174,36 +179,45 @@ class Network:
return stats return stats
PacketInfo = namedtuple('PacketInfo', [
'source', 'destination', 'created', 'time_to_live', 'payload'
class Packet: class Packet:
"""An item to route through the network.""" """An item to route through the network."""
def __init__(self, network, traffic): def __init__(self, network, info):
"""Create a packet within a network, based on traffic.""" """Create a packet within a network, based on traffic.""" = network = network
self._traffic = traffic = info
self.recieved = None self.recieved = None
self._stats = defaultdict(int) self._stats = defaultdict(int)
@property @property
def source(self): def source(self):
"""Return source node of the packet.""" """Source of packet."""
return[self._traffic.source] return[]
@property @property
def destination(self): def destination(self):
"""Return destination node of the packet.""" """Destination of packet."""
return[self._traffic.destination] return[]
@property @property
def created(self): def created(self):
"""Return created time of packet.""" """Time of packet creation."""
return self._traffic.created return
@property @property
def time_to_live(self): def time_to_live(self):
"""Return time to live of packet.""" """Time to live of packet."""
return self._traffic.time_to_live return
def payload(self):
"""Payload of packet."""
@property @property
def deadline(self): def deadline(self):
...@@ -240,7 +254,9 @@ class Packet: ...@@ -240,7 +254,9 @@ class Packet:
def __len__(self): def __len__(self):
"""Return size of packet.""" """Return size of packet."""
return self._traffic.payload if isinstance(self.payload, int):
return self.payload
return len(self.payload)
class Buffer: class Buffer:
...@@ -551,21 +567,101 @@ def csv_trace(path): ...@@ -551,21 +567,101 @@ def csv_trace(path):
yield Contact(now, source, destination, join) yield Contact(now, source, destination, join)
Traffic = namedtuple('Traffic', [ class Traffic:
'source', 'destination', 'created', 'time_to_live', 'payload' """Base traffic generator."""
# pylint: disable=too-few-public-methods
def random_traffic(nodes, start=0, speed=1, seed=None, **options): def __init__(self, **options):
"""Generate traffic from random source to random destination every step.""" """Create traffic generator."""
random = Random(seed) self.time_to_live = options.get('time_to_live', float('inf'))
self.payload = options.get('payload', 0)
def __iter__(self):
"""Yield no traffic."""
def create_packet(self, now, source, destination):
"""Create a packet info for the simulation."""
return PacketInfo(source=source,
class RandomManyToManyTraffic(Traffic):
"""Generate random traffic between nodes."""
def __init__(self, sources, destinations, **options):
Create a random traffic generator.
Option Arguments:
seed -- seed to use for the random number generator (default None)
start -- which time step the first packet is create (default 0)
step -- time between traffic generation (default 1)
traffic_per_step -- traffic to generate during each step (default 1)
time_to_live -- packet time to live (default infinite)
payload -- packet payload (default 0)
self.sources = list(sources)
self.destinations = list(destinations)
if len(self.sources) == 1 and self.sources == self.destinations:
raise ValueError('only source is the same as only destination')
self.seed = options.get('seed', None)
self.start = options.get('start', 0)
self.step = options.get('step', 1)
self.traffic_per_step = options.get('traffic_per_step', 1)
def random_pair(self, random):
"""Return random pair of nodes."""
source = destination = random.choice(self.sources)
while destination == source:
destination = random.choice(self.destinations)
return source, destination
def __iter__(self):
"""Yield infinite random traffic."""
random = Random(self.seed)
for now in count(start=self.start, step=self.step):
for _ in range(self.traffic_per_step):
source, destination = self.random_pair(random)
yield self.create_packet(now, source, destination)
class RandomManyToOneTraffic(RandomManyToManyTraffic):
"""Generate random traffic from any source to a single node."""
def __init__(self, sources, destination, **options):
"""Create many to one traffic generator."""
super().__init__(sources, [destination], **options)
class RandomOneToManyTraffic(RandomManyToManyTraffic):
"""Generate random traffic from a single node to any destination."""
def __init__(self, source, destinations, **options):
"""Create one to many traffic generator."""
super().__init__([source], destinations, **options)
class RandomOneToOneTraffic(RandomManyToManyTraffic):
"""Generate random traffic from a single node to a single node."""
def __init__(self, source, destination, **options):
"""Create one to one traffic generator."""
super().__init__([source], [destination], **options)
if isinstance(nodes, dict):
nodes = list(nodes)
time_to_live = options.get('time_to_live', float('inf')) class RandomTraffic(RandomManyToManyTraffic):
payload = options.get('payload', 0) """Generate random traffic between any node a group."""
for created in count(start=start, step=speed): def __init__(self, nodes, **options):
source, destination = random.sample(nodes, 2) """Create random traffic generator."""
yield Traffic(source, destination, created, time_to_live, payload) super().__init__(nodes, nodes, **options)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment