def _connect_routine(self): self.event_when_connected = "connected" record = yield from self.pick_dns_answer(self.default_domain) if record is not None: host, address, port = record self.address = (address, port) self._service_name = host else: # No DNS records left, stop iterating # and try (host, port) as a last resort self.dns_answers = None yield from asyncio.sleep(self.connect_loop_wait) try: yield from self.loop.create_connection(lambda: self, self.address[0], self.address[1], ssl=self.use_ssl) except Socket.gaierror as e: self.event('connection_failed', 'No DNS record available for %s' % self.default_domain) except OSError as e: log.debug('Connection failed: %s', e) self.event("connection_failed", e) self.connect_loop_wait = self.connect_loop_wait * 2 + 1 asyncio.async(self._connect_routine()) else: self.connect_loop_wait = 0
def start(self, event): """ Process the session_start event. Typical actions for the session_start event are requesting the roster and broadcasting an initial presence stanza. Arguments: event -- An empty dictionary. The session_start event does not provide any additional data. """ future = asyncio.Future() def callback(result): future.set_result(None) try: self.get_roster(callback=callback) yield from future except IqError as err: print('Error: %s' % err.iq['error']['condition']) except IqTimeout: print('Error: Request timed out') self.send_presence() print('Waiting for presence updates...\n') yield from asyncio.sleep(10) print('Roster for %s' % self.boundjid.bare) groups = self.client_roster.groups() for group in groups: print('\n%s' % group) print('-' * 72) for jid in groups[group]: sub = self.client_roster[jid]['subscription'] name = self.client_roster[jid]['name'] if self.client_roster[jid]['name']: print(' %s (%s) [%s]' % (name, jid, sub)) else: print(' %s [%s]' % (jid, sub)) connections = self.client_roster.presence(jid) for res, pres in connections.items(): show = 'available' if pres['show']: show = pres['show'] print(' - %s (%s)' % (res, show)) if pres['status']: print(' %s' % pres['status']) self.disconnect()
def process(self, *, forever=True, timeout=None): """Process all the available XMPP events (receiving or sending data on the socket(s), calling various registered callbacks, calling expired timers, handling signal events, etc). If timeout is None, this function will run forever. If timeout is a number, this function will return after the given time in seconds. """ if timeout is None: if forever: self.loop.run_forever() else: self.loop.run_until_complete(self.disconnected) else: tasks = [asyncio.sleep(timeout, loop=self.loop)] if not forever: tasks.append(self.disconnected) self.loop.run_until_complete(asyncio.wait(tasks, loop=self.loop))
def process(self, *, forever=True, timeout=None): """Process all the available XMPP events (receiving or sending data on the socket(s), calling various registered callbacks, calling expired timers, handling signal events, etc). If timeout is None, this function will run forever. If timeout is a number, this function will return after the given time in seconds. """ if timeout is None: if forever: self.loop.run_forever() else: self.loop.run_until_complete(self.disconnected) else: tasks = [asyncio.sleep(timeout)] if not forever: tasks.append(self.disconnected) self.loop.run_until_complete(asyncio.wait(tasks))