def setHigherProtocol(self, higherProtocol): self._higherProtocol = higherProtocol def connection_made(self, transport): self.higherProtocol().connection_made(transport) def data_received(self, data): self.higherProtocol().data_received(data) def connection_lost(self): self.higherProtocol().connection_lost(None) logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() loop.set_debug(1) f = StackingProtocolFactory(lambda: PassThrough1(), lambda: PassThrough2()) ptConnector = playground.Connector(protocolStack=f) playground.setConnector("passthrough", ptConnector) coro = connect.getConnector("passthrough").create_playground_server( lambda: CoinServerProtocol(), 8080) server = loop.run_until_complete(coro) try: loop.run_forever() except KeyboardInterrupt: pass server.close() loop.run_until_complete(server.wait_closed()) loop.close()
class ClientControl: def __init__(self): self.txProtocol = None def buildProtocol(self): return CoinClientProtocol(None) def connect(self, txProtocol): self.txProtocol = txProtocol print("Client connection to server established.") def send(self, data): self.txProtocol.send(data) logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() loop.set_debug(1) control = ClientControl() testData = RequestFlip() testData.headOrTail = True testData.numFlips = 1000 coro = connect.getConnector().create_playground_connection( CoinClientProtocol, '20174.1.1.1', 8080) transport, protocol = loop.run_until_complete(coro) control.connect(protocol) asyncio.sleep(3) control.send(testData) loop.run_forever() loop.close()
super().__init__() def connection_made(self, transport): self.transport = transport higher_transport = StackingTransport(self.transport) self.higherProtocol().connection_made(higher_transport) def data_received(self, data): print("Server: Data passes up PassThroughLayer2.") self.higherProtocol().data_received(data) def connection_lost(self, exc): self.transport = None self.higherProtocol().connection_lost() if __name__ == "__main__": loop = get_event_loop() loop.set_debug(enabled=True) factory = StackingProtocolFactory(lambda: PassThroughLayer1(), lambda: PassThroughLayer2()) ptConnector = Connector(protocolStack=factory) setConnector("passThrough", ptConnector) coro = getConnector("passThrough").create_playground_server( lambda: ServerProtocol(), 101) server = loop.run_until_complete(coro) print("Server Started at {}".format(server.sockets[0].gethostname())) loop.run_forever() loop.close()
self.transport = transport higher_transport = StackingTransport(self.transport) self.higherProtocol().connection_made(higher_transport) def data_received(self, data): print("Client: Data passes up PassThroughLayer2.") self.higherProtocol().data_received(data) def connection_lost(self, exc): self.transport = None self.higherProtocol().connection_lost() if __name__ == "__main__": loop = get_event_loop() loop.set_debug(enabled=True) factory = StackingProtocolFactory(lambda: PassThroughLayer1(), lambda: PassThroughLayer2()) ptConnector = Connector(protocolStack=factory) setConnector("passThrough", ptConnector) remote_address = "20174.1.1.1" coro = getConnector("passThrough").create_playground_connection( lambda: ClientProtocol("harry", "123456", "*****@*****.**"), remote_address, 101) transport, client = loop.run_until_complete(coro) print("Client Connected. Starting UI t:{}. p:{}.".format( transport, client)) loop.run_forever() loop.close()
rand = random.randint(0,1) if rand: tails += 1 winner_record["TAIL"] += 1 else: heads +=1 winner_record["HEAD"] +=1 winner_record["FLIPS"]+=1 if tails>heads: winner_record["FACE"] = Face.TAIL return (Face.TAIL, tails) elif heads>tails: winner_record["FACE"] = Face.HEAD return (Face.HEAD, heads) else: winner_record["FACE"] = Face.TIE return (Face.TIE, heads) logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() loop.set_debug(1) coro = connect.getConnector().create_playground_server(CoinServerProtocol,8080) server = loop.run_until_complete(coro) try: loop.run_forever() except KeyboardInterrupt: pass server.close() loop.run_until_complete(server.wait_closed()) loop.close()
self._higherProtocol = higherProtocol def connection_made(self, transport): self.higherProtocol().connection_made(transport) def data_received(self, data): self.higherProtocol().data_received(data) def connection_lost(self): self.higherProtocol().connection_lost(None) logging.basicConfig(level=logging.DEBUG) loop = asyncio.get_event_loop() loop.set_debug(1) control = ClientControl() testData = RequestFlip() testData.headOrTail = True testData.numFlips = 1000 f = StackingProtocolFactory(lambda: PassThrough1(), lambda: PassThrough2()) ptConnector = playground.Connector(protocolStack=f) playground.setConnector("passthrough", ptConnector) coro = connect.getConnector("passthrough").create_playground_connection( lambda: CoinClientProtocol(), '20174.1.1.1', 8080) transport, protocol = loop.run_until_complete(coro) control.connect(protocol) asyncio.sleep(3) control.send(testData) loop.run_forever() loop.close()