コード例 #1
0
/**
 * Create a DATA message. seqNum = sequence number of the packet
 */
Packet *Message::make_data_packet(int seqNum){
	Packet *data_packet;
	data_packet = new Packet();

	//Header
	PacketHdr *hdr = data_packet->accessHeader();
	hdr->setIntegerInfo(SENDER_ID, ID_POS);
	hdr->setShortIntegerInfo(DATA_MESSAGE_TYPE, TYPE_POS);
	hdr->setIntegerInfo(seqNum,SEQNUM_POS); //sequence number
	if(seqNum == number_of_packets_){
		hdr->setShortIntegerInfo(1,EOF_POS);
	}else{
		hdr->setShortIntegerInfo(0,EOF_POS);
	}

	//Payload
	int payload_size;
	if(seqNum == number_of_packets_){
		payload_size = length_ - (number_of_packets_ - 1) * DATA_SIZE;
	}
	else{
		payload_size = DATA_SIZE;
	}
	char *data_chunk = new char[payload_size];
	//read the file
	ifstream f1(filename_);
	f1.seekg(0, ios::end);
	int pos = (seqNum - 1) * DATA_SIZE;
	f1.seekg(pos);
	f1.read(data_chunk, payload_size);
	f1.close();
	data_packet->fillPayload(payload_size, data_chunk);
	return data_packet;
}
コード例 #2
0
Packet *Message::make_response_packet(int CID, int HID, const char *filename){
	Packet * response_packet;
	response_packet = new Packet();

	//get some info of the file
	//read file
	ifstream f1(filename);
	f1.seekg(0, ios::end);
	int len = (int)f1.tellg();
	f1.seekg(0, f1.beg);
	char *datachunk = new char[len];
	f1.read(datachunk, len);
	f1.close();

	//Header
	PacketHdr *hdr = response_packet->accessHeader();
	hdr->setOctet(TYPE_RESPONSE, POS_TYPE );
	hdr->setOctet(CID, POS_CID );
	hdr->setOctet(HID, POS_HID );
	hdr->setShortIntegerInfo(len, POS_SIZE);

	// fill payload by content...
	response_packet->fillPayload(len, datachunk);

	return response_packet;
}
コード例 #3
0
/**
 * Create an ACK message
 */
Packet *Message::make_ack_packet(int seqNum){
	Packet *ack_packet;
	ack_packet = new Packet();
	ack_packet->setPayloadSize(0);

	//Header
	PacketHdr *hdr = ack_packet->accessHeader();
	hdr->setIntegerInfo(RECEIVER_ID, ID_POS);
	hdr->setShortIntegerInfo(ACK_MESSAGE_TYPE, TYPE_POS);
	hdr->setIntegerInfo(seqNum, SEQNUM_POS); // sequence number being ACKED

	return ack_packet;
}
コード例 #4
0
/**
 * Create a new init message, with the name of the file being transfered
 */
Packet *Message::make_init_packet(char* filename){
	  Packet * init_packet;
	  init_packet = new Packet();
	  init_packet->setPayloadSize(INIT_SIZE);

	  //Header
	  PacketHdr *hdr = init_packet->accessHeader();
	  hdr->setIntegerInfo(SENDER_ID, ID_POS);
	  hdr->setShortIntegerInfo(INIT_MESSAGE_TYPE, TYPE_POS);
	  hdr->setIntegerInfo(0,SEQNUM_POS); //sequence number

	  //Payload
	  int len = strlen(filename);
	  init_packet->fillPayload(len, filename);
	  return init_packet;
}