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


Python Machine.on_exit_idle方法代码示例

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


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

示例1: Sender

# 需要导入模块: from transitions import Machine [as 别名]
# 或者: from transitions.Machine import on_exit_idle [as 别名]
class Sender(object):
    """
    Class representing the endpoint of the steganographic communication initiating
    the steganographic connection setup.
    """
    config = None
    messenger = None
    transmitter = None
    steganogram = None

    sending_thread = None

    states = [
        State.IDLE,
        State.NOTIFY_SENT,
        State.LISTEN,
        State.AWAIT_REPORT,
        State.FIN
    ]

    def __init__(self, config, messenger, transmitter):
        """
        :param config:          configuration parameters for the sender
        :param messenger:       object responsible for communicating the desire of setting up a covert channel
                                should have:
                                notify() - notifies the other endpoint that is about to start sending stegano packets
                                receive() - listens for and receives confirmations of notify()
        :param transmitter:     object responsible for handling the steganographic communication
        """
        self.config = config
        self.messenger = messenger
        self.transmitter = transmitter

        self._load_config()

        self.triggers = {
            State.IDLE: "send_notify",
            State.NOTIFY_SENT: "recv_notify_ack",
            State.LISTEN: "recv_pkt",
            State.AWAIT_REPORT: "recv_report",
        }

        self.machine = Machine(model=self, states=Sender.states, initial=State.IDLE)

        self.machine.add_transition(trigger=self.triggers[State.IDLE], source=State.IDLE, dest=State.NOTIFY_SENT)
        self.machine.on_exit_idle("notify")

        self.machine.add_transition(trigger=self.triggers[State.NOTIFY_SENT], source=State.NOTIFY_SENT, dest=State.LISTEN,
                                    conditions=["recvd_notify_ack"])
        # TODO: self.machine.on_exit_notify_sent("configure_iptables")

        self.machine.add_transition(trigger=self.triggers[State.LISTEN], source=State.LISTEN, dest=State.AWAIT_REPORT,
                                    conditions=["sent_steg"])

        self.machine.add_transition(trigger=self.triggers[State.AWAIT_REPORT], source=State.AWAIT_REPORT, dest=State.FIN,
                                    conditions=["recvd_report"])
        self.machine.on_exit_await_report("send_report_ack")

    def run(self):
        self.sending_thread = threading.Thread(name="SendThread", target=self._nfqueue_send)
        self.sending_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 notify(self):
        print "Sending {}".format(Messages.NOTIFY)
        self.messenger.notify(Messages.NOTIFY)

    def recvd_notify_ack(self):
        if self.messenger.receive() == Messages.NOTIFY_ACK:
            print "Received {}. Will embed steganogram in next packet".format(Messages.NOTIFY_ACK)
            return True
        return False

    def sent_steg(self):
        if self.steganogram is None:
            print "Steganogram embedded. Waiting for {}".format(Messages.REPORT)
            return True
        return False

    def recvd_report(self):
        if self.messenger.receive() == Messages.REPORT:
            print "Received {}".format(Messages.REPORT)
            return True
        return False

    def send_report_ack(self):
        print "Sending {}. Ending conversation".format(Messages.REPORT_ACK)
        self.messenger.notify(Messages.REPORT_ACK)

    def _nfqueue_send(self):
        self.nfqueue = NetfilterQueue()
        self.nfqueue.bind(self.send_queue_num, self._handle_outgoing_packets)
        self.nfqueue.run()

    def _handle_outgoing_packets(self, pkt):
#.........这里部分代码省略.........
开发者ID:krawczyk-m,项目名称:msc,代码行数:103,代码来源:sender.py


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