Skip to content
Snippets Groups Projects
Commit e474611e authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Modularize routers

parent e703ffd5
No related branches found
No related tags found
1 merge request!3Version 0.2
from .bubble import bubble
from .direct import direct
from .epidemic import epidemic
from .flooding import flooding
from .hcbf import hcbf
types = {
'bubble': bubble,
'direct': direct,
'epidemic': epidemic,
'flooding': flooding,
'hcbf': hcbf,
}
def bubble(self, packet, state):
''''''
if len(self.community) == 0:
return False
dest = packet.destination
community_of = lambda n: self.network.community[n]
same_community = lambda a, b: community_of(a) == community_of(b)
gp_of = lambda n: self.network.community.get_gp(n)
lp_of = lambda n: self.network.community.get_lp(n)
max = lambda a, b: a if a[0] >= b[0] else b
max_gp = (-1, None)
if same_community(self, dest):
max_lp = (lp_of(self), self)
for met in self.links:
if same_community(met, dest):
max_lp = max((lp_of(met), met), max_lp)
if max_lp[1] is not self:
self.send(max_lp[1], packet)
return True
else:
max_gp = (-1, None)
max_lp = (-1, None)
for met in self.links:
if same_community(met, dest):
max_lp = max((lp_of(met), met), max_lp)
max_gp = max((gp_of(met), met), max_gp)
if max_lp[1] is not None:
self.send(max_lp[1], packet)
return True
if max_gp[1] is not None:
self.send(max_gp[1], packet)
return True
return False
def direct(self, packet, state):
'''
Routes a packet via direct contact to the destination.
Returns True if the packet was sent successfully, otherwise False.
'''
for met in self.links:
if packet.destination == met:
self.send(met, packet)
return False
from collections import defaultdict
def epidemic(self, packet, state):
'''
Routes a packet if it hasn't sent the packet on a link yet.
Always returns False.
'''
if 'sent' not in state:
state['sent'] = defaultdict(dict)
sent = state['sent']
for met in self.links:
if packet not in sent[met]:
sent[met][packet] = True
self.send(met, packet)
return False
def flooding(self, packet, state):
'''
Routes a packet via flooding, i.e. sends all packets on all links.
Always returns False.
'''
for met in self.links:
self.send(met, packet)
return False
from collections import defaultdict
def direct(self, packet, state):
'''
Routes a packet via direct contact to the destination.
Returns True if the packet was sent successfully, otherwise False.
'''
for met in self.links:
if packet.destination == met:
self.send(met, packet)
return False
def epidemic(self, packet, state):
'''
Routes a packet if it hasn't sent the packet on a link yet.
Always returns False.
'''
if 'sent' not in state:
state['sent'] = defaultdict(dict)
sent = state['sent']
for met in self.links:
if packet not in sent[met]:
sent[met][packet] = True
self.send(met, packet)
return False
def flooding(self, packet, state):
'''
Routes a packet via flooding, i.e. sends all packets on all links.
Always returns False.
'''
for met in self.links:
self.send(met, packet)
return False
def bubble(self, packet, state):
if len(self.community) == 0:
return False
dest = packet.destination
community_of = lambda n: self.network.community[n]
same_community = lambda a, b: community_of(a) == community_of(b)
gp_of = lambda n: self.network.community.get_gp(n)
lp_of = lambda n: self.network.community.get_lp(n)
max = lambda a, b: a if a[0] >= b[0] else b
max_gp = (-1, None)
if same_community(self, dest):
max_lp = (lp_of(self), self)
for met in self.links:
if same_community(met, dest):
max_lp = max((lp_of(met), met), max_lp)
if max_lp[1] is not self:
self.send(max_lp[1], packet)
return True
else:
max_gp = (-1, None)
max_lp = (-1, None)
for met in self.links:
if same_community(met, dest):
max_lp = max((lp_of(met), met), max_lp)
max_gp = max((gp_of(met), met), max_gp)
if max_lp[1] is not None:
self.send(max_lp[1], packet)
return True
if max_gp[1] is not None:
self.send(max_gp[1], packet)
return True
return False
def hcbf(self, packet, state): def hcbf(self, packet, state):
if len(self.community) == 0: if len(self.community) == 0:
return False return False
...@@ -152,16 +70,3 @@ def hcbf(self, packet, state): ...@@ -152,16 +70,3 @@ def hcbf(self, packet, state):
return False return False
types = {
'bubble': bubble,
'direct': direct,
'epidemic': epidemic,
'flooding': flooding,
'hcbf': hcbf,
}
def get(router):
if router is None:
router = 'direct'
return types[router]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment