Mercurial > libervia-backend
comparison src/memory/disco.py @ 1684:373ce871b0f3
core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Nov 2015 21:41:15 +0100 |
parents | fd143578fe89 |
children | cbcc223c323a |
comparison
equal
deleted
inserted
replaced
1683:9bf1262297f2 | 1684:373ce871b0f3 |
---|---|
25 from twisted.words.protocols.jabber.error import StanzaError | 25 from twisted.words.protocols.jabber.error import StanzaError |
26 from twisted.internet import defer | 26 from twisted.internet import defer |
27 from twisted.internet import reactor | 27 from twisted.internet import reactor |
28 from twisted.python import failure | 28 from twisted.python import failure |
29 from sat.core.constants import Const as C | 29 from sat.core.constants import Const as C |
30 from sat.tools import xml_tools | |
31 from sat.memory import persistent | |
30 from wokkel import disco | 32 from wokkel import disco |
31 from base64 import b64encode | 33 from base64 import b64encode |
32 from hashlib import sha1 | 34 from hashlib import sha1 |
33 | 35 |
34 | 36 |
53 | 55 |
54 def __str__(self): | 56 def __str__(self): |
55 return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name) | 57 return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name) |
56 | 58 |
57 | 59 |
60 class HashManager(object): | |
61 """map object which manage hashes | |
62 | |
63 persistent storage is update when a new hash is added | |
64 """ | |
65 | |
66 def __init__(self, persistent): | |
67 self.hashes = {} | |
68 self.persistent = persistent | |
69 | |
70 def __getitem__(self, key): | |
71 return self.hashes[key] | |
72 | |
73 def __setitem__(self, hash_, disco_info): | |
74 if hash_ in self.hashes: | |
75 log.debug(u"ignoring hash set: it is already known") | |
76 return | |
77 self.hashes[hash_] = disco_info | |
78 self.persistent[hash_] = disco_info.toElement().toXml() | |
79 | |
80 def __contains__(self, hash_): | |
81 return self.hashes.__contains__(hash_) | |
82 | |
83 def load(self): | |
84 def fillHashes(hashes): | |
85 for hash_, xml in hashes.iteritems(): | |
86 element = xml_tools.ElementParser()(xml) | |
87 self.hashes[hash_] = disco.DiscoInfo.fromElement(element) | |
88 log.info(u"Disco hashes loaded") | |
89 d = self.persistent.load() | |
90 d.addCallback(fillHashes) | |
91 return d | |
92 | |
58 class Discovery(object): | 93 class Discovery(object): |
59 """ Manage capabilities of entities """ | 94 """ Manage capabilities of entities """ |
60 | 95 |
61 def __init__(self, host): | 96 def __init__(self, host): |
62 self.host = host | 97 self.host = host |
63 self.hashes = {} # key: capability hash, value: disco features/identities | 98 # TODO: remove legacy hashes |
64 # TODO: save hashes in databse, remove legacy hashes | 99 |
100 def load(self): | |
101 """Load persistent hashes""" | |
102 self.hashes = HashManager(persistent.PersistentDict("disco")) | |
103 return self.hashes.load() | |
65 | 104 |
66 @defer.inlineCallbacks | 105 @defer.inlineCallbacks |
67 def hasFeature(self, feature, jid_=None, profile=C.PROF_KEY_NONE): | 106 def hasFeature(self, feature, jid_=None, profile=C.PROF_KEY_NONE): |
68 """Tell if an entity has the required feature | 107 """Tell if an entity has the required feature |
69 | 108 |