Class: OFController

Inherits:
Object
  • Object
show all
Defined in:
lib/controller.rb

Constant Summary collapse

DEFAULT_IP_ADDRESS =
'0.0.0.0'
DEFAULT_TCP_PORT =
6633

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(debug = false) ⇒ OFController

Returns a new instance of OFController.



32
33
34
35
36
37
38
39
40
41
# File 'lib/controller.rb', line 32

def initialize(debug = false)
  @switches = {}
  @messages = {}
  @logger = Logger.new($stdout).tap do |logger|
    logger.formatter = proc do |severity, datetime, _progname, msg|
      "#{datetime} (#{severity}) -- #{msg}\n"
    end
    logger.level = debug ? Logger::DEBUG : Logger::INFO
  end
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



30
31
32
# File 'lib/controller.rb', line 30

def logger
  @logger
end

Class Method Details

.create(*args) ⇒ Object



17
18
19
# File 'lib/controller.rb', line 17

def self.create(*args)
  (@controller_class || self).new(*args)
end

.inherited(subclass) ⇒ Object



13
14
15
# File 'lib/controller.rb', line 13

def self.inherited(subclass)
  @controller_class = subclass
end

.timer_event(handler, options) ⇒ Object



21
22
23
24
# File 'lib/controller.rb', line 21

def self.timer_event(handler, options)
  @timer_handlers ||= {}
  @timer_handlers[handler] = options.fetch(:interval)
end

.timer_handlersObject



26
27
28
# File 'lib/controller.rb', line 26

def self.timer_handlers
  @timer_handlers || {}
end

Instance Method Details

#broadcast(msg) ⇒ Object



72
73
74
# File 'lib/controller.rb', line 72

def broadcast(msg)
  datapath_ids.each { |did| send_message did, msg }
end

#datapath_idsObject



56
57
58
# File 'lib/controller.rb', line 56

def datapath_ids
  @switches.keys.map(&:to_i)
end

#echo_request(datapath_id, msg) ⇒ Object



96
97
98
# File 'lib/controller.rb', line 96

def echo_request(datapath_id, msg)
  send_message datapath_id, OFEchoReply.new(xid: msg.xid)
end

#error(_datapath_id, _msg) ⇒ Object



95
# File 'lib/controller.rb', line 95

def error(_datapath_id, _msg) end

#eval(input) ⇒ Object



43
44
45
# File 'lib/controller.rb', line 43

def eval(input)
  binding.eval(input)
end

#flow_removed(_datapath_id, _msg) ⇒ Object



103
# File 'lib/controller.rb', line 103

def flow_removed(_datapath_id, _msg) end

#last_messageObject



88
89
90
# File 'lib/controller.rb', line 88

def last_message
  messages.last
end

#last_message_for(datapath_id) ⇒ Object



80
81
82
# File 'lib/controller.rb', line 80

def last_message_for(datapath_id)
  messages_for(datapath_id).last
end

#message_received(_datapath_id, _msg) ⇒ Object



94
# File 'lib/controller.rb', line 94

def message_received(_datapath_id, _msg) end

#messagesObject



84
85
86
# File 'lib/controller.rb', line 84

def messages
  messages_for datapath_ids.first
end

#messages_for(datapath_id) ⇒ Object



76
77
78
# File 'lib/controller.rb', line 76

def messages_for(datapath_id)
  @messages.fetch(datapath_id.to_s)
end

#packet_in(_datapath_id, _msg) ⇒ Object



99
# File 'lib/controller.rb', line 99

def packet_in(_datapath_id, _msg) end

#port_add(_datapath_id, _msg) ⇒ Object



100
# File 'lib/controller.rb', line 100

def port_add(_datapath_id, _msg) end

#port_delete(_datapath_id, _msg) ⇒ Object



101
# File 'lib/controller.rb', line 101

def port_delete(_datapath_id, _msg) end

#port_modify(_datapath_id, _msg) ⇒ Object



102
# File 'lib/controller.rb', line 102

def port_modify(_datapath_id, _msg) end

#run(ip = DEFAULT_IP_ADDRESS, port = DEFAULT_TCP_PORT, *args) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/controller.rb', line 47

def run(ip = DEFAULT_IP_ADDRESS, port = DEFAULT_TCP_PORT, *args)
  maybe_send_handler :start, *args
  socket = TCPServer.open(ip, port)
  socket.setsockopt(:SOCKET, :REUSEADDR, true)
  logger.info "Controller running on #{ip}:#{port}."
  start_timers
  loop { start_switch_thread socket.accept }
end

#send_message(datapath_id, msg = nil) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/controller.rb', line 64

def send_message(datapath_id, msg = nil)
  if msg.nil?
    msg         = datapath_id
    datapath_id = datapath_ids.first
  end
  @switches.fetch(datapath_id.to_s).send(msg)
end

#start(*_args) ⇒ Object



92
# File 'lib/controller.rb', line 92

def start(*_args) end

#switch_ready(_datapath_id) ⇒ Object



93
# File 'lib/controller.rb', line 93

def switch_ready(_datapath_id) end

#switchesObject



60
61
62
# File 'lib/controller.rb', line 60

def switches
  @switches.values
end