comparison src/memory/disco.py @ 1752:cbcc223c323a

core (disco): display a warning when a disco info/items request failed, and return empty DiscoInfo/DiscoITems
author Goffi <goffi@goffi.org>
date Thu, 17 Dec 2015 21:58:41 +0100
parents 373ce871b0f3
children d17772b0fe22
comparison
equal deleted inserted replaced
1751:77870d2e2902 1752:cbcc223c323a
32 from wokkel import disco 32 from wokkel import disco
33 from base64 import b64encode 33 from base64 import b64encode
34 from hashlib import sha1 34 from hashlib import sha1
35 35
36 36
37 PRESENCE = '/presence'
38 NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps'
39 CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]'
40 TIMEOUT = 15 37 TIMEOUT = 15
38 CAP_HASH_ERROR = 'ERROR'
41 39
42 class HashGenerationError(Exception): 40 class HashGenerationError(Exception):
43 pass 41 pass
44 42
45 43
62 60
63 persistent storage is update when a new hash is added 61 persistent storage is update when a new hash is added
64 """ 62 """
65 63
66 def __init__(self, persistent): 64 def __init__(self, persistent):
67 self.hashes = {} 65 self.hashes = {
66 CAP_HASH_ERROR: disco.DiscoInfo(), # used when we can't get disco infos
67 }
68 self.persistent = persistent 68 self.persistent = persistent
69 69
70 def __getitem__(self, key): 70 def __getitem__(self, key):
71 return self.hashes[key] 71 return self.hashes[key]
72 72
87 self.hashes[hash_] = disco.DiscoInfo.fromElement(element) 87 self.hashes[hash_] = disco.DiscoInfo.fromElement(element)
88 log.info(u"Disco hashes loaded") 88 log.info(u"Disco hashes loaded")
89 d = self.persistent.load() 89 d = self.persistent.load()
90 d.addCallback(fillHashes) 90 d.addCallback(fillHashes)
91 return d 91 return d
92
92 93
93 class Discovery(object): 94 class Discovery(object):
94 """ Manage capabilities of entities """ 95 """ Manage capabilities of entities """
95 96
96 def __init__(self, host): 97 def __init__(self, host):
163 def infosCb(disco_infos): 164 def infosCb(disco_infos):
164 cap_hash = self.generateHash(disco_infos) 165 cap_hash = self.generateHash(disco_infos)
165 self.hashes[cap_hash] = disco_infos 166 self.hashes[cap_hash] = disco_infos
166 self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile) 167 self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, cap_hash, profile_key=client.profile)
167 return disco_infos 168 return disco_infos
169 def infosEb(fail):
170 if fail.check(defer.CancelledError):
171 reason = u"request time-out"
172 else:
173 try:
174 reason = unicode(fail.value)
175 except AttributeError:
176 reason = unicode(fail)
177 log.warning(u"Error while requesting disco infos from {jid}: {reason}".format(jid=jid_.full(), reason=reason))
178 self.host.memory.updateEntityData(jid_, C.ENTITY_CAP_HASH, CAP_HASH_ERROR, profile_key=client.profile)
179 disco_infos = self.hashes[CAP_HASH_ERROR]
180 return disco_infos
168 d = client.disco.requestInfo(jid_) 181 d = client.disco.requestInfo(jid_)
169 d.addCallback(infosCb) 182 d.addCallback(infosCb)
183 d.addErrback(infosEb)
170 return d 184 return d
171 else: 185 else:
172 disco_infos = self.hashes[cap_hash] 186 disco_infos = self.hashes[cap_hash]
173 return defer.succeed(disco_infos) 187 return defer.succeed(disco_infos)
174 188
191 except (KeyError, exceptions.UnknownEntityError): 205 except (KeyError, exceptions.UnknownEntityError):
192 log.debug(u"Caching [%s] disco items" % jid_.full()) 206 log.debug(u"Caching [%s] disco items" % jid_.full())
193 items = yield client.disco.requestItems(jid_, nodeIdentifier) 207 items = yield client.disco.requestItems(jid_, nodeIdentifier)
194 self.host.memory.updateEntityData(jid_, "DISCO_ITEMS", items, profile_key=client.profile) 208 self.host.memory.updateEntityData(jid_, "DISCO_ITEMS", items, profile_key=client.profile)
195 else: 209 else:
196 items = yield client.disco.requestItems(jid_, nodeIdentifier) 210 try:
211 items = yield client.disco.requestItems(jid_, nodeIdentifier)
212 except StanzaError as e:
213 log.warning(u"Error while requesting items for {jid}: {reason}"
214 .format(jid=jid_.full(), reason=e.condition))
215 items = disco.DiscoItems()
197 216
198 defer.returnValue(items) 217 defer.returnValue(items)
199 218
200 219
201 def _infosEb(self, failure_, entity_jid): 220 def _infosEb(self, failure_, entity_jid):