1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 """
29 Classes to connect the QT event loop with a messaging
30 server. To enable multiple clients to push model updates
31 to each other or messages for the users.
32
33 As a messaging server, Apache active MQ was tested in combination
34 with the stomp library (http://docs.codehaus.org/display/STOMP/Python)
35 """
36
37 import logging
38 import re
39
40 logger = logging.getLogger('remote_signals')
41
42 from PyQt4.QtCore import *
43
46 QObject.__init__(self)
47 import settings
48 self.entity_update_signal = SIGNAL("entity_update")
49 self.entity_delete_signal = SIGNAL("entity_delete")
50 self.entity_create_signal = SIGNAL("entity_create")
51 self.update_expression = re.compile(r'^/topic/Camelot.Entity.(?P<entity>.*).update$')
52 if hasattr(settings, 'CAMELOT_SERVER') and settings.CAMELOT_SERVER:
53 from stomp import stomp
54 self.connection = stomp.Connection(host_and_ports = [ (settings.CAMELOT_SERVER, 61613) ])
55 self.connection.add_listener(self)
56 self.connection.start()
57 logger.debug('connection to servers started')
58 else:
59 self.connection = None
60 logger.debug('not connected to a server')
64 from elixir import entities
65 logger.debug('received a message %s : %s'%(str(headers),message))
66 match = self.update_expression.match(headers['destination'])
67 if match:
68 entity = match.group('entity')
69 logger.debug(' decoded as update signal for entity %s'%entity)
70 self.emit(self.entity_update_signal, self, [e for e in entities if e.__name__==entity][0].get(eval(message)))
72 logger.debug('try to connect to message service')
73 self.connection.connect()
75 logger.debug('connected to message service %s, %s'%((str(args), str(kwargs))))
76 self.connection.subscribe(destination='/topic/Camelot.Entity.>', ack='auto')
78 logger.debug('stomp service disconnected')
82
83 self.emit(self.entity_update_signal, sender, entity)
84 if self.connection and scope=='remote':
85 self.connection.send(str([entity.id]), destination='/topic/Camelot.Entity.%s.update'%entity.__class__.__name__)
87 if self.connection and scope=='remote':
88 self.connection.send(str([entity.id]), destination='/topic/Camelot.Entity.%s.delete'%entity.__class__.__name__)
90 if self.connection and scope=='remote':
91 self.connection.send(str([entity.id]), destination='/topic/Camelot.Entity.%s.create'%entity.__class__.__name__)
92
93 _signal_handler_ = []
94
97
102
105