Skip to content

Commit

Permalink
Refactoring (closes #426 #428)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuhito committed Sep 8, 2016
1 parent e73bcd2 commit 7c1e0f0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
4 changes: 2 additions & 2 deletions bin/trema
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module Trema
c.switch :openflow13, default_value: false

c.desc 'Overrides the default openflow channel port'
c.flag [:p, :port]
c.flag [:p, :port], default_value: Controller::DEFAULT_TCP_PORT

c.desc 'Set logging level'
c.flag [:l, :logging_level], default_value: :info
Expand All @@ -55,9 +55,9 @@ module Trema
Phut.pid_dir = options[:pid_dir]
Phut.log_dir = options[:log_dir]
Phut.socket_dir = options[:socket_dir]

Pio::OpenFlow.switch_version('OpenFlow13') if options[:openflow13]
options[:logging_level] = :debug if global_options[:verbose]
require 'trema/switch'
Trema.logger.level = options[:logging_level]
@command = Trema::Command.new
@command.run(args, global_options.merge(options))
Expand Down
1 change: 1 addition & 0 deletions lib/trema.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'trema/controller'
require 'trema/dirs'
require 'trema/drb'
require 'trema/switch'
require 'trema/version'
75 changes: 40 additions & 35 deletions lib/trema/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,25 @@ def self.unix_domain_socket(name, check = false)

attr_reader :controller

# rubocop:disable AbcSize
# rubocop:disable MethodLength
def run(args, options)
@args = args
@daemon = options[:daemonize]
ruby_file = @args.first
$LOAD_PATH.unshift File.expand_path(File.dirname(ruby_file))
Object.module_eval IO.read(ruby_file), ruby_file
port_number = (options[:port] || Controller::DEFAULT_TCP_PORT).to_i
@controller = Controller.create(ruby_file,
port_number,
options.fetch(:logging_level))
@options = options

create_controller
trap_signals
create_pid_file
start_phut(options[:conf])
start_phut

if options[:daemonize]
if @options[:daemonize]
run_as_daemon { start_controller_and_drb_threads }
else
start_controller_and_drb_threads
end
rescue NoControllerDefined => e
raise e, "#{ruby_file}: #{e.message}"
end
# rubocop:enable MethodLength
# rubocop:enable AbcSize

def kill(name)
@phut.fetch(name).stop
Expand All @@ -60,9 +54,9 @@ def killall
@controller_thread.kill if @controller_thread
@phut_run_thread.kill if @phut_run_thread
@phut.stop if @phut
FileUtils.rm pid_file if FileTest.exists?(pid_file)
FileUtils.rm pid_file if FileTest.exist?(pid_file)
DRb.stop_service
exit 0 if @daemon
exit 0 if @options[:daemonize]
end
# rubocop:enable CyclomaticComplexity

Expand All @@ -88,11 +82,39 @@ def fetch(name)

private

def create_controller
$LOAD_PATH.unshift File.expand_path(File.dirname(ruby_file))
Object.module_eval IO.read(ruby_file), ruby_file
@controller = Controller.create(@options.fetch(:port).to_i,
@options.fetch(:logging_level))
end

def ruby_file
@args.first
end

# rubocop:disable MethodLength
def trap_signals
@killall_thread = Thread.start do
loop do
if @stop
killall
break
end
sleep 1
end
end
@killall_thread.abort_on_exception = true
Signal.trap(:TERM) { stop }
Signal.trap(:INT) { stop }
end
# rubocop:enable MethodLength

# rubocop:disable MethodLength
def start_phut(config_file)
return unless config_file
def start_phut
return unless @options[:conf]
system 'sudo -v'
@phut = Phut::Parser.new(Trema.logger).parse(config_file)
@phut = Phut::Parser.new(Trema.logger).parse(@options[:conf])
@phut_run_thread = Thread.start { @phut.run }
@phut_run_thread.join
Thread.start { start_sudo_credential_update }
Expand Down Expand Up @@ -134,23 +156,6 @@ def stop
@stop = true
end

# rubocop:disable MethodLength
def trap_signals
@killall_thread = Thread.start do
loop do
if @stop
killall
break
end
sleep 1
end
end
@killall_thread.abort_on_exception = true
Signal.trap(:TERM) { stop }
Signal.trap(:INT) { stop }
end
# rubocop:enable MethodLength

def create_pid_file
raise "#{name} is already running (#{pid_file})." if running?
update_pid_file
Expand All @@ -165,7 +170,7 @@ def pid_file
end

def running?
FileTest.exists? pid_file
FileTest.exist? pid_file
end

def name
Expand Down
7 changes: 2 additions & 5 deletions lib/trema/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,9 @@ def self.inherited(subclass)
end

# @private
def self.create(ruby_file,
port_number = DEFAULT_TCP_PORT,
logging_level = :info)
def self.create(port_number = DEFAULT_TCP_PORT, logging_level = :info)
unless @controller_klass
raise(NoControllerDefined,
"#{ruby_file}: No controller class is defined")
raise NoControllerDefined, 'No controller class is defined'
end
@controller_klass.new(port_number, logging_level)
end
Expand Down

0 comments on commit 7c1e0f0

Please sign in to comment.