Class: Tamashii::Manager::Channel
- Inherits:
-
Set
- Object
- Set
- Tamashii::Manager::Channel
- Defined in:
- lib/tamashii/manager/channel.rb
Overview
:nodoc:
Constant Summary collapse
- SERVER_ID =
0
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .get(id) ⇒ Object
- .idle_channel(channel) ⇒ Object
- .pool ⇒ Object
- .select_channel(client) ⇒ Object
- .servers ⇒ Object
- .subscribe(client) ⇒ Object
- .unsubscribe(client) ⇒ Object
Instance Method Summary collapse
- #broadcast(packet, exclude_server = false) ⇒ Object
- #broadcast_all(packet) ⇒ Object
-
#initialize(id, *args) ⇒ Channel
constructor
A new instance of Channel.
- #send_to(channel_id, packet) ⇒ Object
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
#id ⇒ Object (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 |
.pool ⇒ Object
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 |
.servers ⇒ Object
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 |