当前位置: 首页>>代码示例>>Python>>正文


Python Machine.on_exit_await_notify方法代码示例

本文整理汇总了Python中transitions.Machine.on_exit_await_notify方法的典型用法代码示例。如果您正苦于以下问题:Python Machine.on_exit_await_notify方法的具体用法?Python Machine.on_exit_await_notify怎么用?Python Machine.on_exit_await_notify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在transitions.Machine的用法示例。


在下文中一共展示了Machine.on_exit_await_notify方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Receiver

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import on_exit_await_notify [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."
#.........这里部分代码省略.........
开发者ID:krawczyk-m,项目名称:msc,代码行数:103,代码来源:receiver.py


注:本文中的transitions.Machine.on_exit_await_notify方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。