Project

General

Profile

« Previous | Next » 

Revision 45333094

Added by Marc Dequènes over 13 years ago

  • ID 4533309490bb30fb5b2827d0f1140afa339799bf

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

View differences:

lib/cyborghood/cyborg/conversation.rb
end
def new_message(action_code, parameters = nil, action_id = nil)
Message.new(self, action_code, parameters)
Message.new(self, action_code, parameters, action_id)
end
def next_action_id
......
end
class Message
private_class_method :new
attr_reader :conv_thread, :action_code, :action_parameters, :action_id
attr_reader :conv_thread, :action_code, :action_parameters, :action_id, :reply_to_action_id
def initialize(conv_thread, action_code, action_parameters = nil, action_id = nil)
@conv_thread = conv_thread
@action_code = action_code
@action_parameters = action_parameters
@action_id = action_id
@reply_to_action_id = action_id
@action_id = nil
end
def new?
@reply_to_action_id.nil?
end
def sent?
@action_id.nil?
end
def send
raise CyberError.new(:unrecoverable, "bot/conversation", "Not sending twice the same message") unless self.new?
raise CyberError.new(:unrecoverable, "bot/conversation", "Not sending twice the same message") unless self.sent?
@action_id = @conv_thread.next_action_id
@conv_thread.conversation.send_message(self)
end
def create_reply(action_code, parameters)
def create_reply(action_code, parameters = nil)
raise CyberError.new(:unrecoverable, "bot/conversation", "Cannot reply to a newly created message") if self.new?
new(self, action_code, parameters, @action_id)
self.class.new(@conv_thread, action_code, parameters, @action_id)
end
end
class Conversation < EventMachine::Protocols::LineAndTextProtocol
private_class_method :new
# don't rely on EventMachine's default, it may change one day
MaxLineLength = 16*1024
EOD = "\033[0J"
BOT_ID_PATTERN = "[a-zA-Z0-9]+"
BOT_NAME_PATTERN = "[A-Z][a-zA-Z0-9]+"
ACTION_WORD_PATTERN = "[a-zA-Z0-9]+"
ACTION_PATTERN = "^(#{BOT_ID_PATTERN})(\d{4})-(\d{4})([+]?) (#{ACTION_WORD_PATTERN}( #{ACTION_WORD_PATTERN})*)$"
ACTION_PATTERN = "^(#{BOT_NAME_PATTERN})-([0-9]{4})-([0-9]{4})([+]?) (#{ACTION_WORD_PATTERN}( #{ACTION_WORD_PATTERN})*)$"
MAXIMUM_ERROR_COUNT = 3
MAXIMUM_LINES = 1024
attr_reader :bot
attr_reader :bot, :peer_name, :peer_capabilities
def initialize(bot, &block)
def initialize(bot, block = nil)
@bot = bot
@comm_logic_block = block
......
@config = Config.instance
@receive_error_count = 0
@error_count = 0
@split_data_mode = false
@split_data_message = nil
@split_data = []
# associated conversation threads
@conv_threads = {}
@conv_threads_index = {}
# thread 0 is reserved
@next_thread_id = 0
@system_thread = self.thread('system')
# post-negociation peer info
@peer_id = nil
@peer_name = nil
@peer_capabilities = []
@comm_stop = false
@protocol = BotProtocol.new(self)
end
def post_init
logger.debug "New conversation with #{identifier}"
logger.info "New conversation with #{identifier}"
@protocol.send_announce_helo unless @comm_logic_block.nil?
rescue
logger.error "Conversation post_init: " + $!
connection_close
end
def receive_line(data)
return if data.empty?
return if @comm_stop or data.empty?
clear_receive_info
......
logger.debug "Received data [#{identifier}]: #{data}"
if data =~ Regexp.new(ACTION_PATTERN)
conv_thread_id = $1
action_id = $2
flags = $3 || ""
action_code = $4
# TODO: pass peer_name into Message and use it in Protocol to check replies match
peer_name = $1
conv_thread_id = $2.to_i
action_id = $3.to_i
flags = $4 || ""
action_code = $5
conv_thread = self.thread_by_id(conv_thread_id)
message = conv_thread.new_message(action_code, nil, action_id)
......
end
end
send_reply
check_errors
end
def receive_message(message)
logger.debug "Received message '#{action}' [#{identifier}]"
logger.debug "Received message '#{message.action_code}' [#{identifier}]"
@protocol.process_received_message(message)
end
......
end
def unbind
logger.debug "Conversation finished with #{identifier}"
@bot.unregister_communication @peer_id unless @peer_id.nil?
@conv_threads.each_values {|s| s.close }
logger.info "Conversation finished with #{identifier} (#{@peer_name})"
@bot.unregister_communication @peer_name unless @peer_name.nil?
@conv_threads.each_value {|s| s.close }
@comm_logic_block.call false unless @comm_logic_block.nil? or @protocol.negociation_ok?
end
def bye
send_quit_leaving
@protocol.send_quit_leaving
end
def set_peer_info(id, capabilities)
@peer_id = id
def set_peer_info(name, capabilities)
@peer_name = name
@peer_capabilities = capabilities || []
@bot.register_communication @peer_id, self
logger.info "Peer name for #{identifier}: #{@peer_name}"
logger.info "Peer capabilities for #{identifier}: #{@peer_capabilities.join(", ")}"
@bot.register_communication @peer_name, self
end
def set_comm_ready
@comm_logic_block.call self
logger.info "Protocol negociation with '#{@peer_name}' on #{identifier} succeeded"
@comm_logic_block.call self unless @comm_logic_block.nil?
end
def set_comm_stop
@comm_stop = true
yield if block_given?
close_connection_after_writing
end
def set_error_status(fatal = false)
......
def send_message(message)
raise CyberError.new(:unrecoverable, "bot/conversation", "Cannot send message without action id") if message.action_id.nil?
send_line "#{@config.bot_id}-#{message.conv_thread.id}-#{message.action_id}" + (message.action_parameters.nil? ? "" : "+") + " #{message.action_code}"
flags = ""
flags += "+" unless message.action_parameters.nil?
send_line sprintf("%s-%04d-%04d%s %s", @bot.name, message.conv_thread.id, message.action_id, flags, message.action_code)
unless message.action_parameters.nil?
message.action_parameters.to_yaml.split.each {|l| send_line l }
message.action_parameters.to_yaml.each_line {|l| send_line l }
send_line EOD
end
action_id
end
protected
......
msg_quit = "previous fatal error"
end
unless msg_quit.nil?
send_quit_decline msg_quit
@comm_logic_block.call false
close_connection_after_writing
end
@protocol.send_quit_decline msg_quit unless msg_quit.nil?
end
def new_thread(name, id = nil)
......
if @split_data_mode
logger.debug "Protocol info [#{identifier}]: quit split mode for action '#{@split_data_message.action_id}'"
parameters = YAML.parse(@split_data.join("\n"))
parameters = YAML.load(@split_data.join("\n"))
reply_syntax_error("bad parameters format") if parameters.nil?
message = @split_data_message.conv_thread.new_message(@split_data_message.action_code, parameters, @split_data_message.action_id)
message = @split_data_message.conv_thread.new_message(@split_data_message.action_code, parameters, @split_data_message.reply_to_action_id)
receive_message(message)
else
reply_fatal_error "not in split mode"

Also available in: Unified diff