Code Quality

This commit is contained in:
Peter Šurda 2017-08-22 13:49:27 +02:00
parent 18119339f8
commit 660997b8f4
Signed by: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
7 changed files with 45 additions and 49 deletions

View File

@ -26,7 +26,6 @@ import helper_inbox
import helper_sent import helper_sent
import hashlib import hashlib
import protocol
import state import state
from pyelliptic.openssl import OpenSSL from pyelliptic.openssl import OpenSSL
import queues import queues
@ -980,10 +979,12 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
queues.UISignalQueue.put(('updateStatusBar', message)) queues.UISignalQueue.put(('updateStatusBar', message))
def HandleDeleteAndVacuum(self, params): def HandleDeleteAndVacuum(self, params):
if not params:
sqlStoredProcedure('deleteandvacuume') sqlStoredProcedure('deleteandvacuume')
return 'done' return 'done'
def HandleShutdown(self, params): def HandleShutdown(self, params):
if not params:
shutdown.doCleanShutdown() shutdown.doCleanShutdown()
return 'done' return 'done'
@ -1041,7 +1042,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
def _handle_request(self, method, params): def _handle_request(self, method, params):
if (self.handlers.has_key(method)): if (self.handlers.has_key(method)):
return self.handlers[method](self ,params) return self.handlers[method](self, params)
else: else:
raise APIError(20, 'Invalid method: %s' % method) raise APIError(20, 'Invalid method: %s' % method)

View File

@ -44,8 +44,6 @@ import threading
from class_sqlThread import sqlThread from class_sqlThread import sqlThread
from class_singleCleaner import singleCleaner from class_singleCleaner import singleCleaner
from class_objectProcessor import objectProcessor from class_objectProcessor import objectProcessor
from class_outgoingSynSender import outgoingSynSender
from class_singleListener import singleListener
from class_singleWorker import singleWorker from class_singleWorker import singleWorker
from class_addressGenerator import addressGenerator from class_addressGenerator import addressGenerator
from class_smtpDeliver import smtpDeliver from class_smtpDeliver import smtpDeliver

View File

@ -1,6 +1,4 @@
import Queue
import socket import socket
import sys
import threading import threading
import time import time
@ -43,7 +41,6 @@ class AdvancedDispatcher(asyncore.dispatcher):
def process(self): def process(self):
if not self.connected: if not self.connected:
return False return False
loop = 0
while True: while True:
try: try:
with nonBlocking(self.processingLock): with nonBlocking(self.processingLock):

View File

@ -137,7 +137,7 @@ def set_rates(download, upload):
uploadTimestamp = time.time() uploadTimestamp = time.time()
def update_received(download=0): def update_received(download=0):
global receivedBytes, maxDownloadRate, downloadBucket, downloadTimestamp global receivedBytes, downloadBucket, downloadTimestamp
currentTimestamp = time.time() currentTimestamp = time.time()
receivedBytes += download receivedBytes += download
if maxDownloadRate > 0: if maxDownloadRate > 0:
@ -149,7 +149,7 @@ def update_received(download=0):
downloadTimestamp = currentTimestamp downloadTimestamp = currentTimestamp
def update_sent(upload=0): def update_sent(upload=0):
global sentBytes, maxUploadRate, uploadBucket, uploadTimestamp global sentBytes, uploadBucket, uploadTimestamp
currentTimestamp = time.time() currentTimestamp = time.time()
sentBytes += upload sentBytes += upload
if maxUploadRate > 0: if maxUploadRate > 0:
@ -349,14 +349,14 @@ def kqueue_poller(timeout=0.0, map=None):
flags = select.KQ_EV_ADD | select.KQ_EV_ENABLE flags = select.KQ_EV_ADD | select.KQ_EV_ENABLE
selectables = 0 selectables = 0
for fd, obj in map.items(): for fd, obj in map.items():
filter = 0 kq_filter = 0
if obj.readable(): if obj.readable():
filter |= select.KQ_FILTER_READ kq_filter |= select.KQ_FILTER_READ
if obj.writable(): if obj.writable():
filter |= select.KQ_FILTER_WRITE kq_filter |= select.KQ_FILTER_WRITE
if filter: if kq_filter:
try: try:
ev = select.kevent(fd, filter=filter, flags=flags) ev = select.kevent(fd, filter=kq_filter, flags=flags)
kqueue.control([ev], 0) kqueue.control([ev], 0)
selectables += 1 selectables += 1
except IOError: 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 # argument which should no longer be used in favor of
# "poller" # "poller"
if poller is None: if poller is None:
if hasattr(select, 'epoll'): if use_poll:
poller = poll_poller
elif hasattr(select, 'epoll'):
poller = epoll_poller poller = epoll_poller
elif hasattr(select, 'kqueue'): elif hasattr(select, 'kqueue'):
poller = kqueue_poller poller = kqueue_poller
@ -506,9 +507,9 @@ class dispatcher:
# no poll used, or not registered # no poll used, or not registered
pass pass
def create_socket(self, family=socket.AF_INET, type=socket.SOCK_STREAM): def create_socket(self, family=socket.AF_INET, socket_type=socket.SOCK_STREAM):
self.family_and_type = family, type self.family_and_type = family, socket_type
sock = socket.socket(family, type) sock = socket.socket(family, socket_type)
sock.setblocking(0) sock.setblocking(0)
self.set_socket(sock) self.set_socket(sock)
@ -652,9 +653,9 @@ class dispatcher:
def log(self, message): def log(self, message):
sys.stderr.write('log: %s\n' % str(message)) sys.stderr.write('log: %s\n' % str(message))
def log_info(self, message, type='info'): def log_info(self, message, log_type='info'):
if type not in self.ignore_log_types: if log_type not in self.ignore_log_types:
print('%s: %s' % (type, message)) print('%s: %s' % (log_type, message))
def handle_read_event(self): def handle_read_event(self):
if self.accepting: if self.accepting:
@ -744,7 +745,7 @@ class dispatcher:
def handle_accepted(self, sock, addr): def handle_accepted(self, sock, addr):
sock.close() sock.close()
self.log_info('unhandled accepted event', 'warning') self.log_info('unhandled accepted event on %s' % (addr), 'warning')
def handle_close(self): def handle_close(self):
self.log_info('unhandled close event', 'warning') 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()): for x in list(map.values()):
try: try:
x.close() x.close()
except OSError as x: except OSError as e:
if x.args[0] == EBADF: if e.args[0] == EBADF:
pass pass
elif not ignore_all: elif not ignore_all:
raise raise

View File

@ -1,26 +1,22 @@
import base64 import base64
from binascii import hexlify
import hashlib import hashlib
import math
import time import time
import socket import socket
import struct import struct
import sys
from addresses import calculateInventoryHash
from bmconfigparser import BMConfigParser from bmconfigparser import BMConfigParser
from debug import logger from debug import logger
from inventory import Inventory from inventory import Inventory
import knownnodes import knownnodes
from network.advanceddispatcher import AdvancedDispatcher 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 import network.connectionpool
from network.node import Node from network.node import Node
from network.objectracker import ObjectTracker from network.objectracker import ObjectTracker
from network.proxy import Proxy, ProxyError, GeneralProxyError from network.proxy import Proxy, ProxyError, GeneralProxyError
import addresses import addresses
from bmconfigparser import BMConfigParser
from queues import objectProcessorQueue, portCheckerQueue, invQueue, addrQueue from queues import objectProcessorQueue, portCheckerQueue, invQueue, addrQueue
import shared import shared
import state import state

View File

@ -6,7 +6,7 @@ import knownnodes
from queues import portCheckerQueue from queues import portCheckerQueue
import state import state
def getDiscoveredPeer(stream): def getDiscoveredPeer():
try: try:
peer = random.choice(state.discoveredPeers.keys()) peer = random.choice(state.discoveredPeers.keys())
except (IndexError, KeyError): except (IndexError, KeyError):
@ -27,9 +27,11 @@ def chooseConnection(stream):
return retval return retval
except Queue.Empty: except Queue.Empty:
pass pass
if random.choice((False, True)): # with a probability of 0.5, connect to a discovered peer
return getDiscoveredPeer(stream) if random.choice((False, True)) and not haveOnion:
for i in range(50): # discovered peers are already filtered by allowed streams
return getDiscoveredPeer()
for _ in range(50):
peer = random.choice(knownnodes.knownNodes[stream].keys()) peer = random.choice(knownnodes.knownNodes[stream].keys())
try: try:
rating = knownnodes.knownNodes[stream][peer]["rating"] rating = knownnodes.knownNodes[stream][peer]["rating"]

View File

@ -1,11 +1,7 @@
import time import time
from bmconfigparser import BMConfigParser
from network.connectionpool import BMConnectionPool from network.connectionpool import BMConnectionPool
from inventory import PendingDownloadQueue, PendingUpload
import asyncore_pollchoose as asyncore import asyncore_pollchoose as asyncore
import shared
import throttle
lastReceivedTimestamp = time.time() lastReceivedTimestamp = time.time()
lastReceivedBytes = 0 lastReceivedBytes = 0
@ -16,7 +12,8 @@ currentSentSpeed = 0
def connectedHostsList(): def connectedHostsList():
retval = [] retval = []
for i in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values(): for i in BMConnectionPool().inboundConnections.values() + \
BMConnectionPool().outboundConnections.values():
if not i.fullyEstablished: if not i.fullyEstablished:
continue continue
try: try:
@ -46,22 +43,26 @@ def downloadSpeed():
currentTimestamp = time.time() currentTimestamp = time.time()
if int(lastReceivedTimestamp) < int(currentTimestamp): if int(lastReceivedTimestamp) < int(currentTimestamp):
currentReceivedBytes = asyncore.receivedBytes currentReceivedBytes = asyncore.receivedBytes
currentReceivedSpeed = int((currentReceivedBytes - lastReceivedBytes) / (currentTimestamp - lastReceivedTimestamp)) currentReceivedSpeed = int((currentReceivedBytes - lastReceivedBytes) /
(currentTimestamp - lastReceivedTimestamp))
lastReceivedBytes = currentReceivedBytes lastReceivedBytes = currentReceivedBytes
lastReceivedTimestamp = currentTimestamp lastReceivedTimestamp = currentTimestamp
return currentReceivedSpeed return currentReceivedSpeed
def pendingDownload(): def pendingDownload():
tmp = {} 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(): for k in connection.objectsNewToMe.keys():
tmp[k] = True tmp[k] = True
return len(tmp) return len(tmp)
def pendingUpload(): def pendingUpload():
return 0
tmp = {} 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(): for k in connection.objectsNewToThem.keys():
tmp[k] = True tmp[k] = True
return len(tmp) #This probably isn't the correct logic so it's disabled
#return len(tmp)
return 0