def initialize(readSize=256): hubOne = Router(timeout=TIMEOUT, readSize=readSize, findOpenPort=True) hubTwo = Router(timeout=TIMEOUT, readSize=readSize, findOpenPort=True) hubOne.connect(TEST, HOME, hubTwo.port) while len(hubOne.transports) == 0 or len(hubTwo.transports) == 0: pass while not hubOne.transports[0].opened or not hubTwo.transports[0].opened: pass time.sleep(0.1) return hubOne, hubTwo
def getVideo(): hub = Router(port=8081) print('Started Router on port', hub.port) while hub.transports == []: pass transport = hub.transports[0] print('Starting capture') while transport.getImage() is None: pass while True: key = cv2.waitKey(1) & 0xFF if key == ord('q'): cv2.destroyAllWindows() hub.close() break cv2.imshow('Stream', transport.getImage())
def multiRouterStresstest(self, stress=5, messages=1000): hubs = [] for i in range(stress): hub = Router(timeout=TIMEOUT) hubs.append(hub) for hubOne in hubs: for hubTwo in hubs: hubTwo.connect('Test', HOME, hubOne.port) for hub in hubs: for transport in hub.transports: while not transport.opened: pass while len(hub.transports) != 2 * stress: pass for hub in hubs: self.assertEqual(len(hub.transports), 2 * stress) for hub in hubs: for i in range(messages): hub.writeAll('Test{}'.format(i), TEXT) for hub in hubs: while hub.getAll( 'Test{}'.format(messages - 1)) != [TEXT] * 2 * stress: pass for hub in hubs: for i in range(messages): self.assertEqual(hub.getAll('Test{}'.format(i)), [TEXT] * 2 * stress) for hub in hubs: hub.close()
def sendVideo(): hub = Router(port=8080) print('Started Router on port', hub.port) hub.connect('video', '127.0.0.1', 8081) print('Connected!') sleep(2) transport = hub.transports[0] cam = cv2.VideoCapture(2) while True: _, frame = cam.read() if not COLOR: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = cv2.resize(frame, SIZE) cv2.imshow('Sending', frame) key = cv2.waitKey(1) & 0xFF if key == ord('q'): cv2.destroyAllWindows() hub.close() break transport.writeImage(frame)
def multiRouterStresstest(self, stress=5, messages=1000): routers = [] for i in range(stress): router = Router(timeout=TIMEOUT) routers.append(router) for routerOne in routers: for routerTwo in routers: routerTwo.connect('Test', HOME, routerOne.port) for router in routers: router.waitForAllReady() self.assertEqual(len(router.transports), 2 * stress) for router in routers: for i in range(messages): router.writeAll('Test{}'.format(i), TEXT) for router in routers: router.waitForGetAll('Test{}'.format(messages - 1)) for i in range(messages): self.assertEqual(router.getAll('Test{}'.format(i)), [TEXT] * 2 * stress) for router in routers: router.close()
def stressTest(self, stress=5, messages=1000): routerOne = Router(timeout=TIMEOUT) routerTwo = Router(timeout=TIMEOUT) for i in range(stress): if i % 2 == 0: routerOne.connect('Test{}'.format(i), HOME, routerTwo.port) else: routerTwo.connect('Test{}'.format(i), HOME, routerOne.port) routerOne.waitForAllReady() routerTwo.waitForAllReady() self.assertEqual(len(routerOne.transports), len(routerTwo.transports)) for i in range(messages): routerOne.writeAllWhenReady('Test{}'.format(i), TEXT) routerTwo.writeAllWhenReady('Test{}'.format(i), TEXT) for i in range(messages): time.sleep(0.01) routerOne.waitForGetAll('Test{}'.format(i)) routerTwo.waitForGetAll('Test{}'.format(i)) for i in range(messages): self.assertEqual(routerOne.getAll('Test{}'.format(i)), [TEXT] * stress) self.assertEqual(routerTwo.getAll('Test{}'.format(i)), [TEXT] * stress) routerOne.close() routerTwo.close()
def multiTransports(self, numConn=10, messages=10, bidirectional=False, size=256): routerOne = Router(timeout=TIMEOUT, size=size) routerTwo = Router(timeout=TIMEOUT, size=size) for i in range(numConn): routerOne.connect('Test{}'.format(i), HOME, routerTwo.port) routerOne.waitForAllReady() routerTwo.waitForAllReady() for i in range(messages): routerOne.writeAllWhenReady('Test{}'.format(i), TEXT) if bidirectional: routerTwo.writeAllWhenReady('Test{}'.format(i), TEXT) routerTwo.waitForGetAll('Test{}'.format(messages - 1)) if bidirectional: routerOne.waitForGetAll('Test{}'.format(messages - 1)) for i in range(messages): if bidirectional: self.assertEqual(routerOne.getAll('Test{}'.format(i)), [TEXT] * numConn) self.assertEqual(routerTwo.getAll('Test{}'.format(i)), [TEXT] * numConn) routerOne.close() routerTwo.close()
def multiTransports(self, numConn=10, messages=10, bidirectional=False, size=256): hubOne = Router(timeout=TIMEOUT, size=size) hubTwo = Router(timeout=TIMEOUT, size=size) for i in range(numConn): hubOne.connect('Test{}'.format(i), HOME, hubTwo.port) while len(hubOne.transports) != len(hubTwo.transports): pass for transport in hubOne.transports: while not transport.opened: pass for transport in hubTwo.transports: while not transport.opened: pass for i in range(messages): hubOne.writeAll('Test{}'.format(i), TEXT) if bidirectional: hubTwo.writeAll('Test{}'.format(i), TEXT) for i in range(messages): while hubTwo.getAll('Test{}'.format(i)) != [TEXT] * numConn: time.sleep(0.1) if bidirectional: while hubOne.getAll('Test{}'.format(i)) != [TEXT] * numConn: time.sleep(0.1) for i in range(messages): if bidirectional: self.assertEqual(hubOne.getAll('Test{}'.format(i)), [TEXT] * numConn) self.assertEqual(hubTwo.getAll('Test{}'.format(i)), [TEXT] * numConn) hubOne.close() hubTwo.close()
def stressTest(self, stress=5, messages=1000): hubOne = Router(timeout=TIMEOUT) hubTwo = Router(timeout=TIMEOUT) for i in range(stress): if i % 2 == 0: hubOne.connect('Test{}'.format(i), HOME, hubTwo.port) else: hubTwo.connect('Test{}'.format(i), HOME, hubOne.port) while len(hubOne.transports) != len(hubTwo.transports): pass for transport in hubOne.transports: while not transport.opened: pass for transport in hubTwo.transports: while not transport.opened: pass self.assertEqual(len(hubOne.transports), len(hubTwo.transports)) for i in range(messages): hubOne.writeAll('Test{}'.format(i), TEXT) hubTwo.writeAll('Test{}'.format(i), TEXT) while (hubOne.getAll('Test{}'.format(messages - 1)) != [TEXT] * stress or hubTwo.getAll( 'Test{}'.format(messages - 1)) != [TEXT] * stress): pass for i in range(messages): self.assertEqual(hubOne.getAll('Test{}'.format(i)), [TEXT] * stress) self.assertEqual(hubTwo.getAll('Test{}'.format(i)), [TEXT] * stress) hubOne.close() hubTwo.close()
def testOnewayMessages(self): start() routerOne = Router(timeout=TIMEOUT, findOpenPort=True) routerTwo = Router(timeout=TIMEOUT, findOpenPort=True) self.assertEqual(routerOne.getAll(TEST), []) self.assertEqual(routerOne.getAll(TEST_2), []) self.assertEqual(routerTwo.getAll(TEST), []) self.assertEqual(routerTwo.getAll(TEST_2), []) routerOne.connect(TEST, HOME, routerTwo.port) routerTwo.waitForTransport() routerOne.writeToNameWhenReady(TEST, TEST, PORT_TEST) routerTwo.waitForGetAll(TEST) self.assertEqual(routerOne.getAll(TEST), []) self.assertEqual(routerOne.getAll(TEST_2), []) self.assertEqual(routerTwo.getAll(TEST), [PORT_TEST]) self.assertEqual(routerTwo.getAll(TEST_2), []) routerOne.writeToNameWhenReady(TEST, TEST_2, PORT_TEST_2) routerTwo.waitForGetAll(TEST_2) self.assertEqual(routerOne.getAll(TEST), []) self.assertEqual(routerOne.getAll(TEST_2), []) self.assertEqual(routerTwo.getAll(TEST), [PORT_TEST]) self.assertEqual(routerTwo.getAll(TEST_2), [PORT_TEST_2]) routerOne.close() routerTwo.close() finish('One-way messages Test Passed')
def testEmptyMessages(self): start() routerOne = Router(timeout=TIMEOUT, findOpenPort=True) routerTwo = Router(timeout=TIMEOUT, findOpenPort=True) self.assertEqual(routerOne.getAll(TEST), []) self.assertEqual(routerOne.getAll(TEST_2), []) self.assertEqual(routerTwo.getAll(TEST), []) self.assertEqual(routerTwo.getAll(TEST_2), []) routerOne.connect(TEST, HOME, routerTwo.port) routerTwo.waitForTransport() self.assertEqual(routerOne.getAll(TEST), []) self.assertEqual(routerOne.getAll(TEST_2), []) self.assertEqual(routerTwo.getAll(TEST), []) self.assertEqual(routerTwo.getAll(TEST_2), []) routerOne.close() routerTwo.close() finish('Empty messages Test Passed')
def testTransportSetup(self): start() routerOne = Router(timeout=TIMEOUT, findOpenPort=True) routerTwo = Router(timeout=TIMEOUT, findOpenPort=True) routerOne.connect(CHANNEL, HOME, routerTwo.port) routerOne.waitForTransport() routerTwo.waitForTransport() self.assertEqual(len(routerOne.transports), 1) self.assertEqual(len(routerTwo.transports), 1) transportOne = routerOne.transports[0] transportTwo = routerTwo.transports[0] transportOne.waitForReady() transportTwo.waitForReady() self.assertTrue(transportOne.opened) self.assertFalse(transportOne.stopped) self.assertTrue(transportTwo.opened) self.assertFalse(transportTwo.stopped) transportTwo.waitForName() self.assertEqual(transportOne.name, CHANNEL) self.assertEqual(transportTwo.name, CHANNEL) self.assertEqual(transportOne.addr, HOME) self.assertEqual(transportTwo.addr, HOME) routerOne.close() routerTwo.close() self.assertFalse(transportOne.opened) self.assertTrue(transportOne.stopped) self.assertFalse(transportTwo.opened) self.assertTrue(transportTwo.stopped) finish('Connection Setup Test Passed')
def testRouterTransportSetup(self): start() routerOne = Router(timeout=TIMEOUT, findOpenPort=True) routerTwo = Router(timeout=TIMEOUT, findOpenPort=True) self.assertTrue(routerOne.opened) self.assertFalse(routerOne.stopped) self.assertTrue(routerTwo.opened) self.assertFalse(routerTwo.stopped) routerOne.connect(CHANNEL, HOME, routerTwo.port) routerOne.waitForTransport() routerTwo.waitForTransport() self.assertEqual(len(routerOne.transports), 1) self.assertEqual(len(routerTwo.transports), 1) routerOne.close() routerTwo.close() self.assertFalse(routerOne.opened) self.assertTrue(routerOne.stopped) self.assertFalse(routerTwo.opened) self.assertTrue(routerTwo.stopped) finish('Router Connection Setup Test Passed')