def generateHandshake(infoHash, peerId): return shortIntToBinary(19)+'BitTorrent protocol'+\ (8*shortIntToBinary(0))+\ infoHash+peerId
def __init__(self, config, persister, progPath, version): self.config = config self.persister = persister self.progPath = progPath self.version = version #DEBUG #gc.set_debug(gc.DEBUG_UNCOLLECTABLE) #gc.set_debug(gc.DEBUG_LEAK) #gc.set_debug(gc.DEBUG_COLLECTABLE | gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_INSTANCES | gc.DEBUG_OBJECTS) #log self.log = logging.getLogger('MultiBt') #generate peerid versionDigits = self.version.split('.') self.peerId = '-PB' + shortIntToBinary(versionDigits[0]) + shortIntToBinary(versionDigits[1]) +\ shortIntToBinary(versionDigits[2]) + generateRandomBinary(14) #create samSocketManager samSessionOptions = {'inbound.nickname':self.config.getStr('i2p','samDisplayName'), 'inbound.quantity':self.config.getStr('i2p','samNumOfTunnelsIn'), 'inbound.backupQuantity':self.config.getStr('i2p','samNumOfBackupTunnelsIn'), 'inbound.length':self.config.getStr('i2p','samTunnelLengthIn'), 'inbound.lengthVariance':self.config.getStr('i2p','samTunnelLengthVarianceIn'), 'inbound.allowZeroHop':self.config.getStr('i2p','samZeroHopsIn'), 'outbound.quantity':self.config.getStr('i2p','samNumOfTunnelsOut'), 'outbound.backupQuantity':self.config.getStr('i2p','samNumOfBackupTunnelsOut'), 'outbound.length':self.config.getStr('i2p','samTunnelLengthOut'), 'outbound.lengthVariance':self.config.getStr('i2p','samTunnelLengthVarianceOut'), 'outbound.allowZeroHop':self.config.getStr('i2p','samZeroHopsOut')} self.samSockManager = I2PSocketManager(log='SamSocketManager', asmLog='AsyncSocketManager') self.destNum = self.samSockManager.addDestination(self.config.get('i2p','samIp'), self.config.get('i2p','samPort'), self.config.get('i2p','samSessionName'), 'tcp', 'both', samSessionOptions, defaultOutMaxQueueSize=5120) #create event scheduler self.eventSched = EventScheduler() #create traffic related classes self.inLimiter = SelfRefillingQuotaLimiter(self.eventSched, self.config.get('network','downSpeedLimit')) self.outLimiter = SelfRefillingQuotaLimiter(self.eventSched, self.config.get('network','upSpeedLimit')) self.inRate = Measure(self.eventSched, 60) self.outRate = Measure(self.eventSched, 60) #create connection related classes self.peerPool = PeerPool() self.connStatsCache = ConnectionStatsCache() self.connHandler = ConnectionHandler(self.config, self.connStatsCache, self.peerPool, self.samSockManager.select, self.eventSched,\ self.inLimiter, self.outLimiter, self.peerId) self.connListener = ConnectionListener(self.eventSched, self.connHandler, self.peerPool, self.destNum, self.samSockManager, self.peerId) self.connBuilder = ConnectionBuilder(self.eventSched, self.connHandler, self.peerPool, self.destNum, self.samSockManager, self.peerId) #create choker self.choker = Choker(self.config, self.eventSched, self.connHandler) #create own address watcher class self.ownAddrWatcher = OwnAddressWatcher(self.destNum, self.samSockManager) #create http requester class self.httpRequester = HttpRequester(self.eventSched, self.destNum, self.samSockManager) #add config callbacks callbackSamAddressOptions = {('i2p','samIp'):'ip', ('i2p','samPort'):'port'} callbackSamSessionOptions = {('i2p','samDisplayName'):'inbound.nickname', ('i2p','samNumOfTunnelsIn'):'inbound.quantity', ('i2p','samNumOfBackupTunnelsIn'):'inbound.backupQuantity', ('i2p','samTunnelLengthIn'):'inbound.length', ('i2p','samTunnelLengthVarianceIn'):'inbound.lengthVariance', ('i2p','samZeroHopsIn'):'inbound.allowZeroHop', ('i2p','samNumOfTunnelsOut'):'outbound.quantity', ('i2p','samNumOfBackupTunnelsOut'):'outbound.backupQuantity', ('i2p','samTunnelLengthOut'):'outbound.length', ('i2p','samTunnelLengthVarianceOut'):'outbound.lengthVariance', ('i2p','samZeroHopsOut'):'outbound.allowZeroHop'} self.config.addCallback(callbackSamAddressOptions.keys(), self.samSockManager.changeSessionAddress, funcArgs=[self.destNum], funcKw={'reconnect':True}, valueArgPlace=1, callType='item-funcKwSingle', optionTranslationTable=callbackSamAddressOptions, callWithAllOptions=True) self.config.addCallback((('i2p','samSessionName'),), self.samSockManager.changeSessionName, funcArgs=[self.destNum], funcKw={'reconnect':True}, valueArgPlace=1) self.config.addCallback(callbackSamSessionOptions.keys(), self.samSockManager.replaceSessionOptions, funcArgs=[self.destNum], funcKw={'reconnect':True}, valueArgPlace=1, callType='item-dictArg', optionTranslationTable=callbackSamSessionOptions, callWithAllOptions=True) self.config.addCallback((('network', 'downSpeedLimit'),), self.inLimiter.changeRate) self.config.addCallback((('network', 'upSpeedLimit'),), self.outLimiter.changeRate) #queue self.queue = BtQueueManager(self.choker, self.config, self.connBuilder, self.connListener, self.connHandler, self.eventSched, self.httpRequester, self.inRate, self.outRate, self.ownAddrWatcher, self.peerId, self.peerPool, self.persister, self.progPath, self.version) #lock self.lock = threading.Lock()
def generatePiece(index, begin, block): return _createMessageLength(9+len(block))+shortIntToBinary(7)+\ longIntToBinary(index)+\ longIntToBinary(begin)+\ block
def generateCancel(index, begin, length): return _createMessageLength(13)+shortIntToBinary(8)+\ longIntToBinary(index)+\ longIntToBinary(begin)+\ longIntToBinary(length)
def generateRequest(index, begin, length): return _createMessageLength(13)+shortIntToBinary(6)+\ longIntToBinary(index)+\ longIntToBinary(begin)+\ longIntToBinary(length)
def generateBitfield(bitfield): bitfield = binToBinary(bitfield) return _createMessageLength(1+len(bitfield))+shortIntToBinary(5)+bitfield
def generateHave(pieceIndex): return _createMessageLength(5)+shortIntToBinary(4)+longIntToBinary(pieceIndex)
def generateNotInterested(): return _createMessageLength(1)+shortIntToBinary(3)
def generateUnChoke(): return _createMessageLength(1)+shortIntToBinary(1)