Revision fd4ccbdd
Added by Marc Dequènes over 13 years ago
- ID fd4ccbddaa0989a06add8eac41dba03823d784b2
lib/cyborghood/cyborg/conversation.rb | ||
---|---|---|
# no need for session for system thread
|
||
@session = Session.new unless name == 'system'
|
||
@next_action_id = 0
|
||
|
||
@callbacks = {}
|
||
@locks = Set.new
|
||
end
|
||
|
||
def new_message(action_code, parameters = nil, action_id = nil)
|
||
... | ... | |
def call(*args, &callback)
|
||
@conversation.protocol.send_request_call(self, *args, &callback)
|
||
end
|
||
|
||
def register_callback(message, callback)
|
||
@callbacks[message.action_id] = callback
|
||
end
|
||
|
||
def pop_callback(message)
|
||
cb = @callbacks[message.action_id]
|
||
yield cb if block_given?
|
||
@callbacks.delete(message.action_id)
|
||
check_idle
|
||
cb
|
||
end
|
||
|
||
def lock(name)
|
||
@locks << name
|
||
end
|
||
|
||
def unlock(name)
|
||
@locks.delete(name)
|
||
check_idle
|
||
end
|
||
|
||
def locked?
|
||
not @locks.empty?
|
||
end
|
||
|
||
def idle?
|
||
@callbacks.empty? and @locks.empty?
|
||
end
|
||
|
||
protected
|
||
|
||
def check_idle
|
||
return unless idle?
|
||
|
||
@conversation.bot.get_channel("peer/#{@conversation.identifier}/system") << {
|
||
:topic => 'THREAD IDLE',
|
||
:thread => @name
|
||
}
|
||
end
|
||
end
|
||
|
||
class Message
|
||
... | ... | |
raise CyberError.new(:unrecoverable, "bot/conversation", "Cannot reply to a newly created message") if self.new?
|
||
self.class.new(@conv_thread, action_code, parameters, @action_id)
|
||
end
|
||
|
||
# convenience method
|
||
def pop_callback(&block)
|
||
@conv_thread.pop_callback(self, &block)
|
||
end
|
||
|
||
# convenience method
|
||
def register_callback(callback)
|
||
@conv_thread.register_callback(self, callback)
|
||
end
|
||
end
|
||
|
||
class Conversation < EventMachine::Protocols::LineAndTextProtocol
|
Also available in: Unified diff
[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)