Project

General

Profile

« Previous | Next » 

Revision 688a21d7

Added by Marc Dequènes over 13 years ago

  • ID 688a21d76fad2957379272f7d454ec283f8a4a33

[evol] conversation/bot protocol rework §3 (refs #30)

View differences:

lib/cyborghood/cyborg/conversation.rb
module BotProtocol
VERSION = "0.1"
module_function
# TODO:
# - check for request/reply couples (reply to wrong of non-existent request)
# - check for negociation wip/done
def process_received_message(message)
def initialize(conversation)
@conversation = conversation
@negociation_received = false
@negociation_sent = false
@negociation_ok = false
end
def negociation_ok?
@negociation_ok
end
def receive(action, parameters)
method = "receive_" + action.downcase.tr(" ", "_")
def process_received_message(message)
method = "receive_" + message.action_code.downcase.tr(" ", "_")
if respond_to? method
send(method, parameters)
send(method, message)
else
send_error_protocol("unknown action")
end
end
def receive_announce_helo(parameters)
unless parameters[:bot_name] =~ Conversation::BOT_ID_PATTERN
return send_error_protocol "bad bot name"
def receive_announce_helo(message)
unless message.conv_thread.id == 0
return send_quit_decline "bad negociation"
end
unless parameters[:protocol_version] == VERSION
# TODO: send QUIT DECLINE "protocol version does not match"
return
unless message.parameters[:bot_name] =~ Conversation::BOT_ID_PATTERN
return send_quit_decline "bad bot name"
end
@conversation.set_peer_info(parameters[:bot_name], parameters[:capabilities])
send_reply_ok
unless message.parameters[:protocol_version] == VERSION
return send_quit_decline "protocol version does not match"
end
@negociation_received = true
@conversation.set_peer_info(message.parameters[:bot_name], message.parameters[:capabilities])
if @negociation_sent
send_announce_ok(message)
@negociation_ok = true
else
send_announce_helo(message)
@negociation_sent = true
end
end
def receive_announce_ok(message)
unless @negociation_sent and @negociation_received
send_quit_decline "bad negociation"
end
@negociation_ok = true
end
def send_announce_helo(recv_message = nil)
action_code = "ANNOUNCE HELO"
message = (recv_message.nil? ? @conversation.thread('system').new_message(action_code) :
recv_message.create_reply(action_code))
message.send
end
def send_error_protocol(msg = nil)
@conversation.set_error_status
@conversation.send_peer("ERROR PROTO", { :error => msg || "" })
def send_announce_ok(recv_message)
recv_message.create_reply("ANNOUNCE OK").send
end
def send_reply_ack
@conversation.send_peer("REPLY ACK")
def send_error_protocol(message = nil)
# message can be nil if nothing could be parsed (or we may close the conversation, dunno)
# TODO
# @conversation.set_error_status
# @conversation.send_peer("ERROR PROTO", { :error => msg || "" })
end
def send_quit(reason = nil)
action = "QUIT " + (reason.nil? ? "LEAVING" : "DECLINE")
parameters = (reason.nil? ? nil : { :reason => reason })
@conversation.send_peer(action, parameters)
def send_reply_ack(recv_message)
recv_message.create_reply("REPLY ACK").send
end
def send_quit_decline(recv_message, reason)
recv_message.create_reply("QUIT LEAVING", { :reason => reason }).send
end
def send_quit_leaving
conversation.thread('system').new_message("QUIT LEAVING").send
end
end
class ConversationThread
attr_reader :conversation, :name, :id, :session
@@next_id = 0
def initialized(conversation, name)
def initialized(conversation, id, name)
@conversation = conversation
@name = name
@id = id
@id = @@next_id
@@next_id += 1
@session = Session.new
@next_action_id = 0
end
def new_message(action_code, parameters = nil, action_id = nil)
Message.new(this, action_code, parameters)
Message.new(self, action_code, parameters)
end
def next_action_id
......
def send
raise CyberError.new(:unrecoverable, "bot/conversation", "Not sending twice the same message") unless self.new?
@action_id = @conv_thread.next_action_id
@conv_thread.conversation.send_message(this)
@conv_thread.conversation.send_message(self)
end
def create_reply(action_code, parameters)
raise CyberError.new(:unrecoverable, "bot/conversation", "Cannot reply to a newly created message") if self.new?
new(this, action_code, parameters)
new(self, action_code, parameters)
end
end
......
# associated conversation threads
@conv_threads = {}
# thread 0 is reserved
@next_thread_id = 0
@system_thread = self.thread('system')
# post-negociation peer info
@peer_id = nil
@peer_capabilities = []
@protocol = BotProtocol.new(self)
end
def clear_receive_info
......
def receive_message(message)
logger.debug "Received message '#{action}' [#{identifier}]"
BotProtocol.process_received_message(message)
@protocol.process_received_message(message)
end
def receive_error(msg)
......
end
def thread(name = 'default')
th = @conv_threads[name] || ConversationThread.new(this, name)
# allow searching by id too
@conv_threads_index[th.id] = name
th
@conv_threads[name] || new_thread(name)
end
def thread_by_id(id)
name = @conv_threads_index[id]
name.nil? ? ConversationThread.new(this, "noname/#{index}") : @conv_threads[name]
name.nil? ? new_thread("noname/#{id}", id) : @conv_threads[name]
end
def send_message(message)
......
protected
def new_thread(name, id = nil)
id ||= @next_thread_id
th = ConversationThread.new(self, id, name)
@next_thread_id = [@next_thread_id, id + 1].max
@conv_threads[th.name] = th
# allow searching by id too
@conv_threads_index[th.id] = name
th
end
def reply_syntax_error(msg = nil)
logger.error "Protocol error [#{identifier}]: syntax error (#{msg})"
set_error_status

Also available in: Unified diff