comparison libervia.tac @ 140:09a512d9a0c0

server side: fixed getHistory call and action result management
author Goffi <goffi@goffi.org>
date Wed, 14 Nov 2012 22:24:07 +0100
parents b145da69a218
children 8635bc9db9bf
comparison
equal deleted inserted replaced
139:b6658f3ac8a0 140:09a512d9a0c0
91 91
92 def directoryListing(self): 92 def directoryListing(self):
93 return NoResource() 93 return NoResource()
94 94
95 class SATActionIDHandler(object): 95 class SATActionIDHandler(object):
96 """Manage SàT action id lifecycle""" 96 """Manage SàT action action_id lifecycle"""
97 ID_LIFETIME = 30 #after this time (in seconds), id will be suppressed and action result will be ignored 97 ID_LIFETIME = 30 #after this time (in seconds), action_id will be suppressed and action result will be ignored
98 98
99 def __init__(self): 99 def __init__(self):
100 self.waiting_ids = {} 100 self.waiting_ids = {}
101 101
102 def waitForId(self, id, callback, *args, **kwargs): 102 def waitForId(self, callback, action_id, profile, *args, **kwargs):
103 """Wait for an action result 103 """Wait for an action result
104 @param id: id to wait for
105 @param callback: method to call when action gave a result back 104 @param callback: method to call when action gave a result back
105 @param action_id: action_id to wait for
106 @param profile: %(doc_profile)s
106 @param *args: additional argument to pass to callback 107 @param *args: additional argument to pass to callback
107 @param **kwargs: idem""" 108 @param **kwargs: idem"""
108 self.waiting_ids[id] = (callback, args, kwargs) 109 action_tuple = (action_id, profile)
109 reactor.callLater(self.ID_LIFETIME, self.purgeID, id) 110 self.waiting_ids[action_tuple] = (callback, args, kwargs)
110 111 reactor.callLater(self.ID_LIFETIME, self.purgeID, action_tuple)
111 def purgeID(self, id): 112
112 """Called when an id has not be handled in time""" 113 def purgeID(self, action_tuple):
113 if id in self.waiting_ids: 114 """Called when an action_id has not be handled in time"""
114 warning ("action of id %s has not been managed, id is now ignored" % id) 115 if action_tuple in self.waiting_ids:
115 del self.waiting_ids[id] 116 warning ("action of action_id %s [%s] has not been managed, action_id is now ignored" % action_tuple)
116 117 del self.waiting_ids[action_tuple]
117 def actionResultCb(self, answer_type, id, data): 118
119 def actionResultCb(self, answer_type, action_id, data, profile):
118 """Manage the actionResult signal""" 120 """Manage the actionResult signal"""
119 if id in self.waiting_ids: 121 action_tuple = (action_id, profile)
120 callback, args, kwargs = self.waiting_ids[id] 122 if action_tuple in self.waiting_ids:
121 del self.waiting_ids[id] 123 callback, args, kwargs = self.waiting_ids[action_tuple]
122 callback(answer_type, id, data, *args, **kwargs) 124 del self.waiting_ids[action_tuple]
125 callback(answer_type, action_id, data, *args, **kwargs)
123 126
124 class MethodHandler(jsonrpc.JSONRPC): 127 class MethodHandler(jsonrpc.JSONRPC):
125 128
126 def __init__(self, sat_host): 129 def __init__(self, sat_host):
127 jsonrpc.JSONRPC.__init__(self) 130 jsonrpc.JSONRPC.__init__(self)
244 return {} 247 return {}
245 if JID(from_jid).userhost() != sat_jid.userhost() and JID(to_jid).userhost() != sat_jid.userhost(): 248 if JID(from_jid).userhost() != sat_jid.userhost() and JID(to_jid).userhost() != sat_jid.userhost():
246 error("Trying to get history from a different jid, maybe a hack attempt ?") 249 error("Trying to get history from a different jid, maybe a hack attempt ?")
247 return {} 250 return {}
248 d = defer.Deferred() 251 d = defer.Deferred()
249 self.sat_host.bridge.getHistory(from_jid, to_jid, size, between, callback=d.callback, errback=d.errback) 252 self.sat_host.bridge.getHistory(from_jid, to_jid, size, between, profile, callback=d.callback, errback=d.errback)
250 def show(result_dbus): 253 def show(result_dbus):
251 result = [] 254 result = []
252 for line in result_dbus: 255 for line in result_dbus:
253 #XXX: we have to do this stupid thing because Python D-Bus use its own types instead of standard types 256 #XXX: we have to do this stupid thing because Python D-Bus use its own types instead of standard types
254 # and txJsonRPC doesn't accept D-Bus types, resulting in a empty query 257 # and txJsonRPC doesn't accept D-Bus types, resulting in a empty query
450 self.sat_host.bridge.setParam("JabberID", "%s@%s/%s" % (login, _NEW_ACCOUNT_DOMAIN, _NEW_ACCOUNT_RESOURCE), "Connection", profile) 453 self.sat_host.bridge.setParam("JabberID", "%s@%s/%s" % (login, _NEW_ACCOUNT_DOMAIN, _NEW_ACCOUNT_RESOURCE), "Connection", profile)
451 self.sat_host.bridge.setParam("Server", _NEW_ACCOUNT_SERVER, "Connection", profile) 454 self.sat_host.bridge.setParam("Server", _NEW_ACCOUNT_SERVER, "Connection", profile)
452 self.sat_host.bridge.setParam("Password", password, "Connection", profile) 455 self.sat_host.bridge.setParam("Password", password, "Connection", profile)
453 #and the account 456 #and the account
454 action_id = self.sat_host.bridge.registerNewAccount(login, password, email, _NEW_ACCOUNT_DOMAIN, 5222) 457 action_id = self.sat_host.bridge.registerNewAccount(login, password, email, _NEW_ACCOUNT_DOMAIN, 5222)
455 self.sat_host.action_handler.waitForId(action_id, self._postAccountCreation, profile) 458 self.sat_host.action_handler.waitForId(self._postAccountCreation, action_id, profile)
456 459
457 #time to send the email 460 #time to send the email
458 461
459 _email_host = _REG_EMAIL_SERVER 462 _email_host = _REG_EMAIL_SERVER
460 _email_from = _REG_EMAIL_FROM 463 _email_from = _REG_EMAIL_FROM