/*****************************************************************
* DVsim.c Distance Vector Routing Simulation Package
*
* Event driven simulation of distributed, asynchronous,
* distance vector routing. This file contains the network
* simulation code which emulates layer 2 and below:
*
* - simulates packet tranmissions between two
* physically connected nodes
*
* - assumes with no packet loss or corruption
*
* - calls initializations routines rtinit0, etc., before
* beginning simulation
*
*
* Original code from Kurose and Ross, Computer Networking
*
* Modified by S. Dykes Apr. 2000
* 1. Changed RNG to use drand48() with seed = getpid()
* 2. Replaced 999 with INF
* 3. created common include file DVsim.h
* 4. extended to 6 node network
*
*---------------------------------------------------------------*/
#define DVSIM_H
#include "DVsim.h"
#include "time.h"
#define FROM_LAYER2 2
#define LINK_CHANGE 10
/*----------------------------------------
* Event list and possible events
*---------------------------------------*/
struct event
{
double evtime; /* event time */
int evtype; /* event type code */
int eventity; /* entity where event occurs */
struct rtpkt *rtpktptr; /* ptr to packet (if any) assoc w/ this event */
struct event *prev;
struct event *next;
};
struct event *evlist = NULL;
/*--------------------------------
* Simulation clock
*-------------------------------*/
double clocktime = 0.000;
double get_clock() { return clocktime; }
/*--------------------------------
* Function prototypes
*-------------------------------*/
void init();
void insertevent (struct event *);
void printevlist();
void tolayer2 (struct rtpkt );
/*-------------------------------
* MAIN
*-------------------------------*/
main()
{
int i;
struct event *eventptr;
/*----------------------------------------
* Loop until we run out of events
*---------------------------------------*/
init();
while (1)
{
/*----------------------------------------
* Get next event from top of list
* Done if event list is empty
*---------------------------------------*/
if (! (eventptr = evlist) ) break;
evlist = evlist->next;
if (evlist) evlist->prev=NULL;
/*----------------------------------------
* Trace info
*---------------------------------------*/
if (TRACE>1) {
printf("\nMAIN: rcv event, t=%.3f, at %d",
eventptr->evtime, eventptr->eventity);
if (eventptr->evtype == FROM_LAYER2 ) {
printf(" src:%2d,", eventptr->rtpktptr->sourceid);
printf(" dest:%2d,", eventptr->rtpktptr->destid);
printf(" contents: ");
for (i=0; i<NODES; i++) printf ("%3d ",eventptr->rtpktptr->mincost[i]);
printf("\n");
}
}
/*----------------------------------------
* Update clock to next event time
*---------------------------------------*/
clocktime = eventptr->evtime;
/*----------------------------------------
* Call event handler
*---------------------------------------*/
if (eventptr->evtype == FROM_LAYER2 ) {
if (eventptr->eventity == 0) rtupdate0(eventptr->rtpktptr);
else if (eventptr->eventity == 1) rtupdate1(eventptr->rtpktptr);
else if (eventptr->eventity == 2) rtupdate2(eventptr->rtpktptr);
else if (eventptr->eventity == 3) rtupdate3(eventptr->rtpktptr);
else if (eventptr->eventity == 4) rtupdate4(eventptr->rtpktptr);
else if (eventptr->eventity == 5) rtupdate5(eventptr->rtpktptr);
else { printf("Panic: unknown event entity\n"); exit(0); }
}
else if (eventptr->evtype == LINK_CHANGE ) {
if (clocktime < 10001.0) { linkhandler0(1,20); linkhandler1(0,20); }
else { linkhandler0(1,1); linkhandler1(0,1); }
}
else {
printf("Panic: unknown event type\n");
exit(0);
}
/*--------------------------------------------------------
* Free memory for packet (if any) and event struct
*-------------------------------------------------------*/
if (eventptr->evtype == FROM_LAYER2 ) free(eventptr->rtpktptr);
free(eventptr);
}
/*-------------------------------
* SIMULATION TERMINATED
*-------------------------------*/
printf("\nSimulator terminated at t=%f, no packets in medium\n", clocktime);
}
/*-------------------------------
* init
*-------------------------------*/
void init()
{
int i;
double sum, avg;
struct event *evptr;
/*---------------------
* Input TRACE
*--------------------*/
printf ("Enter value for TRACE (no trace =0, user= 1 or 2, developer= 3): ");
scanf ("%d", &TRACE);
/*---------------------
* Test RNG
*--------------------*/
srand ((long) getpid());
for (i=0, sum=0.; i<1000; i++) sum+= drand();
avg = sum/1000.0;
if (avg < 0.25 || avg > 0.75) {
printf("RNG PROBLEM\n");
printf("Average of 1000 random numbers is %g (expected 0.5).\n", avg);
printf("It is likely that random number generation on your machine\n");
printf("is different from what this emulator expects. Please take\n");
printf("a look at the routine init() in the emulator code. \n");
exit(0);
}
/*-------------------------------------
* Initialize clock and nodes
*------------------------------------*/
clocktime=0.0;
rtinit0();
rtinit1();
rtinit2();
rtinit3();
rtinit4();
rtinit5();
/*-------------------------------------
* Initialize future link changes
*------------------------------------*/
if (LINKCHANGES==1)
{
evptr = (struct event *)malloc(sizeof(struct event));
evptr->evtime = 10000.0;
evptr->evtype = LINK_CHANGE;
evptr->eventity = -1;
evptr->rtpktptr = NULL;
insertevent(evptr);
evptr = (struct event *)malloc(sizeof(struct event));
evptr->evtype = LINK_CHANGE;
evptr->evtime = 20000.0;
evptr->rtpktptr = NULL;
insertevent(evptr);
}
}
/********************* EVENT HANDLING ROUTINES *******
*
* These routines handle the event list
*
*****************************************************/
/*------------------------------------------------------------
* insertevent
*
* Inserts object in event list. List is sorted by time.
* p -> object to be inserted
* q -> top of list
*------------------------------------------------------------*/
void insertevent (struct event *p)
{
struct event *q,*qold;
if (TRACE>3) {
printf(" INSERTEVENT: time is %lf\n",clocktime);
printf(" INSERTEVENT: future time will be %lf\n",p->evtime);
}
q = evlist;
if (q==NULL) {
/*--------------------------------
* Empty list - insert at top
*-------------------------------*/
evlist=p;
p->next=NULL;
p->prev=NULL;
}
else {
/*----------------------------------------
* Find event time > p's time
*---------------------------------------*/
for (qold=q; q !=NULL && p->evtime > q->evtime; q=q->next) qold=q;
if (q==NULL) {
/*---------------------
* End of list
*--------------------*/
qold->next = p;
p->prev = qold;
p->next = NULL;
}
else if (q==evlist) {
/*---------------------
* Top of list
*---------------------*/
p->next=evlist;
p->prev=NULL;
p->next->prev=p;
evlist = p;
}
else {
/*---------------------
* Middle of list
*---------------------*/
p->next=q;
p->prev=q->prev;
q->prev->next=p;
q->prev=p;
}
}
}
/*------------------------------------------------------------
* printevlist - prints event list
*-----------
C语言模拟路由DV算法
4星 · 超过85%的资源 需积分: 13 53 浏览量
2009-11-26
19:40:40
上传
评论
收藏 8KB RAR 举报
lizzywind
- 粉丝: 0
- 资源: 5
最新资源
- 南航电子商务的发展.ppt
- 博客网站开题报告.doc
- 卫星通信链路计算过程.doc
- 基于STM32 F407 Discovery 单片机实现秀吧自动鸡尾酒贩卖机,流量控制源码
- 原发性慢性肾上腺皮质功能减退症chronicadrenocortical.pdf
- 各种回归模型适用条件-logistic回归-(2).pdf
- 基于文本和多模态数据的风险识别题目名称Emoji复杂文本识别python源码+项目说明.zip
- 基于STM32单片机实现的的智能晾衣架
- 基于安卓Android的新闻发布系统app源代码+文档说明(高分项目).zip
- 基于SkyTra-S1216F8-BD模块gps串口模块AD09设计设计硬件(原理图+PCB)文件.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈