Class: GameOverseer::ChannelManager

Inherits:
Object
  • Object
show all
Defined in:
lib/gameoverseer/channels/channel_manager.rb

Overview

Handles routing packets to the services that subscribe to channels

Constant Summary collapse

CHAT =
0
WORLD =
1
HANDSHAKE =
2
FAULT =
3

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChannelManager

Returns a new instance of ChannelManager.



10
11
12
13
# File 'lib/gameoverseer/channels/channel_manager.rb', line 10

def initialize
  @channels = {}
  ChannelManager.instance = self
end

Class Method Details

.instanceChannelManager

Returns instance of active GameOverseer::ChannelManager

Returns:



38
39
40
# File 'lib/gameoverseer/channels/channel_manager.rb', line 38

def self.instance
  @instance
end

.instance=(_instance) ⇒ Object

Sets instance of GameOverseer::ChannelManager that is used through out the system

Parameters:



44
45
46
# File 'lib/gameoverseer/channels/channel_manager.rb', line 44

def self.instance=_instance
  @instance = _instance
end

Instance Method Details

#register_channel(channel, service) ⇒ Object

Enables a service to subscribe to a channel

Parameters:

  • channel (String)
  • service (Service)


18
19
20
21
22
23
24
25
26
# File 'lib/gameoverseer/channels/channel_manager.rb', line 18

def register_channel(channel, service)
  _channel = channel.downcase
  unless @channels[_channel]
    @channels[_channel] = service
    GameOverseer::Console.log("ChannelManager> mapped '#{_channel}' to '#{service.class}'.")
  else
    raise "Could not map channel '#{_channel}' because '#{@channels[data[_channel]].class}' is already using it."
  end
end

#send_to_service(client_id, data) ⇒ Object

Routes packet to Service

Parameters:

  • client_id (Integer)

    ID of client that sent the packet

  • data (Hash)

    data from packet



31
32
33
34
# File 'lib/gameoverseer/channels/channel_manager.rb', line 31

def send_to_service(client_id, data)
  @channels[data['channel']].client_id = client_id
  @channels[data['channel']].process(data)
end