def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece # list of integers representing ids of pieces needed needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) requests = [] # We'll put all the things we want here # count frequency of each needed piece among available pieces from peers, # keeping track of owners of each needed piece piece_frequency = {piece: 0 for piece in needed_pieces} piece_ownerid = {piece: [] for piece in needed_pieces} for peer in peers: # get pieces that peer has and we need av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) # iterate through and update frequency and owner of piece for piece in isect: piece_frequency[piece] += 1 piece_ownerid[piece].append(peer.id) # get list of needed pieces as (piece_id, frequency) list and randomly shuffle # to make sure that not all agents request same pieces with same rarity at the same time # (since all start at rarity 2) piece_frequency_items = piece_frequency.items() random.shuffle(piece_frequency_items) # sort list by rarity (increasing frequency) piece_frequency_items = sorted(piece_frequency_items, key=lambda x: x[1]) # iterate through list and request each piece from each of its current owners if self.pieces: for (piece_id, _) in piece_frequency_items: start_block = self.pieces[piece_id] for owner in piece_ownerid[piece_id]: r = Request(self.id, owner, piece_id, start_block) requests.append(r) else: start_block = self.pieces[0] for owner in piece_ownerid[0]: if owner[:4] == 'seed': r = Request(self.id, owner, 0, start_block) requests.append(r) return requests
async def run(self) -> NoReturn: """Connects to the OBS websocket and endlessly parses MIDI to handle requests and responses.""" with mido.open_input(self.port) as port: async with websockets.connect("ws://localhost:4444") as websocket: readTask = asyncio.create_task(self.read(websocket)) while True: await asyncio.sleep(0.1) for msg in port.iter_pending(): self.parse(msg) for request in self.obs.requests: self.requests.append( Request(self._id, request, self.obs)) self.obs.requests.remove(request) for request in self.requests: await self.send(websocket, request.format()) self.requests.remove(request) for response in self.responses: response.handle() self.responses.remove(response) await readTask
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ # Find the pieces we need needed_pieces = list( set(filter(self.piece_needed, range(len(self.pieces))))) # Initialize an array to hold the request objects we are going to send requests = [] # Iterate through all peers to make requests for their pieces for peer in peers: # Start by finding all pieces we need and this peer also has # Also find the maximium amount of requests to send to this peer available_pieces = set(peer.available_pieces) needed_and_available = available_pieces.intersection(needed_pieces) max_requests = min(self.max_requests, len(needed_and_available)) # Iterate through a random sample of the pieces we identified to break symmetry for piece_id in random.sample(needed_and_available, max_requests): start_block = self.pieces[piece_id] request = Request(self.id, peer.id, piece_id, start_block) requests.append(request) return requests
def listen_and_respond(self): format_width = 55 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.settimeout(None) try: s.bind((self.HOST, self.PORT)) except socket.error as msg: print(str(msg) + ". Bind failed.") quit(-1) print("Listening...\n") s.listen(5) while not self.close_request: # Accepts the connection conn, addr = s.accept() _, time = self.get_datetime() print('+-' + '-' * format_width + '-+') # print('| {0:^{1}} |'.format('Connected by ' + str(addr) + ' on ' + time, format_width)) print('| {0:^{1}} |'.format( 'Connection from ' + str(addr[0]) + ":" + str(addr[1]) + ' at ' + time, format_width)) try: start_time = t.time() # Reads the request data = conn.recv(4096) request = literal_eval(data) request = Request(request['command'], request['parameters']) print('+-' + '-' * format_width + '-+') print('| {0:^{1}} |'.format(request, format_width)) camera_op = "CAMERA" in request.command # Performs an operation response = self.route_request( request) # Routing function, returns a Response # Sends back the data data = str(response.__dict__) #print(data) if camera_op: conn.sendall(data) else: conn.send(data) # conn.shutdown(socket.SHUT_WR) elapsed = t.time() - start_time #print('| {0:^{1}} |'.format(response, format_width)) print('| {0:^{1}} |'.format( "Time elapsed: " + str(round(elapsed, 2)) + "s", format_width)) print('+-' + '-' * format_width + '-+') # Closes the connection conn.close() except KeyboardInterrupt: break # Terminate the server process s.shutdown(socket.SHUT_RDWR) s.close() print('\nTerminated')
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) logging.debug("%s here: still need pieces %s" % ( self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug("look at the AgentHistory class in history.py for details") logging.debug(str(history)) requests = [] # COMPUTE RARITY - number of instances of a given piece counts = pd.DataFrame(data=np.zeros(len(self.pieces))) for peer in peers: for piece_index in set(peer.available_pieces): counts.iloc[piece_index, 0] = counts.iloc[piece_index, 0] + 1 # RAREST FIRST - request the pieces held by the fewest people # shuffle counts to break symmetry, then sort counts = counts.sample(frac=1) sorted_counts = counts.sort_values(0) # filter to only request elements we need filtered_sorted_counts = sorted_counts[sorted_counts.index.isin(np_set)] max_pieces_to_download = min(self.max_requests, filtered_sorted_counts.size) # iterate peers, requesting pieces in order of rarity for peer in peers: pieces_downloaded_from_peer = 0 for piece_id in filtered_sorted_counts.index.values: if piece_id in peer.available_pieces: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) # check that we do not exceed maximum possible downloads pieces_downloaded_from_peer = pieces_downloaded_from_peer + 1 if pieces_downloaded_from_peer == max_pieces_to_download: break return requests
def format_request(self, piece_num, block_byte_offset): block_num_in_piece = block_byte_offset / constants.REQUEST_LENGTH piece = self.file_downloading.piece_list[piece_num] request_len = piece.block_list[block_num_in_piece].expected_length index_pack = pack('!l',piece_num) begin_pack = pack('!l', block_byte_offset) length_pack = pack('!l',request_len) #print 'generating request for piece: ' + str(piece_num)+' and block: ' + str(block_byte_offset / constants.REQUEST_LENGTH) request = Request(index=index_pack, begin=begin_pack, length=length_pack) return request
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed_pieces = set(i for i in xrange(len(self.pieces)) if self.pieces[i] < self.conf.blocks_per_piece) logging.debug("Needed pieces: %s" % needed_pieces) logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug( "look at the AgentHistory class in history.py for details") logging.debug(str(history)) pieces_frequency = Counter( itertools.chain(*(p.available_pieces for p in peers))) requests = [] # We'll put all the things we want here # request all available pieces from all peers! # (up to self.max_requests from each) for peer in peers: available_pieces = set(peer.available_pieces) downloadable_pieces = list( available_pieces.intersection(needed_pieces)) # Do a weighted shuffle so that rare pieces are more likely to be at the front downloadable_pieces.sort( key=lambda x: random.random() * pieces_frequency[x]) # More symmetry breaking -- ask for random pieces. # This would be the place to try fancier piece-requesting strategies # to avoid getting the same thing from multiple peers at a time. for piece_id in downloadable_pieces: # aha! The peer has this piece! Request it. # which part of the piece do we need next? # (must get the next-needed blocks in order) start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) logging.debug("Requests: %s" % requests) return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug( "look at the AgentHistory class in history.py for details") logging.debug(str(history)) requests = [] # We'll put all the things we want here # Symmetry breaking is good... random.shuffle(needed_pieces) # Sort peers by id. This is probably not a useful sort, but other # sorts might be useful peers.sort(key=lambda p: p.id) # request all available pieces from all peers! # (up to self.max_requests from each) for peer in peers: # determine what the number of pieces the av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) # More symmetry breaking -- ask for random pieces. # This would be the place to try fancier piece-requesting strategies # to avoid getting the same thing from multiple peers at a time. for piece_id in random.sample(isect, n): # aha! The peer has this piece! Request it. # which part of the piece do we need next? # (must get the next-needed blocks in order) start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ # get pieces needed needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) random.shuffle(needed_pieces) np_set = set(needed_pieces) # map pieces to rarity pieces_available = [] for p in peers: pieces_available += p.available_pieces pieces_available_count = [(piece, pieces_available.count(piece)) for piece in set(pieces_available)] # make max number of requests to each peer ordered by preference requests = [] num_piece_requests = {piece: 0 for piece in set(pieces_available)} random.shuffle(peers) for peer in peers: # use different randomization for each peer random.shuffle(pieces_available_count) pieces_available_count.sort( key=lambda piece: piece[1] + self.state[ "request_count_factor"] * num_piece_requests[piece[0]]) # preference is rarity + need piece_preference_order = list( filter(lambda piece: piece[0] in np_set, pieces_available_count)) num_requests = 0 for piece, _ in piece_preference_order: if piece in peer.available_pieces and num_requests < self.max_requests: num_requests += 1 num_piece_requests[piece] += 1 start_block = self.pieces[piece] r = Request(self.id, peer.id, piece, start_block) requests.append(r) return requests
def run (self): while not self.stopped(): # Wait for message msg = self.wsconnection.receive_message() # Check if client cliosing connection if msg.type == CLOSE_MESSAGE: self.stop() self.server.remove_client(self) return # Hendle message request = Request() resp = None if not request.decode(msg.data): resp = Response (400) else: if request.path in self.server.paths: data = self.server.paths[request.path](request.data) resp = Response (200, data) else: resp = Response (404) sresp = resp.encode() self.wsconnection.send_message(sresp)
def requests(self, peers, history): """ peers: List of PeerInfo objects. history: AgentHistory object. returns: List of Request objects. requests be called after update_pieces """ sent_requests = [] needed_pieces = self.needed_pieces_list() random.shuffle(needed_pieces) random.shuffle(peers) # Finding which peers have the pieces we need # [(piece_id, [holder_id_list])] pieces_by_holder_id_list = [] for piece_id in needed_pieces: holder_peer_id_list = [] for peer in peers: if piece_id in peer.available_pieces: holder_peer_id_list.append(peer.id) pieces_by_holder_id_list.append((piece_id, holder_peer_id_list)) # Sort pieces by rarity # Tie breaking the sorting by prioritizing pieces that we're close to completing. # This is important to that we can start sharing them as soon as possible. pieces_by_rarity_list = sorted( pieces_by_holder_id_list, key=lambda (piece_id, holders): (len(holders), self.conf.blocks_per_piece - self.pieces[piece_id])) # Keep track of sent requests to not reach the max sent_requests_per_peer = {peer.id: 0 for peer in peers} # Requesting the rarest piece first for piece_id, holder_id_list in pieces_by_rarity_list: for holder_id in holder_id_list: # Don't make more requests than the maximum number of requests if sent_requests_per_peer[holder_id] < self.max_requests: first_block = self.pieces[piece_id] request = Request(self.id, holder_id, piece_id, first_block) sent_requests.append(request) sent_requests_per_peer[holder_id] += 1 return sent_requests
def requests(self, peers, history): """ ** must ask for pieces the peer has ** peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug( "look at the AgentHistory class in history.py for details") logging.debug(str(history)) requests = [] # We'll put all the things we want here # Symmetry breaking is good... random.shuffle(needed_pieces) # get all peices and corresponding counts (viable_pieces_dict, piece_holder_dict) = self.get_piece_presences(peers, np_set) num_of_requests = min(self.max_requests, len(viable_pieces_dict.keys())) while (len(requests) < num_of_requests): rarest_piece_id = min(viable_pieces_dict, key=viable_pieces_dict.get) viable_pieces_dict.pop(rarest_piece_id, None) request = Request( self.id, random.choice(piece_holder_dict[rarest_piece_id]), rarest_piece_id, self.pieces[rarest_piece_id]) requests.append(request) return requests
def api_request(self, method, path, params=[], data=""): try: self.connection.send_message( Request(method, path, params, data).encode()) sresp = self.connection.receive_message() # Check if client cliosing connection if sresp.type == CLOSE_MESSAGE: if self.onclose_callback != None: self.onclose_callback() return None resp = Response() resp.decode(sresp.data) return resp except socket.error: if self.onclose_callback != None: self.onclose_callback() return None
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ # Find all the pieces we ned def needed(i): return self.pieces[i] < self.conf.blocks_per_piece needed_pieces = set(filter(needed, range(len(self.pieces)))) # Create a datastructure for tallying up what pieces are the most rare piece_counter = Counter() for peer in peers: av = set(peer.available_pieces) av_needed = av.intersection(needed_pieces) piece_counter.update(av_needed) # List to keep all requests we want to send out requests = [] # Go through each peer and ask for the n most rare pieces they have and we want for peer in peers: available_piece_set = set(peer.available_pieces) # We can only ask for pieces we (1) need and (2) the peer actually has needed_and_available = available_piece_set.intersection( needed_pieces) # Check how many pieces we can maximally request num_pieces = min(self.max_requests, len(needed_and_available)) # Sort the pieces based on how many peers actually have the piece, rarest first rarest_first = sorted( list(needed_and_available), lambda p1, p2: piece_counter[p1] - piece_counter[p2]) for piece_id in rarest_first[:num_pieces]: start_block = self.pieces[piece_id] request = Request(self.id, peer.id, piece_id, start_block) requests.append(request) return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. # We'll put all the things we want here requests = [] # Create a datastructure for tallying up what pieces are the most rare piece_counter = Counter() for peer in peers: av = set(peer.available_pieces) av_needed = av.intersection(np_set) piece_counter.update(av_needed) # Go through each peer and ask for the most rare pieces they have and we want for peer in peers: available_piece_set = set(peer.available_pieces) isect = available_piece_set.intersection(np_set) n = min(self.max_requests, len(isect)) lisect = list(isect) lisect = sorted( lisect, lambda p1, p2: piece_counter[p1] - piece_counter[p2]) rarest = [lisect.pop(0)] if len(lisect) else [] random.shuffle(lisect) pieces = rarest + lisect[:n - 1] for piece_id in pieces: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) return requests
def requests(self, peers, history): needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) requests = [] #counts how many of each piece is available piece_count = {x: 0 for x in needed_pieces} for peer in peers: for av in peer.available_pieces: if av in piece_count: piece_count[av] += 1 peer_remain = { x.id: min(self.max_requests, len(x.available_pieces)) for x in peers } #how many pieces left we can request from each peer while sum([peer_remain[x] for x in peer_remain ]) > 0: #while we can still request something random.shuffle(peers) for peer in peers: if peer_remain[ peer.id] <= 0: #don't bother with maxed out peers continue av = [x for x in peer.available_pieces if x in piece_count] av.sort(key=lambda x: (piece_count[x], random.random()) ) #sort by rarity, random for breaking ties if len(av) == 0: #this peer has nothing of interest to me peer_remain[peer.id] = 0 continue piece_id = av[0] start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) peer_remain[peer.id] -= 1 piece_count[piece_id] = float( 'inf' ) #I dont want to ask for the same piece from different people in the same round return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. requests = [] # We'll put all the things we want here # Symmetry breaking is good... random.shuffle(needed_pieces) # id sorting useless, we shuffle insteas for symmetry breaking random.shuffle(peers) # let's now find the rarest pieces # this is a list rarest_first_needed_pieces = count_pieces(needed_pieces, peers) # sanity check if rarest_first_needed_pieces == []: return requests # no needed pieces available # loop over all our pieces, rarest first for piece_count, piece_id_lst in rarest_first_needed_pieces: # let's shuffle so we don't always pick the same pieces (we had # problems before were all peers always wanted the same pieces) random.shuffle(piece_id_lst) # loop over all out pieces and request for piece_id in piece_id_lst: # request all available pieces from all peers! # (up to self.max_requests from each) for peer in peers: av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) # if n >= 0, we still have requests we can make if n > 0: if piece_id in av_set: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) n -= 1 # one more request done # done!! return requests
def requests(self, peers, history): needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) requests = [] piece_count = {x: 0 for x in needed_pieces} for peer in peers: for av in peer.available_pieces: if av in piece_count: piece_count[av] += 1 peer_remain = { x.id: min(self.max_requests, len(x.available_pieces)) for x in peers } #how many pieces left we can request from each peer while sum([peer_remain[x] for x in peer_remain]) > 0: random.shuffle(peers) # print(peer_remain) for peer in peers: if peer_remain[peer.id] <= 0: continue av = [[piece_count[x], random.random(), x] for x in peer.available_pieces if x in piece_count] #random for breaking ties av.sort() if len(av) == 0: peer_remain[peer.id] = 0 continue piece_id = av[0][-1] start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) peer_remain[peer.id] -= 1 piece_count[piece_id] = float('inf') return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. requests = [] # We'll put all the things we want here av_dict = {} for i in np_set: av_dict[i] = 0 for peer in peers: if (i in peer.available_pieces): av_dict[i] += 1 for peer in peers: av_dict_tmp = av_dict.copy() av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) """ if piece_id in av_set:""" while n > 0: piece_id = min(av_dict_tmp, key=av_dict.get) av_dict_tmp.pop(piece_id) if piece_id in av_set: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) n -= 1 if len(av_set) == 0: break return requests
def requests(self, peers, history): # make set of pieces I need needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. requests = [] # We'll put all the things we want here random.shuffle(needed_pieces) # break symmetry peers.sort(key=lambda p: p.id) # just some sorting to break symmetry # pieceCounts[pieceNum] -> countOfPieces # initialize countOfPieces to 0 pieceCounts = dict( zip(range(len(self.pieces)), [0] * (len(self.pieces)))) # count appearances of each piece in network for peer in peers: for pieceNum in list(peer.available_pieces): pieceCounts[pieceNum] += 1 # sort by rarest first pieceCountsSorted = sorted(pieceCounts, key=pieceCounts.get) for peer in peers: av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) # filter sorted dictionary to keep only if in isect isectFiltered = [k for k in pieceCountsSorted if k in list(isect)] # ask for first n pieces in isectFiltered for piece_id in isectFiltered[0:n]: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) return requests
def requests(self, peers, history): # make set of pieces I need needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. requests = [] # We'll put all the things we want here random.shuffle(needed_pieces) # break symmetry peers.sort(key=lambda p: p.id) # just some sorting # dictionary of zero counts pieceCounts = dict(zip(range(len(self.pieces)), [0 for _ in range(len(self.pieces))])) # list of how common pieces are for peer in peers: for pieceNum in list(peer.available_pieces): pieceCounts[pieceNum] = pieceCounts[pieceNum] + 1 # sort by rarest first pieceCountsSorted = sorted(pieceCounts, key=pieceCounts.get) for peer in peers: av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) # filter dictionary to keep only if in isect isectFiltered = [k for k in pieceCountsSorted if k in list(isect)] # print "success!" # ask for first n pieces in isectFiltered m = min(len(isectFiltered),n) for piece_id in isectFiltered[0:m]: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) return requests
def requests(self, peers, history): needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. requests = [] # We'll put all the things we want here av_dict = {} # Count the number of each available piece. for i in np_set: av_dict[i] = 0 for peer in peers: if (i in peer.available_pieces): av_dict[i] += 1 # print "needed pieces with availibility" + str(av_dict) # For each peer, find n pieces to request, in order of rearest. for peer in peers: av_dict_tmp = av_dict.copy() av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) while n > 0: # Find the rearest piece. piece_id = min(av_dict_tmp, key=av_dict.get) av_dict_tmp.pop(piece_id) if piece_id in av_set: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) n -= 1 av_set.remove(piece_id) if len(av_set) == 0: break return requests
def __init__(self, path: str, port: str, debug: bool) -> None: """Initializes websocket handler with config from the path.""" self.config: dict = getConfig(path) if debug == True: print(f"Config: {self.config}") self.port: str = "" for option in mido.get_input_names(): if option.startswith(port): self.port = option break if debug == True: print(f"Port: {self.port}") self.debug: bool = debug self._id: Generator[str, None, None] = Id() self.obs: OBS = OBS() self.requests: list[Request] = [] #type: ignore self.responses: list[Response] = [] #type: ignore self.requests.append( Request(self._id, {"type": "GetSceneList"}, self.obs))
def parse(self, msg: mido.Message) -> None: """Parses MIDI message and creates requests based off of the loaded configuration.""" if self.debug: print(msg) trigger: str = msg.type value: int = -1 data: int = -1 if trigger == "note_on" or trigger == "note_off": value = msg.note data = msg.velocity elif trigger == "control_change": value = msg.control data = msg.value else: return for command in self.config[trigger][value]: request = command.copy() request["data"] = data self.requests.append(Request(self._id, request, self.obs))
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. # count number of peers that have each needed piece piecedict = {} for piece in needed_pieces: numhaving = 0 for peer in peers: if piece in peer.available_pieces: numhaving += 1 piecedict[piece] = numhaving logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug( "look at the AgentHistory class in history.py for details") logging.debug(str(history)) requests = [] # We'll put all the things we want here # Symmetry breaking is good... random.shuffle(needed_pieces) # Sort peers by id. This is probably not a useful sort, but other # sorts might be useful random.shuffle(peers) round = history.current_round() if round == 0: self.initialize_beliefs(peers) # request all available pieces from all peers! # (up to self.max_requests from each) for peer in peers: av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) if isect == set(): continue n = min(self.max_requests, len(isect)) # More symmetry breaking -- ask for random pieces. # This would be the place to try fancier piece-requesting strategies # to avoid getting the same thing from multiple peers at a time. # in first 5 rounds, request pieces randomly # after that, look for rarest pieces first # sort by which pieces are rarer ilist = sorted(isect, key=lambda x: piecedict[x]) if round >= 5: for piece_id in ilist[:n]: #rarest first # aha! The peer has this piece! Request it. # which part of the piece do we need next? # (must get the next-needed blocks in order) start_block = self.pieces[piece_id] req = Request(self.id, peer.id, piece_id, start_block) requests.append(req) else: for piece_id in random.sample(isect, n): # aha! The peer has this piece! Request it. # which part of the piece do we need next? # (must get the next-needed blocks in order) start_block = self.pieces[piece_id] req = Request(self.id, peer.id, piece_id, start_block) requests.append(req) return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. # count number of each piece among peers piece_count = {} for p in peers: for piece in p.available_pieces: if piece in piece_count.keys(): piece_count[piece] = piece_count[piece] + 1 else: piece_count[piece] = 1 logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug( "look at the AgentHistory class in history.py for details") logging.debug(str(history)) requests = [] # We'll put all the things we want here # Symmetry breaking is good... random.shuffle(needed_pieces) # Sort peers by id. This is probably not a useful sort, but other # sorts might be useful peers.sort(key=lambda p: p.id) random.shuffle(peers) # request all available pieces from all peers! # (up to self.max_requests from each) for peer in peers: av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) ###n = min(self.max_requests, len(isect)) n = len(isect) available = list(isect) random.shuffle(available) available.sort(key=lambda p: piece_count[p]) available2 = [] inside = [] i = 0 if n > 0: v = piece_count[available[0]] while i < len(available): inside = [] v = piece_count[available[i]] while i < len(available) and v == piece_count[available[i]]: inside.append(available[i]) i += 1 random.shuffle(inside) available2.append(inside) for s in available2: for piece_id in s: start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug( "look at the AgentHistory class in history.py for details") logging.debug(str(history)) # combine all pieces, and flatten the list # a simple assumption: when we say 'rarest pieces' # we mean only those among everyone else! # (this is a fine assumption, so we won't request # own pieces anyway) all_available_pieces = list( itertools.chain(*[p.available_pieces for p in peers])) # now order by frequency pieces_by_frequency = Counter(all_available_pieces).most_common() logging.debug("Here are the most frequent pieces: " + str(pieces_by_frequency)) requests = [] # We'll put all the things we want here # Symmetry breaking is good... random.shuffle(needed_pieces) # Sort peers by id. This is probably not a useful sort, but other # sorts might be useful peers.sort(key=lambda p: p.id) # request all available pieces from all peers! # (up to self.max_requests from each) for peer in peers: av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) # **Implement rarest pieces first** # A simple assumption we can make is to ask for as many # as possible from each peer, as in the dummy, but # ordering by rarest-first instead of randomly num_requested = 0 curr_rare_piece = 1 while num_requested < n and curr_rare_piece <= len( pieces_by_frequency): rare_id = pieces_by_frequency[-1 * curr_rare_piece][0] if rare_id in isect: num_requested += 1 # aha! The peer has this piece! Request it. # which part of the piece do we need next? # (must get the next-needed blocks in order) start_block = self.pieces[rare_id] r = Request(self.id, peer.id, rare_id, start_block) requests.append(r) curr_rare_piece += 1 return requests
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ #Calculate the pieces you still need needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = list(filter(needed, list(range(len(self.pieces))))) np_set = set(needed_pieces) # sets support fast intersection ops. logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) #This code shows you what you have access to in peers and history #You won't need it in your final solution, but may want to uncomment it #and see what it does to help you get started """ logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug("look at the AgentHistory class in history.py for details") logging.debug(str(history)) """ requests = [] # We'll put all the things we want here # Symmetry breaking is good... random.shuffle(needed_pieces) # count frequencies of all pieces that the other peers have # this will be useful for implementing rarest first ########################################################### # you'll need to write the code to compute these yourself # ########################################################### frequencies = {} # Python syntax to perform a sort using a user defined sort key # This exact sort is probably not a useful sort, but other sorts might be useful # peers.sort(key=lambda p: p.id) # request all available pieces from all peers! # (up to self.max_requests from each) ############################################################################# # This code asks for pieces at random, you need to adapt it to rarest first # ############################################################################# for peer in peers: av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) n = min(self.max_requests, len(isect)) # More symmetry breaking -- ask for random pieces. # You could try fancier piece-requesting strategies # to avoid getting the same thing from multiple peers at a time. for piece_id in random.sample(isect, int(n)): # aha! The peer has this piece! Request it. # which part of the piece do we need next? # (must get the next-needed blocks in order) # # If you loop over the piece_ids you want to request above # you don't need to change the rest of this code start_block = self.pieces[piece_id] r = Request(self.id, peer.id, piece_id, start_block) requests.append(r) return requests
break else: data_in = self.socket.recv(4096) self.connection_close() # Close the connection response = data_in except EOFError: response = Response(False, "Reception error") return response def connection_close(self): self.socket.shutdown(socket.SHUT_WR) self.socket.close() if __name__ == "__main__": client = Client() response = client.send_image_request(Request("CAMERA", None)) print(response) img_value = literal_eval(response)['values'] print(type(img_value)) buff = base64.b64decode(img_value) print(type(buff)) buff_arr = np.fromstring(buff, dtype=np.uint8) print(type(buff_arr)) img = cv.imdecode(buff_arr, cv.IMREAD_UNCHANGED) print(type(img)) cv.imshow("", img) cv.waitKey(0) client.connection_close()
def requests(self, peers, history): """ peers: available info about the peers (who has what pieces) history: what's happened so far as far as this peer can see returns: a list of Request() objects This will be called after update_pieces() with the most recent state. """ needed = lambda i: self.pieces[i] < self.conf.blocks_per_piece needed_pieces = filter(needed, range(len(self.pieces))) np_set = set(needed_pieces) # sets support fast intersection ops. logging.debug("%s here: still need pieces %s" % (self.id, needed_pieces)) logging.debug("%s still here. Here are some peers:" % self.id) for p in peers: logging.debug("id: %s, available pieces: %s" % (p.id, p.available_pieces)) logging.debug("And look, I have my entire history available too:") logging.debug( "look at the AgentHistory class in history.py for details") logging.debug(str(history)) requests = [] # Symmetry breaking is good... random.shuffle(needed_pieces) # Sort peers by id. This is probably not a useful sort, but other # sorts might be useful peers.sort(key=lambda p: p.id) piece_directory = {} piece_to_peer = {} num_requests = {} for peer in peers: num_requests[peer.id] = 0 av_set = set(peer.available_pieces) isect = av_set.intersection(np_set) for piece_id in list(isect): if piece_id in piece_directory: piece_to_peer[piece_id].append(peer) piece_directory[piece_id] += 1 else: piece_to_peer[piece_id] = [peer] piece_directory[piece_id] = 1 while piece_directory: most_rare_value = min(piece_directory.values()) rare_pieces = [ k for k in piece_directory if piece_directory[k] == most_rare_value ] rand_piece = random.choice(rare_pieces) rarest_list = piece_to_peer[rand_piece] while rarest_list != []: peer = random.choice(rarest_list) if num_requests[peer.id] < self.max_requests: start_block = self.pieces[rand_piece] r = Request(self.id, peer.id, rand_piece, start_block) requests.append(r) num_requests[peer.id] += 1 rarest_list.remove(peer) piece_directory.pop(rand_piece) return requests