Skip to content

Commit 2c0a0eb

Browse files
committed
Network refactoring
- cleanup
1 parent 659b191 commit 2c0a0eb

25 files changed

+84
-69
lines changed

src/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,8 +1551,8 @@ def HandleListConnections(self):
15511551
Returns bitmessage connection information as dict with keys *inbound*,
15521552
*outbound*.
15531553
"""
1554-
if connectionpool is None:
1555-
raise APIError(21, 'Could not import BMConnectionPool.')
1554+
if connectionpool is None or connectionpool.pool is None:
1555+
raise APIError(21, 'Network is not started.')
15561556
inboundConnections = []
15571557
outboundConnections = []
15581558
for i in connectionpool.pool.inboundConnections.values():

src/network/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
def start(config, state):
2121
"""Start network threads"""
2222
from .announcethread import AnnounceThread
23-
import connectionpool # pylint: disable=relative-import
23+
from . import connectionpool
24+
from .connectionpool import BMConnectionPool
2425
from .addrthread import AddrThread
2526
from .downloadthread import DownloadThread
2627
from .invthread import InvThread
@@ -29,6 +30,9 @@ def start(config, state):
2930
from .receivequeuethread import ReceiveQueueThread
3031
from .uploadthread import UploadThread
3132

33+
# create the connection pool
34+
connectionpool.pool = BMConnectionPool()
35+
3236
# check and set dandelion enabled value at network startup
3337
dandelion_ins.init_dandelion_enabled(config)
3438
# pass pool instance into dandelion class instance

src/network/addrthread.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
import random
55
from six.moves import queue
66

7-
# magic imports!
8-
import connectionpool
7+
from . import connectionpool
98
from protocol import assembleAddrMessage
109
from network import addrQueue # FIXME: init with queue
1110

12-
from threads import StoppableThread
11+
from .threads import StoppableThread
1312

1413

1514
class AddrThread(StoppableThread):

src/network/advanceddispatcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

1212

1313
class ProcessingError(Exception):

src/network/announcethread.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
"""
44
import time
55

6-
# magic imports!
7-
import connectionpool
6+
from . import connectionpool
87
from bmconfigparser import config
98
from protocol import assembleAddrMessage
109

11-
from node import Peer
12-
from threads import StoppableThread
10+
from .node import Peer
11+
from .threads import StoppableThread
1312

1413

1514
class AnnounceThread(StoppableThread):

src/network/bmobject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import protocol
88
import state
9-
import connectionpool
9+
from . import connectionpool
1010
from network import dandelion_ins
1111
from highlevelcrypto import calculateInventoryHash
1212

src/network/bmproto.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
# magic imports!
1414
import addresses
15-
import knownnodes
1615
import protocol
1716
import state
18-
import connectionpool
17+
from . import connectionpool
18+
from . import knownnodes
1919
from bmconfigparser import config
2020
from queues import objectProcessorQueue
2121
from randomtrackingdict import RandomTrackingDict
@@ -27,8 +27,8 @@
2727
)
2828
from network.proxy import ProxyError
2929
from network import dandelion_ins, invQueue, portCheckerQueue
30-
from node import Node, Peer
31-
from objectracker import ObjectTracker, missingObjects
30+
from .node import Node, Peer
31+
from .objectracker import ObjectTracker, missingObjects
3232

3333

3434
logger = logging.getLogger('default')

src/network/connectionchooser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from six.moves import queue
99

10-
import knownnodes
10+
from . import knownnodes
1111
import protocol
1212
import state
1313

src/network/connectionpool.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@
99
import time
1010
import random
1111

12-
import asyncore_pollchoose as asyncore
13-
import knownnodes
12+
from . import asyncore_pollchoose as asyncore
13+
from . import knownnodes
1414
import protocol
1515
import state
1616
from bmconfigparser import config
17-
from connectionchooser import chooseConnection
18-
from node import Peer
19-
from proxy import Proxy
20-
from tcp import (
21-
bootstrap, Socks4aBMConnection, Socks5BMConnection,
22-
TCPConnection, TCPServer)
23-
from udp import UDPSocket
17+
from .connectionchooser import chooseConnection
18+
from .node import Peer
19+
from .proxy import Proxy
2420

2521
logger = logging.getLogger('default')
2622

@@ -123,6 +119,7 @@ def isAlreadyConnected(self, nodeid):
123119

124120
def addConnection(self, connection):
125121
"""Add a connection object to our internal dict"""
122+
from .udp import UDPSocket
126123
if isinstance(connection, UDPSocket):
127124
return
128125
if connection.isOutbound:
@@ -136,6 +133,8 @@ def addConnection(self, connection):
136133

137134
def removeConnection(self, connection):
138135
"""Remove a connection from our internal dict"""
136+
from .tcp import TCPServer
137+
from .udp import UDPSocket
139138
if isinstance(connection, UDPSocket):
140139
del self.udpSockets[connection.listening.host]
141140
elif isinstance(connection, TCPServer):
@@ -177,6 +176,7 @@ def getListeningIP():
177176

178177
def startListening(self, bind=None):
179178
"""Open a listening socket and start accepting connections on it"""
179+
from .tcp import TCPServer
180180
if bind is None:
181181
bind = self.getListeningIP()
182182
port = config.safeGetInt("bitmessagesettings", "port")
@@ -189,6 +189,7 @@ def startUDPSocket(self, bind=None):
189189
Open an UDP socket. Depending on settings, it can either only
190190
accept incoming UDP packets, or also be able to send them.
191191
"""
192+
from .udp import UDPSocket
192193
if bind is None:
193194
host = self.getListeningIP()
194195
udpSocket = UDPSocket(host=host, announcing=True)
@@ -201,6 +202,9 @@ def startUDPSocket(self, bind=None):
201202

202203
def startBootstrappers(self):
203204
"""Run the process of resolving bootstrap hostnames"""
205+
from .tcp import (
206+
bootstrap, Socks4aBMConnection, Socks5BMConnection,
207+
TCPConnection)
204208
proxy_type = config.safeGet(
205209
'bitmessagesettings', 'socksproxytype')
206210
# A plugins may be added here
@@ -252,6 +256,9 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
252256
):
253257
acceptConnections = False
254258

259+
from .tcp import (
260+
Socks4aBMConnection, Socks5BMConnection, TCPConnection)
261+
255262
# pylint: disable=too-many-nested-blocks
256263
if spawnConnections:
257264
if not knownnodes.knownNodesActual:
@@ -401,4 +408,4 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
401408
self.removeConnection(i)
402409

403410

404-
pool = BMConnectionPool()
411+
pool = None

src/network/downloadthread.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import state
77
import addresses
88
import protocol
9-
import connectionpool
9+
from . import connectionpool
1010
from network import dandelion_ins
11-
from objectracker import missingObjects
12-
from threads import StoppableThread
11+
from .objectracker import missingObjects
12+
from .threads import StoppableThread
1313

1414

1515
class DownloadThread(StoppableThread):

0 commit comments

Comments
 (0)