Code quality improvements
This commit is contained in:
parent
243025a1aa
commit
2685fe29b1
|
@ -27,7 +27,6 @@ import socket
|
||||||
import ctypes
|
import ctypes
|
||||||
from struct import pack
|
from struct import pack
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
import time
|
|
||||||
|
|
||||||
from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer
|
from api import MySimpleXMLRPCRequestHandler, StoppableXMLRPCServer
|
||||||
from helper_startup import isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
|
from helper_startup import isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections
|
||||||
|
|
|
@ -53,20 +53,20 @@ class SafeHTMLParser(HTMLParser):
|
||||||
self.allow_external_src = False
|
self.allow_external_src = False
|
||||||
|
|
||||||
def add_if_acceptable(self, tag, attrs = None):
|
def add_if_acceptable(self, tag, attrs = None):
|
||||||
if not tag in SafeHTMLParser.acceptable_elements:
|
if tag not in SafeHTMLParser.acceptable_elements:
|
||||||
return
|
return
|
||||||
self.sanitised += "<"
|
self.sanitised += "<"
|
||||||
if inspect.stack()[1][3] == "handle_endtag":
|
if inspect.stack()[1][3] == "handle_endtag":
|
||||||
self.sanitised += "/"
|
self.sanitised += "/"
|
||||||
self.sanitised += tag
|
self.sanitised += tag
|
||||||
if not attrs is None:
|
if attrs is not None:
|
||||||
for attr, val in attrs:
|
for attr, val in attrs:
|
||||||
if tag == "img" and attr == "src" and not self.allow_picture:
|
if tag == "img" and attr == "src" and not self.allow_picture:
|
||||||
val = ""
|
val = ""
|
||||||
elif attr == "src" and not self.allow_external_src:
|
elif attr == "src" and not self.allow_external_src:
|
||||||
url = urlparse(val)
|
url = urlparse(val)
|
||||||
if url.scheme not in SafeHTMLParser.src_schemes:
|
if url.scheme not in SafeHTMLParser.src_schemes:
|
||||||
val == ""
|
val = ""
|
||||||
self.sanitised += " " + quote_plus(attr)
|
self.sanitised += " " + quote_plus(attr)
|
||||||
if not (val is None):
|
if not (val is None):
|
||||||
self.sanitised += "=\"" + val + "\""
|
self.sanitised += "=\"" + val + "\""
|
||||||
|
|
|
@ -36,14 +36,13 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
raise TypeError("option values must be strings")
|
raise TypeError("option values must be strings")
|
||||||
return ConfigParser.ConfigParser.set(self, section, option, value)
|
return ConfigParser.ConfigParser.set(self, section, option, value)
|
||||||
|
|
||||||
def get(self, section, option, raw=False, vars=None):
|
def get(self, section, option, raw=False, variables=None):
|
||||||
try:
|
try:
|
||||||
if section == "bitmessagesettings" and option == "timeformat":
|
if section == "bitmessagesettings" and option == "timeformat":
|
||||||
return ConfigParser.ConfigParser.get(self, section, option, raw, vars)
|
return ConfigParser.ConfigParser.get(self, section, option, raw, variables)
|
||||||
else:
|
return ConfigParser.ConfigParser.get(self, section, option, True, variables)
|
||||||
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
|
|
||||||
except ConfigParser.InterpolationError:
|
except ConfigParser.InterpolationError:
|
||||||
return ConfigParser.ConfigParser.get(self, section, option, True, vars)
|
return ConfigParser.ConfigParser.get(self, section, option, True, variables)
|
||||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
|
||||||
try:
|
try:
|
||||||
return BMConfigDefaults[section][option]
|
return BMConfigDefaults[section][option]
|
||||||
|
@ -68,8 +67,8 @@ class BMConfigParser(ConfigParser.SafeConfigParser):
|
||||||
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
|
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError, ValueError, AttributeError):
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def items(self, section, raw=False, vars=None):
|
def items(self, section, raw=False, variables=None):
|
||||||
return ConfigParser.ConfigParser.items(self, section, True, vars)
|
return ConfigParser.ConfigParser.items(self, section, True, variables)
|
||||||
|
|
||||||
def addresses(self):
|
def addresses(self):
|
||||||
return filter(lambda x: x.startswith('BM-'), BMConfigParser().sections())
|
return filter(lambda x: x.startswith('BM-'), BMConfigParser().sections())
|
||||||
|
|
|
@ -5,7 +5,6 @@ import time
|
||||||
|
|
||||||
import asyncore_pollchoose as asyncore
|
import asyncore_pollchoose as asyncore
|
||||||
from debug import logger
|
from debug import logger
|
||||||
from bmconfigparser import BMConfigParser
|
|
||||||
|
|
||||||
class AdvancedDispatcher(asyncore.dispatcher):
|
class AdvancedDispatcher(asyncore.dispatcher):
|
||||||
_buf_len = 2097152 # 2MB
|
_buf_len = 2097152 # 2MB
|
||||||
|
@ -34,11 +33,10 @@ class AdvancedDispatcher(asyncore.dispatcher):
|
||||||
def read_buf_sufficient(self, length=0):
|
def read_buf_sufficient(self, length=0):
|
||||||
if len(self.read_buf) < length:
|
if len(self.read_buf) < length:
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def process(self):
|
def process(self):
|
||||||
if self.state != "tls_handshake" and len(self.read_buf) == 0:
|
if self.state != "tls_handshake" and not self.read_buf:
|
||||||
return
|
return
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
return
|
return
|
||||||
|
@ -100,7 +98,7 @@ class AdvancedDispatcher(asyncore.dispatcher):
|
||||||
break
|
break
|
||||||
if bufSize <= 0:
|
if bufSize <= 0:
|
||||||
return
|
return
|
||||||
if len(self.write_buf) > 0:
|
if self.write_buf:
|
||||||
written = self.send(self.write_buf[0:bufSize])
|
written = self.send(self.write_buf[0:bufSize])
|
||||||
asyncore.update_sent(written)
|
asyncore.update_sent(written)
|
||||||
self.sentBytes += written
|
self.sentBytes += written
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import Queue
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
@ -8,7 +7,6 @@ from helper_threading import StoppableThread
|
||||||
from network.bmproto import BMProto
|
from network.bmproto import BMProto
|
||||||
from network.connectionpool import BMConnectionPool
|
from network.connectionpool import BMConnectionPool
|
||||||
from network.udp import UDPSocket
|
from network.udp import UDPSocket
|
||||||
import protocol
|
|
||||||
import state
|
import state
|
||||||
|
|
||||||
class AnnounceThread(threading.Thread, StoppableThread):
|
class AnnounceThread(threading.Thread, StoppableThread):
|
||||||
|
@ -33,6 +31,3 @@ class AnnounceThread(threading.Thread, StoppableThread):
|
||||||
for stream in state.streamsInWhichIAmParticipating:
|
for stream in state.streamsInWhichIAmParticipating:
|
||||||
addr = (stream, state.Peer('127.0.0.1', BMConfigParser().safeGetInt("bitmessagesettings", "port")), time.time())
|
addr = (stream, state.Peer('127.0.0.1', BMConfigParser().safeGetInt("bitmessagesettings", "port")), time.time())
|
||||||
connection.writeQueue.put(BMProto.assembleAddr([addr]))
|
connection.writeQueue.put(BMProto.assembleAddr([addr]))
|
||||||
|
|
||||||
def stopThread(self):
|
|
||||||
super(AnnounceThread, self).stopThread()
|
|
||||||
|
|
|
@ -604,11 +604,6 @@ class dispatcher:
|
||||||
# cheap inheritance, used to pass all other attribute
|
# cheap inheritance, used to pass all other attribute
|
||||||
# references to the underlying socket object.
|
# references to the underlying socket object.
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
try:
|
|
||||||
sys._getframe(200)
|
|
||||||
logger.error("Stack depth warning")
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
try:
|
try:
|
||||||
retattr = getattr(self.socket, attr)
|
retattr = getattr(self.socket, attr)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
|
|
@ -51,18 +51,18 @@ class BMObject(object):
|
||||||
def checkEOLSanity(self):
|
def checkEOLSanity(self):
|
||||||
# EOL sanity check
|
# EOL sanity check
|
||||||
if self.expiresTime - int(time.time()) > BMObject.maxTTL:
|
if self.expiresTime - int(time.time()) > BMObject.maxTTL:
|
||||||
logger.info('This object\'s End of Life time is too far in the future. Ignoring it. Time is %s' % self.expiresTime)
|
logger.info('This object\'s End of Life time is too far in the future. Ignoring it. Time is %i', self.expiresTime)
|
||||||
# TODO: remove from download queue
|
# TODO: remove from download queue
|
||||||
raise BMObjectExpiredError()
|
raise BMObjectExpiredError()
|
||||||
|
|
||||||
if self.expiresTime - int(time.time()) < BMObject.minTTL:
|
if self.expiresTime - int(time.time()) < BMObject.minTTL:
|
||||||
logger.info('This object\'s End of Life time was too long ago. Ignoring the object. Time is %s' % self.expiresTime)
|
logger.info('This object\'s End of Life time was too long ago. Ignoring the object. Time is %i', self.expiresTime)
|
||||||
# TODO: remove from download queue
|
# TODO: remove from download queue
|
||||||
raise BMObjectExpiredError()
|
raise BMObjectExpiredError()
|
||||||
|
|
||||||
def checkStream(self):
|
def checkStream(self):
|
||||||
if self.streamNumber not in state.streamsInWhichIAmParticipating:
|
if self.streamNumber not in state.streamsInWhichIAmParticipating:
|
||||||
logger.debug('The streamNumber %s isn\'t one we are interested in.' % self.streamNumber)
|
logger.debug('The streamNumber %i isn\'t one we are interested in.', self.streamNumber)
|
||||||
raise BMObjectUnwantedStreamError()
|
raise BMObjectUnwantedStreamError()
|
||||||
|
|
||||||
def checkAlreadyHave(self):
|
def checkAlreadyHave(self):
|
||||||
|
|
|
@ -15,14 +15,12 @@ from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInv
|
||||||
import network.connectionpool
|
import network.connectionpool
|
||||||
from network.downloadqueue import DownloadQueue
|
from network.downloadqueue import DownloadQueue
|
||||||
from network.node import Node
|
from network.node import Node
|
||||||
import network.asyncore_pollchoose as asyncore
|
|
||||||
from network.objectracker import ObjectTracker
|
from network.objectracker import ObjectTracker
|
||||||
from network.proxy import Proxy, ProxyError, GeneralProxyError
|
from network.proxy import Proxy, ProxyError, GeneralProxyError
|
||||||
from network.uploadqueue import UploadQueue, UploadElem, AddrUploadQueue, ObjUploadQueue
|
|
||||||
|
|
||||||
import addresses
|
import addresses
|
||||||
from bmconfigparser import BMConfigParser
|
from bmconfigparser import BMConfigParser
|
||||||
from queues import objectProcessorQueue, portCheckerQueue, UISignalQueue, invQueue
|
from queues import objectProcessorQueue, portCheckerQueue, invQueue
|
||||||
import shared
|
import shared
|
||||||
import state
|
import state
|
||||||
import protocol
|
import protocol
|
||||||
|
@ -173,7 +171,6 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
|
|
||||||
retval = []
|
retval = []
|
||||||
size = None
|
size = None
|
||||||
insideDigit = False
|
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
while i < len(pattern):
|
while i < len(pattern):
|
||||||
|
@ -353,14 +350,13 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
self.set_state("tls_init", self.payloadLength)
|
self.set_state("tls_init", self.payloadLength)
|
||||||
self.bm_proto_reset()
|
self.bm_proto_reset()
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
self.set_connection_fully_established()
|
self.set_connection_fully_established()
|
||||||
return True
|
return True
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def bm_command_version(self):
|
def bm_command_version(self):
|
||||||
#self.remoteProtocolVersion, self.services, self.timestamp, padding1, self.myExternalIP, padding2, self.remoteNodeIncomingPort = protocol.VersionPacket.unpack(self.payload[:protocol.VersionPacket.size])
|
self.remoteProtocolVersion, self.services, self.timestamp, self.sockNode, self.peerNode, self.nonce, \
|
||||||
self.remoteProtocolVersion, self.services, self.timestamp, self.sockNode, self.peerNode, self.nonce, self.userAgent, self.streams = self.decode_payload_content("IQQiiQlslv")
|
self.userAgent, self.streams = self.decode_payload_content("IQQiiQlslv")
|
||||||
self.nonce = struct.pack('>Q', self.nonce)
|
self.nonce = struct.pack('>Q', self.nonce)
|
||||||
self.timeOffset = self.timestamp - int(time.time())
|
self.timeOffset = self.timestamp - int(time.time())
|
||||||
logger.debug("remoteProtocolVersion: %i", self.remoteProtocolVersion)
|
logger.debug("remoteProtocolVersion: %i", self.remoteProtocolVersion)
|
||||||
|
@ -377,7 +373,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
self.writeQueue.put(protocol.CreatePacket('verack'))
|
self.writeQueue.put(protocol.CreatePacket('verack'))
|
||||||
self.verackSent = True
|
self.verackSent = True
|
||||||
if not self.isOutbound:
|
if not self.isOutbound:
|
||||||
self.writeQueue.put(protocol.assembleVersionMessage(self.destination.host, self.destination.port, network.connectionpool.BMConnectionPool().streams, True))
|
self.writeQueue.put(protocol.assembleVersionMessage(self.destination.host, self.destination.port, \
|
||||||
|
network.connectionpool.BMConnectionPool().streams, True))
|
||||||
#print "%s:%i: Sending version" % (self.destination.host, self.destination.port)
|
#print "%s:%i: Sending version" % (self.destination.host, self.destination.port)
|
||||||
if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and
|
if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) and
|
||||||
protocol.haveSSL(not self.isOutbound)):
|
protocol.haveSSL(not self.isOutbound)):
|
||||||
|
@ -387,7 +384,6 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
self.set_state("tls_init", self.payloadLength)
|
self.set_state("tls_init", self.payloadLength)
|
||||||
self.bm_proto_reset()
|
self.bm_proto_reset()
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
self.set_connection_fully_established()
|
self.set_connection_fully_established()
|
||||||
return True
|
return True
|
||||||
return True
|
return True
|
||||||
|
@ -415,7 +411,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
shared.timeOffsetWrongCount = 0
|
shared.timeOffsetWrongCount = 0
|
||||||
if len(self.streams) == 0:
|
if not self.streams:
|
||||||
self.writeQueue.put(protocol.assembleErrorMessage(fatal=2,
|
self.writeQueue.put(protocol.assembleErrorMessage(fatal=2,
|
||||||
errorText="We don't have shared stream interests. Closing connection."))
|
errorText="We don't have shared stream interests. Closing connection."))
|
||||||
logger.debug ('Closed connection to %s because there is no overlapping interest in streams.',
|
logger.debug ('Closed connection to %s because there is no overlapping interest in streams.',
|
||||||
|
@ -442,7 +438,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def assembleAddr(peerList):
|
def assembleAddr(peerList):
|
||||||
if type(peerList) is state.Peer:
|
if isinstance(peerList, state.Peer):
|
||||||
peerList = (peerList)
|
peerList = (peerList)
|
||||||
# TODO handle max length, now it's done by upper layers
|
# TODO handle max length, now it's done by upper layers
|
||||||
payload = addresses.encodeVarint(len(peerList))
|
payload = addresses.encodeVarint(len(peerList))
|
||||||
|
|
|
@ -9,7 +9,6 @@ import state
|
||||||
def chooseConnection(stream):
|
def chooseConnection(stream):
|
||||||
if state.trustedPeer:
|
if state.trustedPeer:
|
||||||
return state.trustedPeer
|
return state.trustedPeer
|
||||||
else:
|
|
||||||
try:
|
try:
|
||||||
retval = portCheckerQueue.get(False)
|
retval = portCheckerQueue.get(False)
|
||||||
portCheckerQueue.task_done()
|
portCheckerQueue.task_done()
|
||||||
|
|
|
@ -15,7 +15,6 @@ from network.connectionchooser import chooseConnection
|
||||||
import network.asyncore_pollchoose as asyncore
|
import network.asyncore_pollchoose as asyncore
|
||||||
import protocol
|
import protocol
|
||||||
from singleton import Singleton
|
from singleton import Singleton
|
||||||
import shared
|
|
||||||
import state
|
import state
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import Queue
|
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
import addresses
|
import addresses
|
||||||
|
@ -30,7 +29,7 @@ class DownloadThread(threading.Thread, StoppableThread):
|
||||||
continue
|
continue
|
||||||
# keys with True values in the dict
|
# keys with True values in the dict
|
||||||
request = list((k for k, v in i.objectsNewToMe.iteritems() if v))
|
request = list((k for k, v in i.objectsNewToMe.iteritems() if v))
|
||||||
if len(request) == 0:
|
if not request:
|
||||||
continue
|
continue
|
||||||
if len(request) > DownloadThread.requestChunk - downloadPending:
|
if len(request) > DownloadThread.requestChunk - downloadPending:
|
||||||
request = request[:DownloadThread.requestChunk - downloadPending]
|
request = request[:DownloadThread.requestChunk - downloadPending]
|
||||||
|
@ -43,6 +42,3 @@ class DownloadThread(threading.Thread, StoppableThread):
|
||||||
logger.debug("%s:%i Requesting %i objects", i.destination.host, i.destination.port, len(request))
|
logger.debug("%s:%i Requesting %i objects", i.destination.host, i.destination.port, len(request))
|
||||||
requested += len(request)
|
requested += len(request)
|
||||||
self.stop.wait(1)
|
self.stop.wait(1)
|
||||||
|
|
||||||
def stopThread(self):
|
|
||||||
super(DownloadThread, self).stopThread()
|
|
||||||
|
|
|
@ -43,7 +43,7 @@ class InvThread(threading.Thread, StoppableThread):
|
||||||
except Queue.Empty:
|
except Queue.Empty:
|
||||||
break
|
break
|
||||||
|
|
||||||
if len(self.collectionOfInvs[iterator]) > 0:
|
if self.collectionOfInvs[iterator]:
|
||||||
for connection in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
for connection in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
||||||
hashes = []
|
hashes = []
|
||||||
for stream in connection.streams:
|
for stream in connection.streams:
|
||||||
|
@ -57,23 +57,23 @@ class InvThread(threading.Thread, StoppableThread):
|
||||||
pass
|
pass
|
||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
if len(hashes) > 0:
|
if hashes:
|
||||||
connection.writeQueue.put(protocol.CreatePacket('inv', addresses.encodeVarint(len(hashes)) + "".join(hashes)))
|
connection.writeQueue.put(protocol.CreatePacket('inv', addresses.encodeVarint(len(hashes)) + "".join(hashes)))
|
||||||
self.collectionOfInvs[iterator] = {}
|
self.collectionOfInvs[iterator] = {}
|
||||||
iterator += 1
|
iterator += 1
|
||||||
iterator %= InvThread.size
|
iterator %= InvThread.size
|
||||||
self.stop.wait(1)
|
self.stop.wait(1)
|
||||||
|
|
||||||
def holdHash(self, stream, hash):
|
def holdHash(self, stream, hashId):
|
||||||
i = random.randrange(0, InvThread.size)
|
i = random.randrange(0, InvThread.size)
|
||||||
if stream not in self.collectionOfInvs[i]:
|
if stream not in self.collectionOfInvs[i]:
|
||||||
self.collectionOfInvs[i][stream] = []
|
self.collectionOfInvs[i][stream] = []
|
||||||
self.collectionOfInvs[i][stream].append(hash)
|
self.collectionOfInvs[i][stream].append(hashId)
|
||||||
|
|
||||||
def hasHash(self, hash):
|
def hasHash(self, hashId):
|
||||||
for streamlist in self.collectionOfInvs:
|
for streamlist in self.collectionOfInvs:
|
||||||
for stream in streamlist:
|
for stream in streamlist:
|
||||||
if hash in streamlist[stream]:
|
if hashId in streamlist[stream]:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -18,20 +18,18 @@ def connectedHostsList():
|
||||||
if BMConfigParser().get("network", "asyncore"):
|
if BMConfigParser().get("network", "asyncore"):
|
||||||
retval = []
|
retval = []
|
||||||
for i in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
for i in BMConnectionPool().inboundConnections.values() + BMConnectionPool().outboundConnections.values():
|
||||||
if not i.connected:
|
if not i.fullyEstablished:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
retval.append((i.destination, i.streams[0]))
|
retval.append((i.destination, i.streams[0]))
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
return retval
|
return retval
|
||||||
else:
|
|
||||||
return shared.connectedHostsList.items()
|
return shared.connectedHostsList.items()
|
||||||
|
|
||||||
def sentBytes():
|
def sentBytes():
|
||||||
if BMConfigParser().get("network", "asyncore"):
|
if BMConfigParser().get("network", "asyncore"):
|
||||||
return asyncore.sentBytes
|
return asyncore.sentBytes
|
||||||
else:
|
|
||||||
return throttle.SendThrottle().total
|
return throttle.SendThrottle().total
|
||||||
|
|
||||||
def uploadSpeed():
|
def uploadSpeed():
|
||||||
|
@ -44,13 +42,11 @@ def uploadSpeed():
|
||||||
lastSentBytes = currentSentBytes
|
lastSentBytes = currentSentBytes
|
||||||
lastSentTimestamp = currentTimestamp
|
lastSentTimestamp = currentTimestamp
|
||||||
return currentSentSpeed
|
return currentSentSpeed
|
||||||
else:
|
|
||||||
return throttle.sendThrottle().getSpeed()
|
return throttle.sendThrottle().getSpeed()
|
||||||
|
|
||||||
def receivedBytes():
|
def receivedBytes():
|
||||||
if BMConfigParser().get("network", "asyncore"):
|
if BMConfigParser().get("network", "asyncore"):
|
||||||
return asyncore.receivedBytes
|
return asyncore.receivedBytes
|
||||||
else:
|
|
||||||
return throttle.ReceiveThrottle().total
|
return throttle.ReceiveThrottle().total
|
||||||
|
|
||||||
def downloadSpeed():
|
def downloadSpeed():
|
||||||
|
@ -63,7 +59,6 @@ def downloadSpeed():
|
||||||
lastReceivedBytes = currentReceivedBytes
|
lastReceivedBytes = currentReceivedBytes
|
||||||
lastReceivedTimestamp = currentTimestamp
|
lastReceivedTimestamp = currentTimestamp
|
||||||
return currentReceivedSpeed
|
return currentReceivedSpeed
|
||||||
else:
|
|
||||||
return throttle.ReceiveThrottle().getSpeed()
|
return throttle.ReceiveThrottle().getSpeed()
|
||||||
|
|
||||||
def pendingDownload():
|
def pendingDownload():
|
||||||
|
@ -73,7 +68,6 @@ def pendingDownload():
|
||||||
for k in connection.objectsNewToMe.keys():
|
for k in connection.objectsNewToMe.keys():
|
||||||
tmp[k] = True
|
tmp[k] = True
|
||||||
return len(tmp)
|
return len(tmp)
|
||||||
else:
|
|
||||||
return PendingDownloadQueue.totalSize()
|
return PendingDownloadQueue.totalSize()
|
||||||
|
|
||||||
def pendingUpload():
|
def pendingUpload():
|
||||||
|
@ -84,5 +78,4 @@ def pendingUpload():
|
||||||
for k in connection.objectsNewToThem.keys():
|
for k in connection.objectsNewToThem.keys():
|
||||||
tmp[k] = True
|
tmp[k] = True
|
||||||
return len(tmp)
|
return len(tmp)
|
||||||
else:
|
|
||||||
return PendingUpload().len()
|
return PendingUpload().len()
|
||||||
|
|
|
@ -63,20 +63,18 @@ class TLSDispatcher(AdvancedDispatcher):
|
||||||
|
|
||||||
def writable(self):
|
def writable(self):
|
||||||
try:
|
try:
|
||||||
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
|
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
|
||||||
#print "tls writable, %r" % (self.want_write)
|
#print "tls writable, %r" % (self.want_write)
|
||||||
return self.want_write
|
return self.want_write
|
||||||
else:
|
|
||||||
return AdvancedDispatcher.writable(self)
|
return AdvancedDispatcher.writable(self)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return AdvancedDispatcher.writable(self)
|
return AdvancedDispatcher.writable(self)
|
||||||
|
|
||||||
def readable(self):
|
def readable(self):
|
||||||
try:
|
try:
|
||||||
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
|
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
|
||||||
#print "tls readable, %r" % (self.want_read)
|
#print "tls readable, %r" % (self.want_read)
|
||||||
return self.want_read
|
return self.want_read
|
||||||
else:
|
|
||||||
return AdvancedDispatcher.readable(self)
|
return AdvancedDispatcher.readable(self)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return AdvancedDispatcher.readable(self)
|
return AdvancedDispatcher.readable(self)
|
||||||
|
@ -84,7 +82,7 @@ class TLSDispatcher(AdvancedDispatcher):
|
||||||
def handle_read(self):
|
def handle_read(self):
|
||||||
try:
|
try:
|
||||||
# wait for write buffer flush
|
# wait for write buffer flush
|
||||||
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
|
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
|
||||||
#print "handshaking (read)"
|
#print "handshaking (read)"
|
||||||
self.tls_handshake()
|
self.tls_handshake()
|
||||||
else:
|
else:
|
||||||
|
@ -104,7 +102,7 @@ class TLSDispatcher(AdvancedDispatcher):
|
||||||
def handle_write(self):
|
def handle_write(self):
|
||||||
try:
|
try:
|
||||||
# wait for write buffer flush
|
# wait for write buffer flush
|
||||||
if self.tlsStarted and not self.tlsDone and len(self.write_buf) == 0 and self.writeQueue.empty():
|
if self.tlsStarted and not self.tlsDone and not self.write_buf and self.writeQueue.empty():
|
||||||
#print "handshaking (write)"
|
#print "handshaking (write)"
|
||||||
self.tls_handshake()
|
self.tls_handshake()
|
||||||
else:
|
else:
|
||||||
|
@ -123,13 +121,13 @@ class TLSDispatcher(AdvancedDispatcher):
|
||||||
|
|
||||||
def tls_handshake(self):
|
def tls_handshake(self):
|
||||||
# wait for flush
|
# wait for flush
|
||||||
if len(self.write_buf) > 0:
|
if self.write_buf:
|
||||||
return False
|
return False
|
||||||
# Perform the handshake.
|
# Perform the handshake.
|
||||||
try:
|
try:
|
||||||
#print "handshaking (internal)"
|
#print "handshaking (internal)"
|
||||||
self.sslSocket.do_handshake()
|
self.sslSocket.do_handshake()
|
||||||
except ssl.SSLError, err:
|
except ssl.SSLError as err:
|
||||||
#print "%s:%i: handshake fail" % (self.destination.host, self.destination.port)
|
#print "%s:%i: handshake fail" % (self.destination.host, self.destination.port)
|
||||||
self.want_read = self.want_write = False
|
self.want_read = self.want_write = False
|
||||||
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
||||||
|
|
|
@ -1,32 +1,15 @@
|
||||||
import base64
|
|
||||||
from binascii import hexlify
|
|
||||||
import hashlib
|
|
||||||
import math
|
|
||||||
import time
|
import time
|
||||||
import Queue
|
import Queue
|
||||||
import socket
|
import socket
|
||||||
import struct
|
|
||||||
import random
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
from addresses import calculateInventoryHash
|
|
||||||
from debug import logger
|
from debug import logger
|
||||||
from inventory import Inventory
|
|
||||||
import knownnodes
|
|
||||||
from network.advanceddispatcher import AdvancedDispatcher
|
from network.advanceddispatcher import AdvancedDispatcher
|
||||||
from network.bmproto import BMProtoError, BMProtoInsufficientDataError, BMProtoExcessiveDataError, BMProto
|
from network.bmproto import BMProtoError, BMProtoInsufficientDataError, BMProto
|
||||||
from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInvalidDataError, BMObjectExpiredError, BMObjectUnwantedStreamError, BMObjectInvalidError, BMObjectAlreadyHaveError
|
from network.bmobject import BMObject, BMObjectInsufficientPOWError, BMObjectInvalidDataError, BMObjectExpiredError, BMObjectInvalidError, BMObjectAlreadyHaveError
|
||||||
import network.connectionpool
|
|
||||||
from network.downloadqueue import DownloadQueue
|
|
||||||
from network.node import Node
|
|
||||||
import network.asyncore_pollchoose as asyncore
|
import network.asyncore_pollchoose as asyncore
|
||||||
from network.objectracker import ObjectTracker
|
from network.objectracker import ObjectTracker
|
||||||
from network.uploadqueue import UploadQueue, UploadElem, AddrUploadQueue, ObjUploadQueue
|
|
||||||
|
|
||||||
import addresses
|
from queues import objectProcessorQueue, peerDiscoveryQueue, UISignalQueue
|
||||||
from bmconfigparser import BMConfigParser
|
|
||||||
from queues import objectProcessorQueue, peerDiscoveryQueue, portCheckerQueue, UISignalQueue
|
|
||||||
import shared
|
|
||||||
import state
|
import state
|
||||||
import protocol
|
import protocol
|
||||||
|
|
||||||
|
@ -35,7 +18,7 @@ class UDPSocket(BMProto):
|
||||||
announceInterval = 60
|
announceInterval = 60
|
||||||
|
|
||||||
def __init__(self, host=None, sock=None):
|
def __init__(self, host=None, sock=None):
|
||||||
BMProto.__init__(self, sock)
|
super(BMProto, self).__init__(sock=sock)
|
||||||
self.verackReceived = True
|
self.verackReceived = True
|
||||||
self.verackSent = True
|
self.verackSent = True
|
||||||
# TODO sort out streams
|
# TODO sort out streams
|
||||||
|
|
|
@ -47,7 +47,7 @@ class SqliteInventory(InventoryStorage):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
return len(self._inventory) + sqlQuery('SELECT count(*) FROM inventory')[0][0]
|
return len(self._inventory) + sqlQuery('SELECT count(*) FROM inventory')[0][0]
|
||||||
|
|
||||||
def by_type_and_tag(self, type, tag):
|
def by_type_and_tag(self, objectType, tag):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
values = [value for value in self._inventory.values() if value.type == type and value.tag == tag]
|
values = [value for value in self._inventory.values() if value.type == type and value.tag == tag]
|
||||||
values += (InventoryItem(*value) for value in sqlQuery('SELECT objecttype, streamnumber, payload, expirestime, tag FROM inventory WHERE objecttype=? AND tag=?', type, tag))
|
values += (InventoryItem(*value) for value in sqlQuery('SELECT objecttype, streamnumber, payload, expirestime, tag FROM inventory WHERE objecttype=? AND tag=?', type, tag))
|
||||||
|
|
|
@ -30,7 +30,7 @@ class InventoryStorage(Storage, collections.MutableMapping):
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def by_type_and_tag(self, type, tag):
|
def by_type_and_tag(self, objectType, tag):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def hashes_by_stream(self, stream):
|
def hashes_by_stream(self, stream):
|
||||||
|
|
Reference in New Issue
Block a user