📄 nwlar1.pc
字号:
Message *newMsg; LAR1_RouteRequest *rreq, *pkt; NODE_ADDR *n_addr, *old_n_addr; char *pktptr; int i; int pktSize = sizeof(LAR1_RouteRequest); clocktype delay; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; delay = pc_erand(node->seed) * LAR1_RREQ_JITTER; pkt = (LAR1_RouteRequest *) GLOMO_MsgReturnPacket(oldMsg); pktSize += (sizeof(NODE_ADDR) * (pkt->currentHop + 1)); newMsg = GLOMO_MsgAlloc(node, GLOMO_MAC_LAYER, 0, MSG_MAC_FromNetwork); GLOMO_MsgPacketAlloc(node, newMsg, pktSize); pktptr = (char *) GLOMO_MsgReturnPacket(newMsg); rreq = (LAR1_RouteRequest *) pktptr; /* Position the n_addr pointer onto the array of node addresses */ pktptr += sizeof(LAR1_RouteRequest); n_addr = (NODE_ADDR *) pktptr; /* Position the old_n_addr pointer onto the array of node addresses */ pktptr = (char *) GLOMO_MsgReturnPacket(oldMsg) + sizeof(LAR1_RouteRequest); old_n_addr = (NODE_ADDR *) pktptr; rreq->packetType = LAR1_ROUTE_REQUEST; rreq->sourceAddr = pkt->sourceAddr; rreq->destAddr = pkt->destAddr; rreq->seqNum = pkt->seqNum; rreq->currentHop = pkt->currentHop; rreq->flooding = pkt->flooding; memmove(&(rreq->requestZone), &(pkt->requestZone), sizeof(LAR1_Zone)); memcpy(n_addr, old_n_addr, sizeof(NODE_ADDR) * pkt->currentHop); n_addr[pkt->currentHop] = node->nodeAddr; lar1->RouteRequestsRelayed++; /* Transmit Route Request */ NetworkIpSendRawGlomoMessageWithDelay( node, newMsg, ANY_DEST, CONTROL, IPPROTO_LAR1, 0, delay);} //// FUNCTION NetworkLar1InitiateRouteRequest()// PURPOSE Initiate a new LAR Route Request Packet// PARAMETERS destAddr - the destination for which route is needed//void NetworkLar1InitiateRouteRequest(GlomoNode *node, NODE_ADDR destAddr){ Message *newMsg; LAR1_RouteRequest *rreq; NODE_ADDR *n_addr; char *pktptr; int pktSize = sizeof(LAR1_RouteRequest) + sizeof(NODE_ADDR); clocktype delay; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; delay = pc_erand(node->seed) * LAR1_RREQ_JITTER; newMsg = GLOMO_MsgAlloc(node, GLOMO_MAC_LAYER, 0, MSG_MAC_FromNetwork); GLOMO_MsgPacketAlloc(node, newMsg, pktSize); pktptr = (char *) GLOMO_MsgReturnPacket(newMsg); rreq = (LAR1_RouteRequest *) pktptr; /* Position the n_addr pointer onto the array of node addresses */ pktptr += sizeof(LAR1_RouteRequest); n_addr = (NODE_ADDR *) pktptr; rreq->packetType = LAR1_ROUTE_REQUEST; rreq->sourceAddr = node->nodeAddr; rreq->destAddr = destAddr; rreq->currentHop = 0; rreq->seqNum = lar1->seqNum; lar1->seqNum = (lar1->seqNum + 1) % LAR1_MAX_SEQ_NUM; /* Calculate Request Zone */ if (NetworkLar1CalculateReqZone(node, newMsg, destAddr)) rreq->flooding = FALSE; else rreq->flooding = TRUE; n_addr[0] = node->nodeAddr; lar1->RouteRequestsSentAsSource++; /* Transmit Route Request */ NetworkIpSendRawGlomoMessageWithDelay( node, newMsg, ANY_DEST, CONTROL, IPPROTO_LAR1, 0, delay); NetworkLar1SetTimer(node, MSG_NETWORK_CheckTimeoutAlarm, destAddr, LAR1_REQ_TIMEOUT); NetworkLar1InsertRequestSent(lar1, destAddr);}//// FUNCTION NetworkLar1CalculateReqZone()// PURPOSE Calculate and set the request zone in a LAR Request Packet// for a given destination// PARAMETERS msg - the LAR Request Packet to be sent// destAddr - the destination for which route is needed//BOOL NetworkLar1CalculateReqZone(GlomoNode *node, Message *msg, NODE_ADDR destAddr){ LAR1_Zone zone; double radius; double velocity; long xSource; long ySource; long xDestination; long yDestination; clocktype destinationTime; int i; BOOL found = FALSE; LAR1_RouteCacheEntry *current; LAR1_RouteRequest *rreq; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; rreq = (LAR1_RouteRequest *) GLOMO_MsgReturnPacket(msg); /* Get source location info */ xSource = GLOMO_MobilityReturnPositionX(node); ySource = GLOMO_MobilityReturnPositionY(node); /* Get destination location info */ current = lar1->routeCacheHead; while (current != NULL) { if (current->destAddr == destAddr) { found = TRUE; velocity = current->destVelocity; destinationTime = current->locationTimestamp; xDestination = current->destLocation.x; yDestination = current->destLocation.y; break; } current = current->next; } if(!found){#ifdef DEBUG printf(" No location data on node %d\n", destAddr);#endif memset(&(rreq->requestZone), 0, sizeof(LAR1_Zone)); return FALSE; } else {#ifdef DEBUG printf(" Location data found for node %d\n", destAddr);#endif /* Calculate radius to find the request zone */ radius = velocity * (double)((simclock() - destinationTime) / SECOND);#ifdef DEBUG printf(" velocity = %f radius = %f\n",velocity,radius); printf(" Source = (%d,%d), Dest = (%d, %d)\n", xSource, ySource, xDestination, yDestination);#endif /* Calculate reqest zone if source node is within expected zone */ if ((xSource >= (xDestination - radius)) && (xSource <= (xDestination + radius)) && (ySource >= (yDestination - radius)) && (ySource <= (yDestination + radius))) {#ifdef DEBUG printf(" Source Node is Within Expected Zone.\n");#endif zone.bottomLeft.x = xDestination - radius; zone.bottomLeft.y = yDestination - radius; zone.topLeft.x = xDestination - radius; zone.topLeft.y = yDestination + radius; zone.topRight.x = xDestination + radius; zone.topRight.y = yDestination + radius; zone.bottomRight.x = xDestination + radius; zone.bottomRight.y = yDestination - radius; } /* Calculate request zone if source node is outside expected zone */ else {#ifdef DEBUG printf(" Source Node is Outside Expected Zone.\n");#endif if(xSource < xDestination-radius) { if (ySource < yDestination-radius) { zone.bottomLeft.x = xSource; zone.bottomLeft.y = ySource; zone.topLeft.x = xSource; zone.topLeft.y = yDestination + radius; zone.topRight.x = xDestination + radius; zone.topRight.y = yDestination + radius; zone.bottomRight.x = xDestination + radius; zone.bottomRight.y = ySource; } else if (ySource >= yDestination-radius && ySource <= yDestination+radius) { zone.bottomLeft.x = xSource; zone.bottomLeft.y = yDestination-radius; zone.topLeft.x = xSource; zone.topLeft.y = yDestination + radius; zone.topRight.x = xDestination + radius; zone.topRight.y = yDestination + radius; zone.bottomRight.x = xDestination + radius; zone.bottomRight.y = yDestination - radius; } else if (ySource > yDestination+radius) { zone.bottomLeft.x = xSource; zone.bottomLeft.y = yDestination - radius; zone.topLeft.x = xSource; zone.topLeft.y = ySource; zone.topRight.x = xDestination + radius; zone.topRight.y = ySource; zone.bottomRight.x = xDestination + radius; zone.bottomRight.y = yDestination - radius; } } else if (xSource >= xDestination-radius && xSource <= xDestination+radius) { if (ySource < yDestination-radius) { zone.bottomLeft.x = xDestination - radius; zone.bottomLeft.y = ySource; zone.topLeft.x = xDestination - radius; zone.topLeft.y = yDestination + radius; zone.topRight.x = xDestination + radius; zone.topRight.y = yDestination + radius; zone.bottomRight.x = xDestination + radius; zone.bottomRight.y = ySource; } else if (ySource > yDestination+radius) { zone.bottomLeft.x = xDestination - radius; zone.bottomLeft.y = yDestination - radius; zone.topLeft.x = xDestination - radius; zone.topLeft.y = ySource; zone.topRight.x = xDestination + radius; zone.topRight.y = ySource; zone.bottomRight.x = xDestination + radius; zone.bottomRight.y = yDestination - radius; } } else if (xSource > xDestination+radius) { if (ySource < yDestination-radius) { zone.bottomLeft.x = xDestination - radius; zone.bottomLeft.y = ySource; zone.topLeft.x = xDestination - radius; zone.topLeft.y = yDestination + radius; zone.topRight.x = xSource; zone.topRight.y = yDestination + radius; zone.bottomRight.x = xSource; zone.bottomRight.y = ySource; } else if (ySource >= yDestination-radius && ySource <= yDestination+radius) { zone.bottomLeft.x = xDestination - radius; zone.bottomLeft.y = yDestination - radius; zone.topLeft.x = xDestination - radius; zone.topLeft.y = yDestination + radius; zone.topRight.x = xSource; zone.topRight.y = yDestination + radius; zone.bottomRight.x = xSource; zone.bottomRight.y = yDestination - radius; } else if (ySource > yDestination+radius) { zone.bottomLeft.x = xDestination - radius; zone.bottomLeft.y = yDestination - radius; zone.topLeft.x = xDestination - radius; zone.topLeft.y = ySource; zone.topRight.x = xSource; zone.topRight.y = ySource; zone.bottomRight.x = xSource; zone.bottomRight.y = yDestination - radius; } } }#ifdef DEBUG printf(" zone.bottomLeft = (%d, %d)\n", zone.bottomLeft.x, zone.bottomLeft.y); printf(" zone.topLeft = (%d, %d)\n", zone.topLeft.x, zone.topLeft.y); printf(" zone. topRight = (%d, %d)\n", zone.topRight.x, zone.topRight.y); printf(" zone.bottomRight = (%d, %d)\n", zone.bottomRight.x, zone.bottomRight.y);#endif memmove(&(rreq->requestZone), &zone, sizeof(LAR1_Zone)); return TRUE; }}//// FUNCTION NetworkLar1BufferPacket()// PURPOSE Place packet into send buffer, awaiting valid path// and return TRUE if buffering is successful// PARAMETERS msg - the data packet to be buffered// destAddr - the destination of this data packet//BOOL NetworkLar1BufferPacket(GlomoNode *node, Message *msg, NODE_ADDR destAddr){ LAR1_SendBufferEntry *entry; int bufsize; int index; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; bufsize = (lar1->sendBufTail + LAR1_SEND_BUFFER_SIZE) - lar1->sendBufHead; bufsize = bufsize % LAR1_SEND_BUFFER_SIZE; if (bufsize >= LAR1_SEND_BUFFER_SIZE) return FALSE; index = lar1->sendBufTail; if (lar1->sendBuf[index] == NULL) { lar1->sendBuf[index] = pc_malloc(sizeof(LAR1_SendBufferEntry)); assert (lar1->sendBuf[index]); } entry = lar1->sendBuf[index]; entry->destAddr = destAddr; entry->msg = msg; entry->reTx = FALSE; entry->times = simclock(); lar1->sendBufTail = (lar1->sendBufTail + 1) % LAR1_SEND_BUFFER_SIZE; return TRUE;}//// FUNCTION NetworkLar1PendingRouteReq()// PURPOSE Return TRUE if this node has sent a LAR Route Request Packet// for the given destination.// PARAMETERS destAddr - the destination to check//BOOL NetworkLar1PendingRouteReq(GlomoNode *node, NODE_ADDR destAddr){ GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; LAR1_RequestSentEntry *entry; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; entry = lar1->reqSentHead; while (entry != NULL) { if (entry->destAddr == destAddr) return TRUE; else entry = entry->next; } return FALSE;}//// FUNCTION NetworkLar1RouteExists()// PURPOSE Return TRUE if this node has a valid route to the destination// PARAMETERS destAddr - the destination to check//BOOL NetworkLar1RouteExists(LAR1_RouteCacheEntry *cacheEntry, NODE_ADDR destAddr){ while (cacheEntry != NULL) { if ((cacheEntry->destAddr == destAddr) && (cacheEntry->valid)) return TRUE; cacheEntry = cacheEntry->next; } return FALSE;}//// FUNCTION NetworkLar1RetrieveCacheEntry()// PURPOSE Return the Route Cache Entry for the given destination if// one exists.// PARAMETERS cacheEntry - the head pointer of the route cache// destAddr - the destination to check//LAR1_RouteCacheEntry *NetworkLar1RetrieveCacheEntry( LAR1_RouteCacheEntry *cacheEntry, NODE_ADDR destAddr) { while (cacheEntry != NULL) { if ((cacheEntry->destAddr == destAddr) && (cacheEntry->valid)) return cacheEntry; cacheEntry = cacheEntry->next; } return NULL;}//// FUNCTION NetworkLar1InsertRoute()// PURPOSE Extract route information from a LAR Route Reply Packet// and insert info into the Route Cache.// PARAMETERS pkt - the LAR Route Reply packet// path - the path given in the route reply// numEntries - the number of entries in the path array//void NetworkLar1InsertRoute(GlomoNetworkLar1 *lar1, LAR1_RouteReply *pkt, NODE_ADDR *path, int numEntries){ LAR1_RouteCacheEntry *entry = pc_malloc(sizeof(LAR1_RouteCacheEntry)); int i; assert(entry); entry->destAddr = pkt->sourceAddr; entry->inUse = TRUE; entry->valid = TRUE; memmove(&(entry->destVelocity), &(pkt->destVelocity), sizeof(double)); entry->path = pc_malloc(sizeof(NODE_ADDR) * (numEntries-1)); assert(entry->path); memcpy(entry->path, &(path[1]), (sizeof(NODE_ADDR) * (numEntries-1))); entry->pathLength = numEntries-1; memmove(&(entry->locationTimestamp), &(pkt->locationTimestamp), sizeof(clocktype)); memmove(&(entry->destLocation), &(pkt->destLocation), sizeof(LAR1_Location)); entry->next = lar1->routeCacheHead;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -