Skip to content

Commit f2bb4d1

Browse files
committed
Refactor parent package imports in network module
1 parent 205e253 commit f2bb4d1

19 files changed

+68
-71
lines changed

src/network/__init__.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
"""
22
Network subsystem package
33
"""
4+
try:
5+
import networkdepsinterface
6+
except ImportError:
7+
from pybitmessage import networkdepsinterface
8+
49
from .dandelion import Dandelion
510
from .threads import StoppableThread
611

12+
(
13+
state, queues, config, protocol,
14+
randomtrackingdict, addresses, paths) = networkdepsinterface.importParentPackageDepsToNetwork()
15+
716
dandelion_ins = Dandelion()
817

918
__all__ = ["StoppableThread"]
1019

1120

1221
def start(config, state):
1322
"""Start network threads"""
14-
import state
1523
from .announcethread import AnnounceThread
1624
import connectionpool # pylint: disable=relative-import
1725
from .addrthread import AddrThread

src/network/addrthread.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import connectionpool
88
from helper_random import randomshuffle
99
from protocol import assembleAddrMessage
10-
from queues import addrQueue # FIXME: init with queue
10+
from network import queues # FIXME: init with queue
1111

1212
from threads import StoppableThread
1313

@@ -21,7 +21,7 @@ def run(self):
2121
chunk = []
2222
while True:
2323
try:
24-
data = addrQueue.get(False)
24+
data = queues.addrQueue.get(False)
2525
chunk.append(data)
2626
except queue.Empty:
2727
break
@@ -43,7 +43,7 @@ def run(self):
4343
if filtered:
4444
i.append_write_buf(assembleAddrMessage(filtered))
4545

46-
addrQueue.iterate()
46+
queues.addrQueue.iterate()
4747
for i in range(len(chunk)):
48-
addrQueue.task_done()
48+
queues.addrQueue.task_done()
4949
self.stop.wait(1)

src/network/advanceddispatcher.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import time
77

88
import network.asyncore_pollchoose as asyncore
9-
import state
9+
from network import state
1010
from threads import BusyError, nonBlocking
1111

1212

src/network/announcethread.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
# magic imports!
77
import connectionpool
8-
from bmconfigparser import config
8+
from network import config
99
from protocol import assembleAddrMessage
1010

1111
from node import Peer

src/network/bmobject.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import logging
55
import time
66

7-
import protocol
8-
import state
7+
from network import state, protocol
98
import connectionpool
109
from network import dandelion_ins
1110
from highlevelcrypto import calculateInventoryHash

src/network/bmproto.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@
1111
import time
1212

1313
# magic imports!
14-
import addresses
1514
import knownnodes
16-
import protocol
17-
import state
15+
from network import protocol, state, config, queues, addresses, dandelion_ins
1816
import connectionpool
19-
from bmconfigparser import config
20-
from queues import invQueue, objectProcessorQueue, portCheckerQueue
2117
from randomtrackingdict import RandomTrackingDict
2218
from network.advanceddispatcher import AdvancedDispatcher
2319
from network.bmobject import (
@@ -26,7 +22,6 @@
2622
BMObjectUnwantedStreamError
2723
)
2824
from network.proxy import ProxyError
29-
from network import dandelion_ins
3025
from node import Node, Peer
3126
from objectracker import ObjectTracker, missingObjects
3227

@@ -409,7 +404,7 @@ def bm_command_object(self):
409404

410405
try:
411406
self.object.checkObjectByType()
412-
objectProcessorQueue.put((
407+
queues.objectProcessorQueue.put((
413408
self.object.objectType, buffer(self.object.data))) # noqa: F821
414409
except BMObjectInvalidError:
415410
BMProto.stopDownloadingObject(self.object.inventoryHash, True)
@@ -431,7 +426,7 @@ def bm_command_object(self):
431426
)
432427
self.handleReceivedObject(
433428
self.object.streamNumber, self.object.inventoryHash)
434-
invQueue.put((
429+
queues.invQueue.put((
435430
self.object.streamNumber, self.object.inventoryHash,
436431
self.destination))
437432
return True
@@ -472,7 +467,7 @@ def bm_command_addr(self):
472467

473468
def bm_command_portcheck(self):
474469
"""Incoming port check request, queue it."""
475-
portCheckerQueue.put(Peer(self.destination, self.peerNode.port))
470+
queues.portCheckerQueue.put(Peer(self.destination, self.peerNode.port))
476471
return True
477472

478473
def bm_command_ping(self):

src/network/connectionchooser.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
import random
77

88
import knownnodes
9-
import protocol
10-
import state
11-
from bmconfigparser import config
12-
from queues import queue, portCheckerQueue
9+
from network import protocol, state, config, queues
1310

1411
logger = logging.getLogger('default')
1512

@@ -34,10 +31,10 @@ def chooseConnection(stream):
3431
onionOnly = config.safeGetBoolean(
3532
"bitmessagesettings", "onionservicesonly")
3633
try:
37-
retval = portCheckerQueue.get(False)
38-
portCheckerQueue.task_done()
34+
retval = queues.portCheckerQueue.get(False)
35+
queues.portCheckerQueue.task_done()
3936
return retval
40-
except queue.Empty:
37+
except queues.queue.Empty:
4138
pass
4239
# with a probability of 0.5, connect to a discovered peer
4340
if random.choice((False, True)) and not haveOnion: # nosec B311

src/network/connectionpool.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
import asyncore_pollchoose as asyncore
1212
import helper_random
1313
import knownnodes
14-
import protocol
15-
import state
16-
from bmconfigparser import config
14+
from network import protocol, state, config
1715
from connectionchooser import chooseConnection
1816
from node import Peer
1917
from proxy import Proxy

src/network/downloadthread.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
`DownloadThread` class definition
33
"""
44
import time
5-
import state
6-
import addresses
5+
from network import state, protocol, addresses, dandelion_ins
76
import helper_random
8-
import protocol
97
import connectionpool
10-
from network import dandelion_ins
118
from objectracker import missingObjects
129
from threads import StoppableThread
1310

src/network/invthread.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
import random
66
from time import time
77

8-
import addresses
9-
import protocol
10-
import state
8+
from network import protocol, state, queues, addresses
119
import connectionpool
1210
from network import dandelion_ins
13-
from queues import invQueue
1411
from threads import StoppableThread
1512

1613

@@ -52,9 +49,9 @@ def run(self): # pylint: disable=too-many-branches
5249
chunk = []
5350
while True:
5451
# Dandelion fluff trigger by expiration
55-
handleExpiredDandelion(dandelion_ins.expire(invQueue))
52+
handleExpiredDandelion(dandelion_ins.expire(queues.invQueue))
5653
try:
57-
data = invQueue.get(False)
54+
data = queues.invQueue.get(False)
5855
chunk.append((data[0], data[1]))
5956
# locally generated
6057
if len(data) == 2 or data[2] is None:
@@ -101,9 +98,9 @@ def run(self): # pylint: disable=too-many-branches
10198
addresses.encodeVarint(
10299
len(stems)) + ''.join(stems)))
103100

104-
invQueue.iterate()
101+
queues.invQueue.iterate()
105102
for _ in range(len(chunk)):
106-
invQueue.task_done()
103+
queues.invQueue.task_done()
107104

108105
dandelion_ins.reRandomiseStems()
109106

src/network/knownnodes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
except ImportError:
1616
from collections import Iterable
1717

18-
import state
19-
from bmconfigparser import config
18+
from network import state, config
2019
from network.node import Peer
2120

2221
state.Peer = Peer

src/network/networkthread.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
import network.asyncore_pollchoose as asyncore
55
import connectionpool
6-
from queues import excQueue
6+
from network import queues
77
from threads import StoppableThread
88

99

@@ -16,7 +16,7 @@ def run(self):
1616
while not self._stopped:
1717
connectionpool.pool.loop()
1818
except Exception as e:
19-
excQueue.put((self.name, e))
19+
queues.excQueue.put((self.name, e))
2020
raise
2121

2222
def stopThread(self):

src/network/proxy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import asyncore_pollchoose as asyncore
1010
from advanceddispatcher import AdvancedDispatcher
11-
from bmconfigparser import config
11+
from network import config
1212
from node import Peer
1313

1414
logger = logging.getLogger('default')

src/network/receivequeuethread.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import connectionpool
99
from network.advanceddispatcher import UnknownStateError
10-
from queues import receiveDataQueue
10+
from network import queues
1111
from threads import StoppableThread
1212

1313

@@ -20,7 +20,7 @@ def __init__(self, num=0):
2020
def run(self):
2121
while not self._stopped:
2222
try:
23-
dest = receiveDataQueue.get(block=True, timeout=1)
23+
dest = queues.receiveDataQueue.get(block=True, timeout=1)
2424
except Queue.Empty:
2525
continue
2626

@@ -38,7 +38,7 @@ def run(self):
3838
connection = connectionpool.pool.getConnectionByAddr(dest)
3939
# connection object not found
4040
except KeyError:
41-
receiveDataQueue.task_done()
41+
queues.receiveDataQueue.task_done()
4242
continue
4343
try:
4444
connection.process()
@@ -52,4 +52,4 @@ def run(self):
5252
self.logger.error('Socket error: %s', err)
5353
except: # noqa:E722
5454
self.logger.error('Error processing', exc_info=True)
55-
receiveDataQueue.task_done()
55+
queues.receiveDataQueue.task_done()

src/network/tcp.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@
1010
import time
1111

1212
# magic imports!
13-
import addresses
1413
import helper_random
1514
import l10n
16-
import protocol
17-
import state
15+
from network import protocol, state, config, queues, addresses, dandelion_ins
1816
import connectionpool
19-
from bmconfigparser import config
2017
from highlevelcrypto import randomBytes
21-
from network import dandelion_ins
22-
from queues import invQueue, receiveDataQueue, UISignalQueue
2318
from tr import _translate
2419

2520
import asyncore_pollchoose as asyncore
@@ -109,7 +104,7 @@ def antiIntersectionDelay(self, initial=False):
109104
max_known_nodes = max(
110105
len(knownnodes.knownNodes[x]) for x in knownnodes.knownNodes)
111106
delay = math.ceil(math.log(max_known_nodes + 2, 20)) * (
112-
0.2 + invQueue.queueCount / 2.0)
107+
0.2 + queues.invQueue.queueCount / 2.0)
113108
# take the stream with maximum amount of nodes
114109
# +2 is to avoid problems with log(0) and log(1)
115110
# 20 is avg connected nodes count
@@ -135,7 +130,7 @@ def checkTimeOffsetNotification(self):
135130
if BMProto.timeOffsetWrongCount > \
136131
maximumTimeOffsetWrongCount and \
137132
not self.fullyEstablished:
138-
UISignalQueue.put((
133+
queues.UISignalQueue.put((
139134
'updateStatusBar',
140135
_translate(
141136
"MainWindow",
@@ -158,8 +153,8 @@ def set_connection_fully_established(self):
158153
"""Initiate inventory synchronisation."""
159154
if not self.isOutbound and not self.local:
160155
state.clientHasReceivedIncomingConnections = True
161-
UISignalQueue.put(('setStatusIcon', 'green'))
162-
UISignalQueue.put((
156+
queues.UISignalQueue.put(('setStatusIcon', 'green'))
157+
queues.UISignalQueue.put((
163158
'updateNetworkStatusTab', (self.isOutbound, True, self.destination)
164159
))
165160
self.antiIntersectionDelay(True)
@@ -169,7 +164,7 @@ def set_connection_fully_established(self):
169164
knownnodes.increaseRating(self.destination)
170165
knownnodes.addKnownNode(
171166
self.streams, self.destination, time.time())
172-
dandelion_ins.maybeAddStem(self, invQueue)
167+
dandelion_ins.maybeAddStem(self, queues.invQueue)
173168
self.sendAddr()
174169
self.sendBigInv()
175170

@@ -271,12 +266,12 @@ def handle_connect(self):
271266
connectionpool.pool.streams, dandelion_ins.enabled,
272267
False, nodeid=self.nodeid))
273268
self.connectedAt = time.time()
274-
receiveDataQueue.put(self.destination)
269+
queues.receiveDataQueue.put(self.destination)
275270

276271
def handle_read(self):
277272
"""Callback for reading from a socket"""
278273
TLSDispatcher.handle_read(self)
279-
receiveDataQueue.put(self.destination)
274+
queues.receiveDataQueue.put(self.destination)
280275

281276
def handle_write(self):
282277
"""Callback for writing to a socket"""
@@ -286,7 +281,7 @@ def handle_close(self):
286281
"""Callback for connection being closed."""
287282
host_is_global = self.isOutbound or not self.local and not state.socksIP
288283
if self.fullyEstablished:
289-
UISignalQueue.put((
284+
queues.UISignalQueue.put((
290285
'updateNetworkStatusTab',
291286
(self.isOutbound, False, self.destination)
292287
))

src/network/tls.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import network.asyncore_pollchoose as asyncore
1111
import paths
1212
from network.advanceddispatcher import AdvancedDispatcher
13-
from queues import receiveDataQueue
13+
from network import queues
1414

1515
logger = logging.getLogger('default')
1616

@@ -216,5 +216,5 @@ def tls_handshake(self):
216216

217217
self.bm_proto_reset()
218218
self.set_state("connection_fully_established")
219-
receiveDataQueue.put(self.destination)
219+
queues.receiveDataQueue.put(self.destination)
220220
return False

0 commit comments

Comments
 (0)