本文整理汇总了Python中transitions.Machine.on_exit_listen方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.on_exit_listen方法的具体用法?Python Machine.on_exit_listen怎么用?Python Machine.on_exit_listen使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transitions.Machine
的用法示例。
在下文中一共展示了Machine.on_exit_listen方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Receiver
# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import on_exit_listen [as 别名]
class Receiver(object):
"""
Class representing the endpoint of the steganographic communication waiting for
the steganographic connection setup initiation.
"""
messenger = None
transmitter = None
steganogram = []
listening_thread = None
states = [
State.IDLE,
State.AWAIT_NOTIFY,
State.LISTEN,
State.AWAIT_REPORT_ACK,
State.FIN
]
def __init__(self, config, messenger, transmitter):
"""self._handle_outgoing_packets
:param messenger: object responsible for listening for steganographic connection initiation notificaiton
from another endpoint
should have:
receive() - listens for and responds to notify messages
notify() - for sending acknowledgements
:param transmitter: object responsible for handling the steganographic communication - sending/receiving bits
"""
self.config = config
self.messenger = messenger
self.transmitter = transmitter
self._load_config()
self.triggers = {
State.IDLE: "await_notify",
State.AWAIT_NOTIFY: "recv_notify",
State.LISTEN: "recv_steganogram",
State.AWAIT_REPORT_ACK: "recv_report_ack"
}
self.machine = Machine(model=self, states=Receiver.states, initial=State.IDLE)
self.machine.add_transition(trigger=self.triggers[State.IDLE], source=State.IDLE, dest=State.AWAIT_NOTIFY)
self.machine.add_transition(trigger=self.triggers[State.AWAIT_NOTIFY], source=State.AWAIT_NOTIFY, dest=State.LISTEN,
conditions=["recvd_notify"])
self.machine.on_exit_await_notify("send_notify_ack")
self.machine.add_transition(trigger=self.triggers[State.LISTEN], source=State.LISTEN, dest=State.AWAIT_REPORT_ACK,
conditions=["recvd_steg"])
self.machine.on_exit_listen("send_report")
self.machine.add_transition(trigger=self.triggers[State.AWAIT_REPORT_ACK], source=State.AWAIT_REPORT_ACK,
dest=State.FIN, conditions=["recvd_report_ack"])
def run(self):
self.listening_thread = threading.Thread(name="ListenThread", target=self._nfqueue_receive)
self.listening_thread.start()
while self.state != State.FIN:
print str(self.state)
trigger = self.triggers[self.state]
getattr(self, trigger)()
time.sleep(2) # for presentation purposes on seminar
def recvd_notify(self):
message = self.messenger.receive()
print "Received message: {}".format(message)
return message == Messages.NOTIFY
def send_notify_ack(self):
print "Sending {}".format(Messages.NOTIFY_ACK)
self.messenger.notify(Messages.NOTIFY_ACK)
def recvd_steg(self):
return len(self.steganogram) != 0
# TODO report needs two values - positive and negative
# TODO compare if received steganogram is as expected
def send_report(self):
print "Received steganogram: {}. Sending {}".format(reduce(lambda x, y: str(x) + str(y), self.steganogram, ""),
Messages.REPORT)
self.messenger.notify(Messages.REPORT)
def recvd_report_ack(self):
message = self.messenger.receive()
if message == Messages.REPORT_ACK:
print "Received {}. Ending conversation".format(Messages.REPORT_ACK)
return True
return False
def _nfqueue_receive(self):
self.nfqueue = NetfilterQueue()
self.nfqueue.bind(self.listen_queue_num, self._handle_incoming_packets)
self.nfqueue.run()
def _handle_incoming_packets(self, pkt):
ip_packet = IP(pkt.get_payload())
print "Handling incoming packet."
#.........这里部分代码省略.........