I need to create a little client for a third party web service. The client should poll the service constantly. I need to monitor for new events (they don't have callbacks apparently...) so they keep the connection open for 15 seconds and returns nothing if nothing has happened. If an event happens then it returns when it actually happens. After returning I should immediately request again.
This is fine as long as I only need to poll for one customer (since one request only can check for one customer). The problem is that we need to poll this service for multiple customers at the same time and then I of course need threads.
I was thinking along the following lines (something is wrong with the threading parts, when I run it without threads it works so it is not the soap stuff that is wrong)
require 'soap/rpc/driver'
CreateSessionResponse = Struct.new(:response_code, :session_id, :host)
proxy = SOAP::RPC::Driver.new('url_to_service, 'urn:webservice')
[
%w(createSession password name type tenant domain application),
%w(getEvents sessionId)
].each do |signature|
proxy.add_method(*signature)
end
class Client
def initialize(name = "", password = "", type = "", tenant = "", domain = "", application = "")
@name, @password, @type, @tenant, @domain, @application = name, password, type, tenant, domain, application
end
def run(proxy)
thread = Thread.new {
while true
# log in to server and get a session id
session = CreateSessionResponse.new(*proxy.createSession(@password, @name, @type, @tenant, @domain, @application))
# fetch events. returns after 15 seconds if no new events
events = proxy.getEvents(session.session_id)
if events
notify_stb(events)
else
puts "no new events" # never see's this
end
end
}
thread.join # without joining the program exits immediately
end
def notify_stb(events)
puts "Notifying client with data #{events}" # never see's this
end
end
puts "Starting..."
# when testing I only have data for one client, will be more later
clients = [["name", "pwd","","","domain","application"]].inject() {
result, client_data| result << Client.new(*client_data)}
clients.each { |client| client.run(proxy) }
It might be that I actually do requests. The service is remote and I don't have a chance to check if the requests are actually getting there (and there is also this 4:th of July in the States apparently...).
Can anyone see anything obviously wrong? I haven't really done anything with threads in Ruby before so...
Regards,
Marcus