2013-06-22 00:29:04 +02:00
import time
import threading
import shared
2013-06-22 01:49:50 +02:00
import Queue
from struct import unpack , pack
2013-06-23 20:31:47 +02:00
import hashlib
2013-06-23 21:52:39 +02:00
import random
import sys
import socket
2013-06-22 01:49:50 +02:00
2013-09-07 00:55:12 +02:00
from class_objectHashHolder import *
from addresses import *
2013-06-22 00:29:04 +02:00
# Every connection to a peer has a sendDataThread (and also a
# receiveDataThread).
class sendDataThread ( threading . Thread ) :
def __init__ ( self ) :
threading . Thread . __init__ ( self )
self . mailbox = Queue . Queue ( )
shared . sendDataQueues . append ( self . mailbox )
2013-06-29 19:29:35 +02:00
with shared . printLock :
print ' The length of sendDataQueues at sendDataThread init is: ' , len ( shared . sendDataQueues )
2013-06-22 00:29:04 +02:00
self . data = ' '
2013-09-07 00:55:12 +02:00
self . objectHashHolderInstance = objectHashHolder ( self . mailbox )
self . objectHashHolderInstance . start ( )
2013-06-22 00:29:04 +02:00
def setup (
self ,
sock ,
HOST ,
PORT ,
streamNumber ,
2013-06-24 21:51:01 +02:00
someObjectsOfWhichThisRemoteNodeIsAlreadyAware ) :
2013-06-22 00:29:04 +02:00
self . sock = sock
2013-08-01 12:32:07 +02:00
self . peer = shared . Peer ( HOST , PORT )
2013-06-22 00:29:04 +02:00
self . streamNumber = streamNumber
self . remoteProtocolVersion = - \
1 # This must be set using setRemoteProtocolVersion command which is sent through the self.mailbox queue.
self . lastTimeISentData = int (
time . time ( ) ) # If this value increases beyond five minutes ago, we'll send a pong message to keep the connection alive.
2013-06-24 21:51:01 +02:00
self . someObjectsOfWhichThisRemoteNodeIsAlreadyAware = someObjectsOfWhichThisRemoteNodeIsAlreadyAware
2013-06-29 19:29:35 +02:00
with shared . printLock :
print ' The streamNumber of this sendDataThread (ID: ' , str ( id ( self ) ) + ' ) at setup() is ' , self . streamNumber
2013-06-22 00:29:04 +02:00
def sendVersionMessage ( self ) :
2013-06-24 21:51:01 +02:00
datatosend = shared . assembleVersionMessage (
2013-07-30 22:23:18 +02:00
self . peer . host , self . peer . port , self . streamNumber ) # the IP and port of the remote host, and my streamNumber.
2013-06-22 00:29:04 +02:00
2013-06-29 19:29:35 +02:00
with shared . printLock :
print ' Sending version packet: ' , repr ( datatosend )
2013-06-22 00:29:04 +02:00
try :
self . sock . sendall ( datatosend )
except Exception as err :
# if not 'Bad file descriptor' in err:
2013-06-29 19:29:35 +02:00
with shared . printLock :
sys . stderr . write ( ' sock.sendall error: %s \n ' % err )
2013-06-22 00:29:04 +02:00
self . versionSent = 1
def run ( self ) :
while True :
deststream , command , data = self . mailbox . get ( )
if deststream == self . streamNumber or deststream == 0 :
if command == ' shutdown ' :
2013-07-30 22:23:18 +02:00
if data == self . peer or data == ' all ' :
2013-06-29 19:29:35 +02:00
with shared . printLock :
2013-07-30 22:23:18 +02:00
print ' sendDataThread (associated with ' , self . peer , ' ) ID: ' , id ( self ) , ' shutting down now. '
2013-06-29 19:29:35 +02:00
2013-06-22 00:29:04 +02:00
try :
self . sock . shutdown ( socket . SHUT_RDWR )
self . sock . close ( )
except :
pass
shared . sendDataQueues . remove ( self . mailbox )
2013-06-29 19:29:35 +02:00
with shared . printLock :
print ' len of sendDataQueues ' , len ( shared . sendDataQueues )
2013-06-22 00:29:04 +02:00
break
# When you receive an incoming connection, a sendDataThread is
# created even though you don't yet know what stream number the
# remote peer is interested in. They will tell you in a version
# message and if you too are interested in that stream then you
# will continue on with the connection and will set the
# streamNumber of this send data thread here:
elif command == ' setStreamNumber ' :
2013-07-30 22:23:18 +02:00
peerInMessage , specifiedStreamNumber = data
if peerInMessage == self . peer :
2013-06-29 19:29:35 +02:00
with shared . printLock :
print ' setting the stream number in the sendData thread (ID: ' , id ( self ) , ' ) to ' , specifiedStreamNumber
2013-06-22 00:29:04 +02:00
self . streamNumber = specifiedStreamNumber
elif command == ' setRemoteProtocolVersion ' :
2013-07-30 22:23:18 +02:00
peerInMessage , specifiedRemoteProtocolVersion = data
if peerInMessage == self . peer :
2013-06-29 19:29:35 +02:00
with shared . printLock :
print ' setting the remote node \' s protocol version in the sendData thread (ID: ' , id ( self ) , ' ) to ' , specifiedRemoteProtocolVersion
2013-06-22 00:29:04 +02:00
self . remoteProtocolVersion = specifiedRemoteProtocolVersion
elif command == ' sendaddr ' :
try :
# To prevent some network analysis, 'leak' the data out
# to our peer after waiting a random amount of time
# unless we have a long list of messages in our queue
# to send.
time . sleep ( random . randrange ( 0 , 10 ) )
self . sock . sendall ( data )
self . lastTimeISentData = int ( time . time ( ) )
except :
2013-07-30 22:23:18 +02:00
print ' sendaddr: self.sock.sendall failed '
2013-06-22 00:29:04 +02:00
try :
self . sock . shutdown ( socket . SHUT_RDWR )
self . sock . close ( )
except :
pass
shared . sendDataQueues . remove ( self . mailbox )
2013-07-30 22:23:18 +02:00
print ' sendDataThread thread (ID: ' , str ( id ( self ) ) + ' ) ending now. Was connected to ' , self . peer
2013-06-22 00:29:04 +02:00
break
2013-09-07 00:55:12 +02:00
elif command == ' advertiseobject ' :
self . objectHashHolderInstance . holdHash ( data )
2013-06-22 00:29:04 +02:00
elif command == ' sendinv ' :
2013-09-07 00:55:12 +02:00
payload = ' '
for hash in data :
if hash not in self . someObjectsOfWhichThisRemoteNodeIsAlreadyAware :
payload + = hash
if payload != ' ' :
payload = encodeVarint ( len ( payload ) / 32 ) + payload
2013-06-22 00:29:04 +02:00
headerData = ' \xe9 \xbe \xb4 \xd9 ' # magic bits, slighly different from Bitcoin's magic bits.
headerData + = ' inv \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
headerData + = pack ( ' >L ' , len ( payload ) )
headerData + = hashlib . sha512 ( payload ) . digest ( ) [ : 4 ]
try :
self . sock . sendall ( headerData + payload )
self . lastTimeISentData = int ( time . time ( ) )
except :
2013-07-30 22:23:18 +02:00
print ' sendinv: self.sock.sendall failed '
2013-06-22 00:29:04 +02:00
try :
self . sock . shutdown ( socket . SHUT_RDWR )
self . sock . close ( )
except :
pass
shared . sendDataQueues . remove ( self . mailbox )
2013-07-30 22:23:18 +02:00
print ' sendDataThread thread (ID: ' , str ( id ( self ) ) + ' ) ending now. Was connected to ' , self . peer
2013-06-22 00:29:04 +02:00
break
elif command == ' pong ' :
2013-06-24 21:51:01 +02:00
self . someObjectsOfWhichThisRemoteNodeIsAlreadyAware . clear ( ) # To save memory, let us clear this data structure from time to time. As its function is to help us keep from sending inv messages to peers which sent us the same inv message mere seconds earlier, it will be fine to clear this data structure from time to time.
2013-06-22 00:29:04 +02:00
if self . lastTimeISentData < ( int ( time . time ( ) ) - 298 ) :
# Send out a pong message to keep the connection alive.
2013-06-29 19:29:35 +02:00
with shared . printLock :
2013-07-30 22:23:18 +02:00
print ' Sending pong to ' , self . peer , ' to keep connection alive. '
2013-06-29 19:29:35 +02:00
2013-06-22 00:29:04 +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 ' )
self . lastTimeISentData = int ( time . time ( ) )
except :
print ' send pong failed '
try :
self . sock . shutdown ( socket . SHUT_RDWR )
self . sock . close ( )
except :
pass
shared . sendDataQueues . remove ( self . mailbox )
2013-07-30 22:23:18 +02:00
print ' sendDataThread thread ' , self , ' ending now. Was connected to ' , self . peer
2013-06-22 00:29:04 +02:00
break
else :
2013-06-29 19:29:35 +02:00
with shared . printLock :
print ' sendDataThread ID: ' , id ( self ) , ' ignoring command ' , command , ' because the thread is not in stream ' , deststream
2013-09-07 00:55:12 +02:00
self . objectHashHolderInstance . close ( )