What is the call returning? Is it nil or an empty array? Not sure if
its causing the issue, but you shouldn''t be using instance variables
(@buddy_list vs. buddy_list) as that will propagate from one function
call to the next since I believe that the ChatWorker is only
instantiated once when backgroundrb starts up. Depending on how
you''re calling the functions perhaps one following behind is setting
the instance variable back to [] before you read it?
Side note, you''re making a socket call to send this chunk of work to
backgroundrb, but you''re doing so synchronously; in that case you
would probably just want to do the work in the rails request. I think
perhaps what you mean to do is async_get_buddies, which would offload
the request to backgroundrb and return immediately; then you could
use .ask_result later to get it (more info here:
http://backgroundrb.rubyforge.org/rails/).
Depends on your requirements naturally, but thought the link might
help.
\Peter
On Dec 22, 11:00 pm, Muruga Raj
<li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>
wrote:> Hello all,
>
> Subject: Not able to return object from backgroundrb worker
>
> I am trying to use xmpp4r and backgroundrb for my chat application.
> I am able to create a connectnion with XMPP4r through a worker in
> backgrounddrb.
> I have the problem in returning the object from the worker to my rails
> application.
>
> My code is as follows:
>
> class ChatWorker < BackgrounDRb::MetaWorker
> require ''xmpp4r''
> require ''xmpp4r/bytestreams''
> require ''xmpp4r/roster/helper/roster''
> require ''xmpp4r/roster/iq/roster''
> include Jabber
> Jabber::debug = true
>
> set_worker_name :chat_worker
> def create(args = nil)
> # this method is called, when worker is loaded for the first time
> logger.info ''Initializing Chat Worker.......''
> end
> def connect_chat(args)
> logger.info "Conencting chat.......#{args[:username]} --"
> jid = Jabber::JID.new(args[:username])
> @client = Jabber::Client.new(jid)
> @client.connect
> @client.auth(args[:password])
>
@client.send(Jabber::Presence.new.set_show(nil).set_status(''From
> Rails!''))
> logger.info "Chat Connection status -
#...@client.is_connected?}"
> return @client.is_connected?
> end
> def connected?
> return @client.is_connected?
> end
> def get_buddies
> if self.connected?
> @buddy_list = []
> roster = Jabber::Roster::Helper.new(@client)
> mainthread = Thread.current
> roster.add_query_callback { |iq|
> mainthread.wakeup
> }
> Thread.stop
> roster.groups.each { |group|
> roster.find_by_group(group).each { |item|
> @buddy_list << item#[{:name => item.iname, :jid =>
item.jid,
> :subscription => item.subscription}]
> }
> }
> logger.info "Buddies\n#{@buddy_list.inspect}"
> return @buddy_list
> end
>
> end
>
> When i call the connect_chat worker method from console i am able to
> connect to the server as below.
> MiddleMan.worker(:chat_worker).connect_chat(:arg => {:username =>
> ''...@gmail.com'',:password =>
''xxxx''})
>
> But when i try to call the get_buddies worker method, i am not able to
> get the buddy list as below.
> MiddleMan.worker(:chat_worker).get_buddies
> Even in the log file, i printed the @buddy_list object and its showing
> the correct result. But it is not returning the result.
>
> The problem is with result returning.
>
> Could you please help me in solving the issues. I am struggling with
> this for the whole day.
>
> Thank you for your any kind of suggestions.
>
> Regards
> Murugaraj B
> --
> Posted viahttp://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.