Mercurial > libervia-pubsub
comparison idavoll/idavoll.py @ 67:cdf17c4d3bf9
Made returning disco info use deferreds.
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Sat, 06 Nov 2004 22:17:18 +0000 |
parents | 60e95c60252d |
children | 5d7a924ebddb |
comparison
equal
deleted
inserted
replaced
66:13ced084aa69 | 67:cdf17c4d3bf9 |
---|---|
1 from twisted.protocols.jabber import component | 1 from twisted.protocols.jabber import component |
2 from twisted.application import service | 2 from twisted.application import service |
3 from twisted.python import components | 3 from twisted.python import components |
4 from twisted.internet import defer | |
4 import backend | 5 import backend |
5 import pubsub | 6 import pubsub |
6 import xmpp_error | 7 import xmpp_error |
7 | 8 |
8 import sys | 9 import sys |
32 features = [] | 33 features = [] |
33 | 34 |
34 if not node: | 35 if not node: |
35 features.extend([NS_DISCO_ITEMS, NS_VERSION]) | 36 features.extend([NS_DISCO_ITEMS, NS_VERSION]) |
36 | 37 |
37 return features | 38 return defer.succeed(features) |
38 | 39 |
39 def onVersion(self, iq): | 40 def onVersion(self, iq): |
40 iq.swapAttributeValues("to", "from") | 41 iq.swapAttributeValues("to", "from") |
41 iq["type"] = "result" | 42 iq["type"] = "result" |
42 name = iq.addElement("name", None, 'Idavoll') | 43 name = iq.addElement("name", None, 'Idavoll') |
43 version = iq.addElement("version", None, '0.1') | 44 version = iq.addElement("version", None, '0.1') |
44 self.send(iq) | 45 self.send(iq) |
45 iq.handled = True | 46 iq.handled = True |
46 | 47 |
47 def onDiscoInfo(self, iq): | 48 def onDiscoInfo(self, iq): |
48 identities = [] | 49 identities_deferreds = [] |
49 features = [] | 50 features_deferreds = [] |
50 node = iq.query.getAttribute("node") | 51 node = iq.query.getAttribute("node") |
51 | 52 |
52 for c in self.parent: | 53 for c in self.parent: |
53 if components.implements(c, component.IService): | 54 if components.implements(c, component.IService): |
54 if hasattr(c, "getIdentities"): | 55 if hasattr(c, "getIdentities"): |
55 identities.extend(c.getIdentities(node)) | 56 identities_deferreds.append(c.getIdentities(node)) |
56 if hasattr(c, "getFeatures"): | 57 if hasattr(c, "getFeatures"): |
57 features.extend(c.getFeatures(node)) | 58 features_deferreds.append(c.getFeatures(node)) |
59 print identities_deferreds | |
60 print features_deferreds | |
61 d1 = defer.DeferredList(identities_deferreds, fireOnOneErrback=1) | |
62 d2 = defer.DeferredList(features_deferreds, fireOnOneErrback=1) | |
63 d = defer.DeferredList([d1, d2], fireOnOneErrback=1) | |
64 d.addCallback(self._disco_info_results, iq, node) | |
65 d.addErrback(self._disco_info_error, iq) | |
66 d.addCallback(self.q) | |
67 d.addCallback(self.send) | |
68 | |
69 iq.handled = True | |
70 | |
71 def q(self, result): | |
72 print result | |
73 return result | |
74 | |
75 def _disco_info_results(self, results, iq, node): | |
76 identities = [] | |
77 for i in results[0][1]: | |
78 identities.extend(i[1]) | |
79 | |
80 features = [] | |
81 for f in results[1][1]: | |
82 features.extend(f[1]) | |
58 | 83 |
59 if node and not features and not identities: | 84 if node and not features and not identities: |
60 xmpp_error.error_from_iq(iq, 'item-not-found') | 85 return xmpp_error.error_from_iq(iq, 'item-not-found') |
61 else: | 86 else: |
62 iq.swapAttributeValues("to", "from") | 87 iq.swapAttributeValues("to", "from") |
63 iq["type"] = "result" | 88 iq["type"] = "result" |
64 for identity in identities: | 89 for identity in identities: |
65 i = iq.query.addElement("identity") | 90 i = iq.query.addElement("identity") |
67 print features | 92 print features |
68 for feature in features: | 93 for feature in features: |
69 f = iq.query.addElement("feature") | 94 f = iq.query.addElement("feature") |
70 f["var"] = feature | 95 f["var"] = feature |
71 | 96 |
72 self.send(iq) | 97 |
73 iq.handled = True | 98 return iq |
99 | |
100 def _disco_info_error(self, results, iq): | |
101 return xmpp_error.error_from_iq(iq, 'internal-error') | |
74 | 102 |
75 def onDiscoItems(self, iq): | 103 def onDiscoItems(self, iq): |
76 iq.swapAttributeValues("to", "from") | 104 iq.swapAttributeValues("to", "from") |
77 iq["type"] = "result" | 105 iq["type"] = "result" |
78 iq.query.children = [] | 106 iq.query.children = [] |