Class: Syncano::SyncConnection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/syncano/sync_connection.rb

Overview

Represents connection with Sync Server

Instance Method Summary collapse

Constructor Details

#initializeSyncConnection

Constructor for Syncano::SyncConnection object



7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#append_callback(callback_name, callback) ⇒ Object

Appends callback method to the end of callbacks chain

Parameters:

  • callback_name (Symbol, String)
  • callback (Block)


50
51
52
53
# File 'lib/syncano/sync_connection.rb', line 50

def append_callback(callback_name, callback)
  callbacks[callback_name] = callback
  callbacks_queue << callback_name
end

#connection_completedObject

Eventmachine callback invoked after completing connection



21
22
23
# File 'lib/syncano/sync_connection.rb', line 21

def connection_completed
  start_tls
end

#get_response(message_id) ⇒ Syncano::Packets::CallResponse

Gets call response packet from the responses queue

Parameters:

  • message_id (Integer, String)

Returns:



73
74
75
# File 'lib/syncano/sync_connection.rb', line 73

def get_response(message_id)
  responses.delete(message_id.to_s)
end

#prepend_callback(callback_name, callback) ⇒ Object

Prepends callback method to the beginning of callbacks chain

Parameters:

  • callback_name (Symbol, String)
  • callback (Block)


58
59
60
61
# File 'lib/syncano/sync_connection.rb', line 58

def prepend_callback(callback_name, callback)
  callbacks[callback_name] = callback
  callbacks_queue.unshift(callback_name)
end

#receive_data(data) ⇒ Object

Eventmachine callback invoked after receiving data from socket Data are parsed here and processed by callbacks chain



42
43
44
45
# File 'lib/syncano/sync_connection.rb', line 42

def receive_data(data)
  self.received_data += data
  process_data if data.end_with?("\n")
end

#remove_callback(callback_name) ⇒ Object

Removes callback from callbacks chain

Parameters:

  • callback_name (Symbol, String)


65
66
67
68
# File 'lib/syncano/sync_connection.rb', line 65

def remove_callback(callback_name)
  callbacks.delete(callback_name)
  callbacks_queue.delete(callback_name)
end

#ssl_handshake_completedObject

Eventmachine callback invoked after completing ssl handshake



26
27
28
29
30
31
32
33
34
35
36
37
38
# 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