comparison src/core/xmpp.py @ 2498:d6de69da3dd4

core (client): component improvments: - renamed component boolean to is_component for more clarity - profileConnected/profileDisconnected don't use a suffix anymore, it's called for both client and component. To check for there, the is_component boolean is enough - fixed dependencies handling - componentStart is not mandatory anymore, as a component can just be used with its handler
author Goffi <goffi@goffi.org>
date Wed, 28 Feb 2018 18:28:39 +0100
parents 38f472dbfcf0
children d485e9416493
comparison
equal deleted inserted replaced
2497:38f472dbfcf0 2498:d6de69da3dd4
53 self.profile = profile 53 self.profile = profile
54 self.host_app = host_app 54 self.host_app = host_app
55 self.cache = cache.Cache(host_app, profile) 55 self.cache = cache.Cache(host_app, profile)
56 self._mess_id_uid = {} # map from message id to uid used in history. Key: (full_jid,message_id) Value: uid 56 self._mess_id_uid = {} # map from message id to uid used in history. Key: (full_jid,message_id) Value: uid
57 self.conn_deferred = defer.Deferred() 57 self.conn_deferred = defer.Deferred()
58 self._progress_cb = {} # callback called when a progress is requested (key = progress id)
59 self.actions = {} # used to keep track of actions for retrieval (key = action_id)
58 60
59 ## initialisation ## 61 ## initialisation ##
60 62
61 @defer.inlineCallbacks 63 @defer.inlineCallbacks
62 def _callConnectionTriggers(self): 64 def _callConnectionTriggers(self):
74 plugin.getHandler(self).setHandlerParent(self) 76 plugin.getHandler(self).setHandlerParent(self)
75 77
76 # profileConnecting/profileConnected methods handling 78 # profileConnecting/profileConnected methods handling
77 79
78 # profile connecting is called right now (before actually starting client) 80 # profile connecting is called right now (before actually starting client)
79 connecting_cb = getattr(plugin, "profileConnecting" + self.trigger_suffix, None) 81 connecting_cb = getattr(plugin, "profileConnecting", None)
80 if connecting_cb is not None: 82 if connecting_cb is not None:
81 yield connecting_cb(self) 83 yield connecting_cb(self)
82 84
83 # profile connected is called after client is ready and roster is got 85 # profile connected is called after client is ready and roster is got
84 connected_cb = getattr(plugin, "profileConnected" + self.trigger_suffix, None) 86 connected_cb = getattr(plugin, "profileConnected", None)
85 if connected_cb is not None: 87 if connected_cb is not None:
86 plugin_conn_cb.append((plugin, connected_cb)) 88 plugin_conn_cb.append((plugin, connected_cb))
87 89
88 defer.returnValue(plugin_conn_cb) 90 defer.returnValue(plugin_conn_cb)
89 91
242 def _cleanConnection(self, dummy): 244 def _cleanConnection(self, dummy):
243 """method called on disconnection 245 """method called on disconnection
244 246
245 used to call profileDisconnected* triggers 247 used to call profileDisconnected* triggers
246 """ 248 """
247 trigger_name = "profileDisconnected" + self.trigger_suffix 249 trigger_name = "profileDisconnected"
248 for plugin in self._getPluginsList(): 250 for plugin in self._getPluginsList():
249 disconnected_cb = getattr(plugin, trigger_name, None) 251 disconnected_cb = getattr(plugin, trigger_name, None)
250 if disconnected_cb is not None: 252 if disconnected_cb is not None:
251 yield disconnected_cb(self) 253 yield disconnected_cb(self)
252 254
452 454
453 455
454 class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient): 456 class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
455 implements(iwokkel.IDisco) 457 implements(iwokkel.IDisco)
456 trigger_suffix = "" 458 trigger_suffix = ""
457 component = False 459 is_component = False
458 460
459 def __init__(self, host_app, profile, user_jid, password, host=None, port=C.XMPP_C2S_PORT, max_retries=C.XMPP_MAX_RETRIES): 461 def __init__(self, host_app, profile, user_jid, password, host=None, port=C.XMPP_C2S_PORT, max_retries=C.XMPP_MAX_RETRIES):
460 # XXX: DNS SRV records are checked when the host is not specified. 462 # XXX: DNS SRV records are checked when the host is not specified.
461 # If no SRV record is found, the host is directly extracted from the JID. 463 # If no SRV record is found, the host is directly extracted from the JID.
462 self.started = time.time() 464 self.started = time.time()
490 host = host, 492 host = host,
491 port = port)) 493 port = port))
492 494
493 wokkel_client.XMPPClient.__init__(self, user_jid, password, host or None, port or C.XMPP_C2S_PORT) 495 wokkel_client.XMPPClient.__init__(self, user_jid, password, host or None, port or C.XMPP_C2S_PORT)
494 SatXMPPEntity.__init__(self, host_app, profile, max_retries) 496 SatXMPPEntity.__init__(self, host_app, profile, max_retries)
495 self._progress_cb = {} # callback called when a progress is requested (key = progress id)
496 self.actions = {} # used to keep track of actions for retrieval (key = action_id)
497 497
498 def _getPluginsList(self): 498 def _getPluginsList(self):
499 for p in self.host_app.plugins.itervalues(): 499 for p in self.host_app.plugins.itervalues():
500 if C.PLUG_MODE_CLIENT in p._info[u'modes']: 500 if C.PLUG_MODE_CLIENT in p._info[u'modes']:
501 yield p 501 yield p
580 An entry point plugin is launched after component is connected. 580 An entry point plugin is launched after component is connected.
581 Component need to instantiate MessageProtocol itself 581 Component need to instantiate MessageProtocol itself
582 """ 582 """
583 implements(iwokkel.IDisco) 583 implements(iwokkel.IDisco)
584 trigger_suffix = "Component" # used for to distinguish some trigger points set in SatXMPPEntity 584 trigger_suffix = "Component" # used for to distinguish some trigger points set in SatXMPPEntity
585 component = True 585 is_component = True
586 sendHistory = False # XXX: set to True from entry plugin to keep messages in history for received messages 586 sendHistory = False # XXX: set to True from entry plugin to keep messages in history for received messages
587 587
588 def __init__(self, host_app, profile, component_jid, password, host=None, port=None, max_retries=C.XMPP_MAX_RETRIES): 588 def __init__(self, host_app, profile, component_jid, password, host=None, port=None, max_retries=C.XMPP_MAX_RETRIES):
589 self.started = time.time() 589 self.started = time.time()
590 if port is None: 590 if port is None:
637 637
638 for import_name in current._info.get(C.PI_DEPENDENCIES, []): 638 for import_name in current._info.get(C.PI_DEPENDENCIES, []):
639 # plugins are already loaded as dependencies 639 # plugins are already loaded as dependencies
640 # so we know they are in self.host_app.plugins 640 # so we know they are in self.host_app.plugins
641 dep = self.host_app.plugins[import_name] 641 dep = self.host_app.plugins[import_name]
642 self._checkDependencies(dep, plugins) 642 self._buildDependencies(dep, plugins)
643 643
644 for import_name in current._info.get(C.PI_RECOMMENDATIONS, []): 644 for import_name in current._info.get(C.PI_RECOMMENDATIONS, []):
645 # here plugins are only recommendations, 645 # here plugins are only recommendations,
646 # so they may not exist in self.host_app.plugins 646 # so they may not exist in self.host_app.plugins
647 try: 647 try:
662 self._buildDependencies(self.entry_plugin, plugins) 662 self._buildDependencies(self.entry_plugin, plugins)
663 return plugins 663 return plugins
664 664
665 def entityConnected(self): 665 def entityConnected(self):
666 # we can now launch entry point 666 # we can now launch entry point
667 return self.entry_plugin.componentStart(self) 667 try:
668 start_cb = self.entry_plugin.componentStart
669 except AttributeError:
670 return
671 else:
672 return start_cb(self)
668 673
669 def addPostXmlCallbacks(self, post_xml_treatments): 674 def addPostXmlCallbacks(self, post_xml_treatments):
670 if self.sendHistory: 675 if self.sendHistory:
671 post_xml_treatments.addCallback(self.messageAddToHistory) 676 post_xml_treatments.addCallback(self.messageAddToHistory)
672 677