def _check_age(self, pub, min_interval=timedelta(seconds=0)): """Check the age of the receiver. """ now = datetime.utcnow() if (now - self._last_age_check) <= min_interval: return LOGGER.debug("%s - checking addresses", str(datetime.utcnow())) self._last_age_check = now to_del = [] with self._address_lock: for addr, metadata in self._addresses.items(): atime = metadata["receive_time"] if now - atime > self._max_age: mda = { 'status': False, 'URI': addr, 'service': metadata['service'] } msg = Message('/address/' + metadata['name'], 'info', mda) to_del.append(addr) LOGGER.info("publish remove '%s'", str(msg)) pub.send(msg.encode()) for addr in to_del: del self._addresses[addr]
def test_encode(self): """Test the encoding of a message. """ subject = "/test/whatup/doc" atype = "info" data = "not much to say" msg1 = Message(subject, atype, data=data) sender = "%s@%s" % (msg1.user, msg1.host) self.assertEquals( _MAGICK + subject + " " + atype + " " + sender + " " + str(msg1.time.isoformat()) + " " + msg1.version + " " + "text/ascii" + " " + data, msg1.encode(), )
def test_pub_unicode(self): message = Message("/pџтяöll", "info", 'hej') with Publish("a_service", 9000) as pub: try: pub.send(message.encode()) except UnicodeDecodeError: self.fail("Sending raised UnicodeDecodeError unexpectedly!")
def test_encode_decode(self): """Test the encoding/decoding of the message class. """ msg1 = Message("/test/whatup/doc", "info", data="not much to say") sender = "%s@%s" % (msg1.user, msg1.host) self.assertTrue(sender == msg1.sender, msg="Messaging, decoding user, host from sender failed") msg2 = Message.decode(msg1.encode()) self.assertTrue(str(msg2) == str(msg1), msg="Messaging, encoding, decoding failed")
def test_pub_unicode(self): from posttroll.message import Message from posttroll.publisher import Publish message = Message("/pџтяöll", "info", 'hej') with Publish("a_service", 9000) as pub: try: pub.send(message.encode()) except UnicodeDecodeError: self.fail("Sending raised UnicodeDecodeError unexpectedly!")
def test_encode_decode(self): """Test the encoding/decoding of the message class. """ msg1 = Message('/test/whatup/doc', 'info', data='not much to say') sender = '%s@%s' % (msg1.user, msg1.host) self.assertTrue(sender == msg1.sender, msg='Messaging, decoding user, host from sender failed') msg2 = Message.decode(msg1.encode()) self.assertTrue(str(msg2) == str(msg1), msg='Messaging, encoding, decoding failed')
def test_encode(self): """Test the encoding of a message. """ subject = '/test/whatup/doc' atype = "info" data = 'not much to say' msg1 = Message(subject, atype, data=data) sender = '%s@%s' % (msg1.user, msg1.host) self.assertEquals( _MAGICK + subject + " " + atype + " " + sender + " " + str(msg1.time.isoformat()) + " " + msg1.version + " " + 'text/ascii' + " " + data, msg1.encode())
def test_encode_decode(self): """Test the encoding/decoding of the message class. """ msg1 = Message('/test/whatup/doc', 'info', data='not much to say') sender = '%s@%s' % (msg1.user, msg1.host) self.assertTrue( sender == msg1.sender, msg='Messaging, decoding user, host from sender failed') msg2 = Message.decode(msg1.encode()) self.assertTrue(str(msg2) == str(msg1), msg='Messaging, encoding, decoding failed')
def publish_message(self, mymessage): """Publish the message.""" posttroll_msg = Message(mymessage['header'], mymessage['type'], mymessage['content']) msg_to_publish = posttroll_msg.encode() manager = Manager() publisher_q = manager.Queue() pub_thread = PPSPublisher(publisher_q, self.nameservers) pub_thread.start() LOG.info("Sending: " + str(msg_to_publish)) publisher_q.put(msg_to_publish) pub_thread.stop()
def test_encode(self): """Test the encoding of a message. """ subject = '/test/whatup/doc' atype = "info" data = 'not much to say' msg1 = Message(subject, atype, data=data) sender = '%s@%s' % (msg1.user, msg1.host) self.assertEqual(_MAGICK + subject + " " + atype + " " + sender + " " + str(msg1.time.isoformat()) + " " + msg1.version + " " + 'text/ascii' + " " + data, msg1.encode())
def _check_age(self, pub, min_interval=timedelta(seconds=0)): """Check the age of the receiver. """ now = datetime.utcnow() if (now - self._last_age_check) <= min_interval: return LOGGER.debug("%s - checking addresses", str(datetime.utcnow()) ) self._last_age_check = now with self._address_lock: for addr, metadata in self._addresses.items(): atime = metadata["receive_time"] if now - atime > self._max_age: mda = {'status': False, 'URI': addr, 'service': metadata['service']} msg = Message('/address/' + metadata['name'], 'info', mda) del self._addresses[addr] LOGGER.info("publish remove '%s'", str(msg)) pub.send(msg.encode())
def _check_age(self, pub, min_interval=timedelta(seconds=0)): """Check the age of the receiver. """ now = datetime.utcnow() if (now - self._last_age_check) <= min_interval: return logger.debug(str(datetime.utcnow()) + " checking addresses") self._last_age_check = now self._address_lock.acquire() try: for addr, metadata in self._addresses.items(): atime = metadata["receive_time"] if now - atime > self._max_age: mda = {'status': False, 'URI': addr, 'service': metadata['service']} msg = Message('/address/' + metadata['name'], 'info', mda) del self._addresses[addr] logger.info("publish remove '%s'", str(msg)) pub.send(msg.encode()) finally: self._address_lock.release()