From ed1c8ca1007974b8e8ce1861ad624c9eb03a2751 Mon Sep 17 00:00:00 2001 From: "kuldeep.k@cisinlabs.com" Date: Mon, 15 Nov 2021 21:50:35 +0530 Subject: [PATCH] Added mock code for class_objectProcessor, class_singleWorker, inventory, connectionpool & stats --- src/mock/class_objectProcessor.py | 50 ++++++++++++++++++++++++++++++ src/mock/class_singleWorker.py | 45 +++++++++++++++++++++++++++ src/mock/inventory.py | 14 +++++++++ src/mock/network/connectionpool.py | 25 +++++++++++++++ src/mock/network/stats.py | 13 ++++++++ 5 files changed, 147 insertions(+) create mode 100644 src/mock/class_objectProcessor.py create mode 100644 src/mock/class_singleWorker.py create mode 100644 src/mock/inventory.py create mode 100644 src/mock/network/connectionpool.py create mode 100644 src/mock/network/stats.py diff --git a/src/mock/class_objectProcessor.py b/src/mock/class_objectProcessor.py new file mode 100644 index 00000000..84958bb3 --- /dev/null +++ b/src/mock/class_objectProcessor.py @@ -0,0 +1,50 @@ +""" +The objectProcessor thread, of which there is only one, +processes the network objects +""" +import logging +import random +import threading + +import queues +import state + +from helper_sql import sql_ready, sqlExecute, sqlQuery +from network import bmproto + +logger = logging.getLogger('default') + + +class objectProcessor(threading.Thread): + """ + The objectProcessor thread, of which there is only one, receives network + objects (msg, broadcast, pubkey, getpubkey) from the receiveDataThreads. + """ + def __init__(self): + threading.Thread.__init__(self, name="objectProcessor") + random.seed() + # It may be the case that the last time Bitmessage was running, + # the user closed it before it finished processing everything in the + # objectProcessorQueue. Assuming that Bitmessage wasn't closed + # forcefully, it should have saved the data in the queue into the + # objectprocessorqueue table. Let's pull it out. + sql_ready.wait() + queryreturn = sqlQuery( + 'SELECT objecttype, data FROM objectprocessorqueue') + for objectType, data in queryreturn: + queues.objectProcessorQueue.put((objectType, data)) + sqlExecute('DELETE FROM objectprocessorqueue') + logger.debug( + 'Loaded %s objects from disk into the objectProcessorQueue.', + len(queryreturn)) + self._ack_obj = bmproto.BMStringParser() + self.successfullyDecryptMessageTimings = [] + + def run(self): + """Process the objects from `.queues.objectProcessorQueue`""" + while True: + objectType, data = queues.objectProcessorQueue.get() + + if state.shutdown: + state.shutdown = 2 + break diff --git a/src/mock/class_singleWorker.py b/src/mock/class_singleWorker.py new file mode 100644 index 00000000..af9b8d83 --- /dev/null +++ b/src/mock/class_singleWorker.py @@ -0,0 +1,45 @@ +""" +Thread for performing PoW +""" + +from __future__ import division + +import proofofwork +import queues +import state + +from network import StoppableThread +from six.moves import queue + + +class MockSingleWorker(StoppableThread): + """Thread for performing PoW""" + + def __init__(self): + super(MockSingleWorker, self).__init__(name="singleWorker") + proofofwork.init() + + def stopThread(self): + """Signal through the queue that the thread should be stopped""" + + try: + queues.workerQueue.put(("stopThread", "data")) + except queue.Full: + self.logger.error('workerQueue is Full') + super(MockSingleWorker, self).stopThread() + + def run(self): + + if state.shutdown > 0: + return + + while state.shutdown == 0: + self.busy = 0 + command, data = queues.workerQueue.get() + self.busy = 1 + if command == 'stopThread': + self.busy = 0 + return + + queues.workerQueue.task_done() + self.logger.info("Quitting...") diff --git a/src/mock/inventory.py b/src/mock/inventory.py new file mode 100644 index 00000000..04bceaf6 --- /dev/null +++ b/src/mock/inventory.py @@ -0,0 +1,14 @@ +"""The Inventory singleton""" + +# TODO make this dynamic, and watch out for frozen, like with messagetypes +from singleton import Singleton + + +@Singleton +class MockInventory(): + """ + Inventory singleton class which uses storage backends + to manage the inventory. + """ + def __init__(self): + self.numberOfInventoryLookupsPerformed = 0 diff --git a/src/mock/network/connectionpool.py b/src/mock/network/connectionpool.py new file mode 100644 index 00000000..1981f631 --- /dev/null +++ b/src/mock/network/connectionpool.py @@ -0,0 +1,25 @@ +""" +`BMConnectionPool` class definition +""" +import logging + +import asyncore_pollchoose as asyncore +from bmconfigparser import BMConfigParser +from singleton import Singleton + +logger = logging.getLogger('default') + + +@Singleton +class MockBMConnectionPool(object): + """Pool of all existing connections""" + + def __init__(self): + asyncore.set_rates( + BMConfigParser().safeGetInt( + "bitmessagesettings", "maxdownloadrate"), + BMConfigParser().safeGetInt( + "bitmessagesettings", "maxuploadrate") + ) + self.outboundConnections = {} + self.inboundConnections = {} diff --git a/src/mock/network/stats.py b/src/mock/network/stats.py new file mode 100644 index 00000000..a5fe9072 --- /dev/null +++ b/src/mock/network/stats.py @@ -0,0 +1,13 @@ +""" +Network statistics +""" + + +def MockUploadSpeed(): + """Getting upload speed""" + return 0 + + +def MockDownloadSpeed(): + """Getting download speed""" + return 0