Skip to content
Snippets Groups Projects
Commit 352431ce authored by jkerdreu's avatar jkerdreu
Browse files

Added comments

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/fork@1773 b32b6428-25c9-4566-ad07-03861ab6144f
parent 2bd2a57b
Branches
No related tags found
No related merge requests found
......@@ -29,7 +29,6 @@ from . import tools
import sys
import time
import inspect
import binascii
import collections
......@@ -39,20 +38,20 @@ logger = logging.getLogger(__name__)
class Engine(object):
def __init__(self,address=config.address,port=config.port,hops=config.hops,key=config.key):
self.devices = [] # list of devices / use (un)register_devices()
self.started = False # engine started or not
self.running = False # engine is running or not
self.callbacks = [] # functions to call every loop
self.timers = [] # functions to call periodic
self.__last_timer = 0 # last timer check
self.__attributesChange = [] # list of XAALAttributes instances
self.__txFifo = collections.deque() # tx msg fifo
self.__alives = [] # list of alive devices
self.handle_rx_msg = self.handle_request # default rx flow
# start network
# start network
self.network = NetworkConnector(address, port, hops)
# start msg worker
self.msg_factory = MessageFactory(key)
......@@ -61,31 +60,33 @@ class Engine(object):
# Devices management
#####################################################
def add_device(self, dev):
"""register a new device """
if dev not in self.devices:
self.devices.append(dev)
dev.engine = self
def add_devices(self, devs):
"""register new devices"""
for dev in devs:
self.add_device(dev)
def remove_device(self, dev):
"""unregister a device """
dev.engine = None
# Remove dev from devices list
self.devices.remove(dev)
#####################################################
# xAAL messages Tx handling
#####################################################
# Fifo for msg to send
def queue_tx(self, msg):
"""queue a message"""
self.__txFifo.append(msg)
def process_tx_msg(self):
""" Process (send) message in tx queue
called from the loop()
"""
""" Process (send) message in tx queue called from the loop()"""
while self.__txFifo:
temp = self.__txFifo.popleft()
self.send_msg(temp)
......@@ -94,21 +95,26 @@ class Engine(object):
self.network.send(msg)
def send_request(self,dev,targets,action,body = None):
"""queue a new request"""
msg = self.msg_factory.build_msg(dev, targets, 'request', action, body)
self.queue_tx(msg)
def send_reply(self, dev, targets, action, body=None):
"""queue a new reply"""
msg = self.msg_factory.build_msg(dev, targets, 'reply', action, body)
self.queue_tx(msg)
def send_error(self, dev, errcode, description=None):
"""queue a error message"""
msg = self.msg_factory.build_error_msg(dev, errcode, description)
self.queue_tx(msg)
def send_get_description(self,dev,targets):
"""queue a getDescription request"""
self.send_request(dev,targets,'getDescription')
def send_get_attributes(self,dev,targets):
"""queue a getAttributes request"""
self.send_request(dev,targets,'getAttributes')
#####################################################
......@@ -134,18 +140,20 @@ class Engine(object):
if dev.next_alive < now :
self.send_alive(dev)
dev.update_alive()
#####################################################
# xAAL attributes changes
#####################################################
def add_attributesChange(self, attr):
"""add a new attribute change to the list"""
self.__attributesChange.append(attr)
def get_attributesChange(self):
"""return the pending attributes changes list"""
return self.__attributesChange
def process_attributesChange(self):
"""Processes attributes change for all devices"""
"""Processes (send notify) attributes changes for all devices"""
devices = {}
# Group attributes changed by device
for attr in self.get_attributesChange():
......@@ -166,6 +174,7 @@ class Engine(object):
# xAAL messages rx handlers
#####################################################
def receive_msg(self):
"""return new received message or None"""
result = None
data = self.network.get_data()
if data:
......@@ -178,6 +187,7 @@ class Engine(object):
return result
def process_rx_msg(self):
"""process incomming messages"""
msg = self.receive_msg()
if msg:
self.handle_rx_msg(msg)
......@@ -230,7 +240,7 @@ class Engine(object):
def remove_callback(self,func):
""" remove a given callback """
self.callbacks.remove(func)
def process_callbacks(self):
""" process registered callbacks """
for func in self.callbacks:
......@@ -245,7 +255,7 @@ class Engine(object):
def add_timer(self,func,period):
t = Timer(func,period)
self.timers.append(t)
def remove_timer(self,timer):
self.timers.remove(timer)
......@@ -264,7 +274,7 @@ class Engine(object):
logger.error(e.description)
t.deadline = now + t.period
self.__last_timer = now
#####################################################
# Mainloops & run ..
#####################################################
......@@ -288,7 +298,7 @@ class Engine(object):
# Process timers
self.process_timers()
# Process Alives
self.process_alives()
......@@ -314,7 +324,7 @@ class Engine(object):
while self.running:
self.loop()
def filter_msg_for_devices(msg, devices):
"""loop throught the devices, to find which are
expected w/ the msg
......@@ -363,7 +373,7 @@ def run_action(msg, device):
if msg.body:
method_params = get_args_method(method)
body_params = msg.get_parameters()
for k in body_params:
temp = '_%s' %k
if temp in method_params:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment