⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsma.pc

📁 simulator for ad hoc
💻 PC
📖 第 1 页 / 共 3 页
字号:
 * * RETURN:      None * * ASSUMPTIONS: None */void MacTsmaProcUnicastPkt(GlomoNode *node, GlomoMacTsma *tsma, Message *msg){  MacTsmaHeader *hdr = (MacTsmaHeader *)msg->packet;  #ifdef TSMA_DEBUG  if (node->nodeAddr == MONITOR_ID)    {      char clockStr[GLOMO_MAX_STRING_LENGTH];      ctoa(simclock(), clockStr);      printf("Time(%s) received unicast frame from node %d\n",             clockStr, hdr->sourceAddr);    }  #endif  /* Check for valid frame indentifier. */  if (hdr->seqNum == tsma->pktIds[hdr->sourceAddr])    {     #ifdef TSMA_DEBUG     printf("Ack Was Hosed\n");     #endif    }  else if (hdr->seqNum < tsma->pktIds[hdr->sourceAddr])    {      /* Invalid identifier. */      char clockStr[GLOMO_MAX_STRING_LENGTH];      ctoa(simclock(), clockStr);      printf("Time(%s) %d received frame from %d with invalid seqNum\n",             clockStr, node->nodeAddr, hdr->sourceAddr);      assert(FALSE); abort();    }  else    {      /* Register frame identifier. */      tsma->pktIds[hdr->sourceAddr] = hdr->seqNum;    }  /* Construct and send unicast acknowledgment. */  MacTsmaSendAck(node, tsma, msg);    /* Pass frame to network layer. */  MacTsmaPassPkt(node, tsma, msg);    /* Update unicast counter. */  ++tsma->pktsGotUnicast;}/* * NAME:        MacTsmaProcAckPkt * * PURPOSE:     Process incoming acknowledgment * * PARAMETERS:  node, node receiving the ack *              msg, contains the ack * * RETURN:      None * * ASSUMPTIONS: None */void MacTsmaProcAckPkt(GlomoNode *node, GlomoMacTsma *tsma, Message *msg){  MacTsmaHeader *hdr = (MacTsmaHeader *)msg->packet;  Message *newMsg;  BOOL retval;  /* Make sure we are ready for the acknowledgment. */  assert(tsma->currentState == MAC_TSMA_WF_UACK);  /* Check to see if sequence number is correct. */  if (hdr->seqNum == tsma->seqNum)    {      #ifdef TSMA_DEBUG      if (node->nodeAddr == MONITOR_ID)        {          char clockStr[GLOMO_MAX_STRING_LENGTH];          ctoa(simclock(), clockStr);          printf("Time(%s) received acknowledgment from node %d\n",                 clockStr, hdr->sourceAddr);        }      #endif      /*        * Proper acknowledgment received.  Current frame has been       * send, begin processing next frame.       */            assert(tsma->currentOutgoingFrame != NULL);      GLOMO_MsgFree(node, tsma->currentOutgoingFrame);      tsma->currentOutgoingFrame = NULL;            /* Update unicast counter. */      ++tsma->pktsSentUnicast;            /* Update sequence number. */      ++tsma->seqNum;            /* Reset frame repetitions. */      tsma->pktReps = 0;            /* Check to see if queue is now empty. */      CheckForPacketToTransmit(node, tsma);    }  else    {      char clockStr[GLOMO_MAX_STRING_LENGTH];      ctoa(simclock(), clockStr);      printf("Time(%s) bad acknowledgment received.\n", clockStr);      assert(FALSE); abort();    }}//----------------------------------------------------------------------------//----------------------------------------------------------------------------//----------------------------------------------------------------------------//----------------------------------------------------------------------------/* * NAME:        MacTsmaReceivePacketFromRadio *              (Was:MacTsmaProcRadioPkt). * * PURPOSE:     Process frames sent up from radio layer. * * PARAMETERS:  node, node receiving radio frame *              msg, contains radio frame * * RETURN:      None * * ASSUMPTIONS: None */void MacTsmaReceivePacketFromRadio(  GlomoNode *node, GlomoMacTsma *tsma, Message *msg){  /* Retrieve TSMA frame. */  MacTsmaHeader *hdr = (MacTsmaHeader *)msg->packet;  if (hdr->destAddr == node->nodeAddr ||      hdr->destAddr == ANY_DEST)    {      /*        * Frame addressed to this node.  Determine the frame type,       * and take the appropriate actions.        */      switch (hdr->type)        {        case TSMA_BPKT:          {            /* Process broadcast frame. */            MacTsmaProcBroadcastPkt(node, tsma, msg);            break;          }        case TSMA_UPKT:          {            /* Process unicast frame. */            MacTsmaProcUnicastPkt(node, tsma, msg);            break;          }        case TSMA_ACK:          {            /* Process incoming acknowledgment. */            MacTsmaProcAckPkt(node, tsma, msg);            break;          }        }    }  else if ((tsma->myGlomoMac->promiscuousMode == TRUE) &&           ((hdr->type == TSMA_BPKT) || (hdr->type == TSMA_UPKT)))    {      /*       * The frame is not addressed to this node, but send it       * up to the network layer anyway.       */      MacTsmaHandlePromiscuousMode(node, tsma, msg);      GLOMO_MsgFree(node, msg);    }  else    {      /*       * Disregard this frame.       */      GLOMO_MsgFree(node, msg);    }}/* * NAME:        MacTsmaReceiveRadioStatusChangeNotification * * PURPOSE:     Notifies TSMA when radio status has changed. *              In this case, it cares only when the transistion *              is from transmitting to non-transmitting. * */void MacTsmaReceiveRadioStatusChangeNotification(   GlomoNode* node,   GlomoMacTsma* tsma,   RadioStatusType oldRadioStatus,   RadioStatusType newRadioStatus){   if (oldRadioStatus == RADIO_TRANSMITTING) {      assert(newRadioStatus != RADIO_TRANSMITTING);      MacTsmaPacketSent(node, tsma);   }//if//}/* * NAME:        MacTsmaNetworkLayerHasPacketToSend * * PURPOSE:     Notifies TSMA that the network queue have gone from *              empty to non-empty and should start the processes of *              of sending the packet. * * PARAMETERS:  node, node receiving network frame * * RETURN:      None * * ASSUMPTIONS: None */void MacTsmaNetworkLayerHasPacketToSend(GlomoNode* node, GlomoMacTsma* tsma){  /*    * If current state is READY, then determine frame type, and   * transition to the proper state.   */  if (tsma->currentState == MAC_TSMA_READY)    {       CheckForPacketToTransmit(node, tsma);    }}/* * NAME:        MacTsmaInit * * PURPOSE:     Initialization function for the TSMA protocol. * * PARAMETERS:  node, node being initialized *              input, structure containing contents of input file * * RETURN:      None * * ASSUMPTIONS: None */void MacTsmaInit(GlomoNode *node, int index, const GlomoNodeInput *input){  MacTsmaSynchPkt synch;  clocktype interval;  Message *synchMsg;  char buf[GLOMO_MAX_STRING_LENGTH];  FILE *tsmaInput;  BOOL retval;  int degree, q, k;  int bandwidthBytesSec = node->macData[index]->bandwidth;    /* Allocate space for TSMA structure. */  GlomoMacTsma *tsma = (GlomoMacTsma *)checked_pc_malloc(sizeof(GlomoMacTsma));  memset(tsma, 0, sizeof(GlomoMacTsma));  tsma->myGlomoMac = node->macData[index];  tsma->myGlomoMac->macVar = (void *)tsma;  /*   * Initialize TSMA parameters.   */  tsmaInput = fopen("tsmaparams.dat", "r");  if (tsmaInput == NULL)    {      GLOMO_ReadIntInstance(node->nodeAddr, input, "TSMA-MAX-NODE-DEGREE",                            index, TRUE, &retval, &degree);      if (retval == FALSE)        {          printf("TSMA Error: max node degree unspecified in config file\n");          assert(FALSE); abort();        }      if (degree < 2)        {          printf("TSMA Error: invalid max node degree specified in config file\n");          assert(FALSE); abort();        }      /*        * Generate the proper TSMA parameters, q and k.  The acutal       * parameters will be stored in a file.       */      sprintf(buf, "tsma_param_gen %d %d > tsmaparams.dat",               node->numNodes, degree);      printf("Running:%s\n",buf);      system(buf);      tsmaInput = fopen("tsmaparams.dat", "r");      assert(tsmaInput != NULL);    }  /* Get TSMA parameters, and generate TSMA transmission schedules. */  fscanf(tsmaInput, "%d", &q);  fscanf(tsmaInput, "%d", &k);  fclose(tsmaInput);  tsma->frameLength = q * q;  tsma->maxReps = q;  tsmaInput = fopen("tsmacodes.dat", "r");  if (tsmaInput == NULL)    {      sprintf(buf, "tsma_code_gen %d %d > tsmacodes.dat",              q, k);      system(buf);      tsmaInput = fopen("tsmacodes.dat", "r");      assert(tsmaInput != NULL);    }  tsma->schedule = (char *)pc_malloc(tsma->frameLength * sizeof(char) + 1);  assert(tsma->schedule != NULL);  /* Get proper TSMA transmission schedule. */  fseek(tsmaInput, tsma->frameLength * node->nodeAddr + node->nodeAddr, SEEK_SET);  fread(tsma->schedule, 1, tsma->frameLength, tsmaInput);  tsma->schedule[tsma->frameLength] = '\0';  fclose(tsmaInput);  #ifdef TSMA_DEBUG  if (node->nodeAddr == MONITOR_ID)    {      printf("schedule(%s)\n", tsma->schedule);    }  #endif  tsma->pktIds = (int *)pc_malloc(node->numNodes * sizeof(int));  memset(tsma->pktIds, '\0', node->numNodes * sizeof(int));  tsma->currentState = MAC_TSMA_READY;  tsma->prevState = MAC_TSMA_READY;  tsma->seqNum = 1;  tsma->currentSlot = 0;  tsma->pktsToSend = 0;  tsma->pktsLostOverflow = 0;  tsma->pktsSentUnicast = 0;  tsma->pktsSentBroadcast = 0;  tsma->pktsGotUnicast = 0;  tsma->pktsGotBroadcast = 0;  tsma->currentOutgoingFrame = NULL;    ///* Send synchronization packet. */  //interval =   //  (2 * node->macData[index]->propDelay) +  //  (2 * MAC_DELAY) +  //  (2 * RADIO_PHY_DELAY) +  //  (2 * SYNCHRONIZATION_TIME) +  //  (((MAX_MAC_FRAME_SIZE * SECOND) +  //    (sizeof(MacTsmaHeader) * SECOND))  //   / channelBandwidthBytes) +   //   /* WTF (Jay)*/ 10000000;  /* Send synchronization packet. */  interval =     node->macData[index]->propDelay +    RADIO_PHY_DELAY +     node->macData[index]->propDelay +    SYNCHRONIZATION_TIME +    (((MAX_NW_PKT_SIZE + sizeof(MacTsmaHeader)) * SECOND) / bandwidthBytesSec) +     RADIO_PHY_DELAY +     node->macData[index]->propDelay +    SYNCHRONIZATION_TIME +    ((sizeof(MacTsmaHeader) * SECOND) / bandwidthBytesSec);  #ifdef TSMA_DEBUG  if (node->nodeAddr == MONITOR_ID)    {      char Str[GLOMO_MAX_STRING_LENGTH];      ctoa(interval, Str);      printf("Slot Interval Time = %s\n", Str);    }  #endif    synch.slotInterval = interval;  synchMsg = GLOMO_MsgAlloc(node, GLOMO_MAC_LAYER, 0, MSG_MAC_SlotFront);  GLOMO_MsgInfoAlloc(node, synchMsg, sizeof(synch));  memcpy(synchMsg->info, &synch, sizeof(synch));  GLOMO_MsgSetInstanceId(synchMsg, tsma->myGlomoMac->interfaceIndex);  GLOMO_MsgSend(node, synchMsg, 0);}/* * NAME:        MacTsmaLayer * * PURPOSE:     Models the behavior of the MAC layer with the TSMA protocol *              on receiving a message. * * PARAMETERS:  node, node which received the message *              msg, message received * * RETURN:      None * * ASSUMPTIONS: None */void MacTsmaLayer(GlomoNode *node, int index, Message *msg){  GlomoMacTsma *tsma = (GlomoMacTsma *)node->macData[index]->macVar;  assert(msg->eventType == MSG_MAC_SlotFront);  /*   * The next slot is beginning.   */  MacTsmaBeginSlot(node, tsma);  /* Schedule the next slot. */  MacTsmaScheduleSlot(node, msg);}    /* * NAME:        MacTsmaFinalize *  * PURPOSE:     Called at the end of simulation to collect statisitcs *              associated with MAC layer performance. * * PARAMETERS:  node, node for which stats are to be collected * * RETURN:      None * * ASSUMPTIONS: None */void MacTsmaFinalize(GlomoNode *node, int index){  GlomoMacTsma *tsma = (GlomoMacTsma *)node->macData[index]->macVar;  rename("tsmaparams.dat", "tsmaparams.dat.bak");  rename("tsmacodes.dat", "tsmacodes.dat.bak");  if (node->macData[index]->macStats == TRUE)    {      MacTsmaPrintStats(node, tsma);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -