def fetch(self, resource_id): done = False response = None last_t = time.time() self.subscribe(resource_id) #self.store[resource_id] = Luby.ChunkStore() if resource_id not in self.store: self.store[resource_id] = Luby.ChunkStore() while not done: if len(self.store[resource_id].solved.keys()) == self.Npackets: done = True continue result = select.select([self.sock], [], [], self.fail_timeout) if result[0]: data, addr = self.sock.recvfrom(1024 * 1024) response = Luby.decodeChunk(data) if response['resourceId'] in self.store: self.store[response['resourceId']].insert( response['chunk']) self.store[response['resourceId']].set_length( response['dataLen']) if time.time() - last_t > self.timeout: self.subscribe(resource_id) last_t = time.time() if len(self.store[resource_id].solved.keys()) == self.Npackets: done = True else: self.unsubscribe(resource_id) return None self.unsubscribe(resource_id) #L = response['dataLen'] return self.store[resource_id].summon()
def run(self): while not self.done: resources = self.streams.keys() for resid in resources: with self.locks[resid]: if len(self.streams[resid]) > 0: (chunk, length) = self.generators[resid].get() packet = Luby.encodeChunk(chunk, resid, length) to_be_removed = [] for client in self.streams[resid]: self.sock.sendto(packet, client) key = (client, resid) dt = time.time() - self.timeouts[key] if dt > self.T: to_be_removed.append(key) for client in to_be_removed: del self.timeouts[key] self.streams[client[1]].discard(client[0])
def fetch( self, resource_id ): done = False response = None last_t = time.time() self.subscribe( resource_id ) #self.store[resource_id] = Luby.ChunkStore() if resource_id not in self.store: self.store[resource_id] = Luby.ChunkStore() while not done: if len( self.store[resource_id].solved.keys() ) == self.Npackets: done = True continue result = select.select( [self.sock], [], [], self.fail_timeout ) if result[0]: data, addr = self.sock.recvfrom( 1024 * 1024 ) response = Luby.decodeChunk( data ) if response['resourceId'] in self.store: self.store[ response['resourceId'] ].insert( response['chunk'] ) self.store[ response['resourceId'] ].set_length( response['dataLen'] ) if time.time() - last_t > self.timeout: self.subscribe( resource_id ) last_t = time.time() if len( self.store[resource_id].solved.keys() ) == self.Npackets: done = True else: self.unsubscribe( resource_id ) return None self.unsubscribe( resource_id ) #L = response['dataLen'] return self.store[resource_id].summon()
def __init__(self, data, Npackets=64): self.length = len(data) self.Npackets = Npackets packets = Luby.split(data, Npackets) self.generator = Luby.ChunkGenerator(packets)