📄 tsma.pc
字号:
/* * GloMoSim is COPYRIGHTED software. Release 2.02 of GloMoSim is available * at no cost to educational users only. * * Commercial use of this software requires a separate license. No cost, * evaluation licenses are available for such purposes; please contact * info@scalable-networks.com * * By obtaining copies of this and any other files that comprise GloMoSim2.02, * you, the Licensee, agree to abide by the following conditions and * understandings with respect to the copyrighted software: * * 1.Permission to use, copy, and modify this software and its documentation * for education and non-commercial research purposes only is hereby granted * to Licensee, provided that the copyright notice, the original author's * names and unit identification, and this permission notice appear on all * such copies, and that no charge be made for such copies. Any entity * desiring permission to use this software for any commercial or * non-educational research purposes should contact: * * Professor Rajive Bagrodia * University of California, Los Angeles * Department of Computer Science * Box 951596 * 3532 Boelter Hall * Los Angeles, CA 90095-1596 * rajive@cs.ucla.edu * * 2.NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY * PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. * * 3.Neither the software developers, the Parallel Computing Lab, UCLA, or any * affiliate of the UC system shall be liable for any damages suffered by * Licensee from the use of this software. */// Use the latest version of Parsec if this line causes a compiler error./* * TSMA Model in GloMoSim - version 2.0 * * The TSMA model in GloMoSim is COPYRIGHTED software. It is freely available * without fee for education, or research, or to non-profit agencies. By * obtaining copies of this and other files that comprise the TSMA model in * GloMoSim, you, the Licensee, agree to abide by the following conditions * and understandings with respect to the copyrighted software: * * 1. Permission to use, copy, and modify this software and its documentation * for education, research, and non-profit purposes is hereby granted to * Licensee, provided that the copyright notice, the original author's names * and unit identification, and this permission notice appear on all such * copies, and that no charge be made for such copies. Any entity desiring * permission to incorporate this software into commercial products or to * use it for commercial purposes should contact: * * Professor Imrich Chlamtac * Distinguished Chair in Telecommunications * Director, Center for Advanced Telecommunications Systems and Services * Erik Jonsson School of Engineering and Computer Science * The University of Texas at Dallas * P.O. Box 830688, EC 33 * Richardson, TX 75083-0688 * e-mail: chlamtac@utdallas.edu * * 2. NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY * PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. * * 3. Neither the software developers, the Center for Advanced Tele- * communications Systems and Services (CATSS), UTD, or any affiliate * of the UT system shall be liable for any damages suffered by * Licensee from the use of this software. */#include <stdlib.h>#include <string.h>#include <assert.h>#include "api.h"#include "structmsg.h"#include "fileio.h"#include "message.h"#include "mac.h"#include "tsma.h"#include "radio.h"#define MONITOR_ID 0//#define TSMA_DEBUG 1/* * NAME: MacTsmaTransmit * * PURPOSE: Determine if node has permission to transmit in current * slot. * * PARAMETERS: node, node which we are checking * * RETURN: BOOL indicating whether node has permission * * ASSUMPTIONS: None */static //inline//BOOL MacTsmaTransmit(GlomoNode *node, GlomoMacTsma* tsma){ if (tsma->schedule[tsma->currentSlot] == '1') { assert(tsma->currentOutgoingFrame != NULL); return TRUE; } return FALSE;}/* * NAME: MacTsmaChangeState * * PURPOSE: Update state of TSMA protocol. * * PARAMETERS: node, node for which state is being updated * newState, new state of TSMA protocol * * RETURN: None * * ASSUMPTIONS: None */staticvoid MacTsmaChangeState(GlomoNode *node, GlomoMacTsma* tsma, int newState){ #ifdef TSMA_DEBUG if (node->nodeAddr == MONITOR_ID) { char clockStr[GLOMO_MAX_STRING_LENGTH]; ctoa(simclock(), clockStr); printf("time(%s) state change ", clockStr); switch (tsma->currentState) { case MAC_TSMA_READY: { printf("READY "); break; } case MAC_TSMA_WF_USLOT: { printf("WF_USLOT "); break; } case MAC_TSMA_WF_BSLOT: { printf("WF_BSLOT "); break; } case MAC_TSMA_XMT_UPKT: { printf("XMT_UPKT "); break; } case MAC_TSMA_XMT_BPKT: { printf("XMT_BPKT "); break; } case MAC_TSMA_XMT_UACK: { printf("XMT_UACK "); break; } case MAC_TSMA_WF_UACK: { printf("WF_UACK "); break; } default: { printf("invalid current state!\n"); assert(FALSE); abort(); } } printf("-->"); switch (newState) { case MAC_TSMA_READY: { printf("READY\n"); break; } case MAC_TSMA_WF_USLOT: { printf("WF_USLOT\n"); break; } case MAC_TSMA_WF_BSLOT: { printf("WF_BSLOT\n"); break; } case MAC_TSMA_XMT_UPKT: { printf("XMT_UPKT\n"); break; } case MAC_TSMA_XMT_BPKT: { printf("XMT_BPKT\n"); break; } case MAC_TSMA_XMT_UACK: { printf("XMT_UACK\n"); break; } case MAC_TSMA_WF_UACK: { printf("WF_UACK\n"); break; } default: { printf("invalid state change!\n"); assert(FALSE); abort(); } } } #endif /* Update tsma state. */ tsma->prevState = tsma->currentState; tsma->currentState = newState;}/* * NAME: CheckForPacketToTransmit * * PURPOSE: Checks for an outgoing packet from the network layer * and if it exists starts the TSMA transmission process. * Otherwise TSMA is set to "READY". * * RETURN: None * * ASSUMPTIONS: TSMA has no current frame that it is sending. */ static void CheckForPacketToTransmit(GlomoNode* node, GlomoMacTsma* tsma) { // If no current message and the network queue is not empty, // remove a packet from the queue and make it the current // outgoing frame. if ((tsma->currentOutgoingFrame == NULL) && (!NetworkIpOutputQueueIsEmpty(node, tsma->myGlomoMac->interfaceIndex))) { NetworkQueueingPriorityType NotUsed; NetworkIpOutputQueueDequeuePacket( node, tsma->myGlomoMac->interfaceIndex, &tsma->currentOutgoingFrame, &tsma->currentOutgoingFrameNextHopAddress, &NotUsed); tsma->pktsToSend++; } // // Set the State based on the existance and type of the current // outgoing frame. // if (tsma->currentOutgoingFrame != NULL) { if (tsma->currentOutgoingFrameNextHopAddress == ANY_DEST) { /* Network frame must be broadcast. */ MacTsmaChangeState(node, tsma, MAC_TSMA_WF_BSLOT); } else { /* Network frame must be unicast. */ MacTsmaChangeState(node, tsma, MAC_TSMA_WF_USLOT); } } else { /* Queue is empty. Transition to a ready state. */ MacTsmaChangeState(node, tsma, MAC_TSMA_READY); }}/* * NAME: MacTsmaScheduleSlot * * PURPOSE: Schedule next slot arrival * * PARAMETERS: node, node to receive next slot * msg, contains interval for next slot arrival * * RETURN: None * * ASSUMPTIONS: None */void MacTsmaScheduleSlot(GlomoNode *node, Message *msg){ MacTsmaSynchPkt *synch = (MacTsmaSynchPkt *)msg->info; GLOMO_MsgSend(node, msg, synch->slotInterval);}/* * NAME: MacTsmaPassPkt * * PURPOSE: Pass radio frame up to network layer. * * PARAMETERS: node, node to receive the frame * msg, contains the frame * * RETURN: None * * ASSUMPTIONS: None */void MacTsmaPassPkt(GlomoNode *node, GlomoMacTsma *tsma, Message *msg){ MacTsmaHeader* hdr = (MacTsmaHeader *)msg->packet; NODE_ADDR lastHopAddress = hdr->sourceAddr; hdr = NULL; GLOMO_MsgRemoveHeader(node, msg, sizeof(MacTsmaHeader)); NetworkIpReceivePacketFromMacLayer(node, msg, lastHopAddress);}/* * NAME: MacTsmaSendBroadcastPkt * * PURPOSE: Transmit broadcast frame. * * PARAMETERS: node, node sending broadcast frame * msg, contains the frame to be broadcast * * RETURN: None * * ASSUMPTIONS: None */void MacTsmaSendBroadcastPkt(GlomoNode *node, GlomoMacTsma *tsma){ Message* msg = GLOMO_MsgCopy(node, tsma->currentOutgoingFrame); MacTsmaHeader *hdr; #ifdef TSMA_DEBUG if (node->nodeAddr == MONITOR_ID) { char clockStr[GLOMO_MAX_STRING_LENGTH]; ctoa(simclock(), clockStr); printf("Time(%s) sending broadcast frame [%d]\n", clockStr, tsma->pktReps); } #endif /* Make sure queue is not empty. */ assert(msg != NULL); /* Make sure this is a broadcast frame. */ assert(tsma->currentOutgoingFrameNextHopAddress == ANY_DEST); /* Allocate space for TSMA header. */ GLOMO_MsgAddHeader(node, msg, sizeof(MacTsmaHeader)); /* Initialize TSMA header. */ hdr = (MacTsmaHeader *)msg->packet; hdr->sourceAddr = node->nodeAddr; hdr->destAddr = ANY_DEST; hdr->type = TSMA_BPKT; hdr->seqNum = tsma->seqNum; //hdr->priority = pkt->priority; // Probably not needed. (Jay) /* Change state. */ MacTsmaChangeState(node, tsma, MAC_TSMA_XMT_BPKT); /* Send message to radio layer. */ GLOMO_RadioStartTransmittingPacket( node, tsma->myGlomoMac->interfaceIndex, msg, hdr->destAddr, FALSE, 0);}/* * NAME: MacTsmaSendUnicastPkt * * PURPOSE: Transmit unicast frame. * * PARAMETERS: node, node sending the unicast frame * msg, contains the unicast frame being sent * * RETURN: None * * ASSUMPTIONS: None */void MacTsmaSendUnicastPkt(GlomoNode *node, GlomoMacTsma *tsma){ Message* msg = GLOMO_MsgCopy(node, tsma->currentOutgoingFrame); MacTsmaHeader *hdr; #ifdef TSMA_DEBUG if (node->nodeAddr == MONITOR_ID) { char clockStr[GLOMO_MAX_STRING_LENGTH]; ctoa(simclock(), clockStr); printf("Time(%s) sending unicast frame [%d]\n", clockStr, tsma->pktReps); } #endif /* Make sure this is a unicast frame. */ if (tsma->currentOutgoingFrameNextHopAddress == ANY_DEST) { assert(FALSE); abort(); // Can this really happen? (Jay). GLOMO_MsgFree(node, msg); MacTsmaSendBroadcastPkt(node, tsma); return; } /* Allocate space for TSMA header. */ GLOMO_MsgAddHeader(node, msg, sizeof(MacTsmaHeader)); /* Initialize TSMA header. */ hdr = (MacTsmaHeader *)msg->packet; hdr->sourceAddr = node->nodeAddr; hdr->destAddr = tsma->currentOutgoingFrameNextHopAddress; hdr->type = TSMA_UPKT; hdr->seqNum = tsma->seqNum; // hdr->priority = pkt->priority; // Not Needed (Jay). /* Change state. */ MacTsmaChangeState(node, tsma, MAC_TSMA_XMT_UPKT); /* Send message to radio layer. */ GLOMO_RadioStartTransmittingPacket( node, tsma->myGlomoMac->interfaceIndex, msg, hdr->destAddr, FALSE, 0);}/* * NAME: MacTsmaSendAck * * PURPOSE: Transmit acknowledgment frame. * * PARAMETERS: node, node sending the acknowledgment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -