Project

General

Profile

« Previous | Next » 

Revision feb16bd0

Added by Marc Dequènes about 13 years ago

  • ID feb16bd04a7ec187c0b34b06c5680c51197af84f

[evol] Cyborg/Interface: support returning API call replies asynchronously

View differences:

lib/cyborghood/cyborg/botnet/interface.rb
request.reply.results = list.nil? ? search_among_children(request, lookup_node) :
search_in_list(request, lookup_node, list)
end
request.send_reply
end
end
end
......
not node.nil?
end
def _call(session, node_path, args = nil)
def _call(session, node_path, args = nil, &send_result_cb)
args ||= []
raise CyberError.new(:unrecoverable, 'api/cyborghood', "wrong format for arguments when calling node '#{node_path}'") unless args.is_a? Array
......
raise CyberError.new(:unrecoverable, 'api/cyborghood', "unknown node '#{node_path}'") if node.nil?
logger.debug "[Server API] Node '#{node_path}' found"
node.__send__(:request, session, args)
node.__send__(:request, session, args, &send_result_cb)
end
protected
......
class Request
attr_reader :session, :args, :reply
def initialize(session, args = [])
def initialize(session, args = [], &send_result_cb)
@session = session
@args = args
@send_result_cb = send_result_cb
@reply = {
:results => {},
......
:errors => []
}.to_ostruct
end
def send_reply
@send_result_cb.call @reply.to_hash
end
end
def request(session, args = [])
def request(session, args = [], &send_result_cb)
request = Request.new(session, args, &send_result_cb)
if @request_cb
request = Request.new(session, args)
begin
@request_cb.call(request)
# TODO: full reply needed
request.reply.results
rescue
logger.debug "node request error message: " + $!
logger.debug "node request error backtrace: " + $!.backtrace.join("\n")
raise CyberError.new(:unrecoverable, 'api/cyborghood', "call failed on node '#{node_path}': " + $!)
end
else
visible_nodes_names
request.reply.results = visible_nodes_names
request.send_reply
end
end

Also available in: Unified diff