comparison src/plugins/plugin_xep_0060.py @ 1452:5116d70ddd1c

plugin xep-0060,xep-0277: added getFromMany using new RTDeferredSession mechanism
author Goffi <goffi@goffi.org>
date Sat, 15 Aug 2015 22:22:34 +0200
parents 9b88b19b1ca8
children 4c4f88d7b156
comparison
equal deleted inserted replaced
1451:9b88b19b1ca8 1452:5116d70ddd1c
66 self.host = host 66 self.host = host
67 self.managedNodes = [] 67 self.managedNodes = []
68 self.rt_sessions = sat_defer.RTDeferredSessions() 68 self.rt_sessions = sat_defer.RTDeferredSessions()
69 host.bridge.addMethod("subscribeToMany", ".plugin", in_sign='a(ss)sa{ss}s', out_sign='s', method=self._subscribeToMany) 69 host.bridge.addMethod("subscribeToMany", ".plugin", in_sign='a(ss)sa{ss}s', out_sign='s', method=self._subscribeToMany)
70 host.bridge.addMethod("getSubscribeRTResult", ".plugin", in_sign='ss', out_sign='(ua(sss))', method=self._manySubscribeRTResult, async=True) 70 host.bridge.addMethod("getSubscribeRTResult", ".plugin", in_sign='ss', out_sign='(ua(sss))', method=self._manySubscribeRTResult, async=True)
71 host.bridge.addMethod("getFromMany", ".plugin", in_sign='a(ss)ia{ss}s', out_sign='s', method=self._getFromMany)
72 host.bridge.addMethod("getFromManyRTResult", ".plugin", in_sign='ss', out_sign='(ua(sssasa{ss}))', method=self._getFromManyRTResult, async=True)
71 73
72 def getHandler(self, profile): 74 def getHandler(self, profile):
73 client = self.host.getClient(profile) 75 client = self.host.getClient(profile)
74 client.pubsub_client = SatPubSubClient(self.host, self) 76 client.pubsub_client = SatPubSubClient(self.host, self)
75 return client.pubsub_client 77 return client.pubsub_client
113 115
114 def publish(self, service, nodeIdentifier, items=None, profile_key=C.PROF_KEY_NONE): 116 def publish(self, service, nodeIdentifier, items=None, profile_key=C.PROF_KEY_NONE):
115 client = self.host.getClient(profile_key) 117 client = self.host.getClient(profile_key)
116 return client.pubsub_client.publish(service, nodeIdentifier, items, client.pubsub_client.parent.jid) 118 return client.pubsub_client.publish(service, nodeIdentifier, items, client.pubsub_client.parent.jid)
117 119
118 def getItems(self, service, node, max_items=None, item_ids=None, sub_id=None, rsm=None, profile_key=C.PROF_KEY_NONE): 120 def getItems(self, service, node, max_items=None, item_ids=None, sub_id=None, rsm_request=None, profile_key=C.PROF_KEY_NONE):
119 """Retrieve pubsub items from a node. 121 """Retrieve pubsub items from a node.
120 122
121 @param service (JID): target service. 123 @param service (JID): pubsub service.
122 @param node (str): node id. 124 @param node (str): node id.
123 @param max_items (int): optional limit on the number of retrieved items. 125 @param max_items (int): optional limit on the number of retrieved items.
124 @param item_ids (list[str]): identifiers of the items to be retrieved (can't be used with rsm). 126 @param item_ids (list[str]): identifiers of the items to be retrieved (can't be used with rsm_request).
125 @param sub_id (str): optional subscription identifier. 127 @param sub_id (str): optional subscription identifier.
126 @param rsm (dict): RSM request data 128 @param rsm_request (rsm.RSMRequest): RSM request data
127 @param profile_key (str): %(doc_profile_key)s 129 @param profile_key (unicode): %(doc_profile_key)s
128 @return: a deferred couple (list[dict], dict) containing: 130 @return: a deferred couple (list[dict], dict) containing:
129 - list of items 131 - list of items
130 - metadata with the following keys: 132 - metadata with the following keys:
131 - rsm_first, rsm_last, rsm_count, rsm_index: first, last, count and index value of RSMResponse 133 - rsm_first, rsm_last, rsm_count, rsm_index: first, last, count and index value of RSMResponse
132 """ 134 """
133 if rsm and item_ids: 135 if rsm_request and item_ids:
134 raise ValueError("items_id can't be used with rsm") 136 raise ValueError("items_id can't be used with rsm")
135 client = self.host.getClient(profile_key) 137 client = self.host.getClient(profile_key)
136 ext_data = {'id': unicode(uuid.uuid4()), 'rsm': rsm} if rsm else None 138 ext_data = {'id': unicode(uuid.uuid4()), 'rsm': rsm_request} if rsm_request else None
137 d = client.pubsub_client.items(service, node, max_items, item_ids, sub_id, client.pubsub_client.parent.jid, ext_data) 139 d = client.pubsub_client.items(service, node, max_items, item_ids, sub_id, client.pubsub_client.parent.jid, ext_data)
138 def addMetadata(items): 140 def addMetadata(items):
139 if not rsm: 141 metadata = {}
140 metadata = {} 142 if rsm_request:
141 else:
142 rsm_data = client.pubsub_client.getRSMResponse(ext_data['id']) 143 rsm_data = client.pubsub_client.getRSMResponse(ext_data['id'])
143 metadata = {'rsm_{}'.format(key): value for key, value in rsm_data} 144 metadata.update({'rsm_{}'.format(key): value for key, value in rsm_data})
144 return (items, metadata) 145 return (items, metadata)
145 146
146 d.addCallback(addMetadata) 147 d.addCallback(addMetadata)
147 return d 148 return d
148 149
200 client = self.host.getClient(profile_key) 201 client = self.host.getClient(profile_key)
201 return client.pubsub_client.subscriptions(service, nodeIdentifier) 202 return client.pubsub_client.subscriptions(service, nodeIdentifier)
202 203
203 ## methods to manage several stanzas/jids at once ## 204 ## methods to manage several stanzas/jids at once ##
204 205
206 # generic #
207
208 def getRTResults(self, session_id, on_success=None, on_error=None, profile=C.PROF_KEY_NONE):
209 return self.rt_sessions.getResults(session_id, on_success, on_error, profile)
210
205 def serItemsData(self, items_data, item_cb=lambda item: item.toXml()): 211 def serItemsData(self, items_data, item_cb=lambda item: item.toXml()):
206 """Helper method to serialise result from [getItems] 212 """Helper method to serialise result from [getItems]
207 213
208 the items_data must be a tuple(list[domish.Element], dict[unicode, unicode]) 214 the items_data must be a tuple(list[domish.Element], dict[unicode, unicode])
209 as returned by [getItems]. metadata values are then casted to unicode and 215 as returned by [getItems]. metadata values are then casted to unicode and
297 # log.debug(u"Skip the subscription to [{node}]: node doesn't exist".format(node=nodeIdentifier)) 303 # log.debug(u"Skip the subscription to [{node}]: node doesn't exist".format(node=nodeIdentifier))
298 # continue # avoid sat-pubsub "SubscriptionExists" error 304 # continue # avoid sat-pubsub "SubscriptionExists" error
299 # d_list.append(client.pubsub_client.subscribe(service, nodeIdentifier, sub_jid or client.pubsub_client.parent.jid.userhostJID(), options=options)) 305 # d_list.append(client.pubsub_client.subscribe(service, nodeIdentifier, sub_jid or client.pubsub_client.parent.jid.userhostJID(), options=options))
300 # defer.returnValue(d_list) 306 # defer.returnValue(d_list)
301 307
308 # get #
309
310 def _getFromManyRTResult(self, session_id, profile_key=C.PROF_KEY_DEFAULT):
311 """Get real-time results for getFromMany session
312
313 @param session_id: id of the real-time deferred session
314 @param profile_key: %(doc_profile_key)s
315 @param return (tuple): (remaining, results) where:
316 - remaining is the number of still expected results
317 - results is a list of tuple with
318 - service (unicode): pubsub service
319 - node (unicode): pubsub node
320 - failure (unicode): empty string in case of success, error message else
321 - items (list[s]): raw XML of items
322 - metadata(dict): serialised metadata
323 """
324 profile = self.host.getClient(profile_key).profile
325 d = self.rt_sessions.getResults(session_id,
326 on_success=lambda result: ('', self.serItemsData(result)),
327 on_error=lambda failure: (unicode(failure.value) or UNSPECIFIED, ([],{})),
328 profile=profile)
329 d.addCallback(lambda ret: (ret[0],
330 [(service.full(), node, failure, items, metadata)
331 for (service, node), (success, (failure, (items, metadata))) in ret[1].iteritems()]))
332 return d
333
334 def _getFromMany(self, node_data, max_item=10, rsm_dict=None, profile_key=C.PROF_KEY_NONE):
335 """
336 @param max_item(int): maximum number of item to get, C.NO_LIMIT for no limit
337 """
338 max_item = None if max_item == C.NO_LIMIT else max_item
339 return self.getFromMany([(jid.JID(service), unicode(node)) for service, node in node_data], max_item, rsm.RSMRequest(**rsm_dict) if rsm_dict else None, profile_key)
340
341 def getFromMany(self, node_data, max_item=None, rsm_request=None, profile_key=C.PROF_KEY_NONE):
342 """Get items from many nodes at once
343 @param node_data (iterable[tuple]): iterable of tuple (service, node) where:
344 - service (jid.JID) is the pubsub service
345 - node (unicode) is the node to get items from
346 @param max_items (int): optional limit on the number of retrieved items.
347 @param rsm_request (RSMRequest): RSM request data
348 @param profile_key (unicode): %(doc_profile_key)s
349 @return (str): RT Deferred session id
350 """
351 client = self.host.getClient(profile_key)
352 deferreds = {}
353 for service, node in node_data:
354 deferreds[(service, node)] = self.getItems(service, node, max_item, rsm_request=rsm_request, profile_key=profile_key)
355 return self.rt_sessions.newSession(deferreds, client.profile)
356
302 357
303 class SatPubSubClient(rsm.PubSubClient): 358 class SatPubSubClient(rsm.PubSubClient):
304 implements(disco.IDisco) 359 implements(disco.IDisco)
305 360
306 def __init__(self, host, parent_plugin): 361 def __init__(self, host, parent_plugin):