comparison src/plugins/plugin_xep_0115.py @ 437:02c633a0ddcf

plugin XEP-0115: entity capabilities now use PersistentBinaryDict to store hash cache
author Goffi <goffi@goffi.org>
date Wed, 30 Nov 2011 00:24:41 +0100
parents 7c79d4a8c9e6
children cf005701624b
comparison
equal deleted inserted replaced
436:5e9d28ca5109 437:02c633a0ddcf
22 from logging import debug, info, error, warning 22 from logging import debug, info, error, warning
23 from twisted.words.xish import domish 23 from twisted.words.xish import domish
24 from twisted.words.protocols.jabber import client, jid, xmlstream 24 from twisted.words.protocols.jabber import client, jid, xmlstream
25 from twisted.words.protocols.jabber import error as jab_error 25 from twisted.words.protocols.jabber import error as jab_error
26 from twisted.words.protocols.jabber.xmlstream import IQ 26 from twisted.words.protocols.jabber.xmlstream import IQ
27 from sat.memory.persistent import PersistentBinaryDict
27 import os.path 28 import os.path
28 import types 29 import types
29 30
30 from zope.interface import implements 31 from zope.interface import implements
31 32
71 def __str__(self): 72 def __str__(self):
72 return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name) 73 return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
73 74
74 75
75 class XEP_0115(): 76 class XEP_0115():
76 cap_hash = None 77 cap_hash = None #capabilities hash is class variable as it is common to all profiles
78 #TODO: this code is really dirty, need to clean it and try to move it to Wokkel
77 79
78 def __init__(self, host): 80 def __init__(self, host):
79 info(_("Plugin XEP_0115 initialization")) 81 info(_("Plugin XEP_0115 initialization"))
80 self.host = host 82 self.host = host
81 host.trigger.add("Disco Handled", self.checkHash) 83 host.trigger.add("Disco Handled", self.checkHash)
82 self.hash_cache = host.memory.getPrivate("entity_capabilities_cache") or {} #key = hash or jid 84 self.hash_cache = PersistentBinaryDict(NS_ENTITY_CAPABILITY) #key = hash or jid, value = features
83 self.jid_hash = {} #jid to hash mapping, map to a discoInfo if the hash method is unknown 85 self.hash_cache.load()
86 self.jid_hash = {} #jid to hash mapping, map to a discoInfo features if the hash method is unknown
84 87
85 def checkHash(self, profile): 88 def checkHash(self, profile):
86 if not XEP_0115.cap_hash: 89 if not XEP_0115.cap_hash:
87 XEP_0115.cap_hash = self.generateHash(profile) 90 XEP_0115.cap_hash = self.generateHash(profile)
88 else: 91 else:
163 def getDiscoItems(self, requestor, target, nodeIdentifier=''): 166 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
164 return [] 167 return []
165 168
166 def _updateCache(self, discoResult, from_jid, key): 169 def _updateCache(self, discoResult, from_jid, key):
167 """Actually update the cache 170 """Actually update the cache
168 @param discoResult: result of the requestInfo 171 @param discoResult: result of the requestInfo"""
169 @param known_hash: True if it's a hash method we know, we don't save the cache else"""
170 if key: 172 if key:
171 self.plugin_parent.jid_hash[from_jid] = key 173 self.plugin_parent.jid_hash[from_jid] = key
172 self.plugin_parent.hash_cache[key] = discoResult 174 self.plugin_parent.hash_cache[key] = discoResult.features
173 else: 175 else:
174 #No key, that mean unknown hash method 176 #No key, that means unknown hash method
175 self.plugin_parent.jid_hash[from_jid] = discoResult 177 self.plugin_parent.jid_hash[from_jid] = discoResult.features
176 self.host.memory.setPrivate("entity_capabilities_cache", self.plugin_parent.hash_cache)
177 178
178 179
179 def update(self, presence): 180 def update(self, presence):
180 """ 181 """
181 Manage the capabilities of the entity 182 Manage the capabilities of the entity
189 hash=c_elem['hash'] 190 hash=c_elem['hash']
190 node=c_elem['node'] 191 node=c_elem['node']
191 except KeyError: 192 except KeyError:
192 warning('Received invalid capabilities tag') 193 warning('Received invalid capabilities tag')
193 return 194 return
194 if not self.plugin_parent.jid_hash.has_key(from_jid): 195 if not from_jid in self.plugin_parent.jid_hash:
195 if self.plugin_parent.hash_cache.has_key(ver): 196 if ver in self.plugin_parent.hash_cache:
196 #we know that hash, we just link it with the jid 197 #we know that hash, we just link it with the jid
197 self.plugin_parent.jid_hash[from_jid] = ver 198 self.plugin_parent.jid_hash[from_jid] = ver
198 else: 199 else:
199 if hash!='sha-1': 200 if hash!='sha-1':
200 #unknown hash method 201 #unknown hash method