7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/cirrocumulus/channels/jabber.rb', line 7
def initialize(jid)
@jid = "#{Cirrocumulus::Environment.current.name}-#{jid}"
@channel = JabberChannel.new()
@channel.connect(@jid)
@thrd = Thread.new do
parser = Sexpistol.new
while true do
@channel.tick()
instance = Ontology.query_ontology_instance(self)
if !instance.nil?
while true
msg = @channel.received_messages.pop(true) rescue nil
break if msg.nil?
begin
fipa_message = parser.parse_string(msg.body)
id = RemoteIdentifier.new(msg.from.resource)
next if fipa_message.size < 1
fipa_message = fipa_message.first
next if fipa_message.size < 2
act = fipa_message[0]
fipa_message.delete_at(0)
content = fipa_message
receiver = nil
action_content = nil
options = {}
content.each do |parameter|
next if !parameter.is_a?(Array) || parameter.size < 1
if parameter[0] == :receiver
receiver = parameter[1][2]
elsif parameter[0] == :content
action_content = parameter[1]
elsif [:ontology, :reply_with, :in_reply_to, :conversation_id].include?(parameter[0])
options[parameter[0]] = parameter[1]
elsif parameter[0] == :reply_to
end
end
next if options.has_key?(:ontology) && options[:ontology] != instance.name
next if !options.has_key?(:ontology) && (receiver.nil? || receiver != @jid)
case act
when :query
instance.handle_query(id, action_content, options)
when :query_if
instance.handle_query_if(id, action_content, options)
when :inform
instance.handle_inform(id, action_content, options)
when :request
instance.handle_request(id, action_content, options)
when :agree
instance.handle_agree(id, action_content, options)
when :refuse
instance.handle_refuse(id, action_content[0], action_content[1], options)
when :failure
instance.handle_failure(id, action_content[0], action_content[1], options)
end
rescue Exception => ex
Log4r::Logger['channels'].warn("Failed to process incoming message")
Log4r::Logger['channels'].debug("Message body: #{msg.body}")
Log4r::Logger['channels'].debug("Exception: #{ex.message}")
Log4r::Logger['channels'].debug("Backtrace: #{ex.backtrace.join("\n")}")
end
end
end
sleep 0.1
end
end
end
|