Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to do a blocking request. #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/le.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ def self.new(token, options={})

opt_udp_port = options[:udp_port] || nil
opt_use_data_endpoint = options[:data_endpoint] || false
opt_use_blocking_request = options[:use_blocking_request] || false

self.checkParams(token, opt_datahub_enabled, opt_udp_port)


host = Le::Host.new(token, opt_local, opt_debug, opt_ssl, opt_datahub_endpoint, opt_host_id, opt_custom_host, opt_udp_port, opt_use_data_endpoint)
host = Le::Host.new(token, opt_local, opt_debug, opt_ssl, opt_datahub_endpoint, opt_host_id, opt_custom_host, opt_udp_port, opt_use_data_endpoint, opt_use_blocking_request)

if defined?(ActiveSupport::TaggedLogging) && opt_tag
logger = ActiveSupport::TaggedLogging.new(Logger.new(host))
Expand Down
4 changes: 2 additions & 2 deletions lib/le/host.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Le
module Host

def self.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint)
def self.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint, use_blocking_request)

Le::Host::HTTP.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint)
Le::Host::HTTP.new(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint, use_blocking_request)
end

module InstanceMethods
Expand Down
33 changes: 22 additions & 11 deletions lib/le/host/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class HTTP

include Le::Host::InstanceMethods
#! attr_accessor :token, :queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :dathub_ip, :datahub_port, :host_id, :custom_host, :host_name_enabled, :host_name
attr_accessor :token, :queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :datahub_ip, :datahub_port, :datahub_endpoint, :host_id, :host_name_enabled, :host_name, :custom_host, :udp_port, :use_data_endpoint
attr_accessor :token, :queue, :started, :thread, :conn, :local, :debug, :ssl, :datahub_enabled, :datahub_ip, :datahub_port, :datahub_endpoint, :host_id, :host_name_enabled, :host_name, :custom_host, :udp_port, :use_data_endpoint, :use_blocking_request


def initialize(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint)
def initialize(token, local, debug, ssl, datahub_endpoint, host_id, custom_host, udp_port, use_data_endpoint, use_blocking_request)
if local
device = if local.class <= TrueClass
if defined?(Rails)
Expand All @@ -42,6 +42,7 @@ def initialize(token, local, debug, ssl, datahub_endpoint, host_id, custom_host,
@ssl = ssl
@udp_port = udp_port
@use_data_endpoint = use_data_endpoint
@use_blocking_request = use_blocking_request

@datahub_endpoint = datahub_endpoint
if !@datahub_endpoint[0].empty?
Expand Down Expand Up @@ -132,17 +133,22 @@ def write(message)
@logger_console.add(Logger::Severity::UNKNOWN, message)
end

# Check async.
if message.scan(/\n/).empty?
@queue << "#{ @token } #{ message } \n"
@message = "#{ @token } #{ message } \n"
else
@queue << "#{ message.gsub(/^/, "\1#{ @token } [#{ random_message_id }]") }\n"
@message = "#{ message.gsub(/^/, "\1#{ @token } [#{ random_message_id }]") }\n"
end


if @started
check_async_thread
unless @use_blocking_request
@queue << @message
if @started
check_async_thread
else
start_async_thread
end
else
start_async_thread
run(@message)
end
end

Expand Down Expand Up @@ -173,7 +179,7 @@ def openConnection
else
port = DATA_PORT_UNSECURE
end
else
else
if @udp_port
host = API_SERVER
port = @udp_port
Expand Down Expand Up @@ -259,11 +265,14 @@ def closeConnection
end
end

def run
def run(data=nil)
reopenConnection

loop do
data = @queue.pop
# Get data from queue if we're async, otherwise use param.
unless @use_blocking_request
data = @queue.pop
end
break if data == SHUTDOWN_COMMAND
loop do
begin
Expand All @@ -279,6 +288,8 @@ def run

break
end
# Don't loop if we're not async.
data = SHUTDOWN_COMMAND if @use_blocking_request
end

dbg "LE: Closing Asyncrhonous socket writer"
Expand Down