static BOOL enterAlohaCheckTransmit(Node* node, AlohaData* dataPtr, Message *msg) { //UserCodeStartCheckTransmit BOOL transmit = MAC_OutputQueueIsEmpty(node, dataPtr->myMacData->interfaceIndex); char clockStr[100]; BOOL msgReuse = FALSE; assert(dataPtr->mode == ALOHA_YIELD); if (dataPtr->txMode == ALOHA_TX_ACK) { clocktype jitter = (clocktype) (RANDOM_erand(dataPtr->seed) * ALOHA_DELAY_TIMER); transmit = FALSE; MESSAGE_Send(node, msg, ALOHA_NEXT_PKT_DELAY + jitter); msgReuse = TRUE; } else { transmit = !MAC_OutputQueueIsEmpty(node, dataPtr->myMacData->interfaceIndex); dataPtr->mode = ALOHA_IDLE; } if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: Check Transmit, checking if que has packets to send\n", node->nodeId, clockStr); } //UserCodeEndCheckTransmit if (transmit) { dataPtr->state = ALOHA_TRANSMIT; enterAlohaTransmit(node, dataPtr, msg); } else { dataPtr->state = ALOHA_IDLE_STATE; enterAlohaIdleState(node, dataPtr, msg); } return msgReuse; }
//State Function Definitions void MacAlohaNetworkLayerHasPacketToSend(Node* node, AlohaData* dataPtr) { //UserCodeStartNetworkLayerHasPacketToSend char clockStr[100]; if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: NetworkLayerHasPacketToSend!\n", node->nodeId, clockStr); } //UserCodeEndNetworkLayerHasPacketToSend if ((dataPtr->mode == ALOHA_IDLE) && (dataPtr->txMode == ALOHA_TX_IDLE)) { dataPtr->state = ALOHA_TRANSMIT; enterAlohaTransmit(node, dataPtr, NULL); return; } else { dataPtr->state = ALOHA_IDLE_STATE; enterAlohaIdleState(node, dataPtr, NULL); return; } }
void AppLayerCCNClient(Node* node, Message* msg) { NodeData* nodeData; nodeData = (NodeData*)node->appData.nodeData; Node* source_node; source_node = MAPPING_GetNodePtrFromHash(GlobalNodeData::nodeHash, source_node_id); nodeData->source_nodeData = (NodeData*)source_node->appData.nodeData; NodeData* source_nodeData; source_nodeData = nodeData->source_nodeData; switch(msg->eventType) { // タイマーメッセージ case MSG_APP_TimerExpired: // タイマーメッセージ { AppTimer* timer; timer = (AppTimer *)MESSAGE_ReturnInfo(msg); // timer messageの種類によって処理を分ける switch(timer->type) { case APP_TIMER_SEND_PKT: { printf("この表示がなければ、APP_TIMER_SEND_PKTはビデオストリーミング確定 "); clocktype send_delayTime; uint32_t chunk_num; map<uint32_t, clocktype>::iterator it; chunk_num = 0; do { chunk_num++; it = source_nodeData->dataGenerateTime_map.find(chunk_num); if(it == source_nodeData->dataGenerateTime_map.end()) { ERROR_ReportError("dataGenerateTime_map error\n"); } send_delayTime = it->second - node->getNodeTime(); } while(send_delayTime < 0); nodeData->set_lastChunkNum(msg_videoData_name + node->nodeId % 3, chunk_num - 1); nodeData->set_lastRecvMsg(msg_videoData_name + node->nodeId % 3, chunk_num - 1); CcnMsg* ccnMsg; do { ccnMsg = new CcnMsg(); ccnMsg->ccn_method = nodeData->ccn_method; ccnMsg->resent_times = 0; ccnMsg->msg_type = Interest; ccnMsg->msg_name = msg_videoData_name + node->nodeId % 3; ccnMsg->msg_chunk_num = nodeData->return_lastChunkNum(ccnMsg) + 1; ccnMsg->EncodeFullNameMsg(); ccnMsg->sender_node_id = node->nodeId; ccnMsg->source_node_id = source_node_id; ccnMsg->payload_length = 30; ccnMsg->hops_limit = 20; ccnMsg->interest_genTime = node->getNodeTime(); ccnMsg->content_type = VideoData; ccnMsg->end_chunk_num = UINT32_MAX; it = source_nodeData->dataGenerateTime_map.find(ccnMsg->msg_chunk_num); ccnMsg->data_genTime = it->second; nodeData->set_lastChunkNum(ccnMsg); nodeData->fibSend(node, ccnMsg); if(ccnMsg->ccn_method == DEFAULT) { nodeData->make_reTransMsg(node, ccnMsg); nodeData->set_lastRecvMsg(msg_videoData_name + node->nodeId % 3, UINT32_MAX - 3); nodeData->reqMapInput(node, ccnMsg); } else if(ccnMsg->ccn_method == DEFAULT_FAST) { nodeData->make_reTransMsg(node, ccnMsg); nodeData->set_lastRecvMsg(msg_videoData_name + node->nodeId % 3, UINT32_MAX - 3); nodeData->reqMapInput(node, ccnMsg); } else if(ccnMsg->ccn_method == PRIOR) { nodeData->reqMapInput_PRIOR(node, ccnMsg); break; } else if(ccnMsg->ccn_method == PROPOSAL) { nodeData->set_lastRecvMsg(msg_videoData_name + node->nodeId % 3, UINT32_MAX - 1); nodeData->reqMapInput(node, ccnMsg); //printf("[node%d][msg%d] sendInterest\n", node->nodeId, ccnMsg->msg_name); break; } } while(nodeData->set_windowSize(node, ccnMsg)); break; } // APP_TIMER_SEND_PKT case APP_TIMER_DATA_SEND_PKT: { printf("この表示がなければ、APP_TIMER_DATA_SEND_PKTはいらない子。てかどうやってここはいるんだ… "); CcnMsg* ccnMsg; for(int i = 0; i < 3; i++) { ccnMsg = new CcnMsg(); ccnMsg->resent_times = 0; ccnMsg->ccn_method = PRIOR; ccnMsg->msg_type = Data; ccnMsg->msg_name = msg_videoData_name + i; ccnMsg->msg_chunk_num = nodeData->return_lastChunkNum(ccnMsg) + 1; ccnMsg->EncodeFullNameMsg(); ccnMsg->sender_node_id = node->nodeId; ccnMsg->source_node_id = source_node_id; ccnMsg->payload_length = nodeData->tcp_mss; ccnMsg->hops_limit = 20; ccnMsg->interest_genTime = node->getNodeTime(); ccnMsg->content_type = VideoData; ccnMsg->end_chunk_num = UINT32_MAX; map<uint32_t, clocktype>::iterator it; it = nodeData->dataGenerateTime_map.find(ccnMsg->msg_chunk_num); ccnMsg->data_genTime = it->second; clocktype send_delayTime; send_delayTime = ccnMsg->data_genTime - node->getNodeTime(); if(send_delayTime < 0) send_delayTime = 0; nodeData->set_lastChunkNum(ccnMsg); nodeData->pitSend_PRIOR(node, nodeData->NewCcnMsg(ccnMsg), send_delayTime); APP_SetTimer(node, APP_CCN_CLIENT, 0, APP_CCN_LISTEN_PORT, APP_TIMER_DATA_SEND_PKT, send_delayTime); } delete ccnMsg; break; } // APP_TIMER_DATA_SEND_PKT case APP_TIMER_fakeINTEREST_SEND_PKT: { printf("この表示がなければ、APP_TIMER_fakeINTEREST_SEND_PKTはいらない子。てかここどうやって入るんだ… "); // 動画の配信分をすべて出す CcnMsg* ccnMsg; for(int i = 0; i < 3; i++) { ccnMsg = new CcnMsg(); ccnMsg->resent_times = 0; ccnMsg->ccn_method = PROPOSAL; ccnMsg->msg_type = fakeInterest; ccnMsg->msg_name = msg_videoData_name + i; ccnMsg->msg_chunk_num = nodeData->return_lastChunkNum(ccnMsg) + 1; ccnMsg->EncodeFullNameMsg(); ccnMsg->sender_node_id = node->nodeId; ccnMsg->source_node_id = source_node_id; ccnMsg->payload_length = nodeData->tcp_mss; ccnMsg->hops_limit = 20; ccnMsg->interest_genTime = node->getNodeTime(); ccnMsg->content_type = VideoData; ccnMsg->end_chunk_num = UINT32_MAX; map<uint32_t, clocktype>::iterator it; it = nodeData->dataGenerateTime_map.find(ccnMsg->msg_chunk_num); ccnMsg->data_genTime = it->second; clocktype send_delayTime; send_delayTime = ccnMsg->data_genTime - node->getNodeTime(); if(send_delayTime < 0) send_delayTime = 0; nodeData->set_lastChunkNum(ccnMsg); nodeData->fibModSend(node, nodeData->NewCcnMsg(ccnMsg), send_delayTime); APP_SetTimer(node, APP_CCN_CLIENT, 0, APP_CCN_LISTEN_PORT, APP_TIMER_fakeINTEREST_SEND_PKT, send_delayTime); } delete ccnMsg; break; } // APP_TIMER_fakeINTEREST_SEND_PKT case APP_TIMER_REGULAR_SEND_PKT: { //printf("この表示は 予想ではたくさんでる。APP_TIMER_REGULAR_SEND_PKT\n"); clocktype send_delayTime; uint32_t chunk_num; map<uint32_t, clocktype>::iterator it; uint32_t next_msgName = global_node_data->return_MsgName(); uint32_t end_chunk_num = next_msgName % 40 + 10; CcnMsg* ccnMsg; do { ccnMsg = new CcnMsg(); ccnMsg->resent_times = 0; ccnMsg->ccn_method = DEFAULT; ccnMsg->msg_type = Interest; ccnMsg->msg_name = next_msgName; ccnMsg->msg_chunk_num = nodeData->return_lastChunkNum(ccnMsg) + 1; ccnMsg->EncodeFullNameMsg(); ccnMsg->sender_node_id = node->nodeId; ccnMsg->source_node_id = source_node_id; ccnMsg->payload_length = 30; ccnMsg->hops_limit = 20; ccnMsg->content_type = CommonData; ccnMsg->end_chunk_num = end_chunk_num; ccnMsg->interest_genTime = node->getNodeTime(); ccnMsg->data_genTime = node->getNodeTime(); nodeData->set_lastChunkNum(ccnMsg); nodeData->reqMapInput(node, ccnMsg); nodeData->fibSend_DEFAULT(node, ccnMsg); nodeData->make_reTransMsg(node, ccnMsg); if(ccnMsg->msg_chunk_num > ccnMsg->end_chunk_num) break; } while(nodeData->set_windowSize(node, ccnMsg)); APP_SetTimer(node, APP_CCN_CLIENT, 0, APP_CCN_LISTEN_PORT, APP_TIMER_REGULAR_SEND_PKT, nodeData->commonPacketGenerateTime); break; } // APP_TIMER_REGULAR_SEND_PKT default: ERROR_ReportError("Undefined timer type\n"); } break; } // MSG_APP_TimerExpired case MSG_APP_FromTransOpenResult: // TCPでactive open処理が完了 { // データ送信処理 // 送信データの操作は出来る限りHostの処理にしておく // ここでは送信処理だけ TransportToAppOpenResult *openResult; openResult = (TransportToAppOpenResult*) MESSAGE_ReturnInfo(msg); //TCPコネクションが不成立時(失敗) if (openResult->connectionId < 0) { char buf[MAX_STRING_LENGTH]; ctoa(node->getNodeTime(),buf); node->appData.numAppTcpFailure++; break; } // バッファーからccnメッセージを取り出し CcnMsg* ccnMsg; ccnMsg = nodeData->BufferRetrieve_Client(node, msg); nodeData->StatisticalInfo_sendCcnMsg(node, ccnMsg); ccnMsg->App_TcpSendCcnMsg(node, msg); break; } // MSG_APP_FromTransOpenResult case MSG_APP_FromTransDataSent: // TCPでデータ転送終了 TransportToAppDataSent *openResult; openResult = (TransportToAppDataSent*) MESSAGE_ReturnInfo(msg); APP_TcpCloseConnection(node, openResult->connectionId); break; case MSG_APP_FromTransCloseResult: break; default: ERROR_ReportError("msg->eventType error: undefined eventType\n"); } MESSAGE_Free(node, msg); }
static void MacTdmaInitializeTimer(Node* node, MacDataTdma* tdma) { int i; if (DEBUG) { printf("partition %d, node %d in tdmainittimer\n", node->partitionData->partitionId, node->nodeId); fflush(stdout); } int initialStatus = (int)tdma->frameDescriptor[0]; if (initialStatus == TDMA_STATUS_TX) { i = 0; } else { for (i = 0; i < tdma->numSlotsPerFrame; i++) { if ((int)tdma->frameDescriptor[i] != initialStatus) { break; } } } if (i == tdma->numSlotsPerFrame) { #ifdef PARALLEL //Parallel // This node has no transmit slots PARALLEL_SetLookaheadHandleEOT(node, tdma->lookaheadHandle, CLOCKTYPE_MAX); #endif //endParallel // // Schedule no timer // tdma->currentStatus = initialStatus; if (DEBUG) { printf("No initial timer sent for node %d. " "Initial Status matches all slots in frame\n", node->nodeId); } } else { Message *timerMsg; clocktype delay; delay = (tdma->slotDuration + tdma->guardTime) * i + tdma->interFrameTime; timerMsg = MESSAGE_Alloc(node, MAC_LAYER, 0, MSG_MAC_TimerExpired); tdma->currentStatus = initialStatus; tdma->currentSlotId = 0; tdma->nextStatus = tdma->frameDescriptor[i]; tdma->nextSlotId = i; tdma->timerMsg = timerMsg; tdma->timerExpirationTime = delay + node->getNodeTime(); MESSAGE_SetInstanceId(timerMsg, (short)tdma->myMacData->interfaceIndex); MESSAGE_Send(node, timerMsg, delay); #ifdef PARALLEL //Parallel PARALLEL_SetLookaheadHandleEOT(node, tdma->lookaheadHandle, node->getNodeTime() + delay + 5000); #endif //endParallel if (DEBUG) { char clockStr[MAX_CLOCK_STRING_LENGTH]; char clockStr2[MAX_CLOCK_STRING_LENGTH]; ctoa(delay, clockStr); ctoa(node->getNodeTime(), clockStr2); printf("Initial timer sent for node [%d] with a delay of %s at %s\n\n", node->nodeId, clockStr, clockStr2); printf("Slot duration, guard time, and interframe time are %" TYPES_64BITFMT "d, %" TYPES_64BITFMT "d, and %" TYPES_64BITFMT "d \n", tdma->slotDuration, tdma->guardTime, tdma->interFrameTime); fflush(stdout); } } }
/* * FUNCTION MacTdmaLayer * PURPOSE Models the behaviour of the MAC layer with the TDMA protocol * on receiving the message enclosed in msg. * * Parameters: * node: node which received the message * msg: message received by the layer * * Return: None. * * Assumption: Regardless of the slot duration time, the basic slot unit * is a packet. This need not be the case. * TBD: Use only slot duration length as determinant of data * to be sent, including fragmentation */ void MacTdmaLayer(Node* node, int interfaceIndex, Message* msg) { MacDataTdma* tdma = (MacDataTdma*)node->macData[interfaceIndex]->macVar; int previousStatus = tdma->currentStatus; BOOL phyIsListening; // // Currently, the only message this function receives is // "MSG_MAC_TimerExpired" message sent by this protocol itself. // ERROR_Assert(msg->eventType == MSG_MAC_TimerExpired, "TDMA: Only acceptable event type is timer expiration"); ERROR_Assert(node->getNodeTime() == tdma->timerExpirationTime, "TDMA: Simulation time differs from expected timer expiration"); tdma->currentStatus = tdma->nextStatus; tdma->currentSlotId = tdma->nextSlotId; assert((tdma->currentStatus != previousStatus) || (tdma->currentStatus == TDMA_STATUS_TX && previousStatus == TDMA_STATUS_TX)); phyIsListening = PHY_IsListeningToChannel( node, tdma->myMacData->phyNumber, tdma->channelIndex); switch (tdma->currentStatus) { case TDMA_STATUS_IDLE: { if (phyIsListening) { PHY_StopListeningToChannel( node, tdma->myMacData->phyNumber, tdma->channelIndex); } break; } case TDMA_STATUS_RX: { if (DEBUG) { //printf("Node %ld in RECEIVE slot %d\n", // node->nodeId, tdma->currentSlotId); } if (!phyIsListening) { PHY_StartListeningToChannel( node, tdma->myMacData->phyNumber, tdma->channelIndex); } break; } case TDMA_STATUS_TX: { Message *msg; MacHWAddress nextHopHWAddr; int networkType; TosType priority; TdmaHeader *hdr; clocktype transmissionDelay; if (DEBUG) { //printf("Node %ld in TRANSMIT slot %d\n", // node->nodeId, tdma->currentSlotId); } if (previousStatus == TDMA_STATUS_TX) { // Previous transmission should have ended at this point assert(PHY_GetStatus(node, tdma->myMacData->phyNumber) != PHY_TRANSMITTING); } if (MAC_OutputQueueIsEmpty(node, tdma->myMacData->interfaceIndex)) { tdma->stats.numTxSlotsMissed++; break; } // // Dequeue packet from the network queue. // MAC_OutputQueueDequeuePacket( node, tdma->myMacData->interfaceIndex, &msg, &nextHopHWAddr, &networkType, &priority); assert(msg != NULL); if (DEBUG) { char clockStr[100]; //char buf[MAX_STRING_LENGTH]; ctoa(node->getNodeTime(), clockStr); printf("at %s: ***TRANSMITTING*** node [%d] to node",clockStr, node->nodeId); MAC_PrintHWAddr(&nextHopHWAddr); printf(" during SLOT [%d]\n",tdma->currentSlotId); fflush(stdout); } /* * Assign other fields to packet to be sent * to phy layer. */ MESSAGE_AddHeader(node, msg, sizeof(TdmaHeader), TRACE_TDMA); hdr = (TdmaHeader*)msg->packet; #ifdef CYBER_LIB if (NetworkIpGetUnicastRoutingProtocolType(node, interfaceIndex) == ROUTING_PROTOCOL_ANODR) { // Anonymous TDMA for ANODR memset(&hdr->sourceAddr, 0xff, sizeof(Mac802Address)); memset(&hdr->destAddr, 0xff, sizeof(Mac802Address)); } else { #endif // CYBER_LIB ConvertVariableHWAddressTo802Address(node, &nextHopHWAddr, &hdr->destAddr); ConvertVariableHWAddressTo802Address( node, &node->macData[tdma->myMacData->interfaceIndex]->macHWAddr, &hdr->sourceAddr); #ifdef CYBER_LIB } #endif // CYBER_LIB hdr->priority = priority; transmissionDelay = PHY_GetTransmissionDelay(node, tdma->myMacData->phyNumber, MESSAGE_ReturnPacketSize(msg)); ERROR_Assert(transmissionDelay <= tdma->slotDuration, "TDMA: Message size is greater than slot duration"); PHY_StartTransmittingSignal( node, tdma->myMacData->phyNumber, msg, FALSE, 0); if (MAC_IsBroadcastMac802Address(&hdr->destAddr)) { tdma->stats.pktsSentBroadcast++; } else { tdma->stats.pktsSentUnicast++; } break; } default: { abort(); } } MacTdmaUpdateTimer(node, tdma); }
void MacTdmaReceivePacketFromPhy( Node* node, MacDataTdma* tdma, Message* msg) { if (tdma->currentStatus == TDMA_STATUS_TX || tdma->currentStatus == TDMA_STATUS_IDLE) { MESSAGE_Free(node, msg); return; }//if// if (DEBUG) { //printf("**PRE-RECEIVE** About to check " // "if Receive mode is on at node [%d]\n", node->nodeId); } switch (tdma->currentStatus) { case TDMA_STATUS_RX: { int interfaceIndex = tdma->myMacData->interfaceIndex; TdmaHeader *hdr = (TdmaHeader*)msg->packet; if (DEBUG) { char clockStr[MAX_CLOCK_STRING_LENGTH]; ctoa(node->getNodeTime(), clockStr); printf ("***RECEIVE*** Node [%d] finished RECEIVING packet at slot [%d]\n", node->nodeId, tdma->currentSlotId); } MacHWAddress macHWAddress; Convert802AddressToVariableHWAddress(node, &macHWAddress, &hdr->destAddr); #ifdef CYBER_LIB MacHWAddress srcHWAddress; Convert802AddressToVariableHWAddress(node, &srcHWAddress, &hdr->sourceAddr); TosType priority = hdr->priority; BOOL macMatched = FALSE; if (msg->headerProtocols[msg->numberOfHeaders-1] == TRACE_TDMA) { macMatched = TRUE; MESSAGE_RemoveHeader(node, msg, sizeof(TdmaHeader), TRACE_TDMA); } if (macMatched && IsMyAnodrDataPacket(node, msg)) { srcHWAddress = GetBroadCastAddress(node, interfaceIndex); tdma->stats.pktsGotUnicast++; MAC_HandOffSuccessfullyReceivedPacket( node, interfaceIndex, msg, &srcHWAddress); ERROR_Assert(tdma->currentReceiveSlotId == tdma->currentSlotId, "TDMA: Finished receiving on a different slot"); } else { // Not anonymous TDMA packet for ANODR, restore TDMA header if (macMatched) { MESSAGE_AddHeader(node, msg, sizeof(TdmaHeader), TRACE_TDMA); hdr = (TdmaHeader*)msg->packet; ConvertVariableHWAddressTo802Address(node, &macHWAddress, &hdr->destAddr); ConvertVariableHWAddressTo802Address(node, &srcHWAddress, &hdr->sourceAddr); hdr->priority = priority; } #endif // CYBER_LIB if (MAC_IsMyAddress(node ,&macHWAddress)) { tdma->stats.pktsGotUnicast++; } else if (MAC_IsBroadcastMac802Address(&hdr->destAddr)) { tdma->stats.pktsGotBroadcast++; } if (MAC_IsMyAddress(node, &macHWAddress) || MAC_IsBroadcastHWAddress(&macHWAddress)) { BOOL macMatched = FALSE; if (msg->headerProtocols[msg->numberOfHeaders-1] == TRACE_TDMA) { macMatched = TRUE; MESSAGE_RemoveHeader(node, msg, sizeof(TdmaHeader), TRACE_TDMA); } if (macMatched) { MacHWAddress src_macHWAddr; Convert802AddressToVariableHWAddress(node , &src_macHWAddr, &hdr->sourceAddr); MAC_HandOffSuccessfullyReceivedPacket(node, tdma->myMacData->interfaceIndex, msg, &src_macHWAddr); ERROR_Assert(tdma->currentReceiveSlotId == tdma->currentSlotId, "TDMA: Finished receiving on a different slot"); } } else { if (node->macData[interfaceIndex]->promiscuousMode) { MacTdmaHandlePromiscuousMode(node, tdma, msg, hdr->sourceAddr, hdr->destAddr); } MESSAGE_Free(node, msg); } #ifdef CYBER_LIB } #endif // CYBER_LIB break; } default: assert(FALSE); abort(); }//switch// }
/* print a char in 16 mode */ void print_char_hex(char cc) { char c_str[3]; ctoa(cc,c_str); put_s(c_str); }
/* * NAME: ZigbeeAppServerPrintStats. * PURPOSE: Prints statistics of a ZigbeeAppServer session. * PARAMETERS: node - pointer to this node. * serverPtr - pointer to the zigbeeApp server data structure. * RETURN: none. */ void ZigbeeAppServerPrintStats(Node* node, AppDataZigbeeappServer* serverPtr) { clocktype throughput = 0; clocktype avgJitter = 0; char addrStr[MAX_STRING_LENGTH]; char jitterStr[MAX_STRING_LENGTH]; char clockStr[MAX_STRING_LENGTH]; char startStr[MAX_STRING_LENGTH]; char closeStr[MAX_STRING_LENGTH]; char sessionStatusStr[MAX_STRING_LENGTH]; char throughputStr[MAX_STRING_LENGTH]; char buf[MAX_STRING_LENGTH]; char buf1[MAX_STRING_LENGTH]; TIME_PrintClockInSecond(serverPtr->sessionStart, startStr, node); TIME_PrintClockInSecond(serverPtr->sessionLastReceived, closeStr, node); if (serverPtr->sessionIsClosed == FALSE) { serverPtr->sessionFinish = node->getNodeTime(); sprintf(sessionStatusStr, "Not closed"); } else { sprintf(sessionStatusStr, "Closed"); } if (serverPtr->numPktsRecvd == 0) { TIME_PrintClockInSecond(0, clockStr); } else { TIME_PrintClockInSecond( serverPtr->totalEndToEndDelay / serverPtr->numPktsRecvd, clockStr); } if (serverPtr->sessionFinish <= serverPtr->sessionStart) { throughput = 0; } else { throughput = (clocktype) ((serverPtr->numBytesRecvd * 8.0 * SECOND) / (serverPtr->sessionFinish - serverPtr->sessionStart)); } ctoa(throughput, throughputStr); IO_ConvertIpAddressToString(&serverPtr->remoteAddr, addrStr); sprintf(buf, "Client address = %s", addrStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); sprintf(buf, "First Packet Received at (s) = %s", startStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); sprintf(buf, "Last Packet Received at (s) = %s", closeStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); sprintf(buf, "Session Status = %s", sessionStatusStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); ctoa((Int64) serverPtr->numBytesRecvd, buf1); sprintf(buf, "Total Bytes Received = %s", buf1); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); sprintf(buf, "Total Packets Received = %u", serverPtr->numPktsRecvd); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); sprintf(buf, "Throughput (bits/s) = %s", throughputStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); sprintf(buf, "Average End-to-End Delay (s) = %s", clockStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); // Jitter can only be measured after receiving two packets. if (serverPtr->numPktsRecvd - 1 <= 0) { avgJitter = 0; } else { avgJitter = serverPtr->totalJitter / (serverPtr->numPktsRecvd-1) ; } TIME_PrintClockInSecond(avgJitter, jitterStr); sprintf(buf, "Average Jitter (s) = %s", jitterStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Server", ANY_DEST, serverPtr->sourcePort, buf); }
/* * NAME: ZigbeeAppClientPrintStats. * PURPOSE: Prints statistics of a zigbeeAppClient session. * PARAMETERS: node - pointer to the this node. * clientPtr - pointer to the zigbeeApp client data structure. * RETURN: none. */ void ZigbeeAppClientPrintStats(Node* node, AppDataZigbeeappClient* clientPtr) { clocktype throughput = 0; char addrStr[MAX_STRING_LENGTH]; char startStr[MAX_STRING_LENGTH]; char closeStr[MAX_STRING_LENGTH]; char sessionStatusStr[MAX_STRING_LENGTH]; char throughputStr[MAX_STRING_LENGTH]; char buf[MAX_STRING_LENGTH]; char buf1[MAX_STRING_LENGTH]; TIME_PrintClockInSecond(clientPtr->sessionStart, startStr, node); TIME_PrintClockInSecond(clientPtr->sessionLastSent, closeStr, node); if (clientPtr->sessionIsClosed == FALSE) { clientPtr->sessionFinish = node->getNodeTime(); sprintf(sessionStatusStr, "Not closed"); } else { sprintf(sessionStatusStr, "Closed"); } if (clientPtr->sessionFinish <= clientPtr->sessionStart) { throughput = 0; } else { throughput = (clocktype) ((clientPtr->numBytesSent * 8.0 * SECOND) / (clientPtr->sessionFinish - clientPtr->sessionStart)); } ctoa(throughput, throughputStr); if (clientPtr->remoteAddr.networkType == NETWORK_ATM) { const LogicalSubnet* dstLogicalSubnet = AtmGetLogicalSubnetFromNodeId( node, clientPtr->remoteAddr.interfaceAddr.atm.ESI_pt1, 0); IO_ConvertIpAddressToString( dstLogicalSubnet->ipAddress, addrStr); } else { IO_ConvertIpAddressToString(&clientPtr->remoteAddr, addrStr); } sprintf(buf, "Server Address = %s", addrStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Client", ANY_DEST, clientPtr->sourcePort, buf); sprintf(buf, "First Packet Sent at (s) = %s", startStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Client", ANY_DEST, clientPtr->sourcePort, buf); sprintf(buf, "Last Packet Sent at (s) = %s", closeStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Client", ANY_DEST, clientPtr->sourcePort, buf); sprintf(buf, "Session Status = %s", sessionStatusStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Client", ANY_DEST, clientPtr->sourcePort, buf); ctoa((Int64) clientPtr->numBytesSent, buf1); sprintf(buf, "Total Bytes Sent = %s", buf1); IO_PrintStat(node, "Application", "ZIGBEEAPP Client", ANY_DEST, clientPtr->sourcePort, buf); sprintf(buf, "Total Packets Sent = %u", clientPtr->numPktsSent); IO_PrintStat(node, "Application", "ZIGBEEAPP Client", ANY_DEST, clientPtr->sourcePort, buf); sprintf(buf, "Throughput (bits/s) = %s", throughputStr); IO_PrintStat(node, "Application", "ZIGBEEAPP Client", ANY_DEST, clientPtr->sourcePort, buf); }
static void enterAlohaReceiveACK(Node* node, AlohaData* dataPtr, Message *msg) { //UserCodeStartReceiveACK AlohaHeader *hdr = (AlohaHeader *) msg->packet; Message *sendTimer; char clockStr[100]; char delayStr[100]; clocktype jitter = (clocktype) (RANDOM_erand(dataPtr->seed) * ALOHA_DELAY_TIMER); if (dataPtr->retransmitTimer != NULL) { MESSAGE_CancelSelfMsg(node, dataPtr->retransmitTimer); dataPtr->retransmitTimer = NULL; } if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: ACK Received\n", node->nodeId, clockStr); } if ((dataPtr->mode == ALOHA_EXPECTING_ACK) && MAC_IsIdenticalMac802Address(&dataPtr->ACKAddress, &hdr->sourceAddr)) { if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: expecting source", node->nodeId,clockStr); MAC_PrintMacAddr(&dataPtr->ACKAddress); printf("actual source -"); MAC_PrintMacAddr(&hdr->sourceAddr); } //assert(dataPtr->ACKAddress == lastHopAddress); memset(&dataPtr->ACKAddress, 255, sizeof(Mac802Address)); dataPtr->numTimeouts = 0; if (dataPtr->currentFrame != NULL) { MESSAGE_Free(node, dataPtr->currentFrame); dataPtr->currentFrame = NULL; } //Initiate sending again. dataPtr->mode = ALOHA_YIELD; sendTimer = MESSAGE_Alloc(node, MAC_LAYER, 0, MSG_MAC_CheckTransmit); MESSAGE_SetInstanceId(sendTimer, (short)dataPtr->myMacData->interfaceIndex); if (DEBUG) { ctoa(ALOHA_NEXT_PKT_DELAY, delayStr); ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: Initiating Sending after received ACK, delay - %s\n", node->nodeId, clockStr, delayStr); } //MESSAGE_Free(node, msg); MESSAGE_Send(node, sendTimer, ALOHA_NEXT_PKT_DELAY + jitter); } else { if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: Not Expecting ACK from this address!\n", node->nodeId, clockStr); } } //UserCodeEndReceiveACK dataPtr->state = ALOHA_IDLE_STATE; enterAlohaIdleState(node, dataPtr, msg); }
static void enterAlohaReceivePacket(Node* node, AlohaData* dataPtr, Message *msg) { //UserCodeStartReceivePacket char clockStr[100]; AlohaHeader *hdr = (AlohaHeader *) MESSAGE_ReturnPacket(msg); AlohaHeader* ackHdr = (AlohaHeader *) MEM_malloc(sizeof(AlohaHeader)); MacHWAddress sourceHWAddr; Message* ACK = MESSAGE_Alloc(node, 0, 0, 0); MESSAGE_PacketAlloc(node, ACK, sizeof(AlohaHeader), TRACE_ALOHA); MAC_CopyMac802Address(&ackHdr->sourceAddr, &hdr->destAddr); MAC_CopyMac802Address(&ackHdr->destAddr, &hdr->sourceAddr); ackHdr->ACK = TRUE; memcpy(MESSAGE_ReturnPacket(ACK), ackHdr, sizeof(AlohaHeader)); MEM_free(ackHdr); if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s] : ReceivedPacket : Message Received, sending ACK\n", node->nodeId, clockStr); printf("ALOHA [%d] [%s] : ReceivedPacket : ACK Parameters , From ", node->nodeId, clockStr); MAC_PrintMacAddr(&hdr->destAddr); printf("To"); MAC_PrintMacAddr(&hdr->sourceAddr); printf("\n"); } dataPtr->txMode = ALOHA_TX_ACK; PHY_StartTransmittingSignal( node, dataPtr->myMacData->phyNumber, ACK, FALSE, 0); MESSAGE_RemoveHeader(node, msg, sizeof(AlohaHeader), TRACE_ALOHA); if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: ReceivedPacket : Sending packet to upper Layers\n", node->nodeId, clockStr); } Convert802AddressToVariableHWAddress(node, &sourceHWAddr, &hdr->sourceAddr); MAC_HandOffSuccessfullyReceivedPacket( node, dataPtr->myMacData->interfaceIndex, msg, &sourceHWAddr); dataPtr->stats.pktsReceived++; //UserCodeEndReceivePacket dataPtr->state = ALOHA_IDLE_STATE; enterAlohaIdleState(node, dataPtr, msg); }
static void enterAlohaRetransmit(Node* node, AlohaData* dataPtr, Message *msg) { //UserCodeStartRetransmit Message* retransmitPkt; char clockStr[100]; if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: Retransmitting Packet\n", node->nodeId, clockStr); } if (dataPtr->mode != ALOHA_EXPECTING_ACK) { if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: Retransmitting Failed - not exepcting ACK\n", node->nodeId, clockStr); } } else { assert(dataPtr->currentFrame); retransmitPkt = MESSAGE_Duplicate(node, dataPtr->currentFrame); dataPtr->retransmitTimer = NULL; //ctoa(node->getNodeTime(), clockStr); //printf("ALOHA [%d] [%s]: Retransmitting - Checking Timeouts\n", node->nodeId, clockStr); if (dataPtr->numTimeouts >= ALOHA_MAX_TIMEOUTS) { if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("Aloha [%d] [%s]: Retransmitting Packet Failed - too many timeouts\n", node->nodeId, clockStr); } dataPtr->stats.pktsDropped++; dataPtr->numTimeouts = 0; MESSAGE_Free(node, dataPtr->currentFrame); dataPtr->currentFrame = NULL; MESSAGE_Free(node, retransmitPkt); retransmitPkt = NULL; dataPtr->mode = ALOHA_IDLE; dataPtr->txMode = ALOHA_TX_IDLE; memset(&dataPtr->ACKAddress, 255, MAC_ADDRESS_LENGTH_IN_BYTE); } else { //printf("ALOHA [%d] [%s]: Retransmitting - Timeouts Passed\n", node->nodeId, clockStr); AlohaHeader* hdr = (AlohaHeader*) MESSAGE_ReturnPacket(retransmitPkt); if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] : ReTransmit , sending packet from ",node->nodeId); MAC_PrintMacAddr(&hdr->sourceAddr); printf(" to "); MAC_PrintMacAddr(&hdr->destAddr); printf("\n"); } dataPtr->txMode = ALOHA_TX_DATA; assert(dataPtr->mode == ALOHA_EXPECTING_ACK); PHY_StartTransmittingSignal(node, dataPtr->myMacData->phyNumber, retransmitPkt, FALSE, 0); if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("Aloha [%d] [%s]: Retransmitting Packet SUCCESFULL - Packet Sent\n", node->nodeId, clockStr); } dataPtr->numTimeouts++; } } //UserCodeEndRetransmit dataPtr->state = ALOHA_IDLE_STATE; enterAlohaIdleState(node, dataPtr, msg); }
void MacAlohaReceivePhyStatusChangeNotification(Node* node, AlohaData* dataPtr, PhyStatusType oldPhyStatus, PhyStatusType newPhyStatus) { //UserCodeStartReceivePhyStatusChangeNotification BOOL isQueueEmpty = MAC_OutputQueueIsEmpty(node, dataPtr->myMacData->interfaceIndex); clocktype retransmitDelay; Message *ackTimer; clocktype bandwith = dataPtr->myMacData->bandwidth; char clockStr[100], jitStr[100], retxDelayStr[100]; clocktype jitter = (clocktype) (RANDOM_erand(dataPtr->seed) * ALOHA_DELAY_TIMER); if (oldPhyStatus == PHY_TRANSMITTING) { assert(newPhyStatus != PHY_TRANSMITTING); if (dataPtr->txMode == ALOHA_TX_DATA) { dataPtr->txMode = ALOHA_TX_IDLE; dataPtr->mode = ALOHA_EXPECTING_ACK; if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("ALOHA [%d] [%s]: ReceivePhyStatus - Finished Sending Packet, Now Expecting ACK\n", node->nodeId, clockStr); } ackTimer = MESSAGE_Alloc(node, MAC_LAYER, 0, MSG_MAC_TimerExpired); dataPtr->retransmitTimer = ackTimer; retransmitDelay = (clocktype) ((((2 * ALOHA_PROPAGATION_DELAY) + (ALOHA_ACK_SIZE / bandwith)) * pow(2.0,dataPtr->numTimeouts)) + jitter); if (DEBUG) { ctoa(node->getNodeTime(), clockStr); ctoa(jitter, jitStr); printf("JITTER - %s\n", jitStr); ctoa(retransmitDelay, retxDelayStr); printf("ALOHA [%d] [%s]: ReceivePhyStatus - Retransmit delay - %s\n", node->nodeId, clockStr, retxDelayStr); } MESSAGE_SetInstanceId(ackTimer, (short)dataPtr->myMacData->interfaceIndex); MESSAGE_Send(node, ackTimer, retransmitDelay); } else { if (DEBUG) { ctoa(node->getNodeTime(), clockStr); printf("Aloha [%d] [%s]: ReceivePhyStatus - Finished Sending ACK, Now Idle ACK\n", node->nodeId, clockStr); } assert (dataPtr->txMode == ALOHA_TX_ACK); //assert (dataPtr->mode == ALOHA_IDLE); dataPtr->txMode = ALOHA_TX_IDLE; } } //UserCodeEndReceivePhyStatusChangeNotification if ((oldPhyStatus == PHY_TRANSMITTING) && !isQueueEmpty && (dataPtr->mode == ALOHA_IDLE)) { dataPtr->state = ALOHA_TRANSMIT; enterAlohaTransmit(node, dataPtr, NULL); return; } else { dataPtr->state = ALOHA_IDLE_STATE; enterAlohaIdleState(node, dataPtr, NULL); return; } }
//------------------------------------------------------------------------- // FUNCTION : NdpPrintNeighborTable() // // PURPOSE : Printing the neighbor table of the node // // PARAMETERS : node - Node which is printing neighbor table // ndpData - pointer to NdpData // // RETURN VALUE : None //------------------------------------------------------------------------- static void NdpPrintNeighborTable(Node* node, NdpData* ndpData) { static int j = 0; int i = 0; FILE * fd = NULL; char time[MAX_STRING_LENGTH] = {0}; if (j == 0) { j++; fd = fopen("neighborTable.out","w"); } else { fd = fopen("neighborTable.out","a"); } TIME_PrintClockInSecond(node->getNodeTime(), time); fprintf(fd,"------------------------------------------------------------" "-------------\n" "Simulation Time = %16s\n" "NEIGHBOR TABLE OF NODE : %u numEntry: %10u maxEntry: %10u\n" "------------------------------------------------------------" "-------------\n" "%16s %16s %17s %16s %3s\n" "------------------------------------------------------------" "-------------\n", time, node->nodeId, ndpData->ndpNeighborTable.numEntry, ndpData->ndpNeighborTable.maxEntry, "Neighbor_Address", "SubnetMask_Addr", "Hello_Interval(S)", "Time_Last_Hard", "NDX"); for (i = 0; i < (signed)ndpData->ndpNeighborTable.numEntry; i++) { char ipAddressStr[MAX_STRING_LENGTH] = {0}; char subnetIpAddressStr[MAX_STRING_LENGTH] = {0}; char clockStr[MAX_STRING_LENGTH] = {0}; IO_ConvertIpAddressToString(ndpData->ndpNeighborTable. neighborTable[i].source, ipAddressStr); IO_ConvertIpAddressToString(ndpData->ndpNeighborTable. neighborTable[i].subnetMask, subnetIpAddressStr); ctoa(ndpData->ndpNeighborTable.neighborTable[i].lastHard, clockStr); fprintf(fd,"%16s %16s %17u %16s %3d\n", ipAddressStr, subnetIpAddressStr, ndpData->ndpNeighborTable.neighborTable[i]. neighborHelloInterval, clockStr, ndpData->ndpNeighborTable.neighborTable[i]. interfaceIndex); } fprintf(fd,"------------------------------------------------------------" "-------------\n\n\n"); fclose(fd); }