lib/syncano/sync_connection.rb in syncano-3.1.1.beta2 vs lib/syncano/sync_connection.rb in syncano-3.1.1.beta3

- old
+ new

@@ -12,10 +12,11 @@ self.responses = ::ActiveSupport::HashWithIndifferentAccess.new self.responses_queue = [] self.client = ::Syncano::Clients::Sync.instance + self.received_data = '' end # Eventmachine callback invoked after completing connection def connection_completed start_tls @@ -36,24 +37,12 @@ end # Eventmachine callback invoked after receiving data from socket # Data are parsed here and processed by callbacks chain def receive_data(data) - data = ::ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(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 += data + process_data if data.end_with?("\n") end # Appends callback method to the end of callbacks chain # @param [Symbol, String] callback_name # @param [Block] callback @@ -82,12 +71,41 @@ # @return [Syncano::Packets::CallResponse] def get_response(message_id) responses.delete(message_id.to_s) end + protected + + attr_accessor :received_data + private attr_accessor :client, :callbacks, :callbacks_queue, :responses, :responses_queue + + # Processes data received in the receive_data callback + 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 # Adds call response packet to the responses queue # @param [Syncano::Packets::CallResponse] packet def queue_response(packet) prune_responses_queue \ No newline at end of file