Class: RuneRb::Utils::Controller
- Inherits:
-
Object
- Object
- RuneRb::Utils::Controller
- Includes:
- Logging, Singleton
- Defined in:
- lib/rrb/utils/controller.rb
Overview
The Controller instance facilitates the deployment of game world and server instances.
Constant Summary
Constants included from Logging
Instance Attribute Summary collapse
-
#sessions ⇒ Hash
readonly
A collection of sessions.
Instance Method Summary collapse
-
#about ⇒ Object
Logs information about the worlds, servers, and session states.
- #autorun ⇒ Object
-
#deploy_console ⇒ Object
Deploy a console session.
-
#deploy_gateway ⇒ Integer, NilClass
Construct a singleton instance of the Network::Gateway in a forked process.
-
#deploy_world ⇒ Integer, NilClass
Construct a singleton instance of Game::World::Instance in a forked process.
-
#initialize ⇒ Controller
constructor
Constructs a new Controller object.
-
#shutdown(graceful: true) ⇒ Object
Stop all server instances and close all connected sessions.
-
#up_time ⇒ Object
The current up-time for the server.
Methods included from Logging
#class_name, #err, #err!, #log, #log!
Constructor Details
#initialize ⇒ Controller
Constructs a new Controller object
22 23 24 25 26 |
# File 'lib/rrb/utils/controller.rb', line 22 def initialize @start = { time: Process.clock_gettime(Process::CLOCK_MONOTONIC), stamp: Time.now } @pids = { gateway: nil, world: nil, controller: Process.pid } super() end |
Instance Attribute Details
#sessions ⇒ Hash (readonly)
Returns a collection of sessions.
19 20 21 |
# File 'lib/rrb/utils/controller.rb', line 19 def sessions @sessions end |
Instance Method Details
#about ⇒ Object
Logs information about the worlds, servers, and session states.
45 46 47 48 49 |
# File 'lib/rrb/utils/controller.rb', line 45 def about log COLORS.cyan.bold("[Controller]: #{@pids[:controller]}"), COLORS.magenta.bold("[Server]: #{@pids[:gateway].nil? ? COLORS.red.bold('nil') : COLORS.green.bold(@pids[:gateway])}"), COLORS.yellow.bold("[World]: #{@pids[:world].nil? ? COLORS.red.bold('nil') : COLORS.green.bold(@pids[:world])}") end |
#autorun ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rrb/utils/controller.rb', line 28 def autorun # Setup signal trapping setup_trapping # Deploy gateway process deploy_gateway # Deploy Game world deploy_world # Deploy Console deploy_console ensure shutdown end |
#deploy_console ⇒ Object
Deploy a console session.
52 53 54 |
# File 'lib/rrb/utils/controller.rb', line 52 def deploy_console Pry.start(self) end |
#deploy_gateway ⇒ Integer, NilClass
Construct a singleton instance of the Network::Gateway in a forked process.
58 59 60 |
# File 'lib/rrb/utils/controller.rb', line 58 def deploy_gateway # @pids[:gateway] ||= Process.fork { RuneRb::Network::Gateway.instance.autorun } end |
#deploy_world ⇒ Integer, NilClass
Construct a singleton instance of Game::World::Instance in a forked process.
64 65 66 |
# File 'lib/rrb/utils/controller.rb', line 64 def deploy_world # @pids[:world] ||= Process.fork { RuneRb::Game::World::Instance.instance } end |
#shutdown(graceful: true) ⇒ Object
Stop all server instances and close all connected sessions.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rrb/utils/controller.rb', line 69 def shutdown(graceful: true) # Kill server process Process.kill(graceful ? 'INT' : 'TERM', @pids[:gateway]) unless @pids[:gateway].nil? # Kill World processes Process.kill(graceful ? 'INT' : 'TERM', @pids[:world]) unless @pids[:world].nil? # Log up-time log! COLORS.blue.bold("Total Controller Up-time: #{up_time.to_i.to_ftime}") ensure exit! end |
#up_time ⇒ Object
The current up-time for the server.
83 84 85 |
# File 'lib/rrb/utils/controller.rb', line 83 def up_time (Process.clock_gettime(Process::CLOCK_MONOTONIC) - @start[:time]).round(3) end |