class Syncano::SyncConnection
Represents connection with Sync Server
Attributes
Public Class Methods
Constructor for Syncano::SyncConnection
object
# File lib/syncano/sync_connection.rb, line 7 def initialize super self.callbacks = ::ActiveSupport::HashWithIndifferentAccess.new self.callbacks_queue = [] self.responses = ::ActiveSupport::HashWithIndifferentAccess.new self.responses_queue = [] self.client = ::Syncano::Clients::Sync.instance self.received_data = '' end
Public Instance Methods
Appends callback method to the end of callbacks chain @param [Symbol, String] callback_name @param [Block] callback
# File lib/syncano/sync_connection.rb, line 50 def append_callback(callback_name, callback) callbacks[callback_name] = callback callbacks_queue << callback_name end
Eventmachine callback invoked after completing connection
# File lib/syncano/sync_connection.rb, line 21 def connection_completed start_tls end
Gets call response packet from the responses queue @param [Integer, String] message_id @return [Syncano::Packets::CallResponse]
# File lib/syncano/sync_connection.rb, line 73 def get_response(message_id) responses.delete(message_id.to_s) end
Prepends callback method to the beginning of callbacks chain @param [Symbol, String] callback_name @param [Block] callback
# File lib/syncano/sync_connection.rb, line 58 def prepend_callback(callback_name, callback) callbacks[callback_name] = callback callbacks_queue.unshift(callback_name) end
Eventmachine callback invoked after receiving data from socket Data are parsed here and processed by callbacks chain
# File lib/syncano/sync_connection.rb, line 42 def receive_data(data) self.received_data += data process_data if data.end_with?("\n") end
Removes callback from callbacks chain @param [Symbol, String] callback_name
# File lib/syncano/sync_connection.rb, line 65 def remove_callback(callback_name) callbacks.delete(callback_name) callbacks_queue.delete(callback_name) end
Eventmachine callback invoked after completing ssl handshake
# File lib/syncano/sync_connection.rb, line 26 def ssl_handshake_completed auth_data = { api_key: client.api_key, instance: client.instance_name, 'user-agent' => "syncano-ruby-#{Syncano::VERSION}" } auth_data[:auth_key] = client.auth_key if client.auth_key.present? client.connection = self send_data "#{auth_data.to_json}\n" end
Private Instance Methods
Processes data received in the receive_data
callback
# File lib/syncano/sync_connection.rb, line 86 def process_data begin data = ::ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(received_data)) packet = ::Syncano::Packets::Base.instantize_packet(data) if packet.notification? notification = ::Syncano::Resources::Notifications::Base.instantize_notification(client, packet) callbacks_queue.each do |callback_name| callbacks[callback_name].call(notification) end elsif packet.call_response? queue_response(packet) elsif packet.auth? queue_response(packet) end self.received_data = '' rescue Exception => e p 'EXCEPTION!' p e.inspect end end
Removes old call response packets from the responses queue
# File lib/syncano/sync_connection.rb, line 120 def prune_responses_queue while !responses_queue.empty? message_id = responses_queue.first if responses[message_id].nil? || Time.now - responses[message_id].timestamp.to_time > 2.minutes responses_queue.shift responses.delete(message_id) else break end end end
Adds call response packet to the responses queue @param [Syncano::Packets::CallResponse] packet
# File lib/syncano/sync_connection.rb, line 112 def queue_response(packet) prune_responses_queue message_id = packet.message_id.to_s responses[message_id] = packet responses_queue << message_id.to_s end