Class: Ably::Realtime::Channel::ChannelManager Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ably/realtime/channel/channel_manager.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

ChannelManager is responsible for all actions relating to channel state: attaching, detaching or failure Channel state changes are performed by this class and executed from ChannelStateMachine

This is a private class and should never be used directly by developers as the API is likely to change in future.

Instance Method Summary collapse

Constructor Details

#initialize(channel, connection) ⇒ ChannelManager

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ChannelManager.



12
13
14
15
16
17
# File 'lib/ably/realtime/channel/channel_manager.rb', line 12

def initialize(channel, connection)
  @channel    = channel
  @connection = connection

  setup_connection_event_handlers
end

Instance Method Details

#attachObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Commence attachment



20
21
22
23
24
25
# File 'lib/ably/realtime/channel/channel_manager.rb', line 20

def attach
  if can_transition_to?(:attached)
    connect_if_connection_initialized
    send_attach_protocol_message
  end
end

#attached(attached_protocol_message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Channel is attached, notify presence if sync is expected



37
38
39
40
41
42
43
44
# File 'lib/ably/realtime/channel/channel_manager.rb', line 37

def attached(attached_protocol_message)
  if attached_protocol_message.has_presence_flag?
    channel.presence.manager.sync_expected
  else
    channel.presence.manager.sync_not_expected
  end
  channel.set_attached_serial attached_protocol_message.channel_serial
end

#detach(error = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Commence attachment



28
29
30
31
32
33
34
# File 'lib/ably/realtime/channel/channel_manager.rb', line 28

def detach(error = nil)
  if connection.closed? || connection.connecting? || connection.suspended?
    channel.transition_state_machine :detached, reason: error
  elsif can_transition_to?(:detached)
    send_detach_protocol_message
  end
end

#drop_pending_queue_from_ack(ack_protocol_message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



86
87
88
89
90
91
92
93
94
# File 'lib/ably/realtime/channel/channel_manager.rb', line 86

def drop_pending_queue_from_ack(ack_protocol_message)
  message_serial_up_to = ack_protocol_message.message_serial + ack_protocol_message.count - 1
  connection.__pending_message_ack_queue__.drop_while do |protocol_message|
    if protocol_message.message_serial <= message_serial_up_to
      yield protocol_message
      true
    end
  end
end

#emit_error(error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An error has occurred on the channel



47
48
49
50
# File 'lib/ably/realtime/channel/channel_manager.rb', line 47

def emit_error(error)
  logger.error "ChannelManager: Channel '#{channel.name}' error: #{error}"
  channel.emit :error, error
end

#fail_messages_awaiting_ack(error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

When a channel is no longer attached or has failed, all messages awaiting an ACK response should fail immediately



59
60
61
62
63
64
65
66
# File 'lib/ably/realtime/channel/channel_manager.rb', line 59

def fail_messages_awaiting_ack(error)
  # Allow a short time for other queued operations to complete before failing all messages
  EventMachine.add_timer(0.1) do
    error = Ably::Exceptions::MessageDeliveryFailed.new("Channel cannot publish messages whilst state is '#{channel.state}'") unless error
    fail_messages_in_queue connection.__pending_message_ack_queue__, error
    fail_messages_in_queue connection.__outgoing_message_queue__, error
  end
end

#fail_messages_in_queue(queue, error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
71
72
73
74
75
# File 'lib/ably/realtime/channel/channel_manager.rb', line 68

def fail_messages_in_queue(queue, error)
  queue.delete_if do |protocol_message|
    if protocol_message.channel == channel.name
      nack_messages protocol_message, error
      true
    end
  end
end

#nack_messages(protocol_message, error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
80
81
82
83
84
# File 'lib/ably/realtime/channel/channel_manager.rb', line 77

def nack_messages(protocol_message, error)
  (protocol_message.messages + protocol_message.presence).each do |message|
    logger.debug "Calling NACK failure callbacks for #{message.class.name} - #{message.to_json}, protocol message: #{protocol_message}"
    message.fail error
  end
  logger.debug "Calling NACK failure callbacks for #{protocol_message.class.name} - #{protocol_message.to_json}"
  protocol_message.fail error
end

#suspend(error) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detach a channel as a result of an error



53
54
55
# File 'lib/ably/realtime/channel/channel_manager.rb', line 53

def suspend(error)
  channel.transition_state_machine! :detaching, reason: error
end