This patch introduces a new option "--with-timestamp" which enable to
get timestamp when Ryu received the OpenFlow message.

Suggested-by: Matthew Hayes <matthew_john_ha...@hotmail.com>
Signed-off-by: IWASE Yusuke <iwase.yusu...@gmail.com>
---
 ryu/cmd/manager.py          |  6 ++++++
 ryu/controller/ofp_event.py | 42 ++++++++++++++++++++++++++++--------------
 2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/ryu/cmd/manager.py b/ryu/cmd/manager.py
index 797d8d5..2647902 100755
--- a/ryu/cmd/manager.py
+++ b/ryu/cmd/manager.py
@@ -48,6 +48,8 @@ CONF.register_cli_opts([
                 '(use only for debugging)'),
     cfg.StrOpt('user-flags', default=None,
                help='Additional flags file for user applications'),
+    cfg.BoolOpt('with-timestamp', default=False,
+                help='enable timestamps when receiving OpenFlow messages'),
 ])
 
 
@@ -89,6 +91,10 @@ def main(args=None, prog=None):
         with open(CONF.pid_file, 'w') as pid_file:
             pid_file.write(str(os.getpid()))
 
+    if CONF.with_timestamp:
+        from ryu.controller import ofp_event
+        ofp_event.generate_ofp_msg_ev_classes(with_timestamp=True)
+
     app_lists = CONF.app_lists + CONF.app
     # keep old behavior, run ofp if no application is specified.
     if not app_lists:
diff --git a/ryu/controller/ofp_event.py b/ryu/controller/ofp_event.py
index 6b1c8b3..8808da3 100644
--- a/ryu/controller/ofp_event.py
+++ b/ryu/controller/ofp_event.py
@@ -19,10 +19,10 @@ OpenFlow event definitions.
 """
 
 import inspect
+import time
 
 from ryu.controller import handler
 from ryu import ofproto
-from ryu import utils
 from . import event
 
 
@@ -51,6 +51,17 @@ class EventOFPMsgBase(event.EventBase):
         self.msg = msg
 
 
+class EventOFPMsgBaseWithTimestamp(EventOFPMsgBase):
+    """
+    The base class of OpenFlow event class with timestamp.
+
+    This class takes timestamp when instantiated and store it as ``timestamp``
+    attribute. The other attributes are the same with ``EventOFPMsgBase``.
+    """
+    def __init__(self, msg):
+        super(EventOFPMsgBaseWithTimestamp, self).__init__(msg=msg)
+        self.timestamp = time.time()
+
 #
 # Create ofp_event type corresponding to OFP Msg
 #
@@ -71,32 +82,35 @@ def ofp_msg_to_ev_cls(msg_cls):
     return _OFP_MSG_EVENTS[name]
 
 
-def _create_ofp_msg_ev_class(msg_cls):
+def _create_ofp_msg_ev_class(msg_cls, with_timestamp=False):
     name = _ofp_msg_name_to_ev_name(msg_cls.__name__)
-    # print 'creating ofp_event %s' % name
 
-    if name in _OFP_MSG_EVENTS:
+    if not with_timestamp and name in _OFP_MSG_EVENTS:
         return
 
-    cls = type(name, (EventOFPMsgBase,),
-               dict(__init__=lambda self, msg:
-                    super(self.__class__, self).__init__(msg)))
+    _base_cls = EventOFPMsgBase
+    if with_timestamp:
+        _base_cls = EventOFPMsgBaseWithTimestamp
+
+    cls = type(name, (_base_cls,), {})
     globals()[name] = cls
     _OFP_MSG_EVENTS[name] = cls
 
 
-def _create_ofp_msg_ev_from_module(ofp_parser):
-    # print mod
+def _create_ofp_msg_ev_from_module(ofp_parser, with_timestamp=False):
     for _k, cls in inspect.getmembers(ofp_parser, inspect.isclass):
         if not hasattr(cls, 'cls_msg_type'):
             continue
-        _create_ofp_msg_ev_class(cls)
+        _create_ofp_msg_ev_class(cls, with_timestamp)
+
+
+def generate_ofp_msg_ev_classes(with_timestamp=False):
+    for ofp_mods in ofproto.get_ofp_modules().values():
+        ofp_parser = ofp_mods[1]
+        _create_ofp_msg_ev_from_module(ofp_parser, with_timestamp)
 
 
-for ofp_mods in ofproto.get_ofp_modules().values():
-    ofp_parser = ofp_mods[1]
-    # print 'loading module %s' % ofp_parser
-    _create_ofp_msg_ev_from_module(ofp_parser)
+generate_ofp_msg_ev_classes()
 
 
 class EventOFPStateChange(event.EventBase):
-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to