worked through a couple todos, removed usage of eval within parsing xml files
This commit is contained in:
parent
e4b280196b
commit
8612c73d49
8 changed files with 76 additions and 58 deletions
|
@ -436,6 +436,7 @@ class CoreRequestHandler(SocketServer.BaseRequestHandler):
|
||||||
Handle an incoming message; dispatch based on message type,
|
Handle an incoming message; dispatch based on message type,
|
||||||
optionally sending replies.
|
optionally sending replies.
|
||||||
|
|
||||||
|
:param message: message to handle
|
||||||
:return: nothing
|
:return: nothing
|
||||||
"""
|
"""
|
||||||
if self.session and self.session.broker.handle_message(message):
|
if self.session and self.session.broker.handle_message(message):
|
||||||
|
|
|
@ -6,8 +6,6 @@ import heapq
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from core import logger
|
|
||||||
|
|
||||||
|
|
||||||
class Timer(threading.Thread):
|
class Timer(threading.Thread):
|
||||||
"""
|
"""
|
||||||
|
@ -245,40 +243,3 @@ class EventLoop(object):
|
||||||
if self.running and self.timer is None:
|
if self.running and self.timer is None:
|
||||||
self.__schedule_event()
|
self.__schedule_event()
|
||||||
return event
|
return event
|
||||||
|
|
||||||
|
|
||||||
# TODO: move example to documentation
|
|
||||||
def example():
|
|
||||||
loop = EventLoop()
|
|
||||||
|
|
||||||
def msg(arg):
|
|
||||||
delta = time.time() - loop.start
|
|
||||||
logger.debug("%s arg: %s", delta, arg)
|
|
||||||
|
|
||||||
def repeat(interval, count):
|
|
||||||
count -= 1
|
|
||||||
msg("repeat: interval: %s; remaining: %s" % (interval, count))
|
|
||||||
if count > 0:
|
|
||||||
loop.add_event(interval, repeat, interval, count)
|
|
||||||
|
|
||||||
def sleep(delay):
|
|
||||||
msg("sleep %s" % delay)
|
|
||||||
time.sleep(delay)
|
|
||||||
msg("sleep done")
|
|
||||||
|
|
||||||
def stop(arg):
|
|
||||||
msg(arg)
|
|
||||||
loop.stop()
|
|
||||||
|
|
||||||
loop.add_event(0, msg, "start")
|
|
||||||
loop.add_event(0, msg, "time zero")
|
|
||||||
|
|
||||||
for delay in 5, 4, 10, -1, 0, 9, 3, 7, 3.14:
|
|
||||||
loop.add_event(delay, msg, "time %s" % delay)
|
|
||||||
|
|
||||||
loop.run()
|
|
||||||
|
|
||||||
loop.add_event(0, repeat, 1, 5)
|
|
||||||
loop.add_event(12, sleep, 10)
|
|
||||||
|
|
||||||
loop.add_event(15.75, stop, "stop time: 15.75")
|
|
||||||
|
|
|
@ -49,6 +49,8 @@ class Conf(object):
|
||||||
Provides a configuration object.
|
Provides a configuration object.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
template = Template("")
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
"""
|
"""
|
||||||
Create a Conf instance.
|
Create a Conf instance.
|
||||||
|
@ -64,7 +66,6 @@ class Conf(object):
|
||||||
:return: string representation
|
:return: string representation
|
||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
# TODO: seems like an error here
|
|
||||||
tmp = self.template.substitute(**self.kwargs)
|
tmp = self.template.substitute(**self.kwargs)
|
||||||
if tmp[-1] == "\n":
|
if tmp[-1] == "\n":
|
||||||
tmp = tmp[:-1]
|
tmp = tmp[:-1]
|
||||||
|
|
|
@ -71,20 +71,20 @@ class CoreDocumentParser0(object):
|
||||||
"""
|
"""
|
||||||
Helper to return tuple of attributes common to nodes and nets.
|
Helper to return tuple of attributes common to nodes and nets.
|
||||||
"""
|
"""
|
||||||
id = int(obj.getAttribute("id"))
|
node_id = int(obj.getAttribute("id"))
|
||||||
name = str(obj.getAttribute("name"))
|
name = str(obj.getAttribute("name"))
|
||||||
type = str(obj.getAttribute("type"))
|
node_type = str(obj.getAttribute("type"))
|
||||||
return id, name, type
|
return node_id, name, node_type
|
||||||
|
|
||||||
def parsenets(self):
|
def parsenets(self):
|
||||||
linkednets = []
|
linkednets = []
|
||||||
for net in self.np.getElementsByTagName("NetworkDefinition"):
|
for net in self.np.getElementsByTagName("NetworkDefinition"):
|
||||||
id, name, type = self.getcommonattributes(net)
|
node_id, name, node_type = self.getcommonattributes(net)
|
||||||
nodecls = xmlutils.xml_type_to_node_class(self.session, type)
|
nodecls = xmlutils.xml_type_to_node_class(node_type)
|
||||||
if not nodecls:
|
if not nodecls:
|
||||||
logger.warn("skipping unknown network node '%s' type '%s'", name, type)
|
logger.warn("skipping unknown network node '%s' type '%s'", name, node_type)
|
||||||
continue
|
continue
|
||||||
n = self.session.add_object(cls=nodecls, objid=id, name=name, start=self.start)
|
n = self.session.add_object(cls=nodecls, objid=node_id, name=name, start=self.start)
|
||||||
if name in self.coords:
|
if name in self.coords:
|
||||||
x, y, z = self.coords[name]
|
x, y, z = self.coords[name]
|
||||||
n.setposition(x, y, z)
|
n.setposition(x, y, z)
|
||||||
|
|
|
@ -151,7 +151,7 @@ class CoreDocumentParser1(object):
|
||||||
return nodeutils.get_node_class(NodeTypes.EMANE)
|
return nodeutils.get_node_class(NodeTypes.EMANE)
|
||||||
else:
|
else:
|
||||||
logger.warn('unknown network type: \'%s\'', coretype)
|
logger.warn('unknown network type: \'%s\'', coretype)
|
||||||
return xmlutils.xml_type_to_node_class(self.session, coretype)
|
return xmlutils.xml_type_to_node_class(coretype)
|
||||||
return nodeutils.get_node_class(NodeTypes.WIRELESS_LAN)
|
return nodeutils.get_node_class(NodeTypes.WIRELESS_LAN)
|
||||||
logger.warn('unknown network type: \'%s\'', network_type)
|
logger.warn('unknown network type: \'%s\'', network_type)
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -4,6 +4,19 @@ from core import logger
|
||||||
from core.netns import nodes
|
from core.netns import nodes
|
||||||
|
|
||||||
|
|
||||||
|
_NODE_MAP = {
|
||||||
|
nodes.CoreNode.__name__: nodes.CoreNode,
|
||||||
|
nodes.SwitchNode.__name__: nodes.SwitchNode,
|
||||||
|
nodes.HubNode.__name__: nodes.HubNode,
|
||||||
|
nodes.WlanNode.__name__: nodes.WlanNode,
|
||||||
|
nodes.RJ45Node.__name__: nodes.RJ45Node,
|
||||||
|
nodes.TunnelNode.__name__: nodes.TunnelNode,
|
||||||
|
nodes.GreTapBridge.__name__: nodes.GreTapBridge,
|
||||||
|
nodes.PtpNet.__name__: nodes.PtpNet,
|
||||||
|
nodes.CtrlNet.__name__: nodes.CtrlNet
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def add_elements_from_list(dom, parent, iterable, name, attr_name):
|
def add_elements_from_list(dom, parent, iterable, name, attr_name):
|
||||||
"""
|
"""
|
||||||
XML helper to iterate through a list and add items to parent using tags
|
XML helper to iterate through a list and add items to parent using tags
|
||||||
|
@ -77,7 +90,8 @@ def get_text_elements_to_list(parent):
|
||||||
if n.nodeType != Node.ELEMENT_NODE:
|
if n.nodeType != Node.ELEMENT_NODE:
|
||||||
continue
|
continue
|
||||||
k = str(n.nodeName)
|
k = str(n.nodeName)
|
||||||
v = '' # sometimes want None here?
|
# sometimes want None here?
|
||||||
|
v = ''
|
||||||
for c in n.childNodes:
|
for c in n.childNodes:
|
||||||
if c.nodeType != Node.TEXT_NODE:
|
if c.nodeType != Node.TEXT_NODE:
|
||||||
continue
|
continue
|
||||||
|
@ -251,19 +265,19 @@ def get_params_set_attrs(dom, param_names, target):
|
||||||
param_name = param.getAttribute("name")
|
param_name = param.getAttribute("name")
|
||||||
value = param.getAttribute("value")
|
value = param.getAttribute("value")
|
||||||
if value is None:
|
if value is None:
|
||||||
continue # never reached?
|
# never reached?
|
||||||
|
continue
|
||||||
if param_name in param_names:
|
if param_name in param_names:
|
||||||
setattr(target, param_name, str(value))
|
setattr(target, param_name, str(value))
|
||||||
|
|
||||||
|
|
||||||
def xml_type_to_node_class(session, type):
|
def xml_type_to_node_class(node_type):
|
||||||
"""
|
"""
|
||||||
Helper to convert from a type string to a class name in nodes.*.
|
Helper to convert from a type string to a class name in nodes.*.
|
||||||
"""
|
"""
|
||||||
if hasattr(nodes, type):
|
logger.error("xml type to node type: %s", node_type)
|
||||||
# TODO: remove and use a mapping to known nodes
|
if hasattr(nodes, node_type):
|
||||||
logger.error("using eval to retrieve node type: %s", type)
|
return _NODE_MAP[node_type]
|
||||||
return eval("nodes.%s" % type)
|
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
44
daemon/examples/eventloop.py
Normal file
44
daemon/examples/eventloop.py
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
import time
|
||||||
|
|
||||||
|
from core import logger
|
||||||
|
from core.misc.event import EventLoop
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
loop = EventLoop()
|
||||||
|
|
||||||
|
def msg(arg):
|
||||||
|
delta = time.time() - loop.start
|
||||||
|
logger.debug("%s arg: %s", delta, arg)
|
||||||
|
|
||||||
|
def repeat(interval, count):
|
||||||
|
count -= 1
|
||||||
|
msg("repeat: interval: %s; remaining: %s" % (interval, count))
|
||||||
|
if count > 0:
|
||||||
|
loop.add_event(interval, repeat, interval, count)
|
||||||
|
|
||||||
|
def sleep(delay):
|
||||||
|
msg("sleep %s" % delay)
|
||||||
|
time.sleep(delay)
|
||||||
|
msg("sleep done")
|
||||||
|
|
||||||
|
def stop(arg):
|
||||||
|
msg(arg)
|
||||||
|
loop.stop()
|
||||||
|
|
||||||
|
loop.add_event(0, msg, "start")
|
||||||
|
loop.add_event(0, msg, "time zero")
|
||||||
|
|
||||||
|
for delay in 5, 4, 10, -1, 0, 9, 3, 7, 3.14:
|
||||||
|
loop.add_event(delay, msg, "time %s" % delay)
|
||||||
|
|
||||||
|
loop.run()
|
||||||
|
|
||||||
|
loop.add_event(0, repeat, 1, 5)
|
||||||
|
loop.add_event(12, sleep, 10)
|
||||||
|
|
||||||
|
loop.add_event(15.75, stop, "stop time: 15.75")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
|
@ -118,10 +118,7 @@ def main():
|
||||||
# start a shell on node 1
|
# start a shell on node 1
|
||||||
n[1].term("bash")
|
n[1].term("bash")
|
||||||
|
|
||||||
# TODO: access to remote nodes is currently limited in this script
|
|
||||||
|
|
||||||
print "elapsed time: %s" % (datetime.datetime.now() - start)
|
print "elapsed time: %s" % (datetime.datetime.now() - start)
|
||||||
|
|
||||||
print "To stop this session, use the 'core-cleanup' script on this server"
|
print "To stop this session, use the 'core-cleanup' script on this server"
|
||||||
print "and on the remote slave server."
|
print "and on the remote slave server."
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue