Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,8 @@ def HandleListConnections(self):
Returns bitmessage connection information as dict with keys *inbound*,
*outbound*.
"""
if connectionpool is None:
raise APIError(21, 'Could not import BMConnectionPool.')
if connectionpool is None or connectionpool.pool is None:
raise APIError(21, 'Network is not started.')
inboundConnections = []
outboundConnections = []
for i in connectionpool.pool.inboundConnections.values():
Expand Down
6 changes: 5 additions & 1 deletion src/network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
def start(config, state):
"""Start network threads"""
from .announcethread import AnnounceThread
import connectionpool # pylint: disable=relative-import
from . import connectionpool
from .connectionpool import BMConnectionPool
from .addrthread import AddrThread
from .downloadthread import DownloadThread
from .invthread import InvThread
Expand All @@ -29,6 +30,9 @@ def start(config, state):
from .receivequeuethread import ReceiveQueueThread
from .uploadthread import UploadThread

# create the connection pool
connectionpool.pool = BMConnectionPool()

# check and set dandelion enabled value at network startup
dandelion_ins.init_dandelion_enabled(config)
# pass pool instance into dandelion class instance
Expand Down
5 changes: 2 additions & 3 deletions src/network/addrthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import random
from six.moves import queue

# magic imports!
import connectionpool
from . import connectionpool
from protocol import assembleAddrMessage
from network import addrQueue # FIXME: init with queue

from threads import StoppableThread
from .threads import StoppableThread


class AddrThread(StoppableThread):
Expand Down
2 changes: 1 addition & 1 deletion src/network/advanceddispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import network.asyncore_pollchoose as asyncore
import state
from threads import BusyError, nonBlocking
from .threads import BusyError, nonBlocking


class ProcessingError(Exception):
Expand Down
7 changes: 3 additions & 4 deletions src/network/announcethread.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
"""
import time

# magic imports!
import connectionpool
from . import connectionpool
from bmconfigparser import config
from protocol import assembleAddrMessage

from node import Peer
from threads import StoppableThread
from .node import Peer
from .threads import StoppableThread


class AnnounceThread(StoppableThread):
Expand Down
2 changes: 1 addition & 1 deletion src/network/bmobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import protocol
import state
import connectionpool
from . import connectionpool
from network import dandelion_ins
from highlevelcrypto import calculateInventoryHash

Expand Down
8 changes: 4 additions & 4 deletions src/network/bmproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

# magic imports!
import addresses
import knownnodes
import protocol
import state
import connectionpool
from . import connectionpool
from . import knownnodes
from bmconfigparser import config
from queues import objectProcessorQueue
from randomtrackingdict import RandomTrackingDict
Expand All @@ -27,8 +27,8 @@
)
from network.proxy import ProxyError
from network import dandelion_ins, invQueue, portCheckerQueue
from node import Node, Peer
from objectracker import ObjectTracker, missingObjects
from .node import Node, Peer
from .objectracker import ObjectTracker, missingObjects


logger = logging.getLogger('default')
Expand Down
2 changes: 1 addition & 1 deletion src/network/connectionchooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from six.moves import queue

import knownnodes
from . import knownnodes
import protocol
import state

Expand Down
27 changes: 17 additions & 10 deletions src/network/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@
import time
import random

import asyncore_pollchoose as asyncore
import knownnodes
from . import asyncore_pollchoose as asyncore
from . import knownnodes
import protocol
import state
from bmconfigparser import config
from connectionchooser import chooseConnection
from node import Peer
from proxy import Proxy
from tcp import (
bootstrap, Socks4aBMConnection, Socks5BMConnection,
TCPConnection, TCPServer)
from udp import UDPSocket
from .connectionchooser import chooseConnection
from .node import Peer
from .proxy import Proxy

logger = logging.getLogger('default')

Expand Down Expand Up @@ -123,6 +119,7 @@ def isAlreadyConnected(self, nodeid):

def addConnection(self, connection):
"""Add a connection object to our internal dict"""
from .udp import UDPSocket
if isinstance(connection, UDPSocket):
return
if connection.isOutbound:
Expand All @@ -136,6 +133,8 @@ def addConnection(self, connection):

def removeConnection(self, connection):
"""Remove a connection from our internal dict"""
from .tcp import TCPServer
from .udp import UDPSocket
if isinstance(connection, UDPSocket):
del self.udpSockets[connection.listening.host]
elif isinstance(connection, TCPServer):
Expand Down Expand Up @@ -177,6 +176,7 @@ def getListeningIP():

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

def startBootstrappers(self):
"""Run the process of resolving bootstrap hostnames"""
from .tcp import (
bootstrap, Socks4aBMConnection, Socks5BMConnection,
TCPConnection)
proxy_type = config.safeGet(
'bitmessagesettings', 'socksproxytype')
# A plugins may be added here
Expand Down Expand Up @@ -252,6 +256,9 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
):
acceptConnections = False

from .tcp import (
Socks4aBMConnection, Socks5BMConnection, TCPConnection)

# pylint: disable=too-many-nested-blocks
if spawnConnections:
if not knownnodes.knownNodesActual:
Expand Down Expand Up @@ -401,4 +408,4 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
self.removeConnection(i)


pool = BMConnectionPool()
pool = None
6 changes: 3 additions & 3 deletions src/network/downloadthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import state
import addresses
import protocol
import connectionpool
from . import connectionpool
from network import dandelion_ins
from objectracker import missingObjects
from threads import StoppableThread
from .objectracker import missingObjects
from .threads import StoppableThread


class DownloadThread(StoppableThread):
Expand Down
10 changes: 5 additions & 5 deletions src/network/http.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import socket

from advanceddispatcher import AdvancedDispatcher
import asyncore_pollchoose as asyncore
from proxy import ProxyError
from socks5 import Socks5Connection, Socks5Resolver
from socks4a import Socks4aConnection, Socks4aResolver
from .advanceddispatcher import AdvancedDispatcher
from . import asyncore_pollchoose as asyncore
from .proxy import ProxyError
from .socks5 import Socks5Connection, Socks5Resolver
from .socks4a import Socks4aConnection, Socks4aResolver


class HttpError(ProxyError):
Expand Down
2 changes: 1 addition & 1 deletion src/network/httpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import asyncore
import socket

from tls import TLSHandshake
from .tls import TLSHandshake


class HTTPRequestHandler(asyncore.dispatcher):
Expand Down
4 changes: 2 additions & 2 deletions src/network/https.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncore

from http import HTTPClient
from tls import TLSHandshake
from .http import HTTPClient
from .tls import TLSHandshake

"""
self.sslSock = ssl.wrap_socket(
Expand Down
9 changes: 5 additions & 4 deletions src/network/invthread.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
"""
Thread to send inv annoucements
"""
import Queue
import random
from time import time

from six.moves import queue

import addresses
import protocol
import state
import connectionpool
from . import connectionpool
from network import dandelion_ins, invQueue
from threads import StoppableThread
from .threads import StoppableThread


def handleExpiredDandelion(expired):
Expand Down Expand Up @@ -58,7 +59,7 @@ def run(self): # pylint: disable=too-many-branches
# locally generated
if len(data) == 2 or data[2] is None:
self.handleLocallyGenerated(data[0], data[1])
except Queue.Empty:
except queue.Empty:
break

if chunk:
Expand Down
4 changes: 2 additions & 2 deletions src/network/networkthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
A thread to handle network concerns
"""
import network.asyncore_pollchoose as asyncore
import connectionpool
from . import connectionpool
from queues import excQueue
from threads import StoppableThread
from .threads import StoppableThread


class BMNetworkThread(StoppableThread):
Expand Down
2 changes: 1 addition & 1 deletion src/network/objectracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import time
from threading import RLock

import connectionpool
from . import connectionpool
from network import dandelion_ins
from randomtrackingdict import RandomTrackingDict

Expand Down
6 changes: 3 additions & 3 deletions src/network/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import socket
import time

import asyncore_pollchoose as asyncore
from advanceddispatcher import AdvancedDispatcher
from . import asyncore_pollchoose as asyncore
from .advanceddispatcher import AdvancedDispatcher
from bmconfigparser import config
from node import Peer
from .node import Peer

logger = logging.getLogger('default')

Expand Down
9 changes: 5 additions & 4 deletions src/network/receivequeuethread.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
Process data incoming from network
"""
import errno
import Queue
import socket

import connectionpool
from six.moves import queue

from . import connectionpool
from network.advanceddispatcher import UnknownStateError
from network import receiveDataQueue
from threads import StoppableThread
from .threads import StoppableThread


class ReceiveQueueThread(StoppableThread):
Expand All @@ -21,7 +22,7 @@ def run(self):
while not self._stopped:
try:
dest = receiveDataQueue.get(block=True, timeout=1)
except Queue.Empty:
except queue.Empty:
continue

if self._stopped:
Expand Down
2 changes: 1 addition & 1 deletion src/network/socks4a.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import socket
import struct

from proxy import GeneralProxyError, Proxy, ProxyError
from .proxy import GeneralProxyError, Proxy, ProxyError

logger = logging.getLogger('default')

Expand Down
4 changes: 2 additions & 2 deletions src/network/socks5.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import socket
import struct

from node import Peer
from proxy import GeneralProxyError, Proxy, ProxyError
from .node import Peer
from .proxy import GeneralProxyError, Proxy, ProxyError

logger = logging.getLogger('default')

Expand Down
8 changes: 5 additions & 3 deletions src/network/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"""
import time

import asyncore_pollchoose as asyncore
import connectionpool
from objectracker import missingObjects
from . import asyncore_pollchoose as asyncore
from . import connectionpool
from .objectracker import missingObjects


lastReceivedTimestamp = time.time()
Expand All @@ -18,6 +18,8 @@

def connectedHostsList():
"""List of all the connected hosts"""
if connectionpool.pool is None:
return []
return connectionpool.pool.establishedConnections()


Expand Down
Loading