comparison libervia.tac @ 3:154d4caa57f4

server side: proper profile management in signals generic callback
author Goffi <goffi@goffi.org>
date Sun, 13 Feb 2011 16:16:40 +0100
parents 669c531a857e
children c28a4840e1a8
comparison
equal deleted inserted replaced
2:669c531a857e 3:154d4caa57f4
180 180
181 def __init__(self, sat_host): 181 def __init__(self, sat_host):
182 Resource.__init__(self) 182 Resource.__init__(self)
183 self.register=None 183 self.register=None
184 self.sat_host=sat_host 184 self.sat_host=sat_host
185 self.signalDeferred = None 185 self.signalDeferred = {}
186 186
187 def plugRegister(self, register): 187 def plugRegister(self, register):
188 self.register = register 188 self.register = register
189 189
190 def jsonrpc_getSignals(self): 190 def jsonrpc_getSignals(self):
191 """Keep the connection alive until a signal is received, then send it 191 """Keep the connection alive until a signal is received, then send it
192 @return: (signal, *signal_args)""" 192 @return: (signal, *signal_args)"""
193 self.signalDeferred = defer.Deferred() 193 _session = self.request.getSession()
194 return self.signalDeferred 194 profile = _session.sat_profile
195 self.signalDeferred[profile] = defer.Deferred()
196 return self.signalDeferred[profile]
195 197
196 def getGenericCb(self, function_name): 198 def getGenericCb(self, function_name):
197 """Return a generic function which send all params to signalDeferred.callback""" 199 """Return a generic function which send all params to signalDeferred.callback
200 function must have profile as last argument"""
198 def genericCb(*args): 201 def genericCb(*args):
199 if self.signalDeferred: 202 profile = args[-1]
200 self.signalDeferred.callback((function_name,args)) 203 if profile in self.signalDeferred:
204 self.signalDeferred[profile].callback((function_name,args[:-1]))
201 else: 205 else:
202 print("Warning: signal [%s] can't be sent" % function_name) 206 print("Warning: signal [%s] can't be sent" % function_name)
203 return genericCb 207 return genericCb
204 208
205 def genericCallback(self, *args):
206 """"This method send all params to signalDeferred"""
207 if self.signalDeferred:
208 print ("Sending signals %s")
209
210 def connected(self, profile): 209 def connected(self, profile):
211 assert(self.register) #register must be plugged 210 assert(self.register) #register must be plugged
212 request = self.register.getWaitingRequest(profile) 211 request = self.register.getWaitingRequest(profile)
213 if request: 212 if request:
214 self.register._logged(profile, request) 213 self.register._logged(profile, request)
257 print(u"Can't connect to SàT backend, are you sure it's launched ?") 256 print(u"Can't connect to SàT backend, are you sure it's launched ?")
258 import sys 257 import sys
259 sys.exit(1) 258 sys.exit(1)
260 self.bridge.register("connected", self.signal_handler.connected) 259 self.bridge.register("connected", self.signal_handler.connected)
261 self.bridge.register("connectionError", self.signal_handler.connectionError) 260 self.bridge.register("connectionError", self.signal_handler.connectionError)
262 for signal_name in ['presenceUpdate']: 261 for signal_name in ['presenceUpdate', 'personalEvent']:
263 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name)) 262 self.bridge.register(signal_name, self.signal_handler.getGenericCb(signal_name))
264 263
265 def startService(self): 264 def startService(self):
266 reactor.listenTCP(8080, self.site) 265 reactor.listenTCP(8080, self.site)
267 266