def _make_request(self, context="", correlation_id="", reply_to="", source=""): if not source: source = self._source if not context: context = self._context if not correlation_id: correlation_id = self._correlation_id if not reply_to and not isinstance(reply_to, entry_point.EntryPoint): reply_to = source.service payload = self._kwargs dst = entry_point.Destination(self._service_name, self._method_name) headers = { "correlation_id": correlation_id, "reply_to": str(reply_to), "source": str(source), "destination": str(dst) } request_headers = self._headers.copy() request_headers.update(headers) request = messages.Request(request_headers, context, payload) return request
def send_piece_request(self, piece_index, block, begin=0): """Formats piece request message""" msg = messages.Request() send = msg.assemble(self.torrent, piece_index, begin, block) self.send_next_message(send) # if begin == 0: # print "Sending a piece request for index %r to peer %r..." %(piece_index, self.id) self.requesting = True
def send_request(self): try: # print("\033[92mChoking: {}, Interested: {}\033[0m".format(self.state['peer_choking'], self.state['am_interested'])) # print("\033[93mSending Request Message [{}, {}] to Peer IP: {}, Port: {}\033[0m".format(self.currentPiece.index, block_offset, self.ip, self.port)) final_request = b'' # print("{} , {}".format(self.am_interested(), self.is_unchoked())) if self.am_interested() and self.is_unchoked( ) and self.changeInPipeSinceLastReq: self.changeStatus( "\033[93mAdding {} reqs to request packet\033[0m".format( len(self.request_pipeline))) for req in self.request_pipeline: # self.changeStatus("\033[93mAdding piece [{}, {}] to request packet\033[0m".format(req[0], req[1])) if (req[2]): # Block being requested is the last block request = messages.Request( req[0], req[1] * self.currentPieces[0].block_size, req[3]).encode() else: # Block being requested is not the last block request = messages.Request( req[0], req[1] * self.currentPieces[0].block_size, req[3]).encode() final_request += request # self.changeStatus("\033[93mSending Request Message [{}] to Peer IP: {}, Port: {}\033[0m".format(self.currentPiece.index, self.ip, self.port)) self.send_msg(final_request) self.changeInPipeSinceLastReq = 0 self.changeStatus("\033[92mRequest Sent!\033[0m") except Exception as e: self.isGoodPeer = 0 self.readyToBeChecked = 1 print("\033[91m{}\033[0m".format(e)) print("\033[91m{} [IP: {}, Port: {}]\033[0m".format( e, self.ip, self.port))
def parse_message_from_response(self): length = messages.bytes_to_number( self.message_buffer[0:4] ) + 4 #length indicated by the first 4 bytes + 4 for those first 4 bytes bytestring = self.message_buffer[:length] if self.message_buffer[0:4] == '\x00\x00\x00\x00': #no msg_id message_obj = KeepAlive(response=bytestring) else: message_obj = { 0: lambda: messages.Choke(response=bytestring), 1: lambda: messages.Unchoke(response=bytestring), 2: lambda: messages.Interested(response=bytestring), 3: lambda: messaged.Interested(response=bytestring), 4: lambda: messages.Have(response=bytestring), 5: lambda: messages.Bitfield(response=bytestring), 6: lambda: messages.Request(response=bytestring), 7: lambda: messages.Piece(response=bytestring), 8: lambda: messages.Cancel(response=bytestring), 9: lambda: messages.Port(response=bytestring), }[self.message_buffer[4]]() #response[4] is the msg_id self.message_buffer = self.message_buffer[length:] return message_obj
def send_piece_request(self, piece_index, block, begin=0): msg = messages.Request() send = msg.assemble(self.torrent, piece_index, begin, block) self.send_next_message(send) self.requesting = True