Class: Pio::OpenFlow::Message
- Inherits:
-
Object
- Object
- Pio::OpenFlow::Message
- Defined in:
- lib/pio/open_flow/message.rb
Overview
Defines shortcuts to OpenFlow header fields.
Class Method Summary collapse
-
._define_initialize ⇒ Object
rubocop:disable MethodLength rubocop:disable AbcSize.
-
._define_open_flow_accessors ⇒ Object
rubocop:enable MethodLength.
- ._define_self_read ⇒ Object
-
._define_to_binary ⇒ Object
rubocop:enable MethodLength rubocop:enable AbcSize.
-
._format_class(klass, message_type) ⇒ Object
rubocop:disable MethodLength.
- .factory(klass, message_type, &block) ⇒ Object
Class Method Details
._define_initialize ⇒ Object
rubocop:disable MethodLength rubocop:disable AbcSize
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pio/open_flow/message.rb', line 71 def self._define_initialize proc do def initialize( = {}) = OpenFlowHeader::Options.parse() = () @format = self.class.format.new(open_flow_header: , body: ) end private def () if .respond_to?(:fetch) .delete :transaction_id .delete :xid dpid = [:dpid] [:datapath_id] = dpid if dpid if .keys.size > 1 else [:user_data] || '' end else '' end end end end |
._define_open_flow_accessors ⇒ Object
rubocop:enable MethodLength
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pio/open_flow/message.rb', line 42 def self._define_open_flow_accessors proc do def_delegators :@format, :open_flow_header def_delegators :open_flow_header, :ofp_version def_delegators :open_flow_header, :message_type def_delegators :open_flow_header, :message_length def_delegators :open_flow_header, :transaction_id def_delegator :open_flow_header, :transaction_id, :xid def_delegators :@format, :body def_delegator :@format, :body, :user_data end end |
._define_self_read ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pio/open_flow/message.rb', line 56 def self._define_self_read proc do def self.read(raw_data) allocate.tap do || .instance_variable_set(:@format, format.read(raw_data)) end rescue BinData::ValidityError = name.split('::')[1..-1].join(' ') raise Pio::ParseError, "Invalid #{message_name} message." end end end |
._define_to_binary ⇒ Object
rubocop:enable MethodLength rubocop:enable AbcSize
102 103 104 105 106 |
# File 'lib/pio/open_flow/message.rb', line 102 def self._define_to_binary proc do def_delegator :@format, :to_binary_s, :to_binary end end |
._format_class(klass, message_type) ⇒ Object
rubocop:disable MethodLength
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pio/open_flow/message.rb', line 21 def self._format_class(klass, ) %( class Format < BinData::Record endian :big open_flow_header :open_flow_header, message_type_value: #{message_type} virtual assert: -> do open_flow_header.message_type == #{message_type} end #{klass.const_defined?(:Body) ? 'body' : 'string'} :body end def self.format const_get :Format end ) end |
.factory(klass, message_type, &block) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/pio/open_flow/message.rb', line 10 def self.factory(klass, , &block) klass.extend Forwardable klass.module_eval(&block) if block klass.module_eval _format_class(klass, ) klass.module_eval(&_define_open_flow_accessors) klass.module_eval(&_define_self_read) klass.module_eval(&_define_initialize) klass.module_eval(&_define_to_binary) end |