Class: RuneRb::Utils::Controller

Inherits:
Object
  • Object
show all
Includes:
Logging, Singleton
Defined in:
lib/rrb/utils/controller.rb

Overview

The Controller instance facilitates the deployment of game world and server instances.

Since:

  • 0.0.1

Constant Summary

Constants included from Logging

Logging::LOG_FILE_PATH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#class_name, #err, #err!, #log, #log!

Constructor Details

#initializeController

Constructs a new Controller object

Since:

  • 0.0.1



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

#sessionsHash (readonly)

Returns a collection of sessions.

Returns:

  • (Hash)

    a collection of sessions.

Since:

  • 0.0.1



19
20
21
# File 'lib/rrb/utils/controller.rb', line 19

def sessions
  @sessions
end

Instance Method Details

#aboutObject

Logs information about the worlds, servers, and session states.

Since:

  • 0.0.1



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

#autorunObject

Since:

  • 0.0.1



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_consoleObject

Deploy a console session.

Since:

  • 0.0.1



52
53
54
# File 'lib/rrb/utils/controller.rb', line 52

def deploy_console
  Pry.start(self)
end

#deploy_gatewayInteger, NilClass

Construct a singleton instance of the Network::Gateway in a forked process.

Returns:

  • (Integer, NilClass)

    the process ID for the gateway process.

Since:

  • 0.0.1



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_worldInteger, NilClass

Construct a singleton instance of Game::World::Instance in a forked process.

Returns:

  • (Integer, NilClass)

    the process ID for the world process.

Since:

  • 0.0.1



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.

Since:

  • 0.0.1



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_timeObject

The current up-time for the server.

Since:

  • 0.0.1



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