Skip to content

Commit a9cdf23

Browse files
committed
Added mock code for class_objectProcessor, class_singleWorker, inventory, connectionpool & stats
1 parent 47dffae commit a9cdf23

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

src/mock/class_objectProcessor.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
The objectProcessor thread, of which there is only one,
3+
processes the network objects
4+
"""
5+
import logging
6+
import random
7+
import threading
8+
9+
import queues
10+
import state
11+
12+
from helper_sql import sql_ready, sqlExecute, sqlQuery
13+
from network import bmproto
14+
15+
logger = logging.getLogger('default')
16+
17+
18+
class objectProcessor(threading.Thread):
19+
"""
20+
The objectProcessor thread, of which there is only one, receives network
21+
objects (msg, broadcast, pubkey, getpubkey) from the receiveDataThreads.
22+
"""
23+
def __init__(self):
24+
threading.Thread.__init__(self, name="objectProcessor")
25+
random.seed()
26+
# It may be the case that the last time Bitmessage was running,
27+
# the user closed it before it finished processing everything in the
28+
# objectProcessorQueue. Assuming that Bitmessage wasn't closed
29+
# forcefully, it should have saved the data in the queue into the
30+
# objectprocessorqueue table. Let's pull it out.
31+
sql_ready.wait()
32+
queryreturn = sqlQuery(
33+
'SELECT objecttype, data FROM objectprocessorqueue')
34+
for objectType, data in queryreturn:
35+
queues.objectProcessorQueue.put((objectType, data))
36+
sqlExecute('DELETE FROM objectprocessorqueue')
37+
logger.debug(
38+
'Loaded %s objects from disk into the objectProcessorQueue.',
39+
len(queryreturn))
40+
self._ack_obj = bmproto.BMStringParser()
41+
self.successfullyDecryptMessageTimings = []
42+
43+
def run(self):
44+
"""Process the objects from `.queues.objectProcessorQueue`"""
45+
while True:
46+
objectType, data = queues.objectProcessorQueue.get()
47+
48+
if state.shutdown:
49+
state.shutdown = 2
50+
break

src/mock/class_singleWorker.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Thread for performing PoW
3+
"""
4+
5+
from __future__ import division
6+
7+
import proofofwork
8+
import queues
9+
import state
10+
11+
from network import StoppableThread
12+
from six.moves import queue
13+
14+
15+
class MockSingleWorker(StoppableThread):
16+
"""Thread for performing PoW"""
17+
18+
def __init__(self):
19+
super(MockSingleWorker, self).__init__(name="singleWorker")
20+
proofofwork.init()
21+
22+
def stopThread(self):
23+
"""Signal through the queue that the thread should be stopped"""
24+
25+
try:
26+
queues.workerQueue.put(("stopThread", "data"))
27+
except queue.Full:
28+
self.logger.error('workerQueue is Full')
29+
super(MockSingleWorker, self).stopThread()
30+
31+
def run(self):
32+
33+
if state.shutdown > 0:
34+
return
35+
36+
while state.shutdown == 0:
37+
self.busy = 0
38+
command, data = queues.workerQueue.get()
39+
self.busy = 1
40+
if command == 'stopThread':
41+
self.busy = 0
42+
return
43+
44+
queues.workerQueue.task_done()
45+
self.logger.info("Quitting...")

src/mock/inventory.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""The Inventory singleton"""
2+
3+
# TODO make this dynamic, and watch out for frozen, like with messagetypes
4+
from singleton import Singleton
5+
6+
7+
@Singleton
8+
class MockInventory():
9+
"""
10+
Inventory singleton class which uses storage backends
11+
to manage the inventory.
12+
"""
13+
def __init__(self):
14+
self.numberOfInventoryLookupsPerformed = 0

src/mock/network/connectionpool.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
`BMConnectionPool` class definition
3+
"""
4+
import logging
5+
6+
import asyncore_pollchoose as asyncore
7+
from bmconfigparser import BMConfigParser
8+
from singleton import Singleton
9+
10+
logger = logging.getLogger('default')
11+
12+
13+
@Singleton
14+
class MockBMConnectionPool(object):
15+
"""Pool of all existing connections"""
16+
17+
def __init__(self):
18+
asyncore.set_rates(
19+
BMConfigParser().safeGetInt(
20+
"bitmessagesettings", "maxdownloadrate"),
21+
BMConfigParser().safeGetInt(
22+
"bitmessagesettings", "maxuploadrate")
23+
)
24+
self.outboundConnections = {}
25+
self.inboundConnections = {}

src/mock/network/stats.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
Network statistics
3+
"""
4+
5+
6+
def MockUploadSpeed():
7+
"""Getting upload speed"""
8+
return 0
9+
10+
11+
def MockDownloadSpeed():
12+
"""Getting download speed"""
13+
return 0

0 commit comments

Comments
 (0)