示例#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
/**
 * 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;
}
示例#3
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;
}
void *interface4(void *arg){
	printf("Starting interface4 thread\n");
	struct cShared *sh = (struct cShared *)arg;
	cout<<"This is interface4!"<<endl;

	Packet *recvPacket;
	PacketHdr *hdr;

	char type;
	int cid;
	int hid;
	int hopnum;
	int interface;

	while(1){
		// sleep(0.4);
		recvPacket = sh->recv_port4->receivePacket();
		if(recvPacket!=NULL){
            
		hdr = recvPacket->accessHeader();
		type = hdr->getOctet(0);


		//receive update packet: [type+cid+#hops]
		if(type==(char)0X02){
			int k=0;

			struct routing_Table *temp=new routing_Table;
			struct routing_Table *ptr=NULL;
			cid=hdr->getIntegerInfo(1);
			hopnum=hdr->getIntegerInfo(5);
			temp->CID=cid;
			temp->interf=4;
			temp->hopNum=hopnum;
			temp->nextrow=NULL;
			temp->previous=NULL;
			// updated in 0903 start
			// time_t t=time(0);
			int t=0;
			// updated in 0903 end
			temp->TtoE=t+TTL;

			ptr=sh->rtable;

			//update the routing table
				while(ptr!=NULL){
					if(ptr->CID==temp->CID){
						k=1;
						if(ptr->hopNum >= temp->hopNum){

							ptr->interf=4;
							ptr->hopNum=temp->hopNum;
							ptr->TtoE=temp->TtoE;

							//broadcast updated packet
							hdr->setIntegerInfo(hopnum+1,5);
							sh->send_port2->sendPacket(recvPacket);
							sh->send_port3->sendPacket(recvPacket);
							sh->send_port1->sendPacket(recvPacket);
						}
						else if(ptr->hopNum==temp->hopNum&&ptr->interf==temp->interf){
							ptr->TtoE=temp->TtoE;
						}

					}
						ptr=ptr->nextrow;
				}
				if(k==0){
					temp->nextrow=sh->rtable;
					sh->rtable->previous=temp;
					sh->rtable=temp;

					//broadcast updated packet
					hdr->setIntegerInfo(hopnum+1,5);
					sh->send_port2->sendPacket(recvPacket);
					sh->send_port3->sendPacket(recvPacket);
					sh->send_port1->sendPacket(recvPacket);
				}
			//}
			//delete ptr;

		}
		//receive response packet: [type+cid+hid+payload]
		else if(type==(char)0x01){
			int k=0;
			struct pending_Table *ptr=NULL;
			ptr=sh->ptable;
			cid=hdr->getIntegerInfo(1);
			hid=hdr->getIntegerInfo(5);
			//find the path in pending table
			while(ptr!=NULL){
				if(ptr->rCID==cid && ptr->HID==hid){
					interface=ptr->interf;
					k=1;
					//decided which interface should send the packet
					switch(interface){
						case 1:
							sh->send_port1->sendPacket(recvPacket);
							break;
						case 2:
							sh->send_port2->sendPacket(recvPacket);
							break;
						case 3:
							sh->send_port3->sendPacket(recvPacket);
							break;
						case 4:
							sh->send_port4->sendPacket(recvPacket);
							break;
						default:
							cout<<"Cannot found path!"<<endl;
							break;
					}
				}
				if(k==1)
					break;
			}
			//cout<<"Delivery Error!"<<endl;
			//delete ptr;
		}

		//receive request packet: [type+cid+hid]
		else{
			int k=0;
			int k1=0;

			struct pending_Table *ptemp=new pending_Table;
			struct pending_Table *pptr=NULL;
			struct routing_Table *rptr=NULL;

			cid=hdr->getIntegerInfo(1);
			hid=hdr->getIntegerInfo(5);


			pptr=sh->ptable;
			rptr=sh->rtable;

			ptemp->rCID=cid;
			ptemp->HID=hid;
			ptemp->interf=4;
			// updated in 0903 start
			// time_t t=time(0);
			int t=0;
			// updated in 0903 end
			ptemp->TtoE=t+rTTL;
			ptemp->nextrow=NULL;
			ptemp->previous=NULL;


			//update the pending table
			while(pptr!=NULL){
				if(pptr->rCID==ptemp->rCID && pptr->HID==ptemp->HID){
					k=1;
					//reset TtoE
					break;
				}
				pptr=pptr->nextrow;
			}
			if(k==0){
				ptemp->nextrow=sh->ptable;
				sh->ptable->previous=ptemp;
				sh->ptable=ptemp;
			}
			//figure the path out with routing table


			while(rptr!=NULL){
				if(rptr->CID==ptemp->rCID){
					k1=1;
					interface=rptr->interf;

					//decided which interface should send the packet
					switch(interface){
						case 1:
							sh->send_port1->sendPacket(recvPacket);
							break;
						case 2:
							sh->send_port2->sendPacket(recvPacket);
							break;
						case 3:
							sh->send_port3->sendPacket(recvPacket);
							break;
						case 4:
							sh->send_port4->sendPacket(recvPacket);
							break;
						default:
							cout<<"Cannot found path!"<<endl;
							break;
					}
				}
				if(k1==1)
					break;
				rptr=rptr->nextrow;
			}
			if(k1==0)
				cout<<"Cannot found such content!"<<endl;
			//delete pptr;
			//delete rptr;
			//delete ptemp;
		}
		//delete recvPacket;

		struct routing_Table *p=NULL;
		p=sh->rtable;
		cout<<"The Routing Table is:"<<endl;
		cout<<"ContentID"<<setw(10)<<"Interface"<<setw(6)<<"#Hops"<<setw(5)<<"TtoE"<<endl;
		while(p->nextrow!=NULL){
			cout<<setw(7)<<p->CID<<setw(10)<<p->interf;
			// cout<<setw(5)<<p->hopNum<<setw(7)<<p->TtoE-time(0)<<endl;
			cout<<setw(5)<<p->hopNum<<setw(7)<<p->TtoE-0<<endl;
			p=p->nextrow;
		}

		struct pending_Table *t=NULL;
		t=sh->ptable;
		cout<<"The Pending Table is:"<<endl;
		cout<<"RequstCID"<<setw(7)<<"HostID"<<setw(10)<<"Interface"<<setw(10)<<"TimeToExp"<<endl;
		while(t->nextrow!=NULL){
			cout<<setw(7)<<t->rCID<<setw(7)<<t->HID;
			// cout<<setw(9)<<t->interf<<setw(10)<<t->TtoE-time(0)<<endl;
			cout<<setw(9)<<t->interf<<setw(10)<<t->TtoE-0<<endl;
			t=t->nextrow;
		}
		}
	}
	return NULL;
}