def testBeginlock2(self): rc = RoutineContainer(self.server.scheduler) obj = [0] async def routineLock(key): l = Lock(key, rc.scheduler) locked = l.beginlock(rc) if not locked: await rc.wait_with_timeout(0.2) await l.lock(rc) t = obj[0] await rc.do_events() obj[0] = t + 1 l.unlock() await rc.do_events() await l.lock(rc) t = obj[0] if t != 2: obj[0] = t - 1 l.unlock() rc.subroutine(routineLock('testobj')) rc.subroutine(routineLock('testobj')) self.server.serve() self.assertEqual(obj[0], 2)
class TestModule(Module): def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self.main self.routines.append(self.apiroutine) async def create_timers(self, wide=5, deep=5): w = random() * 10.0 if not deep: if randrange(100) == 0: await self.apiroutine.wait_with_timeout(10) return async def _subroutine(): await self.apiroutine.execute_with_timeout( w, self.create_timers(wide, deep - 1)) s = self.apiroutine.subroutine(_subroutine(), False) await self.apiroutine.execute_with_timeout( w, self.apiroutine.execute_all( [self.create_timers(wide, deep - 1) for _ in range(wide - 1)])) s.close() async def main(self): count = 0 while True: await self.apiroutine.wait_with_timeout(0.1) self.apiroutine.subroutine(self.create_timers()) count += 1 if count % 100 == 0: print(count)
def testSemaphore(self): rc = RoutineContainer(self.server.scheduler) obj = [0] async def routineLock(key): l = Lock(key, rc.scheduler) await l.lock(rc) t = obj[0] await rc.do_events() obj[0] = t + 1 l.unlock() async def main_routine(): smp = Semaphore('testobj', 2, rc.scheduler) smp.create() await rc.execute_all([ routineLock('testobj'), routineLock('testobj'), routineLock('testobj'), routineLock('testobj') ]) await smp.destroy(rc) rc.subroutine(main_routine()) self.server.serve() self.assertEqual(obj[0], 2)
def testBeginlock(self): rc = RoutineContainer(self.server.scheduler) obj = [0] def routineLock(key): l = Lock(key, rc.scheduler) locked = l.beginlock(rc) if not locked: for m in rc.waitWithTimeout(1.0): yield m locked = l.trylock() if not locked: raise ValueError('Not locked') t = obj[0] for m in rc.waitWithTimeout(0.5): yield m obj[0] = t + 1 l.unlock() for m in rc.doEvents(): yield m for m in l.lock(rc): yield m t = obj[0] for m in rc.waitWithTimeout(1.0): yield m obj[0] = t + 1 l.unlock() rc.subroutine(routineLock('testobj')) rc.subroutine(routineLock('testobj')) self.server.serve() self.assertEqual(obj[0], 4)
def testSemaphore(self): rc = RoutineContainer(self.server.scheduler) obj = [0] def routineLock(key): l = Lock(key, rc.scheduler) for m in l.lock(rc): yield m t = obj[0] for m in rc.waitWithTimeout(0.5): yield m obj[0] = t + 1 l.unlock() def main_routine(): smp = Semaphore('testobj', 2, rc.scheduler) smp.create() for m in rc.executeAll([routineLock('testobj'), routineLock('testobj'), routineLock('testobj'), routineLock('testobj')], retnames = ()): yield m for m in smp.destroy(rc): yield m rc.subroutine(main_routine()) self.server.serve() self.assertEqual(obj[0], 2)
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.client = ZooKeeperClient(self.apiroutine, self.serverlist) self.connections.append(self.client) self.apiroutine.main = self.main self.routines.append(self.apiroutine)
class ICMPResponder(FlowBase): _tablerequest = (("l3input", ("l2input", ), ""), ("l2output", ("l3input", ), "")) # True : use flow auto reply icmp ping # False: use controller reply icmp ping # # when ovs 2.5 , icmp_type is not readonly , we can use flow auto reply icmp echo # _default_prepush = False # router use this mac as inner mac _default_inroutermac = '1a:23:67:59:63:33' def __init__(self, server): super(ICMPResponder, self).__init__(server) self.app_routine = RoutineContainer(self.scheduler) self.app_routine.main = self._main self.routines.append(self.app_routine) self._flowupdater = dict() def _main(self): flowinit = FlowInitialize.createMatcher( _ismatch=lambda x: self.vhostbind is None or x.vhost in self. vhostbind) conndown = OpenflowConnectionStateEvent.createMatcher( state=OpenflowConnectionStateEvent.CONNECTION_DOWN, _ismatch=lambda x: self.vhostbind is None or x.createby.vhost in self.vhostbind) while True: yield (flowinit, conndown) if self.app_routine.matcher is flowinit: c = self.app_routine.event.connection self.app_routine.subroutine(self._init_conn(c)) if self.app_routine.matcher is conndown: c = self.app_routine.event.connection self.app_routine.subroutine(self._remove_conn) def _init_conn(self, conn): if conn in self._flowupdater: updater = self._flowupdater.pop(conn) updater.close() updater = ICMPResponderUpdater(conn, self) self._flowupdater[conn] = updater updater.start() if False: yield def _remove_conn(self, conn): if conn in self._flowupdater: updater = self._flowupdater.pop(conn) updater.close() if False: yield
def testLoopConsumer(self): scheduler = Scheduler() scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher()) scheduler.queue.addSubQueue(1, TestConsumerEvent.createMatcher(), 'consumer', 5, 5) rA = RoutineContainer(scheduler) output = bytearray() def mainA(): rA.subroutine(mainB(), True, 'mainB', True) matcher = TestConsumerEvent.createMatcher(rA.mainB) for i in range(0,10): for ms in rA.waitForSend(TestConsumerEvent(rA.mainroutine)): yield ms output.extend(b'A') yield (matcher,) def mainB(): matcher = TestConsumerEvent.createMatcher(producer=rA.mainroutine) while True: yield (matcher,) rA.event.canignore = True output.extend(b'B') for ms in rA.waitForSend(TestConsumerEvent(rA.mainB, canignore = True)): yield ms rA.main = mainA rA.start() scheduler.main() self.assertEqual(output, b'ABABABABABABABABABAB')
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._manage_conns self.routines.append(self.apiroutine) self.managed_conns = {} self.managed_systemids = {} self.managed_bridges = {} self.managed_routines = [] self.endpoint_conns = {} self.createAPI(api(self.getconnection, self.apiroutine), api(self.waitconnection, self.apiroutine), api(self.getdatapathids, self.apiroutine), api(self.getalldatapathids, self.apiroutine), api(self.getallconnections, self.apiroutine), api(self.getbridges, self.apiroutine), api(self.getbridge, self.apiroutine), api(self.getbridgebyuuid, self.apiroutine), api(self.waitbridge, self.apiroutine), api(self.waitbridgebyuuid, self.apiroutine), api(self.getsystemids, self.apiroutine), api(self.getallsystemids, self.apiroutine), api(self.getconnectionbysystemid, self.apiroutine), api(self.waitconnectionbysystemid, self.apiroutine), api(self.getconnectionsbyendpoint, self.apiroutine), api(self.getconnectionsbyendpointname, self.apiroutine), api(self.getendpoints, self.apiroutine), api(self.getallendpoints, self.apiroutine), api(self.getallbridges, self.apiroutine), api(self.getbridgeinfo, self.apiroutine), api(self.waitbridgeinfo, self.apiroutine)) self._synchronized = False
def __init__(self, server): FlowBase.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._main self.routines.append(self.apiroutine) self._flowupdaters = {} self._extra_arps = {}
def testLoopConsumer(self): scheduler = Scheduler() scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher()) scheduler.queue.addSubQueue(1, TestConsumerEvent.createMatcher(), 'consumer', 5, 5) rA = RoutineContainer(scheduler) output = bytearray() def mainA(): rA.subroutine(mainB(), True, 'mainB', True) matcher = TestConsumerEvent.createMatcher(rA.mainB) for i in range(0, 10): for ms in rA.waitForSend(TestConsumerEvent(rA.mainroutine)): yield ms output.extend(b'A') yield (matcher, ) def mainB(): matcher = TestConsumerEvent.createMatcher(producer=rA.mainroutine) while True: yield (matcher, ) rA.event.canignore = True output.extend(b'B') for ms in rA.waitForSend( TestConsumerEvent(rA.mainB, canignore=True)): yield ms rA.main = mainA rA.start() scheduler.main() self.assertEqual(output, b'ABABABABABABABABABAB')
class ICMPResponder(FlowBase): _tablerequest = ( ("l3input",("l2input",),""), ("l2output",("l3input",),"") ) # True : use flow auto reply icmp ping # False: use controller reply icmp ping # # when ovs 2.5 , icmp_type is not readonly , we can use flow auto reply icmp echo # _default_prepush = False # router use this mac as inner mac _default_inroutermac = '1a:23:67:59:63:33' def __init__(self,server): super(ICMPResponder,self).__init__(server) self.app_routine = RoutineContainer(self.scheduler) self.app_routine.main = self._main self.routines.append(self.app_routine) self._flowupdater = dict() def _main(self): flowinit = FlowInitialize.createMatcher(_ismatch=lambda x: self.vhostbind is None or x.vhost in self.vhostbind) conndown = OpenflowConnectionStateEvent.createMatcher(state = OpenflowConnectionStateEvent.CONNECTION_DOWN, _ismatch=lambda x:self.vhostbind is None or x.createby.vhost in self.vhostbind) while True: yield (flowinit,conndown) if self.app_routine.matcher is flowinit: c = self.app_routine.event.connection self.app_routine.subroutine(self._init_conn(c)) if self.app_routine.matcher is conndown: c = self.app_routine.event.connection self.app_routine.subroutine(self._remove_conn(c)) def _init_conn(self,conn): if conn in self._flowupdater: updater = self._flowupdater.pop(conn) updater.close() updater = ICMPResponderUpdater(conn,self) self._flowupdater[conn] = updater updater.start() if False: yield def _remove_conn(self,conn): if conn in self._flowupdater: updater = self._flowupdater.pop(conn) updater.close() if False: yield
def __init__(self, worker_start, matchers=(), scheduler=None, mp=True, inputlimit=0, allowcontrol=True): ''' :param worker_start: func(queuein, queueout), queuein is the input queue, queueout is the output queue. For queuein, each object is (event, matcher) tuple; For queueout, each object is a tuple of events to send. Every object in queuein must have a response in queueout. :param matcheres: match events to be processed by connector. :param scheduler: bind to specified scheduler :param mp: use multiprocessing if possible. For windows, multi-threading is always used. :param inputlimit: input queue size limit. 0 = infinite. :param allowcontrol: if True, the connector accepts ConnectorControlEvent for connector configuration. ''' RoutineContainer.__init__(self, scheduler, True) self.worker_start = worker_start self.matchers = set(matchers) self.mp = mp self.inputlimit = inputlimit self.allowcontrol = allowcontrol self.stopreceive = False self.jobs = 0 self._moreresult_matcher = MoreResultEvent.createMatcher()
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._manage_conns self.routines.append(self.apiroutine) self.managed_conns = {} self.endpoint_conns = {} self.table_modules = set() self._acquiring = False self._acquire_updated = False self._lastacquire = None self._synchronized = False self.createAPI(api(self.getconnections, self.apiroutine), api(self.getconnection, self.apiroutine), api(self.waitconnection, self.apiroutine), api(self.getdatapathids, self.apiroutine), api(self.getalldatapathids, self.apiroutine), api(self.getallconnections, self.apiroutine), api(self.getconnectionsbyendpoint, self.apiroutine), api(self.getconnectionsbyendpointname, self.apiroutine), api(self.getendpoints, self.apiroutine), api(self.getallendpoints, self.apiroutine), api(self.acquiretable, self.apiroutine), api(self.unacquiretable, self.apiroutine), api(self.lastacquiredtables))
def __init__(self, server): Module.__init__(self, server) self._managed_objs = {} self._watches = {} self._watchedkeys = set() self._requests = [] self._transactno = 0 self._stale = False self._updatekeys = set() self._update_version = {} self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._update self.routines.append(self.apiroutine) self.createAPI(api(self.mget, self.apiroutine), api(self.get, self.apiroutine), api(self.mgetonce, self.apiroutine), api(self.getonce, self.apiroutine), api(self.mwatch, self.apiroutine), api(self.watch, self.apiroutine), api(self.munwatch, self.apiroutine), api(self.unwatch, self.apiroutine), api(self.transact, self.apiroutine), api(self.watchlist), api(self.walk, self.apiroutine) )
class DHCPServer(FlowBase): "DHCP server that responds the DHCP discover/request with static IP address settings" _tablerequest = (("l3input", ('l2input', ), ''), ("l2output", ("l3input", ), '')) # Responding DHCP server address _default_serveraddress = '169.254.169.254' # Responding DHCP server MAC address _default_servermac = '1a:23:67:59:63:33' # DHCP leases timeout time _default_leasetime = None # DHCP default T1 option _default_t1 = None # DHCP default T2 option _default_t2 = None def __init__(self, server): FlowBase.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._main self.routines.append(self.apiroutine) self._flowupdaters = {} self._extra_arps = {} def _main(self): flow_init = FlowInitialize.createMatcher( _ismatch=lambda x: self.vhostbind is None or x.vhost in self. vhostbind) conn_down = OpenflowConnectionStateEvent.createMatcher( state=OpenflowConnectionStateEvent.CONNECTION_DOWN, _ismatch=lambda x: self.vhostbind is None or x.createby.vhost in self.vhostbind) while True: yield (flow_init, conn_down) if self.apiroutine.matcher is flow_init: c = self.apiroutine.event.connection self.apiroutine.subroutine( self._init_conn(self.apiroutine.event.connection)) else: c = self.apiroutine.event.connection self.apiroutine.subroutine(self._remove_conn(c)) def _init_conn(self, conn): # Default if conn in self._flowupdaters: updater = self._flowupdaters.pop(conn) updater.close() updater = DHCPUpdater(conn, self) #flowupdater = VXLANFlowUpdater(conn, self) self._flowupdaters[conn] = updater updater.start() if False: yield def _remove_conn(self, conn): # Do not need to modify flows if conn in self._flowupdaters: self._flowupdaters.pop(conn).close() if False: yield
def __init__(self, server): Module.__init__(self, server) self.protocol = ZooKeeper() self.client = Client(self.url, self.protocol, self.scheduler) self.connections.append(self.client) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self.main self.routines.append(self.apiroutine)
def __init__(self, server): FlowBase.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._main self.routines.append(self.apiroutine) self._flowupdaters = {} self._extra_arps = {} self.createAPI(api(self.createproxyarp), api(self.removeproxyarp))
class TestModule(Module): _default_serverlist = ['tcp://localhost:3181/','tcp://localhost:3182/','tcp://localhost:3183/'] def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.client = ZooKeeperClient(self.apiroutine, self.serverlist) self.connections.append(self.client) self.apiroutine.main = self.main self.routines.append(self.apiroutine) def watcher(self): watcher = ZooKeeperWatcherEvent.createMatcher() while True: yield (watcher,) print('WatcherEvent: %r' % (dump(self.apiroutine.event.message),)) def main(self): def _watch(w): for m in w.wait(self.apiroutine): yield m print('Watcher returns:', dump(self.apiroutine.retvalue)) def _watchall(watchers): for w in watchers: if w is not None: self.apiroutine.subroutine(_watch(w)) self.apiroutine.subroutine(self.watcher(), False, daemon = True) up = ZooKeeperSessionStateChanged.createMatcher(ZooKeeperSessionStateChanged.CREATED, self.client) yield (up,) print('Connection is up: %r' % (self.client.currentserver,)) for m in self.client.requests([zk.create(b'/vlcptest', b'test'), zk.getdata(b'/vlcptest', True)], self.apiroutine): yield m print(self.apiroutine.retvalue) pprint(dump(self.apiroutine.retvalue[0])) _watchall(self.apiroutine.retvalue[3]) for m in self.apiroutine.waitWithTimeout(0.2): yield m for m in self.client.requests([zk.delete(b'/vlcptest'), zk.getdata(b'/vlcptest', watch = True)], self.apiroutine): yield m print(self.apiroutine.retvalue) pprint(dump(self.apiroutine.retvalue[0])) _watchall(self.apiroutine.retvalue[3]) for m in self.client.requests([zk.multi( zk.multi_create(b'/vlcptest2', b'test'), zk.multi_create(b'/vlcptest2/subtest', 'test2') ), zk.getchildren2(b'/vlcptest2', True)], self.apiroutine): yield m print(self.apiroutine.retvalue) pprint(dump(self.apiroutine.retvalue[0])) _watchall(self.apiroutine.retvalue[3]) for m in self.client.requests([zk.multi( zk.multi_delete(b'/vlcptest2/subtest'), zk.multi_delete(b'/vlcptest2')), zk.getchildren2(b'/vlcptest2', True)], self.apiroutine): yield m print(self.apiroutine.retvalue) pprint(dump(self.apiroutine.retvalue[0])) _watchall(self.apiroutine.retvalue[3])
def __init__(self, moduleinst, apidefs = None, allowdiscover = True, rejectunknown = True): RoutineContainer.__init__(self, scheduler=moduleinst.scheduler, daemon=False) self.handler = EventHandler(self.scheduler) self.servicename = moduleinst.getServiceName() self.apidefs = apidefs self.registeredAPIs = {} self.discoverinfo = {} self.allowdiscover = allowdiscover self.rejectunknown = rejectunknown
def __init__(self, moduleinst, apidefs = None, allowdiscover = True, rejectunknown = True): RoutineContainer.__init__(self, scheduler=moduleinst.scheduler, daemon=False) self.handler = EventHandler(self.scheduler) self.servicename = moduleinst.getServiceName() self.apidefs = apidefs self.registeredAPIs = {} self.discoverinfo = {} self.allowdiscover = allowdiscover self.rejectunknown = True
class TestModule(Module): _default_url = 'tcp://localhost/' _default_sessiontimeout = 30 def __init__(self, server): Module.__init__(self, server) self.protocol = ZooKeeper() self.client = Client(self.url, self.protocol, self.scheduler) self.connections.append(self.client) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self.main self.routines.append(self.apiroutine) def watcher(self): watcher = ZooKeeperWatcherEvent.createMatcher(connection = self.client) while True: yield (watcher,) print('WatcherEvent: %r' % (dump(self.apiroutine.event.message),)) def main(self): self.apiroutine.subroutine(self.watcher(), False, daemon = True) up = ZooKeeperConnectionStateEvent.createMatcher(ZooKeeperConnectionStateEvent.UP, self.client) notconn = ZooKeeperConnectionStateEvent.createMatcher(ZooKeeperConnectionStateEvent.NOTCONNECTED, self.client) yield (up, notconn) if self.apiroutine.matcher is notconn: print('Not connected') return else: print('Connection is up: %r' % (self.client,)) # Handshake for m in self.protocol.handshake(self.client, zk.ConnectRequest( timeOut = int(self.sessiontimeout * 1000), passwd = b'\x00' * 16, # Why is it necessary... ), self.apiroutine, []): yield m for m in self.protocol.requests(self.client, [zk.create(b'/vlcptest', b'test'), zk.getdata(b'/vlcptest', True)], self.apiroutine): yield m pprint(dump(self.apiroutine.retvalue[0])) for m in self.apiroutine.waitWithTimeout(0.2): yield m for m in self.protocol.requests(self.client, [zk.delete(b'/vlcptest'), zk.getdata(b'/vlcptest', watch = True)], self.apiroutine): yield m pprint(dump(self.apiroutine.retvalue[0])) for m in self.protocol.requests(self.client, [zk.multi( zk.multi_create(b'/vlcptest2', b'test'), zk.multi_create(b'/vlcptest2/subtest', 'test2') ), zk.getchildren2(b'/vlcptest2', True)], self.apiroutine): yield m pprint(dump(self.apiroutine.retvalue[0])) for m in self.protocol.requests(self.client, [zk.multi( zk.multi_delete(b'/vlcptest2/subtest'), zk.multi_delete(b'/vlcptest2')), zk.getchildren2(b'/vlcptest2', True)], self.apiroutine): yield m pprint(dump(self.apiroutine.retvalue[0]))
def __init__(self, vhostbind, prefix, scheduler=None, singlecastlimit = 256, deflate = False): RoutineContainer.__init__(self, scheduler=scheduler, daemon=False) self.vhostbind = vhostbind self.prefix = _bytes(prefix) self._matchers = {} self._publishkey = uuid.uuid1().hex self._publishno = 1 self._publish_wait = set() self._matchadd_wait = set() self._matchremove_wait = set() self._singlecastlimit = singlecastlimit self._deflate = deflate
def __init__(self, connection, initialkeys=(), requestid=None, logger=None): """ Retrieve data objects from ObjectDB and use them to generate flows The module starts ObjectDB.walk from initial keys and walkers. After the walk completes, the retrieved data objects are used by `updateflow()` to generate flows and send them to the OpenFlow connection. When the retrieved objects are updated, FlowUpdater either restart the walk process (re-walk) or directly call another `updateflow()`, according to the objects that are updated. A subclass should re-initialize `self._initialkeys` and `self._walkerdict` before `main()` coroutine starts to customize the process. `updateflow()` is guaranteed for no re-entrance i.e. will not be called until the last call returns. Multiple changes may be merged into the same call. :param connection: OpenFlow connection :param initialkeys: DEPRECATED The key list that triggers a re-walk :param requestid: request id to retrieve data objects from ObjectDB :param logger: inherit a logger from a module """ RoutineContainer.__init__(self, connection.scheduler) # When keys in this sequence are updated, start re-walk self._initialkeys = initialkeys self._connection = connection # Walker dict, will be re-initialized by a subclass self._walkerdict = {} # Walk result self._savedkeys = () self._savedresult = [] # Update notification (with a cache for synchronize) self._updatedset = set() self._updatedset2 = set() if not logger: self._logger = logging.getLogger(__name__ + '.FlowUpdater') else: self._logger = logger if requestid is None: self._requstid = str(uuid1()) else: self._requstid = requestid self._requestindex = 0 # Detect data object updates self._dataupdateroutine = None # Calling updateflow() in the same routine to prevent a re-entrance self._flowupdateroutine = None
def __init__(self, server): ''' Constructor ''' Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._autoreload self._lastcheck = time() if self.autoreload: self.routines.append(self.apiroutine) self.createAPI(api(self.enableAutoReload), api(self.activeModules), api(self.reloadmodules, self.apiroutine), api(self.loadmodule, self.apiroutine), api(self.unloadmodule, self.apiroutine))
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._manage_ports self.routines.append(self.apiroutine) self.managed_ports = {} self.createAPI(api(self.getports, self.apiroutine), api(self.getallports, self.apiroutine), api(self.getportbyno, self.apiroutine), api(self.waitportbyno, self.apiroutine), api(self.getportbyname, self.apiroutine), api(self.waitportbyname, self.apiroutine), api(self.resync, self.apiroutine)) self._synchronized = False
def _test_limiter(limit, expected_result, *numbers): scheduler = Scheduler() rA = RoutineContainer(scheduler) rB = RoutineContainer(scheduler) limiter = RateLimiter(limit, rA) counter = [0, 0] result = [] async def _record(): first = True while True: await rA.do_events() if counter[0] == 0: break result.append((counter[0], counter[1])) counter[0] = 0 counter[1] = 0 async def _limited(use = 1): await limiter.limit(use) counter[0] += 1 counter[1] += use async def _starter(): for t in numbers: if isinstance(t, tuple): for use in t: rB.subroutine(_limited(use)) else: for _ in range(t): rB.subroutine(_limited()) await rB.do_events() rA.subroutine(_record(), False) rB.subroutine(_starter()) scheduler.main() self.assertEqual(result, expected_result)
def testTrylock(self): rc = RoutineContainer(self.server.scheduler) result = [] def routineTrylock(key): l = Lock(key, rc.scheduler) locked = l.trylock() result.append(locked) for m in rc.waitWithTimeout(0.5): yield m l.unlock() rc.subroutine(routineTrylock('testobj')) rc.subroutine(routineTrylock('testobj')) self.server.serve() self.assertEqual(result, [True, False])
def testAsyncWith(self): rc = RoutineContainer(self.server.scheduler) obj = [0] async def routineLock(key): l = Lock(key, rc.scheduler) async with l: t = obj[0] await rc.do_events() obj[0] = t + 1 rc.subroutine(routineLock('testobj')) rc.subroutine(routineLock('testobj')) self.server.serve() self.assertEqual(obj[0], 2)
def testTrylock(self): rc = RoutineContainer(self.server.scheduler) result = [] async def routineTrylock(key): l = Lock(key, rc.scheduler) locked = l.trylock() result.append(locked) await rc.do_events() l.unlock() rc.subroutine(routineTrylock('testobj')) rc.subroutine(routineTrylock('testobj')) self.server.serve() self.assertEqual(result, [True, False])
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._manage_conns self.routines.append(self.apiroutine) self.managed_conns = {} self.endpoint_conns = {} self.table_modules = set() self._acquiring = False self._acquire_updated = False self._lastacquire = None self._synchronized = False self.createAPI(api(self.getconnections, self.apiroutine), api(self.getconnection, self.apiroutine), api(self.waitconnection, self.apiroutine), api(self.getdatapathids, self.apiroutine), api(self.getalldatapathids, self.apiroutine), api(self.getallconnections, self.apiroutine), api(self.getconnectionsbyendpoint, self.apiroutine), api(self.getconnectionsbyendpointname, self.apiroutine), api(self.getendpoints, self.apiroutine), api(self.getallendpoints, self.apiroutine), api(self.acquiretable, self.apiroutine), api(self.unacquiretable, self.apiroutine), api(self.lastacquiredtables) )
def __init__(self, server): Module.__init__(self, server) self._managed_objs = {} self._watches = {} self._watchedkeys = set() self._requests = [] self._transactno = 0 self._stale = False self._updatekeys = set() self._update_version = {} self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._update self.routines.append(self.apiroutine) self.createAPI(api(self.mget, self.apiroutine), api(self.get, self.apiroutine), api(self.mgetonce, self.apiroutine), api(self.getonce, self.apiroutine), api(self.mwatch, self.apiroutine), api(self.watch, self.apiroutine), api(self.munwatch, self.apiroutine), api(self.unwatch, self.apiroutine), api(self.unwatchall, self.apiroutine), api(self.transact, self.apiroutine), api(self.watchlist), api(self.walk, self.apiroutine) )
def __init__(self, server): ''' Constructor ''' Module.__init__(self, server) self._ce_matcher = ConsoleEvent.createMatcher() self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._service_routine self._restore_console_event = threading.Event() @generator_to_async(True, False) def proxy(event, matcher): while True: events = self.sendEventQueue.get() if events is None: break yield events @async_to_async(True, False) @async_processor def processor(event, matcher, queueout): if event.type == 'initproxy': proxy(event, matcher, queueout) self.connector = Connector(processor, (self._ce_matcher, ), self.scheduler, False) self.routines.append(self.apiroutine) self.routines.append(self.connector)
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._manage_conns self.routines.append(self.apiroutine) self.managed_conns = {} self.managed_systemids = {} self.managed_bridges = {} self.managed_routines = [] self.endpoint_conns = {} self.createAPI(api(self.getconnection, self.apiroutine), api(self.waitconnection, self.apiroutine), api(self.getdatapathids, self.apiroutine), api(self.getalldatapathids, self.apiroutine), api(self.getallconnections, self.apiroutine), api(self.getbridges, self.apiroutine), api(self.getbridge, self.apiroutine), api(self.getbridgebyuuid, self.apiroutine), api(self.waitbridge, self.apiroutine), api(self.waitbridgebyuuid, self.apiroutine), api(self.getsystemids, self.apiroutine), api(self.getallsystemids, self.apiroutine), api(self.getconnectionbysystemid, self.apiroutine), api(self.waitconnectionbysystemid, self.apiroutine), api(self.getconnectionsbyendpoint, self.apiroutine), api(self.getconnectionsbyendpointname, self.apiroutine), api(self.getendpoints, self.apiroutine), api(self.getallendpoints, self.apiroutine), api(self.getallbridges, self.apiroutine), api(self.getbridgeinfo, self.apiroutine), api(self.waitbridgeinfo, self.apiroutine) ) self._synchronized = False
class TestModule(Module): _default_serverlist = ['tcp://localhost:3181/','tcp://localhost:3182/','tcp://localhost:3183/'] def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self.main self.routines.append(self.apiroutine) def main(self): clients = [ZooKeeperClient(self.apiroutine, self.serverlist) for _ in range(0,10)] for c in clients: c.start() def test_loop(number): maindir = ('vlcptest_' + str(number)).encode('utf-8') client = clients[number % len(clients)] for _ in range(0, 100): for m in client.requests([zk.multi( zk.multi_create(maindir, b'test'), zk.multi_create(maindir + b'/subtest', 'test2') ), zk.getchildren2(maindir, True)], self.apiroutine): yield m for m in client.requests([zk.multi( zk.multi_delete(maindir + b'/subtest'), zk.multi_delete(maindir)), zk.getchildren2(maindir, True)], self.apiroutine): yield m from time import time starttime = time() for m in self.apiroutine.executeAll([test_loop(i) for i in range(0, 100)]): yield m print('10000 loops in %r seconds, with %d connections' % (time() - starttime, len(clients))) for c in clients: for m in c.shutdown(): yield m
def __init__(self, server): super(NetworkVxlanDriver, self).__init__(server) self.app_routine = RoutineContainer(self.scheduler) self.app_routine.main = self._main self.routines.append(self.app_routine) self.createAPI( publicapi(self.createphysicalnetworks, criteria=lambda networks, type: type == 'vxlan'), publicapi(self.updatephysicalnetworks, criteria=lambda type, networks: type == 'vxlan'), publicapi(self.deletephysicalnetworks, criteria=lambda type, networks: type == 'vxlan'), publicapi(self.createphysicalports, criteria=lambda type, ports: type == 'vxlan'), publicapi( self.updatephysicalports, criteria=lambda phynettype, ports: phynettype == 'vxlan'), publicapi( self.deletephysicalports, criteria=lambda phynettype, ports: phynettype == 'vxlan'), publicapi( self.createlogicalnetworks, criteria=lambda phynettype, networks: phynettype == 'vxlan'), publicapi( self.updatelogicalnetworks, criteria=lambda phynettype, networks: phynettype == 'vxlan'), publicapi( self.deletelogicalnetworks, criteria=lambda phynettype, networks: phynettype == "vxlan"))
def __init__(self, server): super(NetworkNativeDriver, self).__init__(server) self.app_routine = RoutineContainer(self.scheduler) self.app_routine.main = self._main self.routines.append(self.app_routine) self.createAPI( publicapi(self.createphysicalnetworks, criteria=lambda networks, type: type == 'native'), publicapi(self.updatephysicalnetworks, criteria=lambda type, networks: type == 'native'), publicapi(self.deletephysicalnetworks, criteria=lambda type, networks: type == 'native'), publicapi(self.createphysicalports, criteria=lambda type, ports: type == 'native'), publicapi( self.updatephysicalports, criteria=lambda phynettype, ports: phynettype == 'native'), publicapi( self.deletephysicalports, criteria=lambda phynettype, ports: phynettype == 'native'), publicapi( self.createlogicalnetworks, criteria=lambda phynettype, networks: phynettype == 'native'), publicapi( self.updatelogicalnetworks, criteria=lambda phynettype, networks: phynettype == 'native'), publicapi( self.deletelogicalnetworks, criteria=lambda phynettype, networks: phynettype == "native"), #used in IOprocessing module publicapi(self.createioflowparts, criteria=lambda connection, logicalnetwork, physicalport, logicalnetworkid, physicalportid: logicalnetwork. physicalnetwork.type == "native"))
def testLock2(self): rc = RoutineContainer(self.server.scheduler) obj = [0] def routineLock(key): l = Lock(key, rc.scheduler) for m in l.lock(rc): yield m t = obj[0] for m in rc.waitWithTimeout(0.5): yield m obj[0] = t + 1 l.unlock() rc.subroutine(routineLock('testobj')) rc.subroutine(routineLock('testobj2')) self.server.serve() self.assertEqual(obj[0], 1)
def testLock(self): rc = RoutineContainer(self.server.scheduler) obj = [0] async def routineLock(key): l = Lock(key, rc.scheduler) await l.lock(rc) t = obj[0] await rc.do_events() obj[0] = t + 1 l.unlock() rc.subroutine(routineLock('testobj')) rc.subroutine(routineLock('testobj')) self.server.serve() self.assertEqual(obj[0], 2)
class DHCPServer(FlowBase): "Send ARP respond" _tablerequest = (("l3input", ('l2input',), ''), ("l2output", ("l3input",), '')) _default_serveraddress = '169.254.169.254' _default_servermac = '1a:23:67:59:63:33' _default_leasetime = None _default_t1 = None _default_t2 = None def __init__(self, server): FlowBase.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._main self.routines.append(self.apiroutine) self._flowupdaters = {} self._extra_arps = {} def _main(self): flow_init = FlowInitialize.createMatcher(_ismatch = lambda x: self.vhostbind is None or x.vhost in self.vhostbind) conn_down = OpenflowConnectionStateEvent.createMatcher(state = OpenflowConnectionStateEvent.CONNECTION_DOWN, _ismatch = lambda x: self.vhostbind is None or x.createby.vhost in self.vhostbind) while True: yield (flow_init, conn_down) if self.apiroutine.matcher is flow_init: c = self.apiroutine.event.connection self.apiroutine.subroutine(self._init_conn(self.apiroutine.event.connection)) else: c = self.apiroutine.event.connection self.apiroutine.subroutine(self._remove_conn(c)) def _init_conn(self, conn): # Default if conn in self._flowupdaters: updater = self._flowupdaters.pop(conn) updater.close() updater = DHCPUpdater(conn, self) #flowupdater = VXLANFlowUpdater(conn, self) self._flowupdaters[conn] = updater updater.start() if False: yield def _remove_conn(self, conn): # Do not need to modify flows if conn in self._flowupdaters: self._flowupdaters.pop(conn).close() if False: yield
def __init__(self, connection, initialkeys, requestid = None, logger = None): RoutineContainer.__init__(self, connection.scheduler) self._initialkeys = initialkeys self._connection = connection self._walkerdict = {} self._savedkeys = () self._savedresult = [] self._updatedset = set() self._updatedset2 = set() if not logger: self._logger = logging.getLogger(__name__ + '.FlowUpdater') else: self._logger = logger if requestid is None: self._requstid = str(uuid1()) else: self._requstid = requestid self._dataupdateroutine = None self._flowupdateroutine = None
def __init__(self, worker_start, matchers=(), scheduler=None, mp=True, inputlimit=0, allowcontrol=True): """ :param worker_start: func(queuein, queueout), queuein is the input queue, queueout is the output queue. For queuein, each object is (event, matcher) tuple; For queueout, each object is a tuple of events to send. Every object in queuein must have a response in queueout. :param matcheres: match events to be processed by connector. :param scheduler: bind to specified scheduler :param mp: use multiprocessing if possible. For windows, multi-threading is always used. :param inputlimit: input queue size limit. 0 = infinite. :param allowcontrol: if True, the connector accepts ConnectorControlEvent for connector configuration. """ RoutineContainer.__init__(self, scheduler, True) self.worker_start = worker_start self.matchers = set(matchers) self.mp = mp self.inputlimit = inputlimit self.allowcontrol = allowcontrol self.stopreceive = False self.jobs = 0 self._moreresult_matcher = MoreResultEvent.createMatcher()
def testServerClientSsl(self): c1 = Client('ssl://localhost:199', self.protocolClient, self.scheduler, 'testcerts/client.key', 'testcerts/client.crt', 'testcerts/root.crt') s1 = TcpServer('lssl://localhost:199', self.protocolServer, self.scheduler, 'testcerts/server.key', 'testcerts/server.crt', 'testcerts/root.crt') r = RoutineContainer(self.scheduler, True) ret = bytearray() def mainA(): m = TestDataEvent.createMatcher() stopped = False while True: yield (m,) if r.event.connection is c1: ret.extend(b'B') else: ret.extend(b'A') if not stopped: for m in s1.shutdown(): yield m stopped = True r.main = mainA r.start() s1.start() def waitAndStart(c): for m in r.waitWithTimeout(0.5): yield m c.start() r.subroutine(waitAndStart(c1)) self.scheduler.main() self.assertEqual(ret, b'ABABABABABABABABABAB')
def testBlock(self): scheduler = Scheduler() scheduler.queue.addSubQueue(10, RoutineControlEvent.createMatcher()) scheduler.queue.addSubQueue(1, TestConsumerEvent.createMatcher(), 'consumer', 5, 5) rA = RoutineContainer(scheduler) output = bytearray() def mainA(): rA.subroutine(mainB(), daemon = True) for i in range(0,10): for ms in rA.waitForSend(TestConsumerEvent(rA.mainroutine)): yield ms output.extend(b'A') def mainB(): for m in rA.doEvents(): yield m matcher = TestConsumerEvent.createMatcher(producer=rA.mainroutine) while True: yield (matcher,) rA.event.canignore = True output.extend(b'B') def mainC(): for m in rA.doEvents(): yield m output.extend(b'C') rA.main = mainA rA.start() rA.subroutine(mainC()) scheduler.main() self.assertEqual(output, b'AAAAACBABABABABABBBBB')
def testSelfConnectionUnixDgram(self): if not hasattr(socket, 'AF_UNIX'): print('Skip UNIX socket test because not supported') return try: os.remove('/var/run/unixsocktestudp1.sock') except: pass try: os.remove('/var/run/unixsocktestudp2.sock') except: pass c1 = Client('dunix:/var/run/unixsocktestudp2.sock', self.protocolClient, self.scheduler, bindaddress = ((socket.AF_UNIX, '/var/run/unixsocktestudp1.sock'),)) c2 = Client('pdunix:/var/run/unixsocktestudp2.sock', self.protocolServer, self.scheduler) r = RoutineContainer(self.scheduler, True) ret = bytearray() def mainA(): m = TestDataEvent.createMatcher() while True: yield (m,) if r.event.connection is c2: ret.extend(b'A') else: ret.extend(b'B') r.main = mainA r.start() def waitAndStart(c): for m in r.waitWithTimeout(0.5): yield m c.start() r.subroutine(waitAndStart(c1)) c2.start() self.scheduler.main() self.assertEqual(ret, b'ABABABABABABABABABAB')
def testCAVerify3(self): c1 = Client('ssl://localhost:199', self.protocolClient, self.scheduler, None, None, 'testcerts/root.crt') c2 = Client('pssl://localhost:199', self.protocolServer, self.scheduler, 'testcerts/server.key','testcerts/server.crt','testcerts/root.crt') r = RoutineContainer(self.scheduler, True) ret = bytearray() def mainA(): m = TestDataEvent.createMatcher() while True: yield (m,) if r.event.connection is c2: ret.extend(b'A') else: ret.extend(b'B') self.notconnected = False def notConnected(connection): if connection is c1: self.notconnected = True if False: yield self.protocolClient.notconnected = notConnected r.main = mainA r.start() def waitAndStart(c): for m in r.waitWithTimeout(0.5): yield m c.start() r.subroutine(waitAndStart(c1)) c2.start() self.scheduler.main() self.assertTrue(self.notconnected) self.assertEqual(ret, b'')
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._manage_ports self.routines.append(self.apiroutine) self.managed_ports = {} self.createAPI(api(self.getports, self.apiroutine), api(self.getallports, self.apiroutine), api(self.getportbyno, self.apiroutine), api(self.waitportbyno, self.apiroutine), api(self.getportbyname, self.apiroutine), api(self.waitportbyname, self.apiroutine), api(self.resync, self.apiroutine) ) self._synchronized = False
def __init__(self, server): """ Constructor """ Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._autoreload self._lastcheck = time() if self.autoreload: self.routines.append(self.apiroutine) self.createAPI( api(self.enableAutoReload), api(self.activeModules), api(self.reloadmodules, self.apiroutine), api(self.loadmodule, self.apiroutine), api(self.unloadmodule, self.apiroutine), )
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._main self.routines.append(self.apiroutine) self._reqid = 0 self._ownerid = uuid1().hex self.createAPI(api(self.createlogicalnetwork, self.apiroutine), api(self.createlogicalnetworks, self.apiroutine), api(self.createphysicalnetwork, self.apiroutine), api(self.createphysicalnetworks, self.apiroutine), api(self.createphysicalport, self.apiroutine), api(self.createphysicalports, self.apiroutine), api(self.createlogicalport, self.apiroutine), api(self.createlogicalports, self.apiroutine), api(self.getlogicalnetworks, self.apiroutine)) self._logger.setLevel(logging.DEBUG)
def testTimer(self): scheduler = Scheduler() rA = RoutineContainer(scheduler) output = bytearray() def wait(timeout, append): for m in rA.waitWithTimeout(timeout): yield m output.extend(append) rA.subroutine(wait(0.1, b'B')) rA.subroutine(wait(0.5, b'D')) rA.subroutine(wait(0, b'A')) rA.subroutine(wait(0.2, b'C')) curr_time = time() scheduler.main() end_time = time() self.assertEqual(output, b'ABCD') self.assertTrue(0.4 < end_time - curr_time < 0.6)
def __init__(self, server): Module.__init__(self, server) self.apiroutine = RoutineContainer(self.scheduler) self.apiroutine.main = self._manage_ports self.routines.append(self.apiroutine) self.managed_ports = {} self.managed_ids = {} self.monitor_routines = set() self.ports_uuids = {} self.wait_portnos = {} self.wait_names = {} self.wait_ids = {} self.bridge_datapathid = {} self.createAPI(api(self.getports, self.apiroutine), api(self.getallports, self.apiroutine), api(self.getportbyid, self.apiroutine), api(self.waitportbyid, self.apiroutine), api(self.getportbyname, self.apiroutine), api(self.waitportbyname, self.apiroutine), api(self.getportbyno, self.apiroutine), api(self.waitportbyno, self.apiroutine), api(self.resync, self.apiroutine) ) self._synchronized = False