Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Jan 7, 2015
1 parent 7b3349e commit 0e4ccb5
Show file tree
Hide file tree
Showing 16 changed files with 130 additions and 236 deletions.
6 changes: 1 addition & 5 deletions lib/pio/echo.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
require 'pio/echo/reply'
require 'pio/echo/request'
require 'pio/open_flow'
require 'pio/open_flow/parser'

module Pio
# OpenFlow 1.0 Echo Request and Reply message parser.
class Echo
KLASS = { Pio::OpenFlow::Type::ECHO_REQUEST => Pio::Echo::Request,
Pio::OpenFlow::Type::ECHO_REPLY => Pio::Echo::Reply }

module Echo
extend Pio::OpenFlow::Parser
end
end
49 changes: 0 additions & 49 deletions lib/pio/echo/message.rb

This file was deleted.

7 changes: 2 additions & 5 deletions lib/pio/echo/reply.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require 'pio/echo/message'
require 'pio/open_flow'

module Pio
class Echo
module Echo
# OpenFlow 1.0 Echo Reply message generator.
class Reply < Message
def_format Pio::OpenFlow::Type::ECHO_REPLY

class Reply < OpenFlow::Message.factory(OpenFlow::Type::ECHO_REPLY)
# Creates an EchoReply OpenFlow message. This message can be
# used to measure the bandwidth of a controller/switch
# connection as well as to verify its liveness.
Expand Down
9 changes: 2 additions & 7 deletions lib/pio/echo/request.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require 'pio/echo/message'
require 'pio/open_flow'

module Pio
class Echo
module Echo
# OpenFlow 1.0 Echo Request message.
class Request < Message
def_format Pio::OpenFlow::Type::ECHO_REQUEST

class Request < OpenFlow::Message.factory(OpenFlow::Type::ECHO_REQUEST)
# Creates an EchoRequest OpenFlow message. This message can be
# used to measure the bandwidth of a controller/switch
# connection as well as to verify its liveness.
Expand Down Expand Up @@ -36,8 +33,6 @@ class Request < Message
# timestamp to check latency, various lengths to measure
# bandwidth or zero-size(nil) to verify liveness between the
# switch and controller.
#
# rubocop:disable MethodLength
end
end
end
24 changes: 2 additions & 22 deletions lib/pio/features/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module Pio
# OpenFlow 1.0 Features messages
class Features
# OpenFlow 1.0 Features Reply message
class Reply < Pio::OpenFlow::Message
class Reply < OpenFlow::Message.factory(OpenFlow::Type::FEATURES_REPLY)
# Message body of features reply.
class Body < BinData::Record
class ReplyBody < BinData::Record
extend Flags

# enum ofp_capabilities
Expand Down Expand Up @@ -56,33 +56,13 @@ def length
end
end

def_format Pio::OpenFlow::Type::FEATURES_REPLY

def datapath_id
@format.body.datapath_id
end

def_delegators :body, :datapath_id
def_delegator :body, :datapath_id, :dpid
def_delegators :body, :n_buffers
def_delegators :body, :n_tables
def_delegators :body, :capabilities
def_delegators :body, :actions
def_delegators :body, :ports

# @reek This method smells of :reek:FeatureEnvy
def initialize(user_options)
body_options =
{
datapath_id: user_options[:dpid],
n_buffers: user_options[:n_buffers],
n_tables: user_options[:n_tables],
capabilities: user_options[:capabilities],
actions: user_options[:actions],
ports: user_options[:ports]
}
@format = Format.new(user_options.merge(body: body_options))
end
end
end
end
39 changes: 1 addition & 38 deletions lib/pio/features/request.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
require 'forwardable'
require 'pio/open_flow'

module Pio
# OpenFlow 1.0 Features messages
class Features
# OpenFlow 1.0 Features Request message
class Request < Pio::OpenFlow::Message
# Message body of Features Request
class Body < BinData::Record
endian :big

string :body # ignored

def length
0
end

def empty?
true
end
end

def_format Pio::OpenFlow::Type::FEATURES_REQUEST

class Request < OpenFlow::Message.factory(OpenFlow::Type::FEATURES_REQUEST)
# Creates a Features Request OpenFlow message.
#
# @overload initialize()
Expand All @@ -44,25 +26,6 @@ def empty?
# The options to create a message with.
# @option user_options [Number] :transaction_id
# @option user_options [Number] :xid An alias to transaction_id.
#
# @reek This method smells of :reek:FeatureEnvy
# rubocop:disable MethodLength
def initialize(user_options = {})
options = if user_options.respond_to?(:to_i)
{ open_flow_header: { transaction_id: user_options.to_i } }
elsif user_options.respond_to?(:fetch)
transaction_id =
user_options[:transaction_id] || user_options[:xid] || 0
{ open_flow_header: { transaction_id: transaction_id } }
else
fail TypeError
end
if options[:open_flow_header][:transaction_id] >= 2**32
fail ArgumentError, 'Transaction ID >= 2**32'
end
@format = Format.new(options)
end
# rubocop:enable MethodLength
end
end
end
53 changes: 1 addition & 52 deletions lib/pio/hello.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
require 'English'
require 'bindata'
require 'pio/open_flow'
require 'pio/parse_error'

module Pio
# OpenFlow 1.0 Hello message
class Hello < Pio::OpenFlow::Message
# Message body of Hello
class Body < BinData::Record
endian :big

string :body # ignored

def length
0
end

def empty?
true
end
end

def_format Pio::OpenFlow::Type::HELLO

# Parses +raw_data+ binary string into a Hello message object.
#
# @example
# Pio::Hello.read("\x01\x00\x00\b\x00\x00\x00\x00")
# @return [Pio::Hello]
def self.read(raw_data)
hello = allocate
hello.instance_variable_set :@format, Format.read(raw_data)
hello
rescue BinData::ValidityError
raise Pio::ParseError, $ERROR_INFO.message
end

class Hello < OpenFlow::Message.factory(Pio::OpenFlow::Type::HELLO)
# Creates a Hello OpenFlow message.
#
# @overload initialize()
Expand All @@ -56,23 +23,5 @@ def self.read(raw_data)
# @param [Hash] user_options The options to create a message with.
# @option user_options [Number] :transaction_id
# @option user_options [Number] :xid An alias to transaction_id.
#
# @reek This method smells of :reek:FeatureEnvy
# rubocop:disable MethodLength
def initialize(user_options = {})
options = if user_options.respond_to?(:to_i)
{ transaction_id: user_options.to_i }
elsif user_options.respond_to?(:fetch)
{ transaction_id: user_options[:transaction_id] ||
user_options[:xid] || 0 }
else
fail TypeError
end
if options[:transaction_id] >= 2**32
fail ArgumentError, 'Transaction ID >= 2**32'
end
@format = Format.new(open_flow_header: options)
end
# rubocop:enable MethodLength
end
end
Loading

0 comments on commit 0e4ccb5

Please sign in to comment.