Project

General

Profile

« Previous | Next » 

Revision fd4ccbdd

Added by Marc Dequènes over 13 years ago

  • ID fd4ccbddaa0989a06add8eac41dba03823d784b2

[evol] preliminary work for Conversation synchronization in DSL (action callbacks had to be moved from Protocol to ConversationThread to be able to check idle with the new locks, needed to process notifications without falling idle)

View differences:

lib/cyborghood/cyborg/protocol.rb
@negociation_received = false
@negociation_sent = false
@negociation_ok = false
@action_followup = {}
end
def negociation_ok?
......
method = "receive_" + message.action_code.downcase.tr(" ", "_")
if respond_to? method
send(method, message)
check_thread_idle(message.conv_thread)
else
send_error_protocol "unknown action"
end
......
end
def receive_error_action(message)
cb = pop_callback(message)
if cb
error = message.action_parameters[:error]
exception = CyberError.new(error[:severity], error[:category], error[:message])
cb.call({:status => :error, :exception => exception})
else
send_error_protocol("received reply for unknown action")
message.pop_callback do |cb|
if cb
error = message.action_parameters[:error]
exception = CyberError.new(error[:severity], error[:category], error[:message])
cb.call({:status => :error, :exception => exception})
else
send_error_protocol("received reply for unknown action")
end
end
end
......
end
def receive_reply_decline(message)
cb = pop_callback(message)
if cb
cb.call({:status => :decline, :reason => message.action_parameters[:reason]})
else
send_error_protocol("received reply for unknown action")
message.pop_callback do |cb|
if cb
cb.call({:status => :decline, :reason => message.action_parameters[:reason]})
else
send_error_protocol("received reply for unknown action")
end
end
end
def receive_reply_result(message)
cb = pop_callback(message)
if cb
cb.call({:status => :ok, :result => message.action_parameters[:result]})
else
send_error_protocol("received reply for unknown action")
message.pop_callback do |cb|
if cb
cb.call({:status => :ok, :result => message.action_parameters[:result]})
else
send_error_protocol("received reply for unknown action")
end
end
end
......
def send_request_call(conv_thread, node, *parameters, &callback)
message = conv_thread.new_message("REQUEST CALL", { :node => node, :parameters => parameters }).send
register_callback(message, callback)
message.register_callback(callback)
end
def send_request_exists(conv_thread, node)
message = conv_thread.new_message("REQUEST EXISTS", { :node => node }).send
register_callback(message, callback)
message.register_callback(callback)
end
def send_request_describe(conv_thread, node)
message = conv_thread.new_message("REQUEST DESCRIBE", { :node => node }).send
register_callback(message, callback)
message.register_callback(callback)
end
def send_error_protocol(error, fatal = false)
......
@conversation.thread('system').new_message("QUIT LEAVING").send
end
end
protected
def register_callback(message, callback)
@action_followup[message.conv_thread.id] ||= {}
@action_followup[message.conv_thread.id][message.action_id] = callback
end
def pop_callback(message)
return nil unless @action_followup.has_key? message.conv_thread.id
return nil unless @action_followup[message.conv_thread.id].has_key? message.action_id
cb = @action_followup[message.conv_thread.id][message.action_id]
@action_followup[message.conv_thread.id].delete message.action_id
@action_followup.delete message.conv_thread.id if @action_followup[message.conv_thread.id].empty?
cb
end
def check_thread_idle(conv_thread)
return if @action_followup.has_key? conv_thread.id
@conversation.bot.get_channel("peer/#{@conversation.identifier}/system") << {
:topic => 'THREAD IDLE',
:thread => conv_thread.name
}
end
end
end

Also available in: Unified diff