From 6c9e86665b122e1510aa1e60e4ddc2406f2d02d6 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Tue, 19 Sep 2023 14:20:21 +0000 Subject: [PATCH 1/6] Resolve issue 261 to not send empty files --- ssm/ssm2.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index 9cbc28e2..d2b198a0 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -380,7 +380,7 @@ def _send_msg_ams(self, text, msgid): encrypted. """ log.info('Sending message: %s', msgid) - if text is not None: + if text is not None and len(text) > 0: # First we sign the message to_send = crypto.sign(text, self._cert, self._key) # Possibly encrypt the message. @@ -496,8 +496,8 @@ def send_all(self): elif self._protocol == Ssm2.AMS_MESSAGING: # Then we are sending to an Argo Messaging Service instance. argo_id = self._send_msg_ams(text, msgid) - - log_string = "Sent %s, Argo ID: %s" % (msgid, argo_id) + if argo_id is not None: + log_string = "Sent %s, Argo ID: %s" % (msgid, argo_id) else: # The SSM has been improperly configured @@ -505,7 +505,10 @@ def send_all(self): self._protocol) # log that the message was sent - log.info(log_string) + if 'log_string' in locals(): + log.info(log_string) + else: + log.warning("Message %s is empty and returns a None type.", msgid) self._last_msg = None self._outq.remove(msgid) From f00a96e38e99873726f54a4b5d5ef82997275fc4 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Tue, 19 Sep 2023 15:30:52 +0000 Subject: [PATCH 2/6] Edit to ensure sequential nulls aren't sent --- ssm/ssm2.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index d2b198a0..d18977f1 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -498,6 +498,8 @@ def send_all(self): argo_id = self._send_msg_ams(text, msgid) if argo_id is not None: log_string = "Sent %s, Argo ID: %s" % (msgid, argo_id) + else: + log_string = "Message %s is empty and returns a None type." % (msgid) else: # The SSM has been improperly configured @@ -505,10 +507,10 @@ def send_all(self): self._protocol) # log that the message was sent - if 'log_string' in locals(): - log.info(log_string) + if log_string[0] == 'M': + log.warning(log_string) else: - log.warning("Message %s is empty and returns a None type.", msgid) + log.info(log_string) self._last_msg = None self._outq.remove(msgid) From c2b0ef74b720bec8828c376ea03035b1dafdd290 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Thu, 21 Sep 2023 09:29:16 +0000 Subject: [PATCH 3/6] Reconfigured send_all method. --- ssm/ssm2.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index d18977f1..5f7db8c5 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -491,27 +491,22 @@ def send_all(self): # Small sleep to avoid hammering the CPU time.sleep(0.01) - log_string = "Sent %s" % msgid + log.info("Sent %s" % msgid) elif self._protocol == Ssm2.AMS_MESSAGING: # Then we are sending to an Argo Messaging Service instance. argo_id = self._send_msg_ams(text, msgid) + if argo_id is not None: - log_string = "Sent %s, Argo ID: %s" % (msgid, argo_id) + log.info("Sent %s, Argo ID: %s" % (msgid, argo_id)) else: - log_string = "Message %s is empty and returns a None type." % (msgid) + log.warning("Message %s is empty and returns a None type." % (msgid)) else: # The SSM has been improperly configured raise Ssm2Exception('Unknown messaging protocol: %s' % self._protocol) - # log that the message was sent - if log_string[0] == 'M': - log.warning(log_string) - else: - log.info(log_string) - self._last_msg = None self._outq.remove(msgid) From 35d00bd9f57de5f16094152975fef5559a9f6ba5 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Thu, 21 Sep 2023 09:48:26 +0000 Subject: [PATCH 4/6] Final refactor into three functions --- ssm/ssm2.py | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index 5f7db8c5..d9ec1dd5 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -465,6 +465,33 @@ def send_ping(self): def has_msgs(self): """Return True if there are any messages in the outgoing queue.""" return self._outq.count() > 0 + + def send_via_stomp(self, text, msgid): + """ + Sending message via STOMP message broker. + """ + self._send_msg(text, msgid) + + log.info('Waiting for broker to accept message.') + while self._last_msg is None: + if not self.connected: + raise Ssm2Exception('Lost connection.') + # Small sleep to avoid hammering the CPU + time.sleep(0.01) + + log.info("Sent %s" % msgid) + + def send_via_ams(self, text, msgid): + """ + Sending message via HTTPS (to Argo message broker.) + """ + argo_id = self._send_msg_ams(text, msgid) + + if argo_id is not None: + log.info("Sent %s, Argo ID: %s" % (msgid, argo_id)) + else: + log.warning("Message %s is empty and " + "returns a None type." % (msgid)) def send_all(self): """ @@ -482,25 +509,11 @@ def send_all(self): if self._protocol == Ssm2.STOMP_MESSAGING: # Then we are sending to a STOMP message broker. - self._send_msg(text, msgid) - - log.info('Waiting for broker to accept message.') - while self._last_msg is None: - if not self.connected: - raise Ssm2Exception('Lost connection.') - # Small sleep to avoid hammering the CPU - time.sleep(0.01) - - log.info("Sent %s" % msgid) + self.send_via_stomp(text, msgid) elif self._protocol == Ssm2.AMS_MESSAGING: # Then we are sending to an Argo Messaging Service instance. - argo_id = self._send_msg_ams(text, msgid) - - if argo_id is not None: - log.info("Sent %s, Argo ID: %s" % (msgid, argo_id)) - else: - log.warning("Message %s is empty and returns a None type." % (msgid)) + self.send_via_ams(text, msgid) else: # The SSM has been improperly configured From f764f256601e016fe7c1ae8de1f1244789d52d93 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Thu, 21 Sep 2023 09:57:04 +0000 Subject: [PATCH 5/6] Final reformatting --- ssm/ssm2.py | 58 ++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index d9ec1dd5..138e4af7 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -465,33 +465,6 @@ def send_ping(self): def has_msgs(self): """Return True if there are any messages in the outgoing queue.""" return self._outq.count() > 0 - - def send_via_stomp(self, text, msgid): - """ - Sending message via STOMP message broker. - """ - self._send_msg(text, msgid) - - log.info('Waiting for broker to accept message.') - while self._last_msg is None: - if not self.connected: - raise Ssm2Exception('Lost connection.') - # Small sleep to avoid hammering the CPU - time.sleep(0.01) - - log.info("Sent %s" % msgid) - - def send_via_ams(self, text, msgid): - """ - Sending message via HTTPS (to Argo message broker.) - """ - argo_id = self._send_msg_ams(text, msgid) - - if argo_id is not None: - log.info("Sent %s, Argo ID: %s" % (msgid, argo_id)) - else: - log.warning("Message %s is empty and " - "returns a None type." % (msgid)) def send_all(self): """ @@ -509,11 +482,11 @@ def send_all(self): if self._protocol == Ssm2.STOMP_MESSAGING: # Then we are sending to a STOMP message broker. - self.send_via_stomp(text, msgid) + self._send_via_stomp(text, msgid) elif self._protocol == Ssm2.AMS_MESSAGING: # Then we are sending to an Argo Messaging Service instance. - self.send_via_ams(text, msgid) + self._send_via_ams(text, msgid) else: # The SSM has been improperly configured @@ -530,6 +503,33 @@ def send_all(self): except OSError as e: log.warning('OSError raised while purging message queue: %s', e) + def _send_via_stomp(self, text, msgid): + """ + Sending message via STOMP message broker. + """ + self._send_msg(text, msgid) + + log.info('Waiting for broker to accept message.') + while self._last_msg is None: + if not self.connected: + raise Ssm2Exception('Lost connection.') + # Small sleep to avoid hammering the CPU + time.sleep(0.01) + + log.info("Sent %s" % msgid) + + def _send_via_ams(self, text, msgid): + """ + Sending message via HTTPS (to Argo message broker.) + """ + argo_id = self._send_msg_ams(text, msgid) + + if argo_id is not None: + log.info("Sent %s, Argo ID: %s" % (msgid, argo_id)) + else: + log.warning("Message %s is empty and " + "returns a None type." % (msgid)) + ########################################################################### # Connection handling methods ########################################################################### From b885122c81f67d82d06f66a4baf336635eb47979 Mon Sep 17 00:00:00 2001 From: RedProkofiev Date: Thu, 28 Sep 2023 09:23:37 +0000 Subject: [PATCH 6/6] Indentation error --- ssm/ssm2.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/ssm/ssm2.py b/ssm/ssm2.py index 138e4af7..288112d7 100644 --- a/ssm/ssm2.py +++ b/ssm/ssm2.py @@ -504,19 +504,19 @@ def send_all(self): log.warning('OSError raised while purging message queue: %s', e) def _send_via_stomp(self, text, msgid): - """ - Sending message via STOMP message broker. - """ - self._send_msg(text, msgid) - - log.info('Waiting for broker to accept message.') - while self._last_msg is None: - if not self.connected: - raise Ssm2Exception('Lost connection.') - # Small sleep to avoid hammering the CPU - time.sleep(0.01) - - log.info("Sent %s" % msgid) + """ + Sending message via STOMP message broker. + """ + self._send_msg(text, msgid) + + log.info('Waiting for broker to accept message.') + while self._last_msg is None: + if not self.connected: + raise Ssm2Exception('Lost connection.') + # Small sleep to avoid hammering the CPU + time.sleep(0.01) + + log.info("Sent %s" % msgid) def _send_via_ams(self, text, msgid): """