From 49985a435faab8218f06f4b5914f48f651f1c68e Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Sat, 13 Oct 2018 00:12:00 +0300 Subject: [PATCH] Special class BMStringParser for sending ACK (resolves #1374) --- src/class_objectProcessor.py | 15 +++++++++------ src/network/bmproto.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index f7875331..2ed55694 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -19,6 +19,7 @@ import helper_msgcoding import helper_sent from helper_sql import SqlBulkExecute, sqlExecute, sqlQuery from helper_ackPayload import genAckPayload +from network import bmproto import protocol import queues import state @@ -50,6 +51,7 @@ class objectProcessor(threading.Thread): logger.debug( 'Loaded %s objects from disk into the objectProcessorQueue.', len(queryreturn)) + self._ack_obj = bmproto.BMStringParser() def run(self): while True: @@ -743,12 +745,13 @@ class objectProcessor(threading.Thread): # Don't send ACK if invalid, blacklisted senders, invisible # messages, disabled or chan - # if (self.ackDataHasAValidHeader(ackData) and not blockMessage - # and messageEncodingType != 0 and - # not BMConfigParser().safeGetBoolean(toAddress, 'dontsendack') - # and not BMConfigParser().safeGetBoolean(toAddress, 'chan') - # ): - # shared.checkAndShareObjectWithPeers(ackData[24:]) + if ( + self.ackDataHasAValidHeader(ackData) and not blockMessage and + messageEncodingType != 0 and + not BMConfigParser().safeGetBoolean(toAddress, 'dontsendack') and + not BMConfigParser().safeGetBoolean(toAddress, 'chan') + ): + self._ack_obj.send_data(ackData[24:]) # Display timing data timeRequiredToAttemptToDecryptMessage = time.time( diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 5333233d..ccb22ef5 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -4,6 +4,7 @@ import random import socket import struct import time +from binascii import hexlify from bmconfigparser import BMConfigParser from debug import logger @@ -575,3 +576,32 @@ class BMProto(AdvancedDispatcher, ObjectTracker): except AttributeError: logger.debug("Disconnected socket closing") AdvancedDispatcher.handle_close(self) + + +class BMStringParser(BMProto): + """ + A special case of BMProto used by objectProcessor to send ACK + """ + def __init__(self): + super(BMStringParser, self).__init__() + self.destination = state.Peer('127.0.0.1', 8444) + self.payload = None + ObjectTracker.__init__(self) + + def send_data(self, data): + """Send object given by the data string""" + # This class is introduced specially for ACK sending, please + # change log strings if you are going to use it for something else + self.bm_proto_reset() + self.payload = data + try: + self.bm_command_object() + except BMObjectAlreadyHaveError: + pass # maybe the same msg received on different nodes + except BMObjectExpiredError: + logger.debug( + 'Sending ACK failure (expired): %s', hexlify(data)) + except Exception as e: + logger.debug( + 'Exception of type %s while sending ACK', + type(e), exc_info=True)