void *bcast(void *arg){ struct cShared *sh = (struct cShared *)arg; //char *msg= (char *)msg1; mySendingPort *my_send_port1 = sh-> my_send_port; while(1){ Packet *my_packet = new Packet(); PacketHdr *hdr = my_packet->accessHeader(); hdr->setOctet(ANNOUNCEMENT,0); cout<<"into thread"<<endl; list<int>::iterator p1 = list1.begin(); while(p1 != list1.end()){ hdr->setOctet((unsigned char)(*p1),1); cout<<"sending pkt "<<*p1<<endl; //my_send_port1->setACKflag(true); my_send_port1->sendPacket(my_packet); p1++; } // sem_wait(&mutex); pthread_mutex_lock(&lock); // cout<<msg<<endl; pthread_mutex_unlock(&lock); // sem_post(&mutex); sleep(10); } return 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; }
void *MakeAnnouncementPacket(void *arg){ cout << "Start: Make announcement function." <<endl; struct SPShared *sp = (struct SPShared *)arg; while(1){ int i; Packet *packet = new Packet(); PacketHdr *hdr; for(i = 0; i< ListOfContents.size(); i++){ packet = new Packet(); hdr = packet->accessHeader(); hdr->setOctet('2',0); hdr->setOctet(ListOfContents.at(i),1); hdr->setOctet(0,2); sp->send_port->sendPacket(packet); //sp->send_port->setACKflag(true); //sp->send_port->timer_.startTimer(0); } usleep(10000000); } }
/*! \brief Request a certain content by id Function to send a packet requesting content and to resend the request if not received on the receiving thread */ void GetContent(string contentId, struct res *args) { Packet *req_packet; req_packet = new Packet(); req_packet->setPayloadSize(0); //No Payload PacketHdr *rqhdr = req_packet->accessHeader(); rqhdr->setHeaderSize(3); //Need 3 bytes for the header rqhdr->setOctet('0',0); //Request packet = type 0 rqhdr->setOctet((char)host_id,2); //Setting host id int contentId_int = atoi(contentId.c_str()); rqhdr->setOctet((char)contentId_int,1); //Setting content request message args->my_req_port->sendPacket(req_packet); args->my_req_port->lastPkt_ = req_packet; //cout<<"First octet "<<rqhdr->getOctet(0)<<"Second Octet "<<rqhdr->getOctet(1)<<"Third Octet "<<rqhdr->getOctet(2)<<endl; gettimeofday(&rtt, NULL); cout<<"Sent Request"<<endl; args->my_req_port->setACKflag(false); args->my_req_port->timer_.startTimer(5); while(!args->my_req_port->isACKed()) { sleep(1); if(!args->my_req_port->isACKed()) { sleep(3); if(!args->my_req_port->isACKed()) { sleep(5); if(!args->my_req_port->isACKed()) { sleep(7); if(!args->my_req_port->isACKed()) { sleep(9); if(!args->my_req_port->isACKed()) { cout<<"Giving up.."<<endl; args->my_req_port->setACKflag(true); } } } } } else{return;} } return; }
Packet *Message::make_update_packet(int CID, int nhops){ Packet * update_packet; update_packet = new Packet(); update_packet->setPayloadSize(0); //Header PacketHdr *hdr = update_packet->accessHeader(); hdr->setOctet(TYPE_UPDATE, POS_TYPE ); hdr->setOctet(CID, POS_CID ); hdr->setOctet(nhops, POS_HOPS ); // no payload return update_packet; }
Packet *Message::make_request_packet(int CID, int HID){ Packet * request_packet; request_packet = new Packet(); request_packet->setPayloadSize(0); //Header PacketHdr *hdr = request_packet->accessHeader(); hdr->setOctet(TYPE_REQUEST, POS_TYPE ); hdr->setOctet(CID, POS_CID ); hdr->setOctet(HID, POS_HID ); // no payload return request_packet; }
Packet* Router::create_control_packet(char p_type){ Packet * my_packet = new Packet(); PacketHdr *hdr = my_packet->accessHeader(); if(p_type == 'd'){ //Packet is discovery type //Setting the specific header fields hdr->setOctet('d',0); hdr->setOctet(id,1); return my_packet; } else if(p_type == '1'){ //Disc Ack packet type is '1' hdr->setOctet('1',0); hdr->setOctet(id,1); return my_packet; } else if(p_type == 'l'){ //Packet is LSP type hdr->setOctet('l',0); hdr->setOctet('8',1);//TTL hdr->setOctet(id,2); //src_addr = id hdr->setOctet(num_neighbors,3); for(int i=0; i<num_neighbors; i++){ hdr->setOctet(nbr_tbl.at(i).id[0], 3+i+1); } ++ls_seq_num; //inc for the next ls packet formation return my_packet; } }
void MakeRequest(mySendingPort *my_port, unsigned char content_id){ Packet *new_request_packet = new Packet(); PacketHdr *hdr = new_request_packet->accessHeader(); hdr->setOctet('0',0); hdr->setOctet(content_id,1); hdr->setOctet(HOST_ID,2); my_port->sendPacket(new_request_packet); cout<<"Packet Sent"<<endl; my_port->setACKflag(false); my_port->lastPkt_= new_request_packet; my_port->timer_.startTimer(10); ind->receive_ind = '0'; ind->content_requested_id = content_id; }
Packet* Router::create_data_packet(char p_type, char ttl, char src_addr, char num_addr, char* dest_addrs, int payload_size, char* payload){ Packet * my_dpacket; my_dpacket = new Packet();//create the data packet object //Settint the specific header fields PacketHdr *hdr = my_dpacket->accessHeader(); hdr->setOctet(p_type,PACKET_TYPE_INDEX); hdr->setOctet(ttl,TTL_INDEX); hdr->setOctet(src_addr,SRC_ADDR_INDEX); hdr->setOctet(num_addr,NUM_ADDR_INDEX); int int_num_addr = (int)(num_addr-'0'); for(int i=0; i<int_num_addr; i++){ hdr->setOctet(*(dest_addrs+i), NUM_ADDR_INDEX+i+1); } hdr->setOctet(payload_size,PAYLOAD_SIZE_INDEX(int_num_addr)); //Fill the packet with the data payload my_dpacket->fillPayload(payload_size, payload); return my_dpacket; }
void *rxthread(void *arg){ struct cShared *sh = (struct cShared *)arg; //char *msg= (char *)msg1; mySendingPort *my_send_port1 = sh-> my_send_port; LossyReceivingPort *my_recv_port1 = sh-> my_recv_port; Packet *pData; int var2,var1=1; char *varc1 = "0"; while(1){ var1=1; cout<<"entered while(1) loop"<<endl; while(var1){ cout<<"entered while(var1) loop"<<endl; pData = my_recv_port1->receivePacket(); // cout<<"checking for ack"<<endl; if (pData != NULL) { cout<<"valid data"<<endl; var2 = (int)pData->accessHeader()->getOctet(1); //*varc1= pData->accessHeader()->getOctet(0); var1 = 0; cout<<"valid data after header retrieval "<<endl; } } if(pData->accessHeader()->getOctet(0)==REQUEST){ //RESPONDING TO A REQUEST FOR CONTENT ifstream myfile; //char contentName[8]; std::stringstream ss; string filename1,filename; ss << var2; filename1= ss.str(); filename=filename1+".txt"; // strcat(filename,".txt"); cout<<"about to read data "<< filename<<endl; ifstream file(filename.c_str()); vector<char>Data; // used to store all the data from the file. //Check if the File is successfully opened or not. if(!file.is_open()) { cout<<"\n Could Not open File"<<endl; } //Get the file data while (file.good()) // loop while extraction from file is possible { Data.push_back(file.get()); // get character from file } file.close(); //SaveFileAs("3.txt", &Data); Packet *my_packet = new Packet(); PacketHdr *hdr = my_packet->accessHeader(); hdr->setOctet(RESPONSE,0); my_packet->fillPayload( Data.size() , &Data.at(0)); cout<<"sending data pkt "<<endl; //my_send_port1->setACKflag(true); my_send_port1->sendPacket(my_packet); delete my_packet; }else if(pData->accessHeader()->getOctet(0)==RESPONSE){ //I GET DATA //PUT DATA IN FILE ofstream myfile; std::stringstream ss; string filename1,filename; ss << var2; filename1= ss.str(); filename=filename1+".txt"; //filename.c_str() myfile.open (filename.c_str()); myfile << pData->getPayload(); myfile.close(); cout<<"entered data response "<< pData->getPayload()<<" in location "<<filename<<endl; my_send_port1->setACKflag(true); my_send_port1->timer_.stopTimer(); } } return 0; }
void* Router::listen_on_port(void* thread_id){ int* tid = (int *)(thread_id); pthread_mutex_lock( &mutex_lop ); int tlocal_tid = *tid; pthread_mutex_unlock( &mutex_lop ); cout<<endl<<"Starting to listen on port " << nbr_tbl.at(tlocal_tid).RxP<<"......\n"; PacketHdr *hdr; while(1){ Packet* rxed_p = rxing_ports[tlocal_tid]->receivePacket(); hdr = rxed_p->accessHeader(); if(hdr->getOctet(0) == 'd'){ //Hello packet is rxed cout << "Disc packet is rxed from node_id: " << hdr->getOctet(1) << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl; //Send Hello Ack try{ txing_ports[tlocal_tid]->sendPacket(create_control_packet('1')); }catch(const char *reason){ cerr << "Exception:" << reason << endl; } } else if(hdr->getOctet(0) == '1'){ //Hello Ack is rxed cout << "Hello Ack is rxed from node_id: " << hdr->getOctet(1) << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl; //Set the nhop_hello_ack_rxed map correctly to indicate the ACK is rxed from the nhop nhop_hello_ack_rxed.find(hdr->getOctet(1))->second = 1; /* //print nhop_hello_ack_rxed std::map<char,int>::iterator it; std::cout << "nhop_hello_ack_rxed:\n"; for (it=nhop_hello_ack_rxed.begin(); it!=nhop_hello_ack_rxed.end(); ++it){ if(it->second == 0){ std::cout << it->first << " -> " << it->second << std::endl; } } */ } else if(hdr->getOctet(0) == 'l'){ //LSP packet is rxed cout << "LSP packet is rxed from node_id: " << hdr->getOctet(2) << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl; //Extract the connectivity info extract_ls_info(rxed_p); //Send it to outgoing ports int ttl = (int)(hdr->getOctet(1)-'0'); //std::cout << "----------------------------------------\n"; //std::cout << "LS TTL: " << hdr->getOctet(1) << std::endl; if( ttl != 0){ //check ttl if it is 0 then discard:dont forward further --ttl; hdr->setOctet( (char)('0'+ttl),1); //std::cout << "UPDATED LS TTL: " << hdr->getOctet(1) << std::endl; send_ls_to_outgoing_ports(nbr_tbl.at(tlocal_tid).TxP, rxed_p); } //std::cout << "----------------------------------------\n"; } else if(hdr->getOctet(0) == 'm'){ //Data packet is rxed cout << "\nDATA packet is rxed from node_id: " << hdr->getOctet(2) << " over rx_port:" << nbr_tbl.at(tlocal_tid).RxP << endl; forward_incoming_data_packet(rxed_p); } else{ cout << "Some packet with unrecognized type is rxed\n"; } } }