0

i have a few .c files, pro3.c is the only file that complies without a problem however when i go to compile the other ones i get a error message: warning useless storage class specifier in empty declaration, i was hoping someone could tell/show me what i am doing wrong.

prog3.c

node3.c

node2.c

node1.c

node0.c

Attachments
#include <stdlib.h>
#include <stdio.h>
#define INFINITY 9999


extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;
extern float clocktime;

int lkcost0[4];		/*The link cost between node 0 and other nodes*/
int spath0[4];			/*The shortest path between node 0 and other nodes*/
struct distance_table 		/*Define distance table*/
{
  int costs[4][4];
} dt0;


/* students to write the following two routines, and maybe some others */

void rtinit0() 
{
	int i, j;
	printf("At time t=%.3f, rtinit0() called. \n", clocktime);

	/* initialize the link costs */
	lkcost0[0] = 0;	lkcost0[1] = 1;	lkcost0[2] = 3;	lkcost0[3] = 7;

	/*initialze the distance table i denotes the destination node, j denotes the neighbour node*/
	for (i=0; i<4; i++)
		for (j=0; j<4; j++) {
			if (i==j)
				dt0.costs[i][j] = lkcost0[i];
			else
				dt0.costs[i][j] = INFINITY;
		}
		for (i=0; i<4; i++) 		/* compute the shortest path*/
			spath0[i]=lkcost0[i];
		printdt0(&dt0);			/* print distance table */
		sendpkt0();			/* make packet and send to other nodes*/
}


void rtupdate0(rcvdpkt)
  struct rtpkt *rcvdpkt;
{
	int i, j;
	j=rcvdpkt->sourceid;

	printf("At time t=%.3f, rtupdate0() called, and node 0 receives a packet from node %d\n",
		clocktime, j);

	/* update distance table*/
	for ( i= 0; i<4; i++) {
		dt0.costs[i][j] = lkcost0[j] + rcvdpkt->mincost[i];
		if (dt0.costs[i][j] > INFINITY)
			dt0.costs[i][j]=INFINITY;
	}
	printdt0(&dt0);				/*print distance table*/
	if (update0() == 1)	sendpkt0();	/*update shortest paths*/
}


printdt0(dtptr)
  struct distance_table *dtptr;
{
  printf("                via     \n");
  printf("   D0 |    1     2    3 \n");
  printf("  ----|-----------------\n");
  printf("     1|  %3d   %3d   %3d\n",dtptr->costs[1][1], dtptr->costs[1][2],dtptr->costs[1][3]);
  printf("dest 2|  %3d   %3d   %3d\n",dtptr->costs[2][1], dtptr->costs[2][2],dtptr->costs[2][3]);
  printf("     3|  %3d   %3d   %3d\n",dtptr->costs[3][1], dtptr->costs[3][2],dtptr->costs[3][3]);
}

linkhandler0(linkid, newcost)   
  int linkid, newcost;
/* called when cost from 0 to linkid changes from current value to newcost*/
/* You can leave this routine empty if you're an undergrad. If you want */
/* to use this routine, you'll need to change the value of the LINKCHANGE */
/* constant definition in prog3.c from 0 to 1 */	
{

	int i, oldcost;
	printf("At time t=%.3f, linkhandler0() called \n", clocktime);

	oldcost = lkcost0[linkid];
	lkcost0[linkid] = newcost;

	/*update distance table*/
	for (i=0; i<4; i++) {
		dt0.costs[i][linkid] = dt0.costs[i][linkid] - oldcost + newcost;
		if (dt0.costs[i][linkid] > INFINITY) dt0.costs[i][linkid] = INFINITY;
	}

	printdt0(&dt0);			/* print distance table*/
	if (update0()==1) sendpkt0();	/* update shortest paths*/
	
}

int update0()
{
	int i, j;
	int tmp[4];
	int flag =0;

	for (i=0; i<4; i++) {	/*compute new shortest paths*/
		tmp[i] = dt0.costs[i][0];
		for (j=1; j<4; j++) {
			if (tmp[i] > dt0.costs[i][j]) tmp[i] = dt0.costs[i][j];
		}

		if (tmp[i] != spath0[i] ) {
			/*update shortest path*/
			spath0[i] = tmp[i];
			flag =1;
		}
	}
	return flag;
}


sendpkt0()
{
	int i;
	struct rtpkt packet;

	/*make  packet*/
	packet.sourceid = 0;
	for (i=0; i<4; i++) 
		packet.mincost[i] = spath0[i];

	/*send out packet*/
	packet.destid=1;
	tolayer2(packet);
	printf("At time t=%.3f, node 0 sends packet to node 1 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);

	packet.destid=2;
	tolayer2(packet);
	printf("At time t=%.3f, node 0 sends packet to node 2 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);

	packet.destid=3;
	tolayer2(packet);
	printf("At time t=%.3f, node 0 sends packet to node 3 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);

	printf("\n");

}
#include <stdio.h>
#define INFINITY 9999
#include <stdlib.h>


extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;
extern float clocktime;
int lkcost1[4];		/*the link cost between node 0 and other nodes*/
int spath1[4];		/*the shortest path between node 0 and other nodes*/

struct distance_table /*define distance table*/
{
  int costs[4][4];
} dt1;


/* students to write the following two routines, and maybe some others */
void rtinit1() 
{
	int i, j;	
	printf("At time t=%.3f, rtinit1() called \n", clocktime);

	/* initialize the link costs */
	lkcost1[0] = 1;
	lkcost1[1] = 0;
	lkcost1[2] = 1;
	lkcost1[3] = INFINITY;

	/*initialze the distance table 
	i denotes the destination node, j denotes the neighbour node*/
	for (i=0; i<4; i++)
		for (j=0; j<4; j++) {
			if (i==j) 	dt1.costs[i][j] = lkcost1[i];
			else		dt1.costs[i][j] = INFINITY;
		}
		for (i=0; i<4; i++)	/* compute the shortest path*/
			spath1[i]=lkcost1[i];
		printdt1(&dt1);         /* print distance table */
		sendpkt1();		/*make packet and send it to other nodes*/
}


void rtupdate1(rcvdpkt)
  struct rtpkt *rcvdpkt;
{
	int i, j;

	j=rcvdpkt->sourceid;

	printf("At time t=%.3f, rtupdate1() called. node 1 receives a packet from node %d\n",clocktime, j);

	/* update distance table*/
	for ( i= 0; i<4; i++) {
		dt1.costs[i][j] = lkcost1[j] + rcvdpkt->mincost[i];
		if (dt1.costs[i][j] > INFINITY)
			dt1.costs[i][j]=INFINITY;
	}

	printdt1(&dt1);				/*print distance table*/
	if (update1() == 1)	sendpkt1(); 	/*update shortest paths*/
}

printdt1(dtptr)
  struct distance_table *dtptr;
{
  printf("                via     \n");
  printf("   D1 |    0     2 \n");
  printf("  ----|-----------------\n");
  printf("     0|  %3d   %3d \n",dtptr->costs[0][0], dtptr->costs[0][2]);
  printf("dest 2|  %3d   %3d \n",dtptr->costs[2][0], dtptr->costs[2][2]);
  printf("     3|  %3d   %3d \n",dtptr->costs[3][0], dtptr->costs[3][2]);
}

linkhandler1(linkid, newcost)   
  int linkid, newcost;
/* called when cost from 0 to linkid changes from current value to newcost*/
/* You can leave this routine empty if you're an undergrad. If you want */
/* to use this routine, you'll need to change the value of the LINKCHANGE */
/* constant definition in prog3.c from 0 to 1 */	
{

	int i, oldcost;
	printf("At time t=%.3f, linkhandler0() called \n", clocktime);
	oldcost = lkcost1[linkid];
	lkcost1[linkid] = newcost;

	/*update distance table*/
	for (i=0; i<4; i++) {
		dt1.costs[i][linkid] = dt1.costs[i][linkid] - oldcost + newcost;
		if (dt1.costs[i][linkid] > INFINITY)
			dt1.costs[i][linkid] = INFINITY;
	}

	printdt1(&dt1);			/* print distance table*/
	if (update1()==1)		/* update shortest paths*/
		sendpkt1();
}

int update1()
{
	int i, j;
	int tmp[4];
	int flag =0;

	/*compute new shortest paths*/
	for (i=0; i<4; i++) {
		tmp[i] = dt1.costs[i][0];
		for (j=1; j<4; j++) {
			if (tmp[i] > dt1.costs[i][j])
				tmp[i] = dt1.costs[i][j];
		}
		if (tmp[i] != spath1[i] ) {
			/*update shortest path*/
			spath1[i] = tmp[i];
			flag =1;
		}
	}
	return flag;
}


sendpkt1()
{
	int i;
	struct rtpkt packet;
	packet.sourceid = 1;	/*make  packet*/
	for (i=0; i<4; i++) 
		packet.mincost[i] = spath1[i];
	packet.destid=0;	/*send out packet*/
	tolayer2(packet);
	printf("At time t=%.3f, node 1 sends packet to node 0 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);
	packet.destid=2;
	tolayer2(packet);
	printf("At time t=%.3f, node 1 sends packet to node 2 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);
	printf("\n");

}
#include <stdlib.h>
#include <stdio.h>
#define INFINITY 9999


extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;
extern float clocktime;

int lkcost2[4];		/*the link cost between node 0 and other nodes*/
int spath2[4];		/*the shortest path between node 0 and other nodes*/

struct distance_table /*define distance table*/
{
  int costs[4][4];
} dt2;


/* students to write the following two routines, and maybe some others */
void rtinit2()
{
	int i, j;
	printf("At time t=%.3f, rtinit2() called \n", clocktime);

	/* initialize the link costs */
	lkcost2[0] = 3;
	lkcost2[1] = 1;
	lkcost2[2] = 0;
	lkcost2[3] = 2;

	/*initialze the distance table 
	i denotes the destination node, j denotes the neighbour node*/
	for (i=0; i<4; i++)
		for (j=0; j<4; j++) {
			if (i==j)
				dt2.costs[i][j] = lkcost2[i];
			else
				dt2.costs[i][j] = INFINITY;
		}

		/* compute the shortest path*/
		for (i=0; i<4; i++)	spath2[i]=lkcost2[i];
		printdt2(&dt2);		/* print distance table */
		sendpkt2();             /*make packet and send it to other nodes*/
}


void rtupdate2(rcvdpkt)
  struct rtpkt *rcvdpkt;
{
	int i, j;
	j=rcvdpkt->sourceid;
	printf("At time t=%.3f, rtupdate2() called. node 2 receives a packet from node %d\n",
		clocktime, j);

	/* update distance table*/
	for ( i= 0; i<4; i++) {
		dt2.costs[i][j] = lkcost2[j] + rcvdpkt->mincost[i];
		if (dt2.costs[i][j] > INFINITY)
			dt2.costs[i][j]=INFINITY;
	}

	printdt2(&dt2);			/*print distance table*/

	if (update2() == 1)	/*update shortest paths*/
		sendpkt2();
}


printdt2(dtptr)
  struct distance_table *dtptr;
  
{
  printf("                via     \n");
  printf("   D2 |    0     1    3 \n");
  printf("  ----|-----------------\n");
  printf("     0|  %3d   %3d   %3d\n",dtptr->costs[0][0], dtptr->costs[0][1],dtptr->costs[0][3]);
  printf("dest 1|  %3d   %3d   %3d\n",dtptr->costs[1][0], dtptr->costs[1][1],dtptr->costs[1][3]);
  printf("     3|  %3d   %3d   %3d\n",dtptr->costs[3][0], dtptr->costs[3][1],dtptr->costs[3][3]);
}

linkhandler2(linkid, newcost)   
  int linkid, newcost;

/* called when cost from 0 to linkid changes from current value to newcost*/
/* You can leave this routine empty if you're an undergrad. If you want */
/* to use this routine, you'll need to change the value of the LINKCHANGE */
/* constant definition in prog3.c from 0 to 1 */
	
{

	int i, oldcost;
	printf("At time t=%.3f, linkhandler2() called. \n", clocktime);

	oldcost = lkcost2[linkid];
	lkcost2[linkid] = newcost;

	/*update distance table*/
	for (i=0; i<4; i++) {
		dt2.costs[i][linkid] = dt2.costs[i][linkid] - oldcost + newcost;
		if (dt2.costs[i][linkid] > INFINITY)
			dt2.costs[i][linkid] = INFINITY;
	}

	printdt2(&dt2);				/* print distance table*/
	if (update2()==1)           /* update shortest paths*/
		sendpkt2();
}

int update2()
{
	int i, j;
	int tmp[4];
	int flag =0;

	/*compute new shortest paths*/
	for (i=0; i<4; i++) {
		tmp[i] = dt2.costs[i][0];

		for (j=1; j<4; j++) {
			if (tmp[i] > dt2.costs[i][j])
				tmp[i] = dt2.costs[i][j];
		}
		if (tmp[i] != spath2[i] ) { 	/*update shortest path*/
			spath2[i] = tmp[i];
			flag =1;
		}
	}
	return flag;
}

sendpkt2()
{
	int i;
	struct rtpkt packet;
	packet.sourceid = 2;	/*make  packet*/
	for (i=0; i<4; i++) 
		packet.mincost[i] = spath2[i];
	
	/*send out packet*/
	packet.destid=0;
	tolayer2(packet);
	printf("At time t=%.3f, node 2 sends packet to node 0 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);
	packet.destid=1;
	tolayer2(packet);
	printf("At time t=%.3f, node 2 sends packet to node 1 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);
	packet.destid=3;
	tolayer2(packet);
	printf("At time t=%.3f, node 2 sends packet to node 3 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);
	printf("\n");

}
#include <stdlib.h>
#include <stdio.h>
#define INFINITY 9999


extern struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

extern int TRACE;
extern int YES;
extern int NO;
extern float clocktime;
int lkcost3[4];		/*the link cost between node 0 and other nodes*/
int spath3[4];		/*the shortest path between node 0 and other nodes*/

struct distance_table /*define distance table*/
{
  int costs[4][4];
} dt3;


/* students to write the following two routines, and maybe some others */
void rtinit3() 
{
	int i, j;	
	printf("At time t=%.3f, rtinit3() called. \n", clocktime);

	/* initialize the link costs */
	lkcost3[0] = 7;
	lkcost3[1] = INFINITY;
	lkcost3[2] = 2;
	lkcost3[3] = 0;

	/*initialze the distance table 
	i denotes the destination node, j denotes the neighbour node*/
	for (i=0; i<4; i++)
		for (j=0; j<4; j++) {
			if (i==j)
				dt3.costs[i][j] = lkcost3[i];
			else
				dt3.costs[i][j] = INFINITY;
		}
		
		for (i=0; i<4; i++) /* compute the shortest path*/
			spath3[i]=lkcost3[i];

		printdt3(&dt3);		/* print distance table */
		sendpkt3();		/*make packet and send it to other nodes*/
}


void rtupdate3(rcvdpkt)
  struct rtpkt *rcvdpkt;
{
	int i, j;
	j=rcvdpkt->sourceid;
	printf("At time t=%.3f, rtupdate3() called. node 3 receives a packet from node %d\n",
		clocktime, j);

	/* update distance table*/
	for ( i= 0; i<4; i++) {
		dt3.costs[i][j] = lkcost3[j] + rcvdpkt->mincost[i];
		if (dt3.costs[i][j] > INFINITY)
			dt3.costs[i][j]=INFINITY;
	}
	printdt3(&dt3);			/*print distance table*/
	if (update3() == 1)	/*update shortest paths*/
		sendpkt3();
}


printdt3(dtptr)
  struct distance_table *dtptr;
{
  printf("                via     \n");
  printf("   D3 |    0     2 \n");
  printf("  ----|-----------------\n");
  printf("     0|  %3d   %3d\n",dtptr->costs[0][0],dtptr->costs[0][2]);
  printf("dest 1|  %3d   %3d\n",dtptr->costs[1][0],dtptr->costs[1][2]);
  printf("     2|  %3d   %3d\n",dtptr->costs[2][0],dtptr->costs[2][2]);

}

linkhandler3(linkid, newcost)   
  int linkid, newcost;
/* called when cost from 0 to linkid changes from current value to newcost*/
/* You can leave this routine empty if you're an undergrad. If you want */
/* to use this routine, you'll need to change the value of the LINKCHANGE */
/* constant definition in prog3.c from 0 to 1 */
	
{

	int i, oldcost;
	printf("At time t=%.3f, linkhandler3() is called \n", clocktime);

	oldcost = lkcost3[linkid];
	lkcost3[linkid] = newcost;

	/*update distance table*/
	for (i=0; i<4; i++) {
		dt3.costs[i][linkid] = dt3.costs[i][linkid] - oldcost + newcost;
		if (dt3.costs[i][linkid] > INFINITY)
			dt3.costs[i][linkid] = INFINITY;
	}
	printdt3(&dt3);		/* print distance table*/
	if (update3()==1)       /* update shortest paths*/
		sendpkt3();
}

int update3()
{
	int i, j;
	int tmp[4];
	int flag =0;

	/*compute new shortest paths*/
	for (i=0; i<4; i++) {
		tmp[i] = dt3.costs[i][0];
		for (j=1; j<4; j++) {
			if (tmp[i] > dt3.costs[i][j])
				tmp[i] = dt3.costs[i][j];
		}
		if (tmp[i] != spath3[i] ) {
			/*update shortest path*/
			spath3[i] = tmp[i];
			flag =1;
		}
	}
	return flag;
}


sendpkt3()
{
	int i;
	struct rtpkt packet;

	/*make  packet*/
	packet.sourceid = 3;
	for (i=0; i<4; i++) 
		packet.mincost[i] = spath3[i];

	/*send out packet*/
	packet.destid=0;
	tolayer2(packet);
	printf("At time t=%.3f, node 3 sends packet to node 0 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);
	packet.destid=2;
	tolayer2(packet);
	printf("At time t=%.3f, node 3 sends packet to node 2 with: %d %d %d %d\n",
		clocktime, packet.mincost[0], packet.mincost[1], packet.mincost[2],packet.mincost[3]);
	printf("\n");

}
#include <stdio.h>
#include <stdlib.h>

#define LINKCHANGES 1 
/* ******************************************************************
Programming assignment 3: implementing distributed, asynchronous,
                          distance vector routing.

THIS IS THE MAIN ROUTINE.  IT SHOULD NOT BE TOUCHED AT ALL BY STUDENTS!

**********************************************************************/


/* a rtpkt is the packet sent from one routing update process to
   another via the call tolayer3() */
struct rtpkt {
  int sourceid;       /* id of sending router sending this pkt */
  int destid;         /* id of router to which pkt being sent 
                         (must be an immediate neighbor) */
  int mincost[4];    /* min cost to node 0 ... 3 */
  };

int TRACE = 1;             /* for my debugging */
int YES = 1;
int NO = 0;

creatertpkt( initrtpkt, srcid, destid, mincosts)
struct rtpkt *initrtpkt;
int srcid;
int destid;
int mincosts[];

{
  int i;
  initrtpkt->sourceid = srcid;
  initrtpkt->destid = destid;
  for (i=0; i<4; i++)
    initrtpkt->mincost[i] = mincosts[i];
}  


/*****************************************************************
***************** NETWORK EMULATION CODE STARTS BELOW ***********
The code below emulates the layer 2 and below network environment:
  - emulates the tranmission and delivery (with no loss and no
    corruption) between two physically connected nodes
  - calls the initializations routines rtinit0, etc., once before
    beginning emulation

THERE IS NOT REASON THAT ANY STUDENT SHOULD HAVE TO READ OR UNDERSTAND
THE CODE BELOW.  YOU SHOLD NOT TOUCH, OR REFERENCE (in your code) ANY
OF THE DATA STRUCTURES BELOW.  If you're interested in how I designed
the emulator, you're welcome to look at the code - but again, you should have
to, and you defeinitely should not have to modify
******************************************************************/

struct event {
   float 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;   /* the event list */

/* possible events: */
#define  FROM_LAYER2     2
#define  LINK_CHANGE     10

float clocktime = 0.000;


int main()
{
   struct event *eventptr;
   
   init();
   
   while (1) {
     
        eventptr = evlist;            /* get next event to simulate */
        if (eventptr==NULL)
           goto terminate;
        evlist = evlist->next;        /* remove this event from event list */
        if (evlist!=NULL)
           evlist->prev=NULL;
        if (TRACE>1) {
          printf("MAIN: 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: %3d %3d %3d %3d\n", 
              eventptr->rtpktptr->mincost[0], eventptr->rtpktptr->mincost[1],
              eventptr->rtpktptr->mincost[2], eventptr->rtpktptr->mincost[3]);
            }
          }
        clocktime = eventptr->evtime;    /* update time to next event time */
        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 { 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); }
        if (eventptr->evtype == FROM_LAYER2 ) 
          free(eventptr->rtpktptr);        /* free memory for packet, if any */
        free(eventptr);                    /* free memory for event struct   */
      }
   

terminate:
   printf("\nSimulator terminated at t=%f, no packets in medium\n", clocktime);
}



init()                         /* initialize the simulator */
{
  int i;
  float sum, avg;
  float jimsrand();
  struct event *evptr;  
  
   printf("Enter TRACE:");
   scanf("%d",&TRACE);

   srand(9999);              /* init random number generator */
   sum = 0.0;                /* test random number generator for students */
   for (i=0; i<1000; i++)
      sum=sum+jimsrand();    /* jimsrand() should be uniform in [0,1] */
   avg = sum/1000.0;
   if (avg < 0.25 || avg > 0.75) {
    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 jimsrand() in the emulator code. Sorry. \n");
    exit(0);
    }

   clocktime=0.0;                /* initialize time to 0.0 */
   rtinit0();
   rtinit1();
   rtinit2();
   rtinit3();

   /* 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);    
   }
  
}

/****************************************************************************/
/* jimsrand(): return a float in range [0,1].  The routine below is used to */
/* isolate all random number generation in one location.  We assume that the*/
/* system-supplied rand() function return an int in therange [0,mmm]        */
/****************************************************************************/
float jimsrand() 
{
  double mmm = 2147483647;   /* largest int  - MACHINE DEPENDENT!!!!!!!!   */
  float x;                   /* individual students may need to change mmm */ 
  x = rand()/mmm;            /* x should be uniform in [0,1] */
  return(x);
}  

/********************* EVENT HANDLINE ROUTINES *******/
/*  The next set of routines handle the event list   */
/*****************************************************/
 

insertevent(p)
   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;     /* q points to header of list in which p struct inserted */
   if (q==NULL) {   /* list is empty */
        evlist=p;
        p->next=NULL;
        p->prev=NULL;
        }
     else {
        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) { /* front 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()
{
  struct event *q;
  printf("--------------\nEvent List Follows:\n");
  for(q = evlist; q!=NULL; q=q->next) {
    printf("Event time: %f, type: %d entity: %d\n",q->evtime,q->evtype,q->eventity);
    }
  printf("--------------\n");
}


/************************** TOLAYER2 ***************/
tolayer2(packet)
  struct rtpkt packet;
  
{
 struct rtpkt *mypktptr;
 struct event *evptr, *q;
 float jimsrand(),lastime;
 int i;

 int connectcosts[4][4];

 /* initialize by hand since not all compilers allow array initilization */
 connectcosts[0][0]=0;  connectcosts[0][1]=1;  connectcosts[0][2]=3;
 connectcosts[0][3]=7;
 connectcosts[1][0]=1;  connectcosts[1][1]=0;  connectcosts[1][2]=1;
 connectcosts[1][3]=999;
 connectcosts[2][0]=3;  connectcosts[2][1]=1;  connectcosts[2][2]=0;
 connectcosts[2][3]=2;
 connectcosts[3][0]=7;  connectcosts[3][1]=999;  connectcosts[3][2]=2;
 connectcosts[3][3]=0;
    
 /* be nice: check if source and destination id's are reasonable */
 if (packet.sourceid<0 || packet.sourceid >3) {
   printf("WARNING: illegal source id in your packet, ignoring packet!\n");
   return;
   }
 if (packet.destid<0 || packet.destid >3) {
   printf("WARNING: illegal dest id in your packet, ignoring packet!\n");
   return;
   }
 if (packet.sourceid == packet.destid)  {
   printf("WARNING: source and destination id's the same, ignoring packet!\n");
   return;
   }
 if (connectcosts[packet.sourceid][packet.destid] == 999)  {
   printf("WARNING: source and destination not connected, ignoring packet!\n");
   return;
   }

/* make a copy of the packet student just gave me since he/she may decide */
/* to do something with the packet after we return back to him/her */ 
 mypktptr = (struct rtpkt *) malloc(sizeof(struct rtpkt));
 mypktptr->sourceid = packet.sourceid;
 mypktptr->destid = packet.destid;
 for (i=0; i<4; i++)
    mypktptr->mincost[i] = packet.mincost[i];
 if (TRACE>2)  {
   printf("    TOLAYER2: source: %d, dest: %d\n              costs:", 
          mypktptr->sourceid, mypktptr->destid);
   for (i=0; i<4; i++)
        printf("%d  ",mypktptr->mincost[i]);
    printf("\n");
   }

/* create future event for arrival of packet at the other side */
  evptr = (struct event *)malloc(sizeof(struct event));
  evptr->evtype =  FROM_LAYER2;   /* packet will pop out from layer3 */
  evptr->eventity = packet.destid; /* event occurs at other entity */
  evptr->rtpktptr = mypktptr;       /* save ptr to my copy of packet */

/* finally, compute the arrival time of packet at the other end.
   medium can not reorder, so make sure packet arrives between 1 and 10
   time units after the latest arrival time of packets
   cur
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Narue
0

1985 is calling, they want their code back. I'm not surprised if you're using a modern compiler and getting warnings about K&R style C.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.