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

📄 ttnetwork.cpp

📁 一个很棒的网络控制系统仿真软件
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	if (m->waituntil < nextHit && nwsys->postdelay > 0.0) {	  nextHit = m->waituntil;	}	nwsys->nwnodes[nwsys->sending]->state = 0;	nwsys->sending = -1;  // idle and hand over to next node	nwsys->waituntil = nwsys->time + nwsys->minsize / nwsys->datarate;	if (nwsys->waituntil < nextHit) {	  nextHit = nwsys->waituntil;	}      } else { // more to transmit in this frame	if (nwsys->time + remxtime(m) < nextHit) {	  nextHit = nwsys->time + remxtime(m);	}       }    }    break;  case FDMA:    // go through all nodes and count down transmission times    for (i=0; i<nwsys->nbrOfNodes; i++) {      if (nwsys->nwnodes[i]->state == 1) { // node has been sending	m = (NWmsg *)nwsys->nwnodes[i]->inputQ->getFirst();	// decrease remaining number of bytes in current frame	m->remaining -= (nwsys->bandwidths[i] * nwsys->datarate * timeElapsed);	// frame finished?	if (m->remaining < EPS) {	  // transmission is finished, move to outputQ	  // printf("transmission finished\n");	  nwsys->nwnodes[i]->inputQ->removeNode(m);	  	  if (m->receiver == -1) {	    // Broadcast	    for (j=0; j<nwsys->nbrOfNodes; j++) {	      if (j != m->sender) {		nwsys->nwnodes[j]->outputQ->appendNode(m);	      }	    }	  } else {	    nwsys->nwnodes[m->receiver]->outputQ->appendNode(m);	  }	  	  nwsys->nwnodes[i]->state = 0;	  m->waituntil = nwsys->time + nwsys->postdelay;	  // update nextHit?	  if (m->waituntil < nextHit && nwsys->postdelay > 0.0) {	    nextHit = m->waituntil;	  }	} else { // frame not finished	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  } 	}      }    }        // check if any new transmissions should be started    for (i=0; i<nwsys->nbrOfNodes; i++) {      if (nwsys->nwnodes[i]->state == 0) { // idle?	// check if we should start a new transmission	if ((m = (NWmsg *)nwsys->nwnodes[i]->inputQ->getFirst()) != NULL) {	  //printf("Node %d starting transmission of frame at %f\n", i, nwsys->time);	  nwsys->nwnodes[i]->state = 1; // we're sending	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  } 	}      }    }    break;  case TDMA:    if (nwsys->sending != -1) { // someone has been sending      m = (NWmsg *)nwsys->nwnodes[nwsys->sending]->inputQ->getFirst();      // decrease remaining number of bytes in current frame      m->remaining -= (nwsys->datarate * timeElapsed);      if (m->remaining < EPS) {	// transmission is finished, move to outputQ	// printf("transmission finished\n");	nwsys->nwnodes[nwsys->sending]->inputQ->removeNode(m);		if (m->receiver == -1) {	  // Broadcast	  for (j=0; j<nwsys->nbrOfNodes; j++) {	    if (j != m->sender) {	      nwsys->nwnodes[j]->outputQ->appendNode(m);	    }	  }	} else {	  nwsys->nwnodes[m->receiver]->outputQ->appendNode(m);	}		m->waituntil = nwsys->time + nwsys->postdelay;	if (m->waituntil < nextHit && nwsys->postdelay > 0.0) {	  nextHit = m->waituntil;	}	// will we have time and are there more messages?	if (nwsys->time < nwsys->currslottime + nwsys->slottime && (m = (NWmsg *)nwsys->nwnodes[nwsys->sending]->inputQ->getFirst()) != NULL) {	  // printf("Node %d starting transmission of new message at %f\n", nwsys->sending, nwsys->time);	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  }	} else {	  nwsys->nwnodes[nwsys->sending]->state = 0; // idle	  nwsys->sending = -1;	}      } else { // frame not finished	// update nextHit?	if (nwsys->time + remxtime(m) < nextHit) {	  nextHit = nwsys->time + remxtime(m);	}       }    }    // time for new slot?    if (nwsys->time >= nwsys->currslottime + nwsys->slottime) {      nwsys->slotcount = (nwsys->slotcount + 1) % nwsys->schedsize;      // printf("New slot: index %d, sender %d\n", nwsys->slotcount, nwsys->schedule[nwsys->slotcount]);      nwsys->currslottime += nwsys->slottime;      // preempt current transmission?      if (nwsys->sending != -1 && nwsys->schedule[nwsys->slotcount] != nwsys->sending) {	// printf("current transmission by %d interrupted!\n", nwsys->sending);	nwsys->nwnodes[nwsys->sending]->state = 2; // wait state	nwsys->sending = -1;      }    }      // start/resume a transmission?    if (nwsys->schedule[nwsys->slotcount] != nwsys->sending) {      i = nwsys->schedule[nwsys->slotcount];      // resuming an old transmission?      if (nwsys->nwnodes[i]->state == 2) {	// printf("Node %d resuming its transmission\n", i);	nwsys->nwnodes[i]->state = 1;	nwsys->sending = i;	m = (NWmsg *)nwsys->nwnodes[i]->inputQ->getFirst();	if (nwsys->time + remxtime(m) < nextHit) {	  nextHit = nwsys->time + remxtime(m);	}       } else { // new transmission	// printf("node %d may start new transmission\n", i);	if ((m = (NWmsg *)nwsys->nwnodes[i]->inputQ->getFirst()) != NULL) {	  // printf("Node %d starting transmission of frame at %f\n", i, nwsys->time);	  nwsys->nwnodes[i]->state = 1; // we're sending	  nwsys->sending = i;	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  } 	} else {	  // printf("Node %d has nothing to send...\n");	}      }    }    if (nwsys->currslottime + nwsys->slottime < nextHit) {      nextHit = nwsys->currslottime + nwsys->slottime;    }    break;  case SFDSE: // Symmetric Full Duplex Switched Ethernet        // FROM SENDER TO SWITCH    // go through all nodes and count down transmission times    for (i=0; i<nwsys->nbrOfNodes; i++) {      if (nwsys->nwnodes[i]->state == 1) { // node has been sending	m = (NWmsg *)nwsys->nwnodes[i]->inputQ->getFirst();	// decrease remaining number of bytes in current frame	m->remaining -= (nwsys->datarate * timeElapsed);	// frame finished?	if (m->remaining < EPS) {	  // transmission is finished, move to switchQ	  //printf("transmission finished, move to switchQ\n");	  m->remaining = max(m->length, nwsys->minsize); // restore remaining	  if (switchmalloc(i,m->receiver,m->length)) {	    nwsys->nwnodes[i]->inputQ->removeNode(m);	    if (m->receiver == -1) {	      // Broadcast	      nwsys->nwnodes[i]->inputQ->removeNode(m);	      for (j=0; j<nwsys->nbrOfNodes; j++) {		if (j != m->sender) {		  nwsys->nwnodes[j]->switchQ->appendNode(m);		}	      }	    } else {	      nwsys->nwnodes[m->receiver]->switchQ->appendNode(m);	    }	  } else {	    if (nwsys->overflow == BUFFULLRETRY) {	      printf("Switch buffer full, retransmitting!\n");	    } else {	      printf("Switch buffer full, dropping!\n");	      nwsys->nwnodes[i]->inputQ->deleteNode(m);	    }	  }	  	  nwsys->nwnodes[i]->state = 0;	} else { // frame not finished	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  } 	}      }    }        // check if any new transmissions should be started    for (i=0; i<nwsys->nbrOfNodes; i++) {      if (nwsys->nwnodes[i]->state == 0) { // idle?	// check if we should start a new transmission	if ((m = (NWmsg *)nwsys->nwnodes[i]->inputQ->getFirst()) != NULL) {	  //printf("Node %d starting transmission of frame at %f\n", i, nwsys->time);	  nwsys->nwnodes[i]->state = 1; // we're sending	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  } 	}      }    }    // FROM SWITCH TO RECEIVER    // go through all nodes and count down transmission times    for (i=0; i<nwsys->nbrOfNodes; i++) {      if (nwsys->nwnodes[i]->swstate == 1) { // node has been sending	m = (NWmsg *)nwsys->nwnodes[i]->switchQ->getFirst();	// decrease remaining number of bytes in current frame	m->remaining -= (nwsys->datarate * timeElapsed);	// frame finished?	if (m->remaining < EPS) {	  // transmission is finished, move to outputQ	  //printf("transmission finished, move to outputQ\n");	  m->remaining = max(m->length, nwsys->minsize); // restore remaining	  nwsys->nwnodes[i]->switchQ->removeNode(m);	  nwsys->nwnodes[i]->outputQ->appendNode(m);	  nwsys->nwnodes[i]->swstate = 0;	  switchfree(i, m->length);	} else { // frame not finished	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  } 	}      }    }        // check if any new transmissions should be started    for (i=0; i<nwsys->nbrOfNodes; i++) {      if (nwsys->nwnodes[i]->swstate == 0) { // idle?	// check if we should start a new transmission	if ((m = (NWmsg *)nwsys->nwnodes[i]->switchQ->getFirst()) != NULL) {	  //printf("Switch output %d starting transmission of frame at %f\n", i, nwsys->time);	  nwsys->nwnodes[i]->swstate = 1; // we're sending	  // update nextHit?	  if (nwsys->time + remxtime(m) < nextHit) {	    nextHit = nwsys->time + remxtime(m);	  } 	}      }    }        break;  default:    printf("Protocol not implemented!\n");    break;  }  // Check if messages have finished waiting in the outputQ's  for (i=0; i<nwsys->nbrOfNodes; i++) {    m = (NWmsg *)nwsys->nwnodes[i]->outputQ->getFirst();    while (m != NULL) {      next = (NWmsg *)m->getNext();      if (m->waituntil - nwsys->time < EPS) {	// finished waiting, move to postprocQ	//printf("*** moving message from outputQ %d to postprocQ at %f\n", i+1, nwsys->time);	nwsys->nwnodes[i]->outputQ->removeNode(m);	double rNbr = ((double) rand() / (double) RAND_MAX);	if (rNbr < nwsys->lossprob) {	  // packet lost, do not forward to post-proc	  //printf("Network: A packet headed for node # %d, was lost at time %f\n", i + 1 , nwsys->time);	  delete m;	} else {	  nwsys->nwnodes[i]->postprocQ->appendNode(m);	  nwsys->outputs[i] += 1.0; // trigger rcv output	}      } else {	// update nextHit?	if (m->waituntil < nextHit) {	  nextHit = m->waituntil;	}      }      m = next; // get next    }  }  // done  //printf("Next hit scheduled for %f\n", nextHit);  // produce output graph  for (i=0; i<nwsys->nbrOfNodes; i++) {    if (nwsys->nwnodes[i]->state == 1) {      nwsys->sendschedule[i] = i+1.5;  // sending    } else if ((m = (NWmsg *)nwsys->nwnodes[i]->inputQ->getFirst()) != NULL) {      nwsys->sendschedule[i] = i+1.25; // waiting    } else {      nwsys->sendschedule[i] = i+1.0;     // idle    }  }  return nextHit;}// ------- Simulink callback functions ------- #ifdef __cplusplusextern "C" { // use the C fcn-call standard for all functions  #endif       // defined within this scope   #define S_FUNCTION_NAME ttnetwork#define S_FUNCTION_LEVEL 2#include "simstruc.h"static void mdlInitializeSizes(SimStruct *S){  int nbrOfNodes;  const mxArray *arg;  ssSetNumSFcnParams(S, 14);  /* Number of expected parameters */  if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {    return; /* Parameter mismatch will be reported by Simulink */  }    // Parse second argument only, to determine nbrOfNodes  // 2 - Number of nodes  arg = ssGetSFcnParam(S, 2);  if (mxIsDoubleScalar(arg)) {     nbrOfNodes = (int) *mxGetPr(arg);  }  if (nbrOfNodes < 1) {    ssSetErrorStatus(S, "TrueTime Network: The number of nodes must be an integer > 0");    return;  }  ssSetNumContStates(S, 0);  ssSetNumDiscStates(S, 0);  

⌨️ 快捷键说明

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