Class: Zashoku::Daemon
- Inherits:
-
Object
- Object
- Zashoku::Daemon
- Defined in:
- lib/daemon.rb
Instance Method Summary collapse
- #cleanup ⇒ Object
-
#initialize ⇒ Daemon
constructor
A new instance of Daemon.
- #lay_traps! ⇒ Object
- #listen ⇒ Object
- #respond(message) ⇒ Object
- #update(e) ⇒ Object
Constructor Details
#initialize ⇒ Daemon
Returns a new instance of Daemon.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/daemon.rb', line 35 def initialize @semaphore = Mutex.new Zashoku.logger = Logger.new(STDOUT) Zashoku.logger.level = Zashoku::CConf[:app][:log_level][:daemon] # Load conf Zashoku.conf = DaemonConfig.new # Load modules Zashoku.modules = Zashoku::Module.load(Zashoku::CConf[:app][:modules][:daemon]) # Update conf with module defaults Zashoku.conf.reload Zashoku.controllers = Zashoku.modules.keys.zip(Zashoku.modules.values.map(&:controller)).to_h Zashoku.controllers.each_value { |c| c.add_observer(self) } end |
Instance Method Details
#cleanup ⇒ Object
94 95 96 97 |
# File 'lib/daemon.rb', line 94 def cleanup @server.exit exit end |
#lay_traps! ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/daemon.rb', line 86 def lay_traps! Signal.trap('INT') do Thread.new do @semaphore.synchronize { cleanup } end end end |
#listen ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/daemon.rb', line 53 def listen lay_traps! @server = Net::Server.new(Zashoku::CConf[:app][:net][:port]) @server.handler = method(:respond) Zashoku.logger.info('server listening') sleep end |
#respond(message) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/daemon.rb', line 66 def respond() Zashoku.logger.debug("daemon.respond #{}") case ['msg'] when 'modules' Zashoku.modules.map { |m, _| m } when 'items' Zashoku.command(mod: [:mod], meth: :items) when 'fwd' Zashoku.command() when 'disconnect' nil when 'stop' cleanup when 'up?' true else 'what?' end end |