changeset 2342:f047d5410040

core (memory/disco): added use_cache parameter to discoInfos/discoItems (set to False to ignore cache)
author Goffi <goffi@goffi.org>
date Tue, 22 Aug 2017 22:12:57 +0200 (2017-08-22)
parents f9580b4a105a
children ca1ab42c7ae9
files frontends/src/bridge/dbus_bridge.py src/bridge/bridge_constructor/bridge_template.ini src/bridge/dbus_bridge.py src/memory/disco.py
diffstat 4 files changed, 36 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/dbus_bridge.py	Sun Aug 20 17:37:43 2017 +0200
+++ b/frontends/src/bridge/dbus_bridge.py	Tue Aug 22 22:12:57 2017 +0200
@@ -209,23 +209,23 @@
             error_handler = lambda err:errback(dbus_to_bridge_exception(err))
         return self.db_core_iface.delContact(entity_jid, profile_key, timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler)
 
-    def discoInfos(self, entity_jid, node=u'', profile_key=u"@DEFAULT@", callback=None, errback=None):
+    def discoInfos(self, entity_jid, node=u'', use_cache=True, profile_key=u"@DEFAULT@", callback=None, errback=None):
         if callback is None:
             error_handler = None
         else:
             if errback is None:
                 errback = log.error
             error_handler = lambda err:errback(dbus_to_bridge_exception(err))
-        return self.db_core_iface.discoInfos(entity_jid, node, profile_key, timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler)
+        return self.db_core_iface.discoInfos(entity_jid, node, use_cache, profile_key, timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler)
 
-    def discoItems(self, entity_jid, node=u'', profile_key=u"@DEFAULT@", callback=None, errback=None):
+    def discoItems(self, entity_jid, node=u'', use_cache=True, profile_key=u"@DEFAULT@", callback=None, errback=None):
         if callback is None:
             error_handler = None
         else:
             if errback is None:
                 errback = log.error
             error_handler = lambda err:errback(dbus_to_bridge_exception(err))
-        return self.db_core_iface.discoItems(entity_jid, node, profile_key, timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler)
+        return self.db_core_iface.discoItems(entity_jid, node, use_cache, profile_key, timeout=const_TIMEOUT, reply_handler=callback, error_handler=error_handler)
 
     def disconnect(self, profile_key="@DEFAULT@", callback=None, errback=None):
         if callback is None:
--- a/src/bridge/bridge_constructor/bridge_template.ini	Sun Aug 20 17:37:43 2017 +0200
+++ b/src/bridge/bridge_constructor/bridge_template.ini	Tue Aug 22 22:12:57 2017 +0200
@@ -696,14 +696,16 @@
 async=
 type=method
 category=core
-sig_in=sss
+sig_in=ssbs
 sig_out=(asa(sss)a{sa(a{ss}as)})
 param_1_default=u''
-param_2_default=u"@DEFAULT@"
+param_2_default=True
+param_3_default=u"@DEFAULT@"
 doc=Discover infos on an entity
 doc_param_0=entity_jid: JID to discover
 doc_param_1=node: node to use
-doc_param_2=%(doc_profile_key)s
+doc_param_2=use_cache: use cached data if available
+doc_param_3=%(doc_profile_key)s
 doc_return=discovery data:
  - list of features
  - list of identities (category, type, name)
@@ -720,14 +722,16 @@
 async=
 type=method
 category=core
-sig_in=sss
+sig_in=ssbs
 sig_out=a(sss)
 param_1_default=u''
-param_2_default=u"@DEFAULT@"
+param_2_default=True
+param_3_default=u"@DEFAULT@"
 doc=Discover items of an entity
 doc_param_0=entity_jid: JID to discover
 doc_param_1=node: node to use
-doc_param_2=%(doc_profile_key)s
+doc_param_2=use_cache: use cached data if available
+doc_param_3=%(doc_profile_key)s
 doc_return=array of tuple (entity, node identifier, name)
 
 [saveParamsTemplate]
--- a/src/bridge/dbus_bridge.py	Sun Aug 20 17:37:43 2017 +0200
+++ b/src/bridge/dbus_bridge.py	Tue Aug 22 22:12:57 2017 +0200
@@ -234,16 +234,16 @@
         return self._callback("delContact", unicode(entity_jid), unicode(profile_key), callback=callback, errback=errback)
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
-                         in_signature='sss', out_signature='(asa(sss)a{sa(a{ss}as)})',
+                         in_signature='ssbs', out_signature='(asa(sss)a{sa(a{ss}as)})',
                          async_callbacks=('callback', 'errback'))
-    def discoInfos(self, entity_jid, node=u'', profile_key=u"@DEFAULT@", callback=None, errback=None):
-        return self._callback("discoInfos", unicode(entity_jid), unicode(node), unicode(profile_key), callback=callback, errback=errback)
+    def discoInfos(self, entity_jid, node=u'', use_cache=True, profile_key=u"@DEFAULT@", callback=None, errback=None):
+        return self._callback("discoInfos", unicode(entity_jid), unicode(node), use_cache, unicode(profile_key), callback=callback, errback=errback)
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
-                         in_signature='sss', out_signature='a(sss)',
+                         in_signature='ssbs', out_signature='a(sss)',
                          async_callbacks=('callback', 'errback'))
-    def discoItems(self, entity_jid, node=u'', profile_key=u"@DEFAULT@", callback=None, errback=None):
-        return self._callback("discoItems", unicode(entity_jid), unicode(node), unicode(profile_key), callback=callback, errback=errback)
+    def discoItems(self, entity_jid, node=u'', use_cache=True, profile_key=u"@DEFAULT@", callback=None, errback=None):
+        return self._callback("discoItems", unicode(entity_jid), unicode(node), use_cache, unicode(profile_key), callback=callback, errback=errback)
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
                          in_signature='s', out_signature='',
--- a/src/memory/disco.py	Sun Aug 20 17:37:43 2017 +0200
+++ b/src/memory/disco.py	Tue Aug 22 22:12:57 2017 +0200
@@ -147,17 +147,21 @@
         if identity is not None and identity not in disco_infos.identities:
             raise failure.Failure(exceptions.FeatureNotFound())
 
-    def getInfos(self, client, jid_=None, node=u''):
+    def getInfos(self, client, jid_=None, node=u'', use_cache=True):
         """get disco infos from jid_, filling capability hash if needed
 
         @param jid_: jid of the target, or None for profile's server
         @param node(unicode): optional node to use for disco request
+        @param use_cache(bool): if True, use cached data if available
         @return: a Deferred which fire disco.DiscoInfo
         """
         if jid_ is None:
             jid_ = jid.JID(client.jid.host)
         try:
             cap_hash = self.host.memory.getEntityData(jid_, [C.ENTITY_CAP_HASH], client.profile)[C.ENTITY_CAP_HASH]
+            if not use_cache:
+                # we ignore cache, so we pretend we haven't found it
+                raise KeyError
         except (KeyError, exceptions.UnknownEntityError):
             # capability hash is not available, we'll compute one
             def infosCb(disco_infos):
@@ -186,11 +190,12 @@
             return defer.succeed(disco_infos)
 
     @defer.inlineCallbacks
-    def getItems(self, client, jid_=None, node=u''):
+    def getItems(self, client, jid_=None, node=u'', use_cache=True):
         """get disco items from jid_, cache them for our own server
 
         @param jid_(jid.JID): jid of the target, or None for profile's server
         @param node(unicode): optional node to use for disco request
+        @param use_cache(bool): if True, use cached data if available
         @return: a Deferred which fire disco.DiscoItems
         """
         if jid_ is None:
@@ -199,6 +204,9 @@
             try:
                 items = self.host.memory.getEntityData(jid_, ["DISCO_ITEMS"], client.profile)["DISCO_ITEMS"]
                 log.debug(u"[%s] disco items are in cache" % jid_.full())
+                if not use_cache:
+                    # we ignore cache, so we pretend we haven't found it
+                    raise KeyError
             except (KeyError, exceptions.UnknownEntityError):
                 log.debug(u"Caching [%s] disco items" % jid_.full())
                 items = yield client.disco.requestItems(jid_, nodeIdentifier=node)
@@ -308,16 +316,17 @@
         return cap_hash
 
     @defer.inlineCallbacks
-    def _discoInfos(self, entity_jid_s, node=u'', profile_key=C.PROF_KEY_NONE):
+    def _discoInfos(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
         """ Discovery method for the bridge
         @param entity_jid_s: entity we want to discover
+        @param use_cache(bool): if True, use cached data if available
         @param node(unicode): optional node to use
 
         @return: list of tuples
         """
         client = self.host.getClient(profile_key)
         entity = jid.JID(entity_jid_s)
-        disco_infos = yield self.getInfos(client, entity, node)
+        disco_infos = yield self.getInfos(client, entity, node, use_cache)
         extensions = {}
         for form_type, form in disco_infos.extensions.items():
             fields = []
@@ -350,14 +359,15 @@
             yield (item.entity.full(), item.nodeIdentifier or '', item.name or '')
 
     @defer.inlineCallbacks
-    def _discoItems(self, entity_jid_s, node=u'', profile_key=C.PROF_KEY_NONE):
+    def _discoItems(self, entity_jid_s, node=u'', use_cache=True, profile_key=C.PROF_KEY_NONE):
         """ Discovery method for the bridge
 
         @param entity_jid_s: entity we want to discover
         @param node(unicode): optional node to use
+        @param use_cache(bool): if True, use cached data if available
         @return: list of tuples"""
         client = self.host.getClient(profile_key)
         entity = jid.JID(entity_jid_s)
-        disco_items = yield self.getItems(client, entity, node)
+        disco_items = yield self.getItems(client, entity, node, use_cache)
         ret = list(self.items2tuples(disco_items))
         defer.returnValue(ret)