2

我试图理解 Python 中的两个函数。它来自 ryu 开源控制器的代码。我怀疑它以某种方式实现了 LLDP 并尝试存储拓扑信息。我不知道是什么

 @set_ev_cls(event.EventSwitchRequest)

@set_ev_cls(event.EventLinkRequest)

我不知道上面的语句在 Python 中是什么意思。如果有人能解释一下意思就好了。整个文件都在这里给出。链接到python源文件

 @set_ev_cls(event.EventSwitchRequest)
    def switch_request_handler(self, req):
        # LOG.debug(req)
        dpid = req.dpid

        switches = []
        if dpid is None:
            # reply all list
            for dp in self.dps.itervalues():
                switches.append(self._get_switch(dp.id))
        elif dpid in self.dps:
            switches.append(self._get_switch(dpid))

        rep = event.EventSwitchReply(req.src, switches)
        self.reply_to_request(req, rep)

    @set_ev_cls(event.EventLinkRequest)
    def link_request_handler(self, req):
        # LOG.debug(req)
        dpid = req.dpid

        if dpid is None:
            links = self.links
        else:
            links = [link for link in self.links if link.src.dpid == dpid]
        rep = event.EventLinkReply(req.src, dpid, links)
        self.reply_to_request(req, rep)
4

2 回答 2

0

观察事件:Ryu 应用程序可以通过使用 ryu.controller.handler.set_ev_cls 装饰器提供处理程序方法来注册其对特定类型事件的兴趣。

访问:http ://ryu.readthedocs.org/en/latest/ryu_app_api.html

于 2014-05-27T01:22:47.880 回答
0

这些@set_event_cls(event.*)行是python 装饰器。有很多资源可以解释它们,所以我不会在这里做。

在这种情况下,它们用于注册网络事件的回调。基本上,set_event_cls装饰器将该函数(例如switch_request_handler)与一个事件类(例如event.EventSwitchRequest,这样当 Ryu 接收到一个EventSwitchRequest,它switch_request_handler使用作为参数传递的请求进行调用req

实际的装饰器函数set_event_cls()handler.py中定义。

于 2015-05-05T06:06:23.933 回答