cn-uofbasel / PiCN

PiCN: Python ICN and NFN by University of Basel
BSD 3-Clause "New" or "Revised" License
18 stars 14 forks source link

Layer stack #17

Closed s3lph closed 6 years ago

s3lph commented 6 years ago

Implemented a class that manages the layers and communication queues in a PiCN application. Instead of having to manage each needed multiprocessing.Queue individually and assigning it as the {to,from}_{higher,lower} queues of each used layer individually, this now allows the following:

# Creates a layerstack (with repolayer as the topmost layer) and automatically creates
# all queues required for communication between the layers.
# repolayer's queue_{to,from}_higher is made accessible from the outside as
# layerstack.queue_{to,from}_higher
# linklayer's queue_{to,from}_lower will be exposed equivalently. 
layerstack: LayerStack = LayerStack([
    repolayer,
    chunklayer,
    packetencodinglayer,
    linklayer
])
# As long as the LayerStack was not started, new layers can be inserted, e.g. to
# enable conditional features. Queues will be automatically rearranged:
if feature_x_enabled:
    layerstack.insert(featurexlayer, on_top_of=chunklayer)
# or (equivalent):
    layerstack.insert(featurexlayer, below_of=repolayer)

# Start the processes of all layers managed by the LayerStack
layerstack.start_all()
# Stop the processes of all layers managed by the LayerStack
layerstack.stop_all()
# Close all queues managed by the LayerStack
layerstack.close_all()