以l3_learning为例
def launch (fakeways="", arp_for_unknowns=None):
fakeways = fakeways.replace(","," ").split()
fakeways = [IPAddr(x) for x in fakeways]
if arp_for_unknowns is None:
arp_for_unknowns = len(fakeways) > 0
else:
arp_for_unknowns = str_to_bool(arp_for_unknowns)
core.registerNew(l3_switch, fakeways, arp_for_unknowns)
def registerNew (self, __componentClass, *args, **kw):
"""
Give it a class (and optional __init__ arguments), and it will
create an instance and register it using the class name. If the
instance has a _core_name property, it will use that instead.
It returns the new instance.
core.registerNew(FooClass, arg) is roughly equivalent to
core.register("FooClass", FooClass(arg)).
"""
name = __componentClass.__name__
obj = __componentClass(*args, **kw)
if hasattr(obj, '_core_name'):
# Default overridden
name = obj._core_name
self.register(name, obj)
return obj
该注册方法core.registerNew(FooClass, arg)与core.register("FooClass", FooClass(arg))基本相同
register函数:
def register (self, name, component=None):
"""
Makes the object "component" available as pox.core.core.name.
If only one argument is specified, the given argument is registered
using its class name as the name.
"""
#TODO: weak references?
if component is None:
component = name
name = component.__class__.__name__
if hasattr(component, '_core_name'):
# Default overridden
name = component._core_name
if name in self.components:
log.warn("Warning: Registered '%s' multipled times" % (name,))
self.components[name] = component
self.raiseEventNoErrors(ComponentRegistered, name, component)
self._try_waiters()
self.raiseEventNoErrors(ComponentRegistered, name, component)
说明core引发一个事件"ComponentRegistered"。注意这个事件的参数是组件和组件的名字,即该组件的注册事件。
此即注册事件的第一步,在core中注册组件,之后学习如何将组件中的event_handler与相关的具体事件绑定在一起。