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

Cleans up node transmission code

parent 361c79c4
No related branches found
No related tags found
1 merge request!3Version 0.2
...@@ -16,8 +16,9 @@ class Buffer: ...@@ -16,8 +16,9 @@ class Buffer:
if self.used < self.capacity: if self.used < self.capacity:
self.used += 1 self.used += 1
self.buffer[packet] = None self.buffer[packet] = None
return True
else: else:
raise Exception('buffer full') return False
def remove(self, packet): def remove(self, packet):
self.used -= 1 self.used -= 1
......
...@@ -33,35 +33,51 @@ class Node(TickProcess): ...@@ -33,35 +33,51 @@ class Node(TickProcess):
self.transfer_queue = Store(self.env) self.transfer_queue = Store(self.env)
def route_packets(self): def route_packet(self, packet):
packets_to_delete = [] '''
Applies the routing heuristic to packet and queues packet for
transfer if requested
for packet in self.buffer: Returns:
# remove expired packets from buffer True - if the packet should be deleted from the buffer
if packet.expired(self.env.now): False - otherwise
packets_to_delete.append(packet) '''
continue
# ask the router what to do # check if packet has expired
targets, reason, delete = self.router(packet) if packet.expired(self.env.now):
return True
# check if there are targets to send to # ask the router what to do
if targets is None: targets, reason, delete = self.router(packet)
continue
# allow for targets to be iterable or single item # check if there are targets to send to
if not isinstance(targets, Iterable): if targets is None:
targets = [targets] return False
# allow for targets to be iterable or single item
if not isinstance(targets, Iterable):
targets = [targets]
# add the packet to the transfer queue
self.transfer_queue.put((packet, targets, reason))
return delete
def route_packets(self):
'''
Applies the routing heuristic to all packets in the buffer and
queues transfers if requested. Additionally, removes packets
from the buffer if the packet has exipred or if the router
requests the packet to be removed.
'''
# add the packet to the transfer queue packets_to_delete = []
self.transfer_queue.put((packet, targets, reason))
#self.send(targets, packet, reason)
# if the router requested deletion from the buffer do it for packet in self.buffer:
delete = self.route_packet(packet)
if delete: if delete:
packets_to_delete.append(packet) packets_to_delete.append(packet)
for packet in packets_to_delete: for packet in packets_to_delete:
self.buffer.remove(packet) self.buffer.remove(packet)
...@@ -70,6 +86,8 @@ class Node(TickProcess): ...@@ -70,6 +86,8 @@ class Node(TickProcess):
while True: while True:
packet, targets, reason = yield self.transfer_queue.get() packet, targets, reason = yield self.transfer_queue.get()
failed = False
# simulate transfer delay of packet # simulate transfer delay of packet
delay = packet.size / self.bandwidth delay = packet.size / self.bandwidth
yield self.env.timeout(delay) yield self.env.timeout(delay)
...@@ -78,19 +96,44 @@ class Node(TickProcess): ...@@ -78,19 +96,44 @@ class Node(TickProcess):
# simulates broadcast # simulates broadcast
for target in targets: for target in targets:
# try sending packet # try sending packet
if self.network.send_link(self, target, packet): if self.send(target, packet):
success.append(target) success.append(target)
self.router.on_send_success(target, packet) self.router.on_send_success(target, packet)
else: else:
failed = True
self.router.on_send_failure(target, packet) self.router.on_send_failure(target, packet)
packet.send(self, success, reason=reason) packet.send(self, success, reason=reason)
if failed:
delete = self.route_packet(packet)
if delete:
self.buffer.remove(packet)
def send(self, target, packet):
'''
Instantly sends a packet to a target if they are connected.
Returns:
True - if successful
False - otherwise
'''
if self.connected_to(target):
return target.recv(packet)
else:
return False
def recv(self, packet): def recv(self, packet):
'''
Tells the node to recieve a packet.
Returns:
True - if the node was able to recieved the packet.
False - otherwise, triggered by buffer being full.
'''
if packet.destination == self: if packet.destination == self:
packet.recv() packet.recv()
return True
else: else:
self.buffer.add(packet) return self.buffer.add(packet)
@property @property
def community(self): def community(self):
......
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