Code Quality

This commit is contained in:
Peter Šurda 2017-08-22 13:49:27 +02:00
parent 18119339f8
commit 660997b8f4
Signed by untrusted user: 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 hashlib
import protocol
import state
from pyelliptic.openssl import OpenSSL
import queues
@ -980,10 +979,12 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler):
queues.UISignalQueue.put(('updateStatusBar', message))
def HandleDeleteAndVacuum(self, params):
if not params:
sqlStoredProcedure('deleteandvacuume')
return 'done'
def HandleShutdown(self, params):
if not params:
shutdown.doCleanShutdown()
return 'done'

View File

@ -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

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -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"]

View File

@ -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