Added mock code for class_objectProcessor, class_singleWorker, inventory, connectionpool & stats

This commit is contained in:
kuldeep.k@cisinlabs.com 2021-11-15 21:50:35 +05:30
parent 971c79b3f5
commit ed1c8ca100
No known key found for this signature in database
GPG Key ID: AF4FB299BF7C7C2A
5 changed files with 147 additions and 0 deletions

View File

@ -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

View File

@ -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...")

14
src/mock/inventory.py Normal file
View File

@ -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

View File

@ -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 = {}

13
src/mock/network/stats.py Normal file
View File

@ -0,0 +1,13 @@
"""
Network statistics
"""
def MockUploadSpeed():
"""Getting upload speed"""
return 0
def MockDownloadSpeed():
"""Getting download speed"""
return 0