From 660997b8f4a94a21a73d050134e7e886dee12b9c Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Tue, 22 Aug 2017 13:49:27 +0200 Subject: [PATCH] Code Quality --- src/api.py | 13 ++++++----- src/bitmessagemain.py | 2 -- src/network/advanceddispatcher.py | 3 --- src/network/asyncore_pollchoose.py | 37 +++++++++++++++--------------- src/network/bmproto.py | 8 ++----- src/network/connectionchooser.py | 10 ++++---- src/network/stats.py | 21 +++++++++-------- 7 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/api.py b/src/api.py index 05b27432..e20854fc 100644 --- a/src/api.py +++ b/src/api.py @@ -26,7 +26,6 @@ import helper_inbox import helper_sent import hashlib -import protocol import state from pyelliptic.openssl import OpenSSL import queues @@ -980,12 +979,14 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): queues.UISignalQueue.put(('updateStatusBar', message)) def HandleDeleteAndVacuum(self, params): - sqlStoredProcedure('deleteandvacuume') - return 'done' + if not params: + sqlStoredProcedure('deleteandvacuume') + return 'done' def HandleShutdown(self, params): - shutdown.doCleanShutdown() - return 'done' + if not params: + shutdown.doCleanShutdown() + return 'done' handlers = {} handlers['helloWorld'] = HandleHelloWorld @@ -1041,7 +1042,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): def _handle_request(self, method, params): if (self.handlers.has_key(method)): - return self.handlers[method](self ,params) + return self.handlers[method](self, params) else: raise APIError(20, 'Invalid method: %s' % method) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 894c9e08..1d37123f 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -44,8 +44,6 @@ import threading from class_sqlThread import sqlThread from class_singleCleaner import singleCleaner from class_objectProcessor import objectProcessor -from class_outgoingSynSender import outgoingSynSender -from class_singleListener import singleListener from class_singleWorker import singleWorker from class_addressGenerator import addressGenerator from class_smtpDeliver import smtpDeliver diff --git a/src/network/advanceddispatcher.py b/src/network/advanceddispatcher.py index 005aa038..eb636aed 100644 --- a/src/network/advanceddispatcher.py +++ b/src/network/advanceddispatcher.py @@ -1,6 +1,4 @@ -import Queue import socket -import sys import threading import time @@ -43,7 +41,6 @@ class AdvancedDispatcher(asyncore.dispatcher): def process(self): if not self.connected: return False - loop = 0 while True: try: with nonBlocking(self.processingLock): diff --git a/src/network/asyncore_pollchoose.py b/src/network/asyncore_pollchoose.py index c2568e9f..caa9d650 100644 --- a/src/network/asyncore_pollchoose.py +++ b/src/network/asyncore_pollchoose.py @@ -137,7 +137,7 @@ def set_rates(download, upload): uploadTimestamp = time.time() def update_received(download=0): - global receivedBytes, maxDownloadRate, downloadBucket, downloadTimestamp + global receivedBytes, downloadBucket, downloadTimestamp currentTimestamp = time.time() receivedBytes += download if maxDownloadRate > 0: @@ -149,7 +149,7 @@ def update_received(download=0): downloadTimestamp = currentTimestamp def update_sent(upload=0): - global sentBytes, maxUploadRate, uploadBucket, uploadTimestamp + global sentBytes, uploadBucket, uploadTimestamp currentTimestamp = time.time() sentBytes += upload if maxUploadRate > 0: @@ -349,14 +349,14 @@ def kqueue_poller(timeout=0.0, map=None): flags = select.KQ_EV_ADD | select.KQ_EV_ENABLE selectables = 0 for fd, obj in map.items(): - filter = 0 + kq_filter = 0 if obj.readable(): - filter |= select.KQ_FILTER_READ + kq_filter |= select.KQ_FILTER_READ if obj.writable(): - filter |= select.KQ_FILTER_WRITE - if filter: + kq_filter |= select.KQ_FILTER_WRITE + if kq_filter: try: - ev = select.kevent(fd, filter=filter, flags=flags) + ev = select.kevent(fd, filter=kq_filter, flags=flags) kqueue.control([ev], 0) selectables += 1 except IOError: @@ -383,9 +383,10 @@ def loop(timeout=30.0, use_poll=False, map=None, count=None, # argument which should no longer be used in favor of # "poller" - if poller is None: - if hasattr(select, 'epoll'): + if use_poll: + poller = poll_poller + elif hasattr(select, 'epoll'): poller = epoll_poller elif hasattr(select, 'kqueue'): poller = kqueue_poller @@ -506,9 +507,9 @@ class dispatcher: # no poll used, or not registered pass - def create_socket(self, family=socket.AF_INET, type=socket.SOCK_STREAM): - self.family_and_type = family, type - sock = socket.socket(family, type) + def create_socket(self, family=socket.AF_INET, socket_type=socket.SOCK_STREAM): + self.family_and_type = family, socket_type + sock = socket.socket(family, socket_type) sock.setblocking(0) self.set_socket(sock) @@ -652,9 +653,9 @@ class dispatcher: def log(self, message): sys.stderr.write('log: %s\n' % str(message)) - def log_info(self, message, type='info'): - if type not in self.ignore_log_types: - print('%s: %s' % (type, message)) + def log_info(self, message, log_type='info'): + if log_type not in self.ignore_log_types: + print('%s: %s' % (log_type, message)) def handle_read_event(self): if self.accepting: @@ -744,7 +745,7 @@ class dispatcher: def handle_accepted(self, sock, addr): sock.close() - self.log_info('unhandled accepted event', 'warning') + self.log_info('unhandled accepted event on %s' % (addr), 'warning') def handle_close(self): self.log_info('unhandled close event', 'warning') @@ -808,8 +809,8 @@ def close_all(map=None, ignore_all=False): for x in list(map.values()): try: x.close() - except OSError as x: - if x.args[0] == EBADF: + except OSError as e: + if e.args[0] == EBADF: pass elif not ignore_all: raise diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 30068d6d..dbdc26d2 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -1,26 +1,22 @@ import base64 -from binascii import hexlify import hashlib -import math import time import socket import struct -import sys -from addresses import calculateInventoryHash from bmconfigparser import BMConfigParser from debug import logger from inventory import Inventory import knownnodes from network.advanceddispatcher import AdvancedDispatcher -from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInvalidDataError, BMObjectExpiredError, BMObjectUnwantedStreamError, BMObjectInvalidError, BMObjectAlreadyHaveError +from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInvalidDataError, \ + BMObjectExpiredError, BMObjectUnwantedStreamError, BMObjectInvalidError, BMObjectAlreadyHaveError import network.connectionpool from network.node import Node from network.objectracker import ObjectTracker from network.proxy import Proxy, ProxyError, GeneralProxyError import addresses -from bmconfigparser import BMConfigParser from queues import objectProcessorQueue, portCheckerQueue, invQueue, addrQueue import shared import state diff --git a/src/network/connectionchooser.py b/src/network/connectionchooser.py index 681291ad..ee2a8b40 100644 --- a/src/network/connectionchooser.py +++ b/src/network/connectionchooser.py @@ -6,7 +6,7 @@ import knownnodes from queues import portCheckerQueue import state -def getDiscoveredPeer(stream): +def getDiscoveredPeer(): try: peer = random.choice(state.discoveredPeers.keys()) except (IndexError, KeyError): @@ -27,9 +27,11 @@ def chooseConnection(stream): return retval except Queue.Empty: pass - if random.choice((False, True)): - return getDiscoveredPeer(stream) - for i in range(50): + # with a probability of 0.5, connect to a discovered peer + if random.choice((False, True)) and not haveOnion: + # discovered peers are already filtered by allowed streams + return getDiscoveredPeer() + for _ in range(50): peer = random.choice(knownnodes.knownNodes[stream].keys()) try: rating = knownnodes.knownNodes[stream][peer]["rating"] diff --git a/src/network/stats.py b/src/network/stats.py index 45961ac1..ade56ac0 100644 --- a/src/network/stats.py +++ b/src/network/stats.py @@ -1,11 +1,7 @@ import time -from bmconfigparser import BMConfigParser from network.connectionpool import BMConnectionPool -from inventory import PendingDownloadQueue, PendingUpload import asyncore_pollchoose as asyncore -import shared -import throttle lastReceivedTimestamp = time.time() lastReceivedBytes = 0 @@ -16,7 +12,8 @@ currentSentSpeed = 0 def connectedHostsList(): retval = [] - for i in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values(): + for i in BMConnectionPool().inboundConnections.values() + \ + BMConnectionPool().outboundConnections.values(): if not i.fullyEstablished: continue try: @@ -46,22 +43,26 @@ def downloadSpeed(): currentTimestamp = time.time() if int(lastReceivedTimestamp) < int(currentTimestamp): currentReceivedBytes = asyncore.receivedBytes - currentReceivedSpeed = int((currentReceivedBytes - lastReceivedBytes) / (currentTimestamp - lastReceivedTimestamp)) + currentReceivedSpeed = int((currentReceivedBytes - lastReceivedBytes) / + (currentTimestamp - lastReceivedTimestamp)) lastReceivedBytes = currentReceivedBytes lastReceivedTimestamp = currentTimestamp return currentReceivedSpeed def pendingDownload(): tmp = {} - for connection in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values(): + for connection in BMConnectionPool().inboundConnections.values() + \ + BMConnectionPool().outboundConnections.values(): for k in connection.objectsNewToMe.keys(): tmp[k] = True return len(tmp) def pendingUpload(): - return 0 tmp = {} - for connection in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values(): + for connection in BMConnectionPool().inboundConnections.values() + \ + BMConnectionPool().outboundConnections.values(): for k in connection.objectsNewToThem.keys(): tmp[k] = True - return len(tmp) + #This probably isn't the correct logic so it's disabled + #return len(tmp) + return 0