def updateBlock(self, ret, encodedfragment, peer, bw, recTime, sendTime): s = self.getscheduler(ret.streamid) if s is None: self.breakCall() self.receivedFragments += 1 if ret.blockid not in self.schedulers[s]: self.schedulers[s][ret.blockid] = CBlock(ret.streamid, ret.blockid, ret.fragments) self.receiving[ret.blockid] = s self.schedulers[s][ret.blockid].sendTime = sendTime self.schedulers[s][ret.blockid].peer = peer self.schedulers[s][ret.blockid].receivedTime = recTime self.schedulers[s][ret.blockid].rate = bw self.schedulers[s][ret.blockid].times = [] self.schedulers[s][ret.blockid].times.append(recTime) self.schedulers[s][ret.blockid].lastReceived = recTime self.schedulers[s][ret.blockid].retr = False #print 'STT:',time.time()-recTime,bw,ret.fragments else: self.schedulers[s][ret.blockid].times.append(recTime) self.schedulers[s][ret.blockid].lastReceived = recTime if self.schedulers[s][ret.blockid].retr: print 'received retransmitted fragment' print ret.blockid print ret.fragmentid print recTime if not self.schedulers[s][ret.blockid].complete: complete, duplicate = self.schedulers[s][ret.blockid].receive( encodedfragment, ret, peer) if complete: dup = 1.0 * self.duplicateFragments / self.receivedFragments setValue(self, 'duplicates', dup * 1000) self.receiving.pop(ret.blockid) message = self.schedulers[s][ret.blockid].block self.checkRate(self.schedulers[s][ret.blockid]) d = deferToThread(scanBlocks, message, peer) d.addCallback(self.triggerActions, message, peer) if duplicate: self.duplicateFragments = +1 else: self.duplicateFragments = +1 print 'received duplicate fragment ', ret.blockid, ret.fragmentid self.breakCall()
def send(self, res, msg, data, peer): if not self.startTime: self.startTime = time.time() for r in res: pack = (r, peer) self.que.append(pack) self.asked = False if self.stuck: if self.lastIdleTime: self.idleTime += (time.time() - self.lastIdleTime) idle = self.idleTime / (time.time() - self.startTime) setValue(self, 'netIdle', 1000 * idle) self.lastIdleTime = 0 self.stuck = False #self.log.debug('queue size %d',len(self.que)) reactor.callLater(0, self.sendfromque) self.breakCall() return res
def shift(self, norequests=False): n = self.overlay.getNeighbours() outID, hit = self.buffer.shift() setLPB(self, self.buffer.lpb) if self.buffer.lpb - self.buffer.flpb > self.buffer.buffersize: if not hit: self.countMiss += 1 else: self.countHit += 1 hitRatio = self.countHit / float(self.countHit + self.countMiss) setValue(self, 'scheduler', hitRatio * 1000) #self.log.debug('hit ratio %f',hitRatio) if not norequests: #send buffers if self.buffer.lpb % self.reqInterval == 0: d = self.trafficPipe.call("getreceiving", self) d.addCallback(self.makeRequests, self.buffer.getFalseBIDList(), n) d.addCallback(self.sendRequests) d.addErrback(self.errback) else: #print 'sending buffer' bw = self.root.trafficPipe.callSimple('getReportedBw') BufferMessage.send(self.stream.id, bw, self.buffer, None, n, self.controlPipe) #self.log.debug('%s',self.buffer) try: idleRatio = self.idleTime / (time() - self.startTime) except: idleRatio = 0 #self.log.debug('idle:%f',idleRatio) setValue(self, 'idle', idleRatio * 1000) #print self.buffer #push block to output outdata = self.trafficPipe.call("popblockdata", self, outID) outdata.addCallback(self.output.write)
def shift(self): from time import time #print "SHIFT: ", time() #self.log.debug("SHIFT: %f",time()) chunk = self.input.read() lpb = self.buffer.lpb #print self.buffer if chunk is None: self.log.warning('empty chunk') self.stop() return "EOF" if len(chunk) > 0: self.buffer.update(lpb) d = self.trafficPipe.call("inputblock", self, lpb, chunk) destination = self.overlay.getNeighbours() if len(destination)>0: sent=[] for i in range(min(len(destination),2)): gp=[p for p in destination if '150.140.186' in p.getIP() and p not in sent] if gp: np=gp else: np=[p for p in destination if p not in sent] if np: peer=choice(np) self.log.debug('sending block to %s %d %d',peer,self.buffer.lpb,len(chunk)) d.addCallback(self.sendblock, lpb, peer) sent.append(peer) outID,hit = self.buffer.shift() setLPB(self, self.buffer.lpb) #self.log.debug('%s',self.buffer) outdata = self.trafficPipe.call("popblockdata", self, outID) outdata.addCallback(self.output.write) counter(self, "sent_block") setValue(self,'scheduler',1000)
def updateStats(self): en = self.getEnergy() setValue(self, "peerEnergy", en) setValue(self, 'peerNeigh', len(self.getNeighbours()))
def writeStat(self): setValue(self, 'controlover', self.countBytes * 8 / (2.0)) self.countBytes = 0