Commit 9a84b6a5 authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Removes old code

parent 1e2e0e5b
from .bubble import BubbleRouter
from .direct import DirectRouter
from .epidemic import EpidemicRouter
from .flooding import FloodingRouter
from .hcbf import HCBFRouter
types = {
'bubble': BubbleRouter,
'direct': DirectRouter,
'epidemic': EpidemicRouter,
'flooding': FloodingRouter,
'hcbf': HCBFRouter,
}
class Router:
'''
Base Router class
-----------------
All routers are callable objects
'''
def __init__(self, node, **kwargs):
self.env = node.env
self.node = node
def __call__(self, packet):
raise NotImplementedError
def on_send_success(self, target, packet):
pass
def on_send_failure(self, target, packet):
pass
from .community import CommunityRouter
class BubbleRouter(CommunityRouter):
def __call__(self, packet):
if packet.destination in self.node.neighbours:
return packet.destination, 'direct', True
if self.local_community:
best, lp = self.best_lp()
if lp > self.lp():
return best, 'lp', True
elif self.not_local_community:
best, gp = self.best_gp()
if gp > self.gp():
return best, 'gp', True
return None, None, False
def on_send_failure(self, target, packet):
self.node.buffer.add(packet)
from .base import Router
class CommunityRouter(Router):
def __init__(self, node, **kwargs):
super().__init__(node, **kwargs)
self.community = self.node.network.community
@property
def local_community(self):
neighbours = self.node.neighbours
community = self.node.community
return [met for met in neighbours if met in community]
@property
def not_local_community(self):
neighbours = self.node.neighbours
community = self.node.community
return [met for met in neighbours if met not in community]
@staticmethod
def best(nodes, key):
best = max(nodes, key=key)
return best, key(best)
def ui(self):
return self.get_ui(self.node)
def get_ui(self, x):
return self.community.get_ui(x)
def best_ui(self):
return self.best(self.local_community, self.get_ui)
def lp(self):
return self.get_lp(self.node)
def get_lp(self, x):
return self.community.get_lp(x)
def best_lp(self):
return self.best(self.local_community, self.get_lp)
def gp(self):
return self.get_gp(self.node)
def get_gp(self, x):
return self.community.get_gp(x)
def best_gp(self):
return self.best(self.not_local_community, self.get_gp)
def cbc(self, c_y):
return self.get_cbc(self.node, c_y)
def get_cbc(self, x, c_y):
return self.community.get_cbc(x.community, c_y)
def best_cbc(self, c_y):
get_cbc = lambda x: self.get_cbc(x, c_y)
return self.best(self.not_local_community, get_cbc)
def ncf(self, c_y):
return self.get_ncf(self.node, c_y)
def get_ncf(self, x, c_y):
return self.community.get_ncf(x, c_y)
def best_ncf(self, c_y):
get_ncf = lambda x: self.get_ncf(x, c_y)
return self.best(self.local_community, get_ncf)
from .base import Router
class DirectRouter(Router):
def __call__(self, packet):
if packet.destination in self.node.neighbours:
return packet.destination, 'direct', True
return None, None, False
def on_send_failure(self, target, packet):
self.node.buffer.add(packet)
from collections import defaultdict
from .base import Router
class EpidemicRouter(Router):
def __init__(self, node, **kwargs):
super().__init__(node, **kwargs)
# maps a packet to a set of nodes that the packet has been sent to
self.sent = defaultdict(set)
def __call__(self, packet):
# get list of currently encountered nodes that do not have the packet
targets = [
met
for met in self.node.neighbours
if met not in self.sent[packet]
]
# return list of nodes to send packet to
# (targets, remove from buffer, reason)
if targets:
return targets, 'epidemic', False
else:
return None, None, False
def on_send_success(self, target, packet):
self.sent[packet].add(target)
from .base import Router
class FloodingRouter(Router):
def __call__(self, packet):
return self.node.neighbours, 'flood', False
from .community import CommunityRouter
class HCBFRouter(CommunityRouter):
def __call__(self, packet):
me = self.node
dest = packet.destination
if dest in me.neighbours:
return dest, 'direct', True
if me.community is not dest.community and self.not_local_community:
best, cbc = self.best_cbc(dest.community)
if cbc > self.cbc(dest.community):
return best, 'cbc', True
if self.local_community:
if me.community is not dest.community:
best, ncf = self.best_ncf(dest.community)
if ncf > self.ncf(dest.community):
return best, 'ncf', True
elif ncf < self.ncf(dest.community):
return None, None, False
best, ui = self.best_ui()
if ui > self.ui():
return best, 'ui', True
elif ui < self.ui():
return None, None, False
best, lp = self.best_lp()
if lp > self.lp():
return best, 'lp', True
return None, None, False
def on_send_failure(self, target, packet):
self.node.buffer.add(packet)
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