2013-02-18 21:22:48 +01:00
#!/usr/bin/env python2.7
2012-11-19 20:45:05 +01:00
# Copyright (c) 2012 Jonathan Warren
# Copyright (c) 2012 The Bitmessage developers
# Distributed under the MIT/X11 software license. See the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#Right now, PyBitmessage only support connecting to stream 1. It doesn't yet contain logic to expand into further streams.
2013-05-02 22:05:31 +02:00
#The software version variable is now held in shared.py
2013-03-30 20:56:01 +01:00
verbose = 1
2012-11-19 20:45:05 +01:00
maximumAgeOfAnObjectThatIAmWillingToAccept = 216000 #Equals two days and 12 hours.
lengthOfTimeToLeaveObjectsInInventory = 237600 #Equals two days and 18 hours. This should be longer than maximumAgeOfAnObjectThatIAmWillingToAccept so that we don't process messages twice.
2013-02-11 22:28:38 +01:00
lengthOfTimeToHoldOnToAllPubkeys = 2419200 #Equals 4 weeks. You could make this longer if you want but making it shorter would not be advisable because there is a very small possibility that it could keep you from obtaining a needed pubkey for a period of time.
2012-11-19 20:45:05 +01:00
maximumAgeOfObjectsThatIAdvertiseToOthers = 216000 #Equals two days and 12 hours
maximumAgeOfNodesThatIAdvertiseToOthers = 10800 #Equals three hours
2013-02-26 21:07:51 +01:00
storeConfigFilesInSameDirectoryAsProgramByDefault = False #The user may de-select Portable Mode in the settings if they want the config files to stay in the application data folder.
2013-01-22 20:29:49 +01:00
useVeryEasyProofOfWorkForTesting = False #If you set this to True while on the normal network, you won't be able to send or sometimes receive messages.
2013-04-30 18:22:47 +02:00
encryptedBroadcastSwitchoverTime = 1369735200
2012-11-19 20:45:05 +01:00
import sys
2013-05-01 22:06:55 +02:00
import ConfigParser
2012-11-19 20:45:05 +01:00
import Queue
2013-05-02 17:53:54 +02:00
from addresses import *
#from shared import *
import shared
2012-11-19 20:45:05 +01:00
from defaultKnownNodes import *
import time
import socket
import threading
import hashlib
from struct import *
import pickle
import random
import sqlite3
2013-05-02 17:53:54 +02:00
import threading
2013-04-04 18:32:25 +02:00
from time import strftime , localtime , gmtime
2013-02-26 21:07:51 +01:00
import shutil #used for moving the messages.dat file
2012-12-04 18:11:14 +01:00
import string
2012-12-18 19:09:10 +01:00
import socks
2013-01-21 01:00:46 +01:00
import highlevelcrypto
2013-01-16 17:52:52 +01:00
from pyelliptic . openssl import OpenSSL
import ctypes
from pyelliptic import arithmetic
2013-05-01 22:06:55 +02:00
import signal #Used to capture a Ctrl-C keypress so that Bitmessage can shutdown gracefully.
2013-03-22 18:23:44 +01:00
#The next 3 are used for the API
2013-03-19 18:32:37 +01:00
from SimpleXMLRPCServer import *
import json
from subprocess import call #used when the API must execute an outside program
2012-11-19 20:45:05 +01:00
2013-05-01 22:06:55 +02:00
2013-04-12 19:51:14 +02:00
#For each stream to which we connect, several outgoingSynSender threads will exist and will collectively create 8 connections with peers.
2013-05-01 22:06:55 +02:00
class outgoingSynSender ( threading . Thread ) :
def __init__ ( self ) :
threading . Thread . __init__ ( self )
2012-11-19 20:45:05 +01:00
def setup ( self , streamNumber ) :
self . streamNumber = streamNumber
def run ( self ) :
time . sleep ( 1 )
2013-04-12 19:51:14 +02:00
global alreadyAttemptedConnectionsListResetTime
2012-11-19 20:45:05 +01:00
while True :
2013-05-02 17:53:54 +02:00
#time.sleep(999999)#I sometimes use this to prevent connections for testing.
2013-04-12 19:51:14 +02:00
if len ( selfInitiatedConnections [ self . streamNumber ] ) < 8 : #maximum number of outgoing connections = 8
2012-11-19 20:45:05 +01:00
random . seed ( )
2013-05-02 17:53:54 +02:00
HOST , = random . sample ( shared . knownNodes [ self . streamNumber ] , 1 )
2013-04-12 19:51:14 +02:00
alreadyAttemptedConnectionsListLock . acquire ( )
2013-05-03 18:05:57 +02:00
while HOST in alreadyAttemptedConnectionsList or HOST in shared . connectedHostsList :
2013-04-12 19:51:14 +02:00
alreadyAttemptedConnectionsListLock . release ( )
2012-11-19 20:45:05 +01:00
#print 'choosing new sample'
random . seed ( )
2013-05-02 17:53:54 +02:00
HOST , = random . sample ( shared . knownNodes [ self . streamNumber ] , 1 )
2012-11-19 20:45:05 +01:00
time . sleep ( 1 )
#Clear out the alreadyAttemptedConnectionsList every half hour so that this program will again attempt a connection to any nodes, even ones it has already tried.
2013-04-12 19:51:14 +02:00
if ( time . time ( ) - alreadyAttemptedConnectionsListResetTime ) > 1800 :
alreadyAttemptedConnectionsList . clear ( )
alreadyAttemptedConnectionsListResetTime = int ( time . time ( ) )
alreadyAttemptedConnectionsListLock . acquire ( )
alreadyAttemptedConnectionsList [ HOST ] = 0
alreadyAttemptedConnectionsListLock . release ( )
2013-05-02 17:53:54 +02:00
PORT , timeNodeLastSeen = shared . knownNodes [ self . streamNumber ] [ HOST ]
2012-12-18 19:09:10 +01:00
sock = socks . socksocket ( socket . AF_INET , socket . SOCK_STREAM )
2013-04-29 19:46:09 +02:00
#This option apparently avoids the TIME_WAIT state so that we can rebind faster
sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 )
2012-12-19 07:22:04 +01:00
sock . settimeout ( 20 )
2013-05-02 17:53:54 +02:00
if shared . config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' none ' and verbose > = 2 :
shared . printLock . acquire ( )
2012-12-18 19:09:10 +01:00
print ' Trying an outgoing connection to ' , HOST , ' : ' , PORT
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-12-18 19:09:10 +01:00
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2013-05-02 17:53:54 +02:00
elif shared . config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' SOCKS4a ' :
2013-04-12 19:51:14 +02:00
if verbose > = 2 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-12 19:51:14 +02:00
print ' (Using SOCKS4a) Trying an outgoing connection to ' , HOST , ' : ' , PORT
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-12-18 19:09:10 +01:00
proxytype = socks . PROXY_TYPE_SOCKS4
2013-05-02 17:53:54 +02:00
sockshostname = shared . config . get ( ' bitmessagesettings ' , ' sockshostname ' )
socksport = shared . config . getint ( ' bitmessagesettings ' , ' socksport ' )
2012-12-18 19:09:10 +01:00
rdns = True #Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
2013-05-02 17:53:54 +02:00
if shared . config . getboolean ( ' bitmessagesettings ' , ' socksauthentication ' ) :
socksusername = shared . config . get ( ' bitmessagesettings ' , ' socksusername ' )
sockspassword = shared . config . get ( ' bitmessagesettings ' , ' sockspassword ' )
2012-12-18 19:09:10 +01:00
sock . setproxy ( proxytype , sockshostname , socksport , rdns , socksusername , sockspassword )
else :
sock . setproxy ( proxytype , sockshostname , socksport , rdns )
2013-05-02 17:53:54 +02:00
elif shared . config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) == ' SOCKS5 ' :
2013-04-12 19:51:14 +02:00
if verbose > = 2 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-12 19:51:14 +02:00
print ' (Using SOCKS5) Trying an outgoing connection to ' , HOST , ' : ' , PORT
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-12-18 19:09:10 +01:00
proxytype = socks . PROXY_TYPE_SOCKS5
2013-05-02 17:53:54 +02:00
sockshostname = shared . config . get ( ' bitmessagesettings ' , ' sockshostname ' )
socksport = shared . config . getint ( ' bitmessagesettings ' , ' socksport ' )
2012-12-18 19:09:10 +01:00
rdns = True #Do domain name lookups through the proxy; though this setting doesn't really matter since we won't be doing any domain name lookups anyway.
2013-05-02 17:53:54 +02:00
if shared . config . getboolean ( ' bitmessagesettings ' , ' socksauthentication ' ) :
socksusername = shared . config . get ( ' bitmessagesettings ' , ' socksusername ' )
sockspassword = shared . config . get ( ' bitmessagesettings ' , ' sockspassword ' )
2012-12-18 19:09:10 +01:00
sock . setproxy ( proxytype , sockshostname , socksport , rdns , socksusername , sockspassword )
else :
sock . setproxy ( proxytype , sockshostname , socksport , rdns )
2012-11-19 20:45:05 +01:00
try :
sock . connect ( ( HOST , PORT ) )
rd = receiveDataThread ( )
2013-05-01 22:06:55 +02:00
rd . daemon = True # close the main program even if there are threads left
#self.emit(SIGNAL("passObjectThrough(PyQt_PyObject)"),rd)
2013-02-03 06:16:50 +01:00
objectsOfWhichThisRemoteNodeIsAlreadyAware = { }
2013-04-12 19:51:14 +02:00
rd . setup ( sock , HOST , PORT , self . streamNumber , objectsOfWhichThisRemoteNodeIsAlreadyAware )
2012-11-19 20:45:05 +01:00
rd . start ( )
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-03-26 17:19:18 +01:00
print self , ' connected to ' , HOST , ' during an outgoing attempt. '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
sd = sendDataThread ( )
2013-02-03 06:16:50 +01:00
sd . setup ( sock , HOST , PORT , self . streamNumber , objectsOfWhichThisRemoteNodeIsAlreadyAware )
2012-11-19 20:45:05 +01:00
sd . start ( )
sd . sendVersionMessage ( )
2012-12-18 19:09:10 +01:00
except socks . GeneralProxyError , err :
2013-04-12 19:51:14 +02:00
if verbose > = 2 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-12 19:51:14 +02:00
print ' Could NOT connect to ' , HOST , ' during outgoing attempt. ' , err
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
PORT , timeLastSeen = shared . knownNodes [ self . streamNumber ] [ HOST ]
if ( int ( time . time ( ) ) - timeLastSeen ) > 172800 and len ( shared . knownNodes [ self . streamNumber ] ) > 1000 : # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the shared.knownNodes data-structure.
shared . knownNodesLock . acquire ( )
del shared . knownNodes [ self . streamNumber ] [ HOST ]
shared . knownNodesLock . release ( )
print ' deleting ' , HOST , ' from shared.knownNodes because it is more than 48 hours old and we could not connect to it. '
2012-12-18 19:09:10 +01:00
except socks . Socks5AuthError , err :
2013-05-01 22:06:55 +02:00
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS5 Authentication problem: "+str(err))
2013-05-02 17:53:54 +02:00
shared . UISignalQueue . put ( ( ' updateStatusBar ' , " SOCKS5 Authentication problem: " + str ( err ) ) )
2012-12-18 19:09:10 +01:00
except socks . Socks5Error , err :
pass
print ' SOCKS5 error. (It is possible that the server wants authentication).) ' , str ( err )
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS5 error. Server might require authentication. "+str(err))
except socks . Socks4Error , err :
print ' Socks4Error: ' , err
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"SOCKS4 error: "+str(err))
except socket . error , err :
2013-05-02 17:53:54 +02:00
if shared . config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) [ 0 : 5 ] == ' SOCKS ' :
2012-12-18 22:36:37 +01:00
print ' Bitmessage MIGHT be having trouble connecting to the SOCKS server. ' + str ( err )
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),"Problem: Bitmessage can not connect to the SOCKS server. "+str(err))
2012-12-18 19:09:10 +01:00
else :
2013-04-12 19:51:14 +02:00
if verbose > = 1 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-12 19:51:14 +02:00
print ' Could NOT connect to ' , HOST , ' during outgoing attempt. ' , err
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
PORT , timeLastSeen = shared . knownNodes [ self . streamNumber ] [ HOST ]
if ( int ( time . time ( ) ) - timeLastSeen ) > 172800 and len ( shared . knownNodes [ self . streamNumber ] ) > 1000 : # for nodes older than 48 hours old if we have more than 1000 hosts in our list, delete from the knownNodes data-structure.
shared . knownNodesLock . acquire ( )
del shared . knownNodes [ self . streamNumber ] [ HOST ]
shared . knownNodesLock . release ( )
2012-11-19 20:45:05 +01:00
print ' deleting ' , HOST , ' from knownNodes because it is more than 48 hours old and we could not connect to it. '
2012-12-18 19:32:48 +01:00
except Exception , err :
2013-04-17 21:00:42 +02:00
sys . stderr . write ( ' An exception has occurred in the outgoingSynSender thread that was not caught by other exception types: %s \n ' % err )
2013-02-03 06:16:50 +01:00
time . sleep ( 0.1 )
2012-11-19 20:45:05 +01:00
#Only one singleListener thread will ever exist. It creates the receiveDataThread and sendDataThread for each incoming connection. Note that it cannot set the stream number because it is not known yet- the other node will have to tell us its stream number in a version message. If we don't care about their stream, we will close the connection (within the recversion function of the recieveData thread)
2013-05-01 22:06:55 +02:00
class singleListener ( threading . Thread ) :
def __init__ ( self ) :
threading . Thread . __init__ ( self )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def run ( self ) :
2012-12-18 19:09:10 +01:00
#We don't want to accept incoming connections if the user is using a SOCKS proxy. If they eventually select proxy 'none' then this will start listening for connections.
2013-05-02 17:53:54 +02:00
while shared . config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) [ 0 : 5 ] == ' SOCKS ' :
2012-12-18 19:09:10 +01:00
time . sleep ( 300 )
2012-11-19 20:45:05 +01:00
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-02-03 06:16:50 +01:00
print ' Listening for incoming connections. '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
HOST = ' ' # Symbolic name meaning all available interfaces
2013-05-02 17:53:54 +02:00
PORT = shared . config . getint ( ' bitmessagesettings ' , ' port ' )
2012-11-19 20:45:05 +01:00
sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM )
#This option apparently avoids the TIME_WAIT state so that we can rebind faster
sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 )
sock . bind ( ( HOST , PORT ) )
sock . listen ( 2 )
while True :
2013-01-22 06:21:32 +01:00
#We don't want to accept incoming connections if the user is using a SOCKS proxy. If the user eventually select proxy 'none' then this will start listening for connections.
2013-05-02 17:53:54 +02:00
while shared . config . get ( ' bitmessagesettings ' , ' socksproxytype ' ) [ 0 : 5 ] == ' SOCKS ' :
2012-12-18 19:09:10 +01:00
time . sleep ( 10 )
2012-11-19 20:45:05 +01:00
a , ( HOST , PORT ) = sock . accept ( )
2013-01-22 06:21:32 +01:00
#Users are finding that if they run more than one node in the same network (thus with the same public IP), they can not connect with the second node. This is because this section of code won't accept the connection from the same IP. This problem will go away when the Bitmessage network grows beyond being tiny but in the mean time I'll comment out this code section.
2013-05-03 18:05:57 +02:00
""" while HOST in shared.connectedHostsList:
print ' incoming connection is from a host in shared.connectedHostsList (we are already connected to it). Ignoring it. '
2012-11-19 20:45:05 +01:00
a . close ( )
2012-12-27 17:26:52 +01:00
a , ( HOST , PORT ) = sock . accept ( ) """
2013-02-03 06:16:50 +01:00
objectsOfWhichThisRemoteNodeIsAlreadyAware = { }
2012-11-19 20:45:05 +01:00
sd = sendDataThread ( )
2013-02-03 06:16:50 +01:00
sd . setup ( a , HOST , PORT , - 1 , objectsOfWhichThisRemoteNodeIsAlreadyAware )
2012-11-19 20:45:05 +01:00
sd . start ( )
2013-05-07 22:25:01 +02:00
rd = receiveDataThread ( )
rd . daemon = True # close the main program even if there are threads left
rd . setup ( a , HOST , PORT , - 1 , objectsOfWhichThisRemoteNodeIsAlreadyAware )
rd . start ( )
shared . printLock . acquire ( )
print self , ' connected to ' , HOST , ' during INCOMING request. '
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
#This thread is created either by the synSenderThread(for outgoing connections) or the singleListenerThread(for incoming connectiosn).
2013-05-01 22:06:55 +02:00
class receiveDataThread ( threading . Thread ) :
def __init__ ( self ) :
threading . Thread . __init__ ( self )
2012-11-19 20:45:05 +01:00
self . data = ' '
self . verackSent = False
self . verackReceived = False
2013-04-12 19:51:14 +02:00
def setup ( self , sock , HOST , port , streamNumber , objectsOfWhichThisRemoteNodeIsAlreadyAware ) :
2012-11-19 20:45:05 +01:00
self . sock = sock
self . HOST = HOST
self . PORT = port
self . sock . settimeout ( 600 ) #We'll send out a pong every 5 minutes to make sure the connection stays alive if there has been no other traffic to send lately.
self . streamNumber = streamNumber
2013-01-22 06:21:32 +01:00
self . payloadLength = 0 #This is the protocol payload length thus it doesn't include the 24 byte message header
self . receivedgetbiginv = False #Gets set to true once we receive a getbiginv message from our peer. An abusive peer might request it too much so we use this variable to check whether they have already asked for a big inv message.
2013-02-04 22:49:02 +01:00
self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave = { }
2013-05-03 18:05:57 +02:00
shared . connectedHostsList [ self . HOST ] = 0 #The very fact that this receiveData thread exists shows that we are connected to the remote host. Let's add it to this list so that an outgoingSynSender thread doesn't try to connect to it.
2012-11-19 20:45:05 +01:00
self . connectionIsOrWasFullyEstablished = False #set to true after the remote node and I accept each other's version messages. This is needed to allow the user interface to accurately reflect the current number of connections.
if self . streamNumber == - 1 : #This was an incoming connection. Send out a version message if we accept the other node's version message.
self . initiatedConnection = False
else :
self . initiatedConnection = True
2013-04-12 19:51:14 +02:00
selfInitiatedConnections [ streamNumber ] [ self ] = 0
2012-12-19 17:51:51 +01:00
self . ackDataThatWeHaveYetToSend = [ ] #When we receive a message bound for us, we store the acknowledgement that we need to send (the ackdata) here until we are done processing all other data received from this peer.
2013-02-03 06:16:50 +01:00
self . objectsOfWhichThisRemoteNodeIsAlreadyAware = objectsOfWhichThisRemoteNodeIsAlreadyAware
2012-11-19 20:45:05 +01:00
def run ( self ) :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-05-03 18:05:57 +02:00
print ' ID of the receiveDataThread is ' , str ( id ( self ) ) + ' . The size of the shared.connectedHostsList is now ' , len ( shared . connectedHostsList )
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
while True :
try :
2013-04-25 22:11:00 +02:00
self . data + = self . sock . recv ( 4096 )
2012-11-19 20:45:05 +01:00
except socket . timeout :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-30 16:54:30 +02:00
print ' Timeout occurred waiting for data from ' , self . HOST + ' . Closing receiveData thread. (ID: ' , str ( id ( self ) ) + ' ) '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
break
except Exception , err :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-30 16:54:30 +02:00
print ' sock.recv error. Closing receiveData thread (HOST: ' , self . HOST , ' ID: ' , str ( id ( self ) ) + ' ). ' , err
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
break
#print 'Received', repr(self.data)
if self . data == " " :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-30 16:54:30 +02:00
print ' Connection to ' , self . HOST , ' closed. Closing receiveData thread. (ID: ' , str ( id ( self ) ) + ' ) '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
break
else :
self . processData ( )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
2013-04-29 20:12:15 +02:00
""" try:
2013-04-29 19:46:09 +02:00
#self.sock.shutdown(socket.SHUT_RDWR)
2012-11-19 20:45:05 +01:00
self . sock . close ( )
except Exception , err :
2013-04-29 20:12:15 +02:00
print ' Within receiveDataThread run(), self.sock.shutdown or .close() failed. ' , err """
2013-02-18 21:22:48 +01:00
2013-04-12 20:01:22 +02:00
try :
del selfInitiatedConnections [ self . streamNumber ] [ self ]
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-04-29 18:46:33 +02:00
print ' removed self (a receiveDataThread) from selfInitiatedConnections '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-04-12 20:01:22 +02:00
except :
pass
2013-05-02 17:53:54 +02:00
shared . broadcastToSendDataQueues ( ( 0 , ' shutdown ' , self . HOST ) )
2012-12-18 19:09:10 +01:00
try :
2013-05-03 18:05:57 +02:00
del shared . connectedHostsList [ self . HOST ]
2012-12-18 19:09:10 +01:00
except Exception , err :
2013-05-07 22:31:18 +02:00
shared . printLock . acquire ( )
2013-05-03 18:05:57 +02:00
print ' Could not delete ' , self . HOST , ' from shared.connectedHostsList. ' , err
2013-05-07 22:31:18 +02:00
shared . printLock . release ( )
2013-05-03 18:05:57 +02:00
shared . UISignalQueue . put ( ( ' updateNetworkStatusTab ' , ' no data ' ) )
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-05-03 21:53:38 +02:00
print ' The size of the connectedHostsList is now: ' , len ( shared . connectedHostsList )
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-02-18 21:22:48 +01:00
2012-11-19 20:45:05 +01:00
def processData ( self ) :
global verbose
2013-03-28 22:56:20 +01:00
#if verbose >= 3:
2013-05-02 17:53:54 +02:00
#shared.printLock.acquire()
2012-12-05 18:47:30 +01:00
#print 'self.data is currently ', repr(self.data)
2013-05-02 17:53:54 +02:00
#shared.printLock.release()
2013-02-12 21:00:04 +01:00
if len ( self . data ) < 20 : #if so little of the data has arrived that we can't even unpack the payload length
2012-11-19 20:45:05 +01:00
pass
elif self . data [ 0 : 4 ] != ' \xe9 \xbe \xb4 \xd9 ' :
2013-03-28 22:56:20 +01:00
if verbose > = 1 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-02-04 23:13:10 +01:00
sys . stderr . write ( ' The magic bytes were not correct. First 40 bytes of data: %s \n ' % repr ( self . data [ 0 : 40 ] ) )
2013-03-14 16:58:52 +01:00
print ' self.data: ' , self . data . encode ( ' hex ' )
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-02-12 21:00:04 +01:00
self . data = " "
2012-12-05 18:47:30 +01:00
else :
self . payloadLength , = unpack ( ' >L ' , self . data [ 16 : 20 ] )
2013-02-18 21:01:47 +01:00
if len ( self . data ) > = self . payloadLength + 24 : #check if the whole message has arrived yet. If it has,...
2012-12-05 18:47:30 +01:00
if self . data [ 20 : 24 ] == hashlib . sha512 ( self . data [ 24 : self . payloadLength + 24 ] ) . digest ( ) [ 0 : 4 ] : #test the checksum in the message. If it is correct...
#print 'message checksum is correct'
#The time we've last seen this node is obviously right now since we just received valid data from it. So update the knownNodes list so that other peers can be made aware of its existance.
if self . initiatedConnection : #The remote port is only something we should share with others if it is the remote node's incoming port (rather than some random operating-system-assigned outgoing port).
2013-05-02 17:53:54 +02:00
shared . knownNodesLock . acquire ( )
shared . knownNodes [ self . streamNumber ] [ self . HOST ] = ( self . PORT , int ( time . time ( ) ) )
shared . knownNodesLock . release ( )
2013-02-06 22:17:49 +01:00
if self . payloadLength < = 180000000 : #If the size of the message is greater than 180MB, ignore it. (I get memory errors when processing messages much larger than this though it is concievable that this value will have to be lowered if some systems are less tolarant of large messages.)
remoteCommand = self . data [ 4 : 16 ]
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-03-25 19:13:56 +01:00
print ' remoteCommand ' , repr ( remoteCommand . replace ( ' \x00 ' , ' ' ) ) , ' from ' , self . HOST
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-02-06 22:17:49 +01:00
if remoteCommand == ' version \x00 \x00 \x00 \x00 \x00 ' :
2013-03-28 22:56:20 +01:00
self . recversion ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' verack \x00 \x00 \x00 \x00 \x00 \x00 ' :
self . recverack ( )
elif remoteCommand == ' addr \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
2013-03-28 22:56:20 +01:00
self . recaddr ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' getpubkey \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
2013-03-28 22:56:20 +01:00
self . recgetpubkey ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' pubkey \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
2013-03-28 22:56:20 +01:00
self . recpubkey ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' inv \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
2013-03-28 22:56:20 +01:00
self . recinv ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' getdata \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
2013-03-28 22:56:20 +01:00
self . recgetdata ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' getbiginv \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . sendBigInv ( )
elif remoteCommand == ' msg \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
2013-03-28 22:56:20 +01:00
self . recmsg ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' broadcast \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
2013-03-28 22:56:20 +01:00
self . recbroadcast ( self . data [ 24 : self . payloadLength + 24 ] )
2013-02-06 22:17:49 +01:00
elif remoteCommand == ' getaddr \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . sendaddr ( )
elif remoteCommand == ' ping \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
self . sendpong ( )
elif remoteCommand == ' pong \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
pass
elif remoteCommand == ' alert \x00 \x00 \x00 \x00 \x00 \x00 \x00 ' and self . connectionIsOrWasFullyEstablished :
pass
2012-12-05 18:47:30 +01:00
self . data = self . data [ self . payloadLength + 24 : ] #take this message out and then process the next message
if self . data == ' ' :
2013-02-04 22:49:02 +01:00
while len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave ) > 0 :
2012-12-05 18:47:30 +01:00
random . seed ( )
2013-02-04 22:49:02 +01:00
objectHash , = random . sample ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave , 1 )
2013-05-02 17:53:54 +02:00
if objectHash in shared . inventory :
shared . printLock . acquire ( )
2012-12-19 16:38:45 +01:00
print ' Inventory (in memory) already has object listed in inv message. '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-02-04 22:49:02 +01:00
del self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave [ objectHash ]
2012-12-05 18:47:30 +01:00
elif isInSqlInventory ( objectHash ) :
2013-03-30 20:56:01 +01:00
if verbose > = 3 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-03-28 22:56:20 +01:00
print ' Inventory (SQL on disk) already has object listed in inv message. '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-02-04 22:49:02 +01:00
del self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave [ objectHash ]
2012-12-05 18:47:30 +01:00
else :
self . sendgetdata ( objectHash )
2013-02-04 22:49:02 +01:00
del self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave [ objectHash ] #It is possible that the remote node doesn't respond with the object. In that case, we'll very likely get it from someone else anyway.
2013-03-30 20:56:01 +01:00
if len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave ) == 0 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-03-30 23:32:30 +01:00
print ' (concerning ' , self . HOST + ' ) ' , ' number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now ' , len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave )
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-12-05 18:47:30 +01:00
break
2013-03-30 20:56:01 +01:00
if len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave ) == 0 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-03-30 23:32:30 +01:00
print ' (concerning ' , self . HOST + ' ) ' , ' number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now ' , len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave )
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-02-04 22:49:02 +01:00
if len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave ) > 0 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-03-30 23:32:30 +01:00
print ' (concerning ' , self . HOST + ' ) ' , ' number of objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave is now ' , len ( self . objectsThatWeHaveYetToCheckAndSeeWhetherWeAlreadyHave )
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-12-19 17:51:51 +01:00
if len ( self . ackDataThatWeHaveYetToSend ) > 0 :
self . data = self . ackDataThatWeHaveYetToSend . pop ( )
2012-12-05 18:47:30 +01:00
self . processData ( )
else :
print ' Checksum incorrect. Clearing this message. '
self . data = self . data [ self . payloadLength + 24 : ]
2012-11-19 20:45:05 +01:00
2013-04-26 19:20:30 +02:00
def isProofOfWorkSufficient ( self , data , nonceTrialsPerByte = 0 , payloadLengthExtraBytes = 0 ) :
2013-05-02 21:59:10 +02:00
if nonceTrialsPerByte < shared . networkDefaultProofOfWorkNonceTrialsPerByte :
nonceTrialsPerByte = shared . networkDefaultProofOfWorkNonceTrialsPerByte
if payloadLengthExtraBytes < shared . networkDefaultPayloadLengthExtraBytes :
payloadLengthExtraBytes = shared . networkDefaultPayloadLengthExtraBytes
2013-03-28 22:56:20 +01:00
POW , = unpack ( ' >Q ' , hashlib . sha512 ( hashlib . sha512 ( data [ : 8 ] + hashlib . sha512 ( data [ 8 : ] ) . digest ( ) ) . digest ( ) ) . digest ( ) [ 0 : 8 ] )
2012-12-05 18:47:30 +01:00
#print 'POW:', POW
2013-04-26 19:20:30 +02:00
return POW < = 2 * * 64 / ( ( len ( data ) + payloadLengthExtraBytes ) * ( nonceTrialsPerByte ) )
2012-11-19 20:45:05 +01:00
def sendpong ( self ) :
print ' Sending pong '
2013-05-03 18:24:47 +02:00
try :
self . sock . sendall ( ' \xE9 \xBE \xB4 \xD9 \x70 \x6F \x6E \x67 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xcf \x83 \xe1 \x35 ' )
except Exception , err :
#if not 'Bad file descriptor' in err:
shared . printLock . acquire ( )
sys . stderr . write ( ' sock.sendall error: %s \n ' % err )
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
def recverack ( self ) :
print ' verack received '
self . verackReceived = True
if self . verackSent == True :
#We have thus both sent and received a verack.
self . connectionFullyEstablished ( )
def connectionFullyEstablished ( self ) :
self . connectionIsOrWasFullyEstablished = True
if not self . initiatedConnection :
2013-05-01 22:06:55 +02:00
#self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"),'green')
2013-05-02 17:53:54 +02:00
shared . UISignalQueue . put ( ( ' setStatusIcon ' , ' green ' ) )
2013-05-03 18:05:57 +02:00
shared . UISignalQueue . put ( ( ' updateNetworkStatusTab ' , ' no data ' ) )
2013-05-02 17:53:54 +02:00
remoteNodeIncomingPort , remoteNodeSeenTime = shared . knownNodes [ self . streamNumber ] [ self . HOST ]
shared . printLock . acquire ( )
2012-11-23 09:22:56 +01:00
print ' Connection fully established with ' , self . HOST , remoteNodeIncomingPort
2013-05-03 18:05:57 +02:00
print ' The size of the connectedHostsList is now ' , len ( shared . connectedHostsList )
2013-05-02 17:53:54 +02:00
print ' The length of sendDataQueues is now: ' , len ( shared . sendDataQueues )
2012-11-19 20:45:05 +01:00
print ' broadcasting addr from within connectionFullyEstablished function. '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-11-23 09:22:56 +01:00
self . broadcastaddr ( [ ( int ( time . time ( ) ) , self . streamNumber , 1 , self . HOST , remoteNodeIncomingPort ) ] ) #This lets all of our peers know about this new node.
2013-01-22 06:21:32 +01:00
self . sendaddr ( ) #This is one large addr message to this one peer.
2013-05-03 18:05:57 +02:00
if not self . initiatedConnection and len ( shared . connectedHostsList ) > 200 :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2012-11-19 20:45:05 +01:00
print ' We are connected to too many people. Closing connection. '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-04-29 20:12:15 +02:00
#self.sock.shutdown(socket.SHUT_RDWR)
#self.sock.close()
2013-05-02 17:53:54 +02:00
shared . broadcastToSendDataQueues ( ( 0 , ' shutdown ' , self . HOST ) )
2012-11-19 20:45:05 +01:00
return
self . sendBigInv ( )
def sendBigInv ( self ) : #I used capitals in for this function name because there is no such Bitmessage command as 'biginv'.
if self . receivedgetbiginv :
print ' We have already sent a big inv message to this peer. Ignoring request. '
return
else :
self . receivedgetbiginv = True
2013-05-02 17:53:54 +02:00
shared . sqlLock . acquire ( )
2012-11-19 20:45:05 +01:00
#Select all hashes which are younger than two days old and in this stream.
2013-04-12 21:31:45 +02:00
t = ( int ( time . time ( ) ) - maximumAgeOfObjectsThatIAdvertiseToOthers , int ( time . time ( ) ) - lengthOfTimeToHoldOnToAllPubkeys , self . streamNumber )
2013-05-02 17:53:54 +02:00
shared . sqlSubmitQueue . put ( ''' SELECT hash FROM inventory WHERE ((receivedtime>? and objecttype<> ' pubkey ' ) or (receivedtime>? and objecttype= ' pubkey ' )) and streamnumber=? ''' )
shared . sqlSubmitQueue . put ( t )
queryreturn = shared . sqlReturnQueue . get ( )
shared . sqlLock . release ( )
2012-11-19 20:45:05 +01:00
bigInvList = { }
for row in queryreturn :
hash , = row
2013-02-03 06:16:50 +01:00
if hash not in self . objectsOfWhichThisRemoteNodeIsAlreadyAware :
bigInvList [ hash ] = 0
else :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-02-03 06:16:50 +01:00
print ' Not including an object hash in a big inv message because the remote node is already aware of it. ' #This line is here to check that this feature is working.
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-01-22 06:21:32 +01:00
#We also have messages in our inventory in memory (which is a python dictionary). Let's fetch those too.
2013-05-02 17:53:54 +02:00
for hash , storedValue in shared . inventory . items ( ) :
2013-02-03 06:16:50 +01:00
if hash not in self . objectsOfWhichThisRemoteNodeIsAlreadyAware :
objectType , streamNumber , payload , receivedTime = storedValue
if streamNumber == self . streamNumber and receivedTime > int ( time . time ( ) ) - maximumAgeOfObjectsThatIAdvertiseToOthers :
bigInvList [ hash ] = 0
else :
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-02-03 06:16:50 +01:00
print ' Not including an object hash in a big inv message because the remote node is already aware of it. ' #This line is here to check that this feature is working.
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
numberOfObjectsInInvMessage = 0
payload = ' '
2013-01-22 06:21:32 +01:00
#Now let us start appending all of these hashes together. They will be sent out in a big inv message to our new peer.
2012-11-19 20:45:05 +01:00
for hash , storedValue in bigInvList . items ( ) :
payload + = hash
numberOfObjectsInInvMessage + = 1
if numberOfObjectsInInvMessage > = 50000 : #We can only send a max of 50000 items per inv message but we may have more objects to advertise. They must be split up into multiple inv messages.
2013-01-22 06:21:32 +01:00
self . sendinvMessageToJustThisOnePeer ( numberOfObjectsInInvMessage , payload )
2012-11-19 20:45:05 +01:00
payload = ' '
numberOfObjectsInInvMessage = 0
if numberOfObjectsInInvMessage > 0 :
2012-11-23 09:22:56 +01:00
self . sendinvMessageToJustThisOnePeer ( numberOfObjectsInInvMessage , payload )
2013-02-18 21:22:48 +01:00
2012-11-23 09:22:56 +01:00
#Self explanatory. Notice that there is also a broadcastinv function for broadcasting invs to everyone in our stream.
def sendinvMessageToJustThisOnePeer ( self , numberOfObjects , payload ) :
2012-11-19 20:45:05 +01:00
payload = encodeVarint ( numberOfObjects ) + payload
headerData = ' \xe9 \xbe \xb4 \xd9 ' #magic bits, slighly different from Bitcoin's magic bits.
2013-02-04 22:49:02 +01:00
headerData + = ' inv \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) )
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
2013-05-02 17:53:54 +02:00
shared . printLock . acquire ( )
2013-02-03 06:16:50 +01:00
print ' Sending huge inv message with ' , numberOfObjects , ' objects to just this one peer '
2013-05-02 17:53:54 +02:00
shared . printLock . release ( )
2013-05-03 18:24:47 +02:00
try :
self . sock . sendall ( headerData + payload )
except Exception , err :
#if not 'Bad file descriptor' in err:
shared . printLock . acquire ( )
sys . stderr . write ( ' sock.sendall error: %s \n ' % err )
shared . printLock . release ( )
2012-11-19 20:45:05 +01:00
#We have received a broadcast message
2013-03-28 22:56:20 +01:00
def recbroadcast ( self , data ) :
2013-02-06 22:17:49 +01:00
self . messageProcessingStartTime = time . time ( )
2012-11-19 20:45:05 +01:00
#First we must check to make sure the proof of work is sufficient.
2013-03-28 22:56:20 +01:00
if not self . isProofOfWorkSufficient ( data ) :
2012-11-23 09:22:56 +01:00
print ' Proof of work in broadcast message insufficient. '
2012-11-19 20:45:05 +01:00
return
2013-04-17 20:24:16 +02:00
readPosition = 8 #bypass the nonce
embeddedTime , = unpack ( ' >I ' , data [ readPosition : readPosition + 4 ] )
#This section is used for the transition from 32 bit time to 64 bit time in the protocol.
if embeddedTime == 0 :
embeddedTime , = unpack ( ' >Q ' , data [ readPosition : readPosition + 8 ] )
readPosition + = 8
else :
readPosition + = 4
2012-11-19 20:45:05 +01:00
if embeddedTime > ( int ( time . time ( ) ) + 10800 ) : #prevent funny business
print ' The embedded time in this broadcast message is more than three hours in the future. That doesn \' t make sense. Ignoring message. '
return
if embeddedTime < ( int ( time . time ( ) ) - maximumAgeOfAnObjectThatIAmWillingToAccept ) :
print ' The embedded time in this broadcast message is too old. Ignoring message. '
return
2013-04-02 18:23:34 +02:00
if len ( data ) < 180 :
2012-11-19 20:45:05 +01:00
print ' The payload length of this broadcast packet is unreasonably low. Someone is probably trying funny business. Ignoring message. '
return
2013-04-26 19:20:30 +02:00
#Let us check to make sure the stream number is correct (thus preventing an individual from sending broadcasts out on the wrong streams or all streams).
broadcastVersion , broadcastVersionLength = decodeVarint ( data [ readPosition : readPosition + 10 ] )
if broadcastVersion > = 2 :
2013-04-26 19:38:58 +02:00
streamNumber , streamNumberLength = decodeVarint ( data [ readPosition + broadcastVersionLength : readPosition + broadcastVersionLength + 10 ] )
2013-04-26 19:20:30 +02:00
if streamNumber != self . streamNumber :
print ' The stream number encoded in this broadcast message ( ' + str ( streamNumber ) + ' ) does not match the stream number on which it was received. Ignoring it. '
return
2013-05-02 17:53:54 +02:00
shared . inventoryLock . acquire ( )
2013-03-28 22:56:20 +01:00
self . inventoryHash = calculateInventoryHash ( data )
2013-05-02 17:53:54 +02:00
if self . inventoryHash in shared . inventory :
2012-11-19 20:45:05 +01:00
print ' We have already received this broadcast object. Ignoring. '
2013-05-02 17:53:54 +02:00
shared . inventoryLock . release ( )
2012-11-19 20:45:05 +01:00
return
2013-02-06 22:17:49 +01:00
elif isInSqlInventory ( self . inventoryHash ) :
2012-11-19 20:45:05 +01:00
print ' We have already received this broadcast object (it is stored on disk in the SQL inventory). Ignoring it. '
2013-05-02 17:53:54 +02:00
shared . inventoryLock . release ( )
2012-11-19 20:45:05 +01:00
return
#It is valid so far. Let's let our peers know about it.
objectType = ' broadcast '
2013-05-02 17:53:54 +02:00
shared . inventory [ self . inventoryHash ] = ( objectType , self . streamNumber , data , embeddedTime )
shared . inventoryLock . release ( )
2013-02-06 22:17:49 +01:00
self . broadcastinv ( self . inventoryHash )
2013-05-01 22:06:55 +02:00
#self.emit(SIGNAL("incrementNumberOfBroadcastsProcessed()"))
2013-05-02 17:53:54 +02:00
shared . UISignalQueue . put ( ( ' incrementNumberOfBroadcastsProcessed ' , ' no data ' ) )
2013-02-18 21:22:48 +01:00
2013-02-06 22:17:49 +01:00
2013-04-17 20:24:16 +02:00
self . processbroadcast ( readPosition , data ) #When this function returns, we will have either successfully processed this broadcast because we are interested in it, ignored it because we aren't interested in it, or found problem with the broadcast that warranted ignoring it.
2013-02-06 22:17:49 +01:00
2013-02-18 21:22:48 +01:00
# Let us now set lengthOfTimeWeShouldUseToProcessThisMessage. If we haven't used the specified amount of time, we shall sleep. These values are mostly the same values used for msg messages although broadcast messages are processed faster.
2013-04-02 18:23:34 +02:00
if len ( data ) > 100000000 : #Size is greater than 100 megabytes
2013-02-06 22:17:49 +01:00
lengthOfTimeWeShouldUseToProcessThisMessage = 100 #seconds.
2013-04-02 18:23:34 +02:00
elif len ( data ) > 10000000 : #Between 100 and 10 megabytes
2013-02-06 22:17:49 +01:00
lengthOfTimeWeShouldUseToProcessThisMessage = 20 #seconds.
2013-04-02 18:23:34 +02:00
elif len ( data ) > 1000000 : #Between 10 and 1 megabyte
2013-02-18 21:22:48 +01:00
lengthOfTimeWeShouldUseToProcessThisMessage = 3 #seconds.
2013-02-06 22:17:49 +01:00
else : #Less than 1 megabyte
2013-04-26 19:38:58 +02:00
lengthOfTimeWeShouldUseToProcessThisMessage = .6 #seconds.
2013-02-06 22:17:49 +01:00