def testEndPointConnectDoesWhatItSaysOnTheBox(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); for i in range(0, 100): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(endPoint[0].isConnected()); self.assertTrue(endPoint[1].isConnected());
def handle(self, header, payload): if header == FrameReceiver.SYN_DISCONNECT(): pass elif header == FrameReceiver.SYN_REQUEST(): self.outer.enterState(self.outer.syncResponseSent) else: self.sendSyn = True
def __init__(self, tty, switchEventQueue): ''' Constructor ''' self.id = None self.log = logging.getLogger("EndPoint[{}]".format(self.id)) self.switchEventQueue = switchEventQueue escapingSink = EscapingSink(TtySink(tty)) escapingSource = EscapingSource(TtySource(tty)) self.outgoingFrameBuffer = deque() frameTransmitter = FrameTransmitter(escapingSink) frameReceiver = FrameReceiver(escapingSource) self.incomingFrameHandler = IncomingFrameHandler() self.endPoint = EndPoint(escapingSource, frameReceiver, self.incomingFrameHandler, self.outgoingFrameBuffer, frameTransmitter, escapingSink) frameReceiver.setFrameHandler(self.endPoint) self.incomingFrameHandler.frameNotifier.addObserver(self) self.pingPong = PingPong(self.outgoingFrameBuffer) self.incomingFrameHandler.frameNotifier.addObserver(self.pingPong) self.hardwareRuleManager = HardwareRuleManager( self.outgoingFrameBuffer) self.incomingFrameHandler.frameNotifier.addObserver( self.hardwareRuleManager)
def testEndPointConnectDoesNothingWhileDisconnected(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) outgoingFrameBuffer[0] += [[0x42]] for i in range(0, 100): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(0 == len(userFrameHandler[0].payloads)); self.assertTrue(0 == len(userFrameHandler[1].payloads));
def testEndPointIsBornDisconnected(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) self.assertFalse(endPoint[0].isConnected())
def handle(self, header, payload): if header == FrameReceiver.SYN_DISCONNECT(): self.outer.enterState(self.outer.disconnected) elif header == FrameReceiver.SYN_REQUEST(): self.outer.enterState(self.outer.syncResponseSent) elif header == FrameReceiver.SYN_RESPONSE(): self.outer.connected.sendSyn = True self.outer.enterState(self.outer.connected) else: self.sendSyn = True
def testEndPointIgnoresFramesWithUnexpectedSequenceNumber(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] endPoint[0].connected.timeout = 0x0000000F endPoint[1].connected.timeout = 0x0000000F frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); self.assertTrue(0 == len(userFrameHandler[0].payloads)); self.assertTrue(0 == len(userFrameHandler[1].payloads)); mediumAB += [EscapingSource.FLAG()] crc = 0xFFFF mediumAB += [0x00 ^ 0x01] crc = crc_ccitt_update(crc, 0x00 ^ 0x01) mediumAB += [0x42] crc = crc_ccitt_update(crc, 0x42) mediumAB += [crc >> 8] mediumAB += [crc & 0xFF] mediumAB += [EscapingSource.FLAG()] while len(mediumAB) > 0: endPoint[1].schedule() self.assertTrue(len(userFrameHandler[1].payloads) == 0)
def testEndPointMovesOneHundredFramesInEachDirectionWithNoise(self): seed(1) mediumAB = deque() mediumBA = deque() writerA = MockSink(mediumAB) sinkA = NoisySink(writerA) writerB = MockSink(mediumBA) sinkB = NoisySink(writerB) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); frameCount = 100 for i in range(0, frameCount): outgoingFrameBuffer[0] += [[i]] outgoingFrameBuffer[1] += [[i]] while len(userFrameHandler[0].payloads) < frameCount or len(userFrameHandler[1].payloads) < frameCount: endPoint[0].schedule() endPoint[1].schedule() for i in range(0, frameCount): self.assertEqual([i], userFrameHandler[0].payloads[i]) self.assertEqual([i], userFrameHandler[1].payloads[i])
def testEndPointMovesOneFrameInEachDirection(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); outgoingFrameBuffer[0] += [[0x42]] outgoingFrameBuffer[1] += [[0x43]] for i in range(0, 100): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(0 == len(outgoingFrameBuffer[0])) self.assertTrue(1 == len(userFrameHandler[1].payloads)) self.assertEqual([0x42], userFrameHandler[1].payloads[0]) self.assertTrue(0 == len(outgoingFrameBuffer[1])) self.assertTrue(1 == len(userFrameHandler[0].payloads)) self.assertEqual([0x43], userFrameHandler[0].payloads[0])
def testEndPointMovesSeventyFrames(self): mediumAB = deque() mediumBA = deque() sinkA = MockSink(mediumAB) sinkB = MockSink(mediumBA) readerA = MockSource(mediumBA) readerB = MockSource(mediumAB) escapingSink = [EscapingSink(sinkA), EscapingSink(sinkB)] escapingSource = [EscapingSource(readerA), EscapingSource(readerB)] outgoingFrameBuffer = [deque(), deque()] frameTransmitter = [FrameTransmitter(escapingSink[0]), FrameTransmitter(escapingSink[1])] userFrameHandler = [FrameHandler(), FrameHandler()] frameReceiver = [FrameReceiver(escapingSource[0]), FrameReceiver(escapingSource[1])] endPoint = [ EndPoint(escapingSource[0], frameReceiver[0], userFrameHandler[0], outgoingFrameBuffer[0], frameTransmitter[0], escapingSink[0]), EndPoint(escapingSource[1], frameReceiver[1], userFrameHandler[1], outgoingFrameBuffer[1], frameTransmitter[1], escapingSink[1]) ] frameReceiver[0].setFrameHandler(endPoint[0]) frameReceiver[1].setFrameHandler(endPoint[1]) endPoint[0].connect(); while not endPoint[1].isConnected(): endPoint[0].schedule(); endPoint[1].schedule(); frameCount = 70; for i in range(0, frameCount) : outgoingFrameBuffer[0] += [[0x42 + i]]; for i in range(0, 1600): endPoint[0].schedule() endPoint[1].schedule() self.assertTrue(0 == len(outgoingFrameBuffer[0])) self.assertTrue(frameCount == len(userFrameHandler[1].payloads)) for i in range(0, frameCount): self.assertEqual([0x42 + i], userFrameHandler[1].payloads[i])
def testFrameReceiverIgnoresFrameWithBadCrc(self): medium = deque() medium += [EscapingSource.FLAG()] crc = 0xFFFF medium += [0x00] crc = crc_ccitt_update(crc, 0x00) medium += [0x42] crc = crc_ccitt_update(crc, 0x42) crc ^= 0x01 medium += [crc >> 8] medium += [crc & 0xFF] medium += [EscapingSource.FLAG()] source = EscapingSource(MockSource(medium)) frameHandler = FrameHandler() frameReceiver = FrameReceiver(source) frameReceiver.setFrameHandler(frameHandler) source.schedule() while (source.isReady()): frameReceiver.schedule() source.schedule() self.assertFalse(frameHandler.hasFrame())
def testFrameReceiverReceivesFrame(self): medium = deque() medium += [EscapingSource.FLAG()] crc = 0xFFFF medium += [0x00] crc = crc_ccitt_update(crc, 0x00) medium += [0x42] crc = crc_ccitt_update(crc, 0x42) medium += [crc >> 8] medium += [crc & 0xFF] medium += [EscapingSource.FLAG()] source = EscapingSource(MockSource(medium)) frameHandler = FrameHandler() frameReceiver = FrameReceiver(source) frameReceiver.setFrameHandler(frameHandler) source.schedule() while (source.isReady()): frameReceiver.schedule() source.schedule() self.assertEqual(0x00, frameHandler.header) self.assertEqual([0x42], frameHandler.payload)
def testFrameReceiverReceivesMultipleFrames(self): medium = deque() count = 7 for i in range(0, count): medium += [EscapingSource.FLAG()] crc = 0xFFFF medium += [0x00 + i] crc = crc_ccitt_update(crc, 0x00 + i) medium += [i] crc = crc_ccitt_update(crc, i) medium += [crc >> 8] medium += [crc & 0xFF] medium += [EscapingSource.FLAG()] source = EscapingSource(MockSource(medium)) frameHandler = FrameHandler() frameReceiver = FrameReceiver(source) frameReceiver.setFrameHandler(frameHandler) source.schedule() while (source.isReady()): frameReceiver.schedule() source.schedule() self.assertEqual(count, frameHandler.count)
def testFrameReceiverRecognizesAckFrames(self): medium = deque() medium += [EscapingSource.FLAG()] crc = 0xFFFF medium += [FrameReceiver.ACK() + 0x24] crc = crc_ccitt_update(crc, FrameReceiver.ACK() + 0x24) medium += [crc >> 8] medium += [crc & 0xFF] medium += [EscapingSource.FLAG()] source = EscapingSource(MockSource(medium)) frameHandler = FrameHandler() frameReceiver = FrameReceiver(source) frameReceiver.setFrameHandler(frameHandler) source.schedule() while (source.isReady()): frameReceiver.schedule() source.schedule() self.assertEqual(FrameReceiver.ACK() + 0x24, frameHandler.header) self.assertEqual([], frameHandler.payload)
def testFrameReceiverIgnoresEmptyFrames(self): medium = deque() medium += [EscapingSource.FLAG()] medium += [EscapingSource.FLAG()] source = EscapingSource(MockSource(medium)) frameHandler = FrameHandler() frameReceiver = FrameReceiver(source) frameReceiver.setFrameHandler(frameHandler) source.schedule() while (source.isReady()): frameReceiver.schedule() source.schedule() self.assertFalse(frameHandler.hasFrame())
def handle(self, header, payload): if header == FrameReceiver.SYN_DISCONNECT(): self.outer.enterState(self.outer.disconnected) elif header == FrameReceiver.SYN_REQUEST(): self.outer.enterState(self.outer.syncResponseSent) elif header == FrameReceiver.SYN_RESPONSE(): self.outer.connected.sendSyn = True elif header == FrameReceiver.SYN_COMPLETE(): pass else: if (header & FrameReceiver.CONTROL_BITS()) == FrameReceiver.ACK(): self.lastAckReceived = SequenceNumber(header) self.sendUserFrame = True else: self.sendAck = True if (header == self.expectedSequenceNumber.value): self.expectedSequenceNumber += 1 self.outer.handler.handle(header, payload)