Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jun 19, 2015
1 parent a25cdce commit 94907de
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/pio/open_flow13/apply.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'bindata'
require 'forwardable'

module Pio
Expand Down
35 changes: 23 additions & 12 deletions lib/pio/open_flow13/flow_mod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Base module.
module Pio
remove_const :FlowMod
remove_const :FlowMod if const_defined?(:FlowMod)

# OpenFlow 1.3 FlowMod message parser and generator
class FlowMod
Expand All @@ -31,45 +31,51 @@ def set(value)

# Buffered packet to apply to, or :no_buffer.
class BufferId < BinData::Primitive
NO_BUFFER = 0xffffffff

endian :big
uint32 :buffer_id, initial_value: 0xffffffff
uint32 :buffer_id, initial_value: NO_BUFFER

def get
buffer_id == 0xffffffff ? :no_buffer : buffer_id
(buffer_id == NO_BUFFER) ? :no_buffer : buffer_id
end

def set(value)
self.buffer_id = (value == :no_buffer ? 0xffffffff : value)
self.buffer_id = (value == :no_buffer ? NO_BUFFER : value)
end
end

# For delete commands, require matching entries to include this as
# an output port.
class OutPort < BinData::Primitive
ANY = 0xffffffff

endian :big
uint32 :out_port, initial_value: 0xffffffff
uint32 :out_port, initial_value: ANY

def get
out_port == 0xffffffff ? :any : out_port
(out_port == ANY) ? :any : out_port
end

def set(value)
self.out_port = (value == :any ? 0xffffffff : value)
self.out_port = (value == :any ? ANY : value)
end
end

# For delete commands, require matching entries to include this as
# an output group.
class OutGroup < BinData::Primitive
ANY = 0xffffffff

endian :big
uint32 :out_group, initial_value: 0xffffffff
uint32 :out_group, initial_value: ANY

def get
out_group == 0xffffffff ? :any : out_group
(out_group == ANY) ? :any : out_group
end

def set(value)
self.out_group = (value == :any ? 0xffffffff : value)
self.out_group = (value == :any ? ANY : value)
end
end

Expand All @@ -81,7 +87,7 @@ class Instructions < BinData::Primitive
string :instructions, read_length: :instructions_length

def set(object)
self.instructions = object.to_binary_s
self.instructions = [object].flatten.map(&:to_binary_s).join
end

# rubocop:disable MethodLength
Expand All @@ -91,6 +97,10 @@ def get
while tmp.length > 0
instruction_type = BinData::Uint16be.read(tmp)
instruction = case instruction_type
when 1
GotoTable.read(tmp)
when 2
WriteMetadata.read(tmp)
when 4
Apply.read(tmp)
else
Expand Down Expand Up @@ -148,7 +158,8 @@ class Format < BinData::Record
endian :big

open_flow_header :open_flow_header,
ofp_version_value: 4, message_type_value: 14
ofp_version_value: 4,
message_type_value: OpenFlow::FLOW_MOD
body :body

def_delegators :open_flow_header, :ofp_version
Expand Down
2 changes: 1 addition & 1 deletion lib/pio/open_flow13/send_out_port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Base module.
module Pio
remove_const :SendOutPort
remove_const :SendOutPort if const_defined?(:SendOutPort)

# Output to switch port.
class SendOutPort
Expand Down

0 comments on commit 94907de

Please sign in to comment.