Ryuのソースコードリーディング Ryuの基本的なクラス
Ryuの中で基本的なクラスに関してまとめてみました。ソースコードを読む際の参考にしてください。
アプリケーションクラス
ryu.base.app_manager.RyuAppを継承したクラスです。
ユーザはryu.controller.handler.set_ev_clsデコレータで修飾されたイベントハンドラを用いて、イベントドリブン型の処理を記述します。
アプリケーションの例
class Hello_world(app_manager.RyuApp): def __init__(self, *args, **kwargs): super(Hello_world, self).__init__(*args, **kwargs) @set_ev_cls(ofp_event.EventOFPEchoRequest, MAIN_DISPATCHER) def hello_handler(self): self.logger.info('Hello world.')
オープンフローメッセージクラス
ryu.ofproto.ofproto_parser.MsgBaseを継承したクラスです。
OpenFlowのバージョンに依存するメッセージもあります。
オープンフローメッセージ例
@_register_parser @_set_msg_type(ofproto_v1_0.OFPT_HELLO) class OFPHello(MsgBase): def __init__(self, datapath): super(OFPHello, self).__init__(datapath)
イベントクラス
すべてのイベントクラスはryu.controller.event.EventBaseを継承しています。
特にオープンフローメッセージクラスに関連するクラスはEventBaseを継承したryu.controller.ofp_event.EventOFPMsgBaseのサブクラスになります。
その場合動的にイベントクラスが生成されるため、ソースコード中に明示的な形では記載されていません。
イベントクラスの例
上のアプリケーションクラスから該当箇所を抜粋しました。Ryuのソースコードに明示的に記載されていない、動的に生成したEventOFPEchoRequestクラスを用いてハンドリングを行っているのが見て取れます。
@set_ev_cls(ofp_event.EventOFPEchoRequest, MAIN_DISPATCHER) def hello_handler(self): self.logger.info('Hello world.')
CONTEXT
RyuのAPIドキュメントではhttp://ryu.readthedocs.org/en/latest/ryu_app_api.htmlによると、
Contexts are ordinary python objects shared among Ryu applications. The use of contexts are discouraged for new code.