Class: CableRoom::Room::Base

Inherits:
Object
  • Object
show all
Includes:
Ports, Broadcasting, Callbacks, ChannelAdapter, InputHandling, Lifecycle, PortManagement, PortScoping, Reaping, Threading, UserManagement
Defined in:
lib/cable_room/room/base.rb

Constant Summary collapse

ROOM_OUT_CHANNEL =

One-to-many: messages the Room broadcasts out to its members

:from_room
ROOM_IN_CHANNEL =

Many-to-one: messages members send in to the Room

:to_room
LOCK_DURATION =
15.seconds
WATCH_DOG_INTERVAL =
15.seconds

Constants included from PortManagement

PortManagement::PORT_TIMEOUT

Constants included from ChannelAdapter

ChannelAdapter::Channel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Broadcasting

#broadcast

Methods included from UserManagement

#on_user_joined, #on_user_left

Methods included from PortManagement

#_apply_port_scope, #reply

Methods included from PortScoping

#simplify_port_scope, #with_port_scope, #with_port_scope!, #without_port_scope

Methods included from Threading

#async, #on_room_thread

Methods included from Ports

#close_streamed_ports!, #port_transmit, #ports, #stream_port

Constructor Details

#initialize(vchannel, key = nil) ⇒ Base

Returns a new instance of Base.



79
80
81
82
83
# File 'lib/cable_room/room/base.rb', line 79

def initialize(vchannel, key = nil)
  super()
  @key = key
  @cable_channel = vchannel
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



75
76
77
# File 'lib/cable_room/room/base.rb', line 75

def key
  @key
end

Class Method Details

.ensure(key = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cable_room/room/base.rb', line 11

def ensure(key = nil)
  lock_key = room_port_key(key)
  lock_info = CableRoom.lock_manager.lock(lock_key, self::LOCK_DURATION.in_milliseconds)
  return false unless lock_info

  vchannel = self::Channel.new(
    lock_info,
    self,
    key,
    {
      watchdog_interval: self::WATCH_DOG_INTERVAL,
      lock_duration: self::LOCK_DURATION,
    }
  )
  vchannel.subscribe_to_channel

  true
rescue => e
  CableRoom.lock_manager.unlock(lock_info) if lock_info
  raise e
end

.locally_running_instancesObject



43
44
45
46
47
# File 'lib/cable_room/room/base.rb', line 43

def locally_running_instances
  ChannelTracker.instance.room_channels.select do |chan|
    chan.room_class == self
  end.map(&:room)
end

.room_port_key(room_key, port = nil) ⇒ Object



33
34
35
36
37
# File 'lib/cable_room/room/base.rb', line 33

def room_port_key(room_key, port = nil)
  full_key = [room_key]
  full_key << port if port
  self::Channel.broadcasting_for(full_key)
end

.send_message(room_key, data, port: ROOM_IN_CHANNEL) ⇒ Object



39
40
41
# File 'lib/cable_room/room/base.rb', line 39

def send_message(room_key, data, port: ROOM_IN_CHANNEL)
  ActionCable.server.broadcast(room_port_key(room_key, port), data)
end

Instance Method Details

#<<(data) ⇒ Object



85
86
87
# File 'lib/cable_room/room/base.rb', line 85

def <<(data)
  port_transmit(ROOM_OUT_CHANNEL, data)
end