Class: Tamashii::Manager::Channel

Inherits:
Set
  • Object
show all
Defined in:
lib/tamashii/manager/channel.rb

Overview

:nodoc:

Constant Summary collapse

SERVER_ID =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, *args) ⇒ Channel

Returns a new instance of Channel.



67
68
69
70
# File 'lib/tamashii/manager/channel.rb', line 67

def initialize(id, *args)
  super(*args)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



65
66
67
# File 'lib/tamashii/manager/channel.rb', line 65

def id
  @id
end

Class Method Details

.get(id) ⇒ Object



14
15
16
# File 'lib/tamashii/manager/channel.rb', line 14

def get(id)
  pool[id]
end

.idle_channel(channel) ⇒ Object



57
58
59
60
61
62
# File 'lib/tamashii/manager/channel.rb', line 57

def idle_channel(channel)
  pool.idle(channel.id)
  # rubocop:disable Metrics/LineLength
  Manager.logger.debug("Channel Pool add - ##{channel.id}, available channels: #{pool.idle.size}")
  # rubocop:enable Metrics/LineLength
end

.poolObject



10
11
12
# File 'lib/tamashii/manager/channel.rb', line 10

def pool
  @pool ||= ChannelPool.new
end

.select_channel(client) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/tamashii/manager/channel.rb', line 22

def select_channel(client)
  case client.type
  when :checkin
    servers
  else
    return pool.idle || pool.create! if pool[client.tag].nil?
    pool[client.tag]
  end
end

.serversObject



18
19
20
# File 'lib/tamashii/manager/channel.rb', line 18

def servers
  @servers ||= Channel.new(SERVER_ID)
end

.subscribe(client) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tamashii/manager/channel.rb', line 32

def subscribe(client)
  channel = select_channel(client)
  channel.add(client)
  client.tag = channel.id

  pool.ready(channel)

  Manager.logger.info(
    "Client #{client.id} subscribe to Channel ##{channel.id}"
  )

  channel
end

.unsubscribe(client) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/tamashii/manager/channel.rb', line 46

def unsubscribe(client)
  channel = select_channel(client)
  channel.delete(client)

  Manager.logger.info(
    "Client #{client.id} unsubscribe to Channel ##{channel.id}"
  )

  idle_channel(channel) if channel.empty? && channel.id != SERVER_ID
end

Instance Method Details

#broadcast(packet, exclude_server = false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/tamashii/manager/channel.rb', line 78

def broadcast(packet, exclude_server = false)
  Manager.logger.info("Broadcast \"#{packet}\" to Channel ##{@id}")
  each do |client|
    client.send(packet)
  end

  # rubocop:disable Metrics/LineLength
  Channel.servers.broadcast(packet) unless id == SERVER_ID || exclude_server
  # rubocop:enable Metrics/LineLength
end

#broadcast_all(packet) ⇒ Object



89
90
91
92
93
94
# File 'lib/tamashii/manager/channel.rb', line 89

def broadcast_all(packet)
  Channel.pool.each do |_id, channel|
    channel&.broadcast(packet, true)
  end
  Channel.servers.broadcast(packet) unless id == SERVER_ID
end

#send_to(channel_id, packet) ⇒ Object



72
73
74
75
76
# File 'lib/tamashii/manager/channel.rb', line 72

def send_to(channel_id, packet)
  channel = Channel.pool[channel_id]
  return unless channel.nil?
  channel.broadcast(packet)
end