void coacsAgent::becomeQD() { type = QDNODE; QDcache.init(capQD); if(QDcount == 0) QDs = new int [1]; //Im the first QD QDs[QDcount] = here_.addr_; myQDindex = QDcount++; QDAPsent = false; //reset flag LastAssignedQD = true; //set flag sendCIP(IP_BROADCAST); fprintf(actionfile,"Qd_fun:becomeQD\n"); fprintf(actionfile,"My address is :%i\n",Myaddr()); fprintf(actionfile,"****************************************************************\n"); } void coacsAgent::NQDCalc() { //probeDSDV();//Discover numNodes from DSDV //while (numNodes == 0) { printf("numNodes ==0");} printf("\t*****numNodes == %i\n",numNodes ); Ropt = (double) capCN*numNodes/numQ ; if ( Ropt > 1 ) Ropt = 1; printf ("Ropt is set to %f\n",Ropt); //***calculate NQD NQDmax = getNQDmax(Ropt, XY, R); printf ("Max # of QDs is set to %i\n",NQDmax); NQDload = getNQDload(Ropt);//5;//atoi(argv[8]);// printf ("NQDload is set to %i\n",NQDload); if( NQDuser != 0) { NQDopt = NQDuser; NQDmax = NQDuser; printf ("Number of QDs is set by THE USER to %i\n",NQDuser); } else { NQDopt = NQDload; if(NQDmax < NQDload) NQDopt = NQDmax; printf ("Number of QDs (NQDopt) is set to %i\n",NQDopt); } capQD = (int) (Ropt*numQ/NQDopt); printf ("QD capacity is set to %i queries\n",capQD); //StartRhit = getStartRhit(NQDload, XY, R); //printf ("StartRhit is set to %f\n",StartRhit); fprintf(actionfile,"Qd_fun:NQDCalc\n"); fprintf(actionfile,"My address is :%i\n",Myaddr()); fprintf(actionfile,"****************************************************************\n"); } void coacsAgent::initiateCoacs() { fprintf(actionfile,"Qd_fun:initiateCoacs\n"); fprintf(actionfile,"My address is :%i\n",Myaddr()); Packet* pkt = allocpkt(CONTSIZE); hdr_coacs* ph = hdr_coacs::access(pkt); hdr_ip* iph = hdr_ip::access(pkt); //***calculate NQD QDcount = NQDopt; //set QD IDs array QDs = new int [QDcount]; ph->QDlist = new int [QDcount]; ph->checked = new int [QDcount]; for (int i = 0; i < QDcount; i++) { QDs[i] = i+1; //Should change if election implemented ph->QDlist[i] = QDs[i]; ph->checked[i] = 0; } ph->QDcount = QDcount; ph->pType = COACSQDA; //become QD myQDindex = 0; type = QDNODE; QDcache.init(capQD); if (QDcount == 1) return; //No QDs to inform //***Inform QDs //set my index to checked in the array. ph->checked[myQDindex] = 1; //printf("QDcount %i ---*** \n",QDcount); fprintf(actionfile,"QDcount %i ---*** \n",QDcount); fprintf(actionfile,"****************************************************************\n"); sendByMDPF(ph); Packet::free(pkt); } void coacsAgent::sendCIP(int destination) { fprintf(actionfile,"Qd_fun:sendCIP\n"); fprintf(actionfile,"My address is :%i\n",Myaddr()); fprintf(actionfile,"destination is :%i\n",destination); Packet* pkt = allocpkt(CONTSIZE); hdr_ip* iph = HDR_IP(pkt); hdr_coacs* ph = hdr_coacs::access(pkt); //set QD IDs array ph->checked = new int [QDcount]; ph->QDlist = new int [QDcount]; for (int i = 0; i < QDcount; i++) ph->QDlist[i] = QDs[i]; ph->QDcount = QDcount; ph->src = Myaddr(); // store my address ph->seq = ++CIPseqQD; CIPsrc = Myaddr(); //So I do not forward Bcast again ph->pType = COACSCIP; //Coacs info packet ph->doDSDV = 0 ; iph->daddr() = destination; // Myaddr(); // iph->dport() = iph->sport(); // ROUTER_PORT; // send(pkt, (Handler*) 0); CountPacket(COACSCIP); // sendPacket(ph, COACSCIP, CONTSIZE ,0, destination); fprintf(actionfile,"****************************************************************\n"); } void coacsAgent::ReceiveCIP(hdr_coacs* hdr, hdr_ip* hdrip) { //********* fprintf(actionfile,"Qd_fun:ReceiveCIP\n"); fprintf(actionfile,"My address is :%i\n",Myaddr()); //If this is first time recieved forward broadcast CIP & save info if( hdr->src != CIPsrc ) //&& hdr->seq != CIPseq //Check later { printf("\t\t********%i ReceiveCIP from %i seq %i, QD %i\n",Myaddr(),hdrip->saddr(), hdr->seq, hdr->src); fprintf(actionfile,"\t\t********%i ReceiveCIP from %i seq %i, QD %i\n",Myaddr(),hdrip->saddr(), hdr->seq, hdr->src); coacsGO =true;//Comment this for START if(type == QDNODE) { QDAPsent = false; //reset flag LastAssignedQD = false; //set flag } //Save values QDcount = hdr->QDcount; QDs = new int [QDcount]; for (int i = 0; i < QDcount; i++) QDs[i] = hdr->QDlist[i]; CIPsrc = hdr->src; CIPseq = hdr->seq; //Forward CIP sendPacket(hdr, COACSCIP, CONTSIZE ,0, IP_BROADCAST); //sendCIP(IP_BROADCAST); } fprintf(actionfile,"****************************************************************\n"); } int coacsAgent::findNewQD() { fprintf(actionfile,"Qd_fun:findNewQD\n"); fprintf(actionfile,"My address is :%i\n",Myaddr()); fprintf(actionfile,"Number of QD is= %i\n",QDcount); //Pick node with next ID in order //For later check node scores to Pick int max= 0; for (int i = 0; i < QDcount; i++) { if(QDs[i] > max) max = QDs[i]; fprintf(actionfile,"QD:%i with index:%i\n",i,QDs[i]); } if(max == 0) printf("Error: findNewQD"); return ++max; fprintf(actionfile,"****************************************************************\n"); } void coacsAgent::ReceiveQDAP(hdr_coacs* hdr, hdr_ip* hdrip) { fprintf(actionfile,"Qd_fun:ReceiveQDAP\n"); fprintf(actionfile,"My address is :%i and time is: %i\n",Myaddr(),now()); fprintf(actionfile,"At time:\t %f \tQD\t %i\t Assigned\n",now(), Myaddr()); fprintf(actionfile,"****************************************************************\n"); fprintf(TraceFile,"At time:\t %f \tQD\t %i\t Assigned\n",now(), Myaddr()); printf("******************%i ReceiveQDAP from QD %i\n",Myaddr(),hdrip->saddr()); if (QDcount==0 || !coacsGO) //NEW { //become QD type = QDNODE; QDcache.init(capQD); //Save values QDcount = hdr->QDcount; QDs = new int [QDcount]; for (int i = 0; i < QDcount; i++) { QDs[i] = hdr->QDlist[i]; if (QDs[i] == Myaddr() ) myQDindex = i; } //set my index to checked in the array. hdr->checked[myQDindex] = 1; //find out if all QDs have been checked yet bool allchecked = true; for (int j = 0; j < QDcount; j++) if (hdr->checked[j] == 0) { allchecked = false; break; } if (allchecked == 0 ) //Not all QD are checked Forward to nearest unchecked QD sendByMDPF(hdr); else//***Inform Network sendCIP(IP_BROADCAST); return; } if(type == QDNODE) { printf("Node %i ReceiveQDAP and is already QD\n",Myaddr()); // fprintf(actionfile,"Node %i ReceiveQDAP and is already QD\n",Myaddr()); sendCIP(hdrip->saddr()); } else becomeQD(); //Add pending cache and send Ack if (QDcache.addQ(hdr->req, hdr->src) == CADDOK) sendPacket(hdr, CADDACK, REQSIZE, hdr->req, hdr->src); else //Should never reach here printf("ERROR: NEW QD(%i) cannot Add Request: %i for CN: %i\n",Myaddr(),hdr->req,hdr->src); } void coacsAgent::sendCRhit() { fprintf(actionfile,"Qd_fun:sendCRhit\n"); fprintf(actionfile,"My address is :%i and time is %i\n",Myaddr(),now()); Packet* pkt = allocpkt(CONTSIZE); hdr_ip* iph = HDR_IP(pkt); hdr_coacs* ph = hdr_coacs::access(pkt); //set QD IDs array ph->checked = new int [QDcount]; ph->QDlist = new int [QDcount]; for (int i = 0; i < QDcount; i++) { ph->QDlist[i] = QDs[i]; ph->checked[i] = 0; } ph->QDcount = QDcount; //set my index to checked in the array. ph->checked[myQDindex] = 1; ph->pType = COACSRHIT; ph->nqc = QDcache.Qcount; sendByMDPF(ph); fprintf(actionfile,"****************************************************************\n"); } void coacsAgent::ReceiveCRhit(hdr_coacs* hdr, hdr_ip* hdrip) { fprintf(actionfile,"Qd_fun:ReceiveCRhit\n"); fprintf(actionfile,"My address is :%i and time is %i\n",Myaddr(),now()); printf(" **** %i receive COACSRHIT nqc :%i\n", Myaddr(), hdr->nqc ); //set my index to checked in the array. hdr->checked[myQDindex] = 1; //find out if all QDs have been checked yet bool allchecked = true; for (int j = 0; j < QDcount; j++) if (hdr->checked[j] == 0) { allchecked = false; break; } hdr->nqc = hdr->nqc + QDcache.Qcount; if (allchecked == 0 ) //Not all QD are checked Forward request to nearest unchecked QD sendByMDPF(hdr); else //Check Rhit if Ok send START { Ract = (double)(hdr->nqc)/numQ; printf("Ract is now %f and StartRhit %f \n",Ract, StartRhit); if(Ract >= 0.25) //StartRhit { hdr->src = Myaddr(); // store my address sendPacket(hdr, COACSTART, CONTSIZE, 0, IP_BROADCAST); coacsGO = true; } } fprintf(actionfile,"****************************************************************\n"); } /* void coacsAgent::sendQDAP(hdr_coacs* hdr) { //??? //send by MDPF to inform QDs of NEW QDAP or Multicast to QDs // for (int i = 0; i < QDcount -1 ; i++) // if(QDs[i] != Myaddr()) sendPacket(hdr, COACSQDA, CONTSIZE, 0, QDs[i]); sendPacket(hdr, COACSQDA, CONTSIZE, hdr->req, findNewQD()); QDAPsent = true; //set flag } */ /* //Raji: Obsolete code void coacsAgent::ReceiveQDAP(hdr_coacs* hdr, hdr_ip* hdrip) { printf("******************%i ReceiveQDAP from QD %i\n",Myaddr(),hdrip->saddr()); if(type == QDNODE) { printf("ERROR: %i ReceiveQDAP and is already QD\n",Myaddr()); //QDAPsent = true; //set flag //find out if all QDs have been checked yet bool allchecked = true; for (int j = 0; j < hdr->QDcount; j++) if (hdr->checked[j] == 0) { allchecked = false; break; } if (allchecked )// if all QDs have been checked Forward QDAP to New QD sendPacket(hdr, COACSQDA, CONTSIZE, hdr->req, findNewQD()); else // else not all QDs checked yet: Forward QDAP to the Nearest QD sendQDAP(hdr); } } void coacsAgent::sendQDAP(hdr_coacs* hdr) { //??? //send by MDPF to inform QDs of NEW QDAP QDAPsent = true; //set flag Packet* pkt = allocpkt(CONTSIZE); hdr_ip* iph = HDR_IP(pkt); hdr_coacs* ph = hdr_coacs::access(pkt); copyHeader(ph, hdr);//IF DEL or ACK no need?? //set QD IDs array ph->checked = new int [QDcount]; ph->QDlist = new int [QDcount]; for (int i = 0; i < QDcount; i++) { ph->QDlist[i] = QDs[i]; ph->checked[i] = 0; } //set my index to checked in the array. ph->checked[myQDindex] = 1; ph->QDcount = QDcount; ph->pType = COACSQDA; //QD assign packet ph->doDSDV = 1 ; iph->daddr() = Myaddr(); iph->dport() = ROUTER_PORT; send(pkt, (Handler*) 0); } */