def req(body, agent=nil, options=false, waitfor=0)
if body.is_a?(Message)
agent = body.agent
waitfor = body.discovered_hosts.size || 0
@options = body.options
end
@options = options if options
stat = {:starttime => Time.now.to_f, :discoverytime => 0, :blocktime => 0, :totaltime => 0}
timeout = @discoverer.discovery_timeout(@options[:timeout], @options[:filter])
STDOUT.sync = true
hosts_responded = 0
reqid = nil
begin
Log.debug("Publishing request to agent %s with timeout %d" % [agent, timeout])
Timeout.timeout(timeout) do
reqid = sendreq(body, agent, @options[:filter])
loop do
resp = receive(reqid)
hosts_responded += 1
yield(resp.payload)
break if (waitfor != 0 && hosts_responded >= waitfor)
end
end
rescue Interrupt => e
rescue Timeout::Error => e
ensure
unsubscribe(agent, :reply)
end
stat[:totaltime] = Time.now.to_f - stat[:starttime]
stat[:blocktime] = stat[:totaltime] - stat[:discoverytime]
stat[:responses] = hosts_responded
stat[:noresponsefrom] = []
stat[:requestid] = reqid
@stats = stat
return stat
end