module PluginTool @@notification_announce_reconnect = false def self.start_notifications @@notification_queue_name = nil Thread.new do while true begin self.do_notifications rescue => e puts "NOTICE: Lost notification connection, will attempt to reconnect soon." @@notification_announce_reconnect = true sleep(5) # throttle errors end end end end def self.do_notifications http = make_http_connection puts "NOTICE: Notification connection established." if @@notification_announce_reconnect while true sleep(0.25) # small throttle of requests path = '/api/development_plugin_loader/get_notifications' path << "?queue=#{@@notification_queue_name}" if @@notification_queue_name request = Net::HTTP::Get.new(path) setup_request(request) # Server uses long-polling, and will respond after a long timeout, or when a notification # has been queued for sending to this process. response = http.request(request) if response.kind_of?(Net::HTTPOK) @@notification_queue_name = response['X-Queue-Name'] begin decode_and_handle_notifications response.body rescue puts "NOTICE: Error handling notification from server." end else raise "Bad response" end end end def self.decode_and_handle_notifications(encoded) size = encoded.length pos = 0 while pos < (size - 12) type = encoded[pos, 4] data_size = encoded[pos + 4, 8].to_i(16) data = encoded[pos + 12, data_size] pos += 12 + data_size handle_notification(type, data) end end def self.handle_notification(type, data) case type when 'log ' # Output from console.log() puts "LOG:#{data}" else puts "WARNING: Unknown notification received from server. Upgrade the plugin tool." sleep(5) # throttle problematic responses end end end