Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.84 KB | None | 0 0
  1. /*****************************************************************
  2. ***************** NETWORK EMULATION CODE STARTS BELOW ***********
  3. The code below emulates the layer 3 and below network environment:
  4. - emulates the tranmission and delivery (possibly with bit-level corruption
  5. and packet loss) of packets across the layer 3/4 interface
  6. - handles the starting/stopping of a timer, and generates timer
  7. interrupts (resulting in calling students timer handler).
  8. - generates message to be sent (passed from later 5 to 4)
  9.  
  10. THERE IS NOT REASON THAT ANY STUDENT SHOULD HAVE TO READ OR UNDERSTAND
  11. THE CODE BELOW. YOU SHOLD NOT TOUCH, OR REFERENCE (in your code) ANY
  12. OF THE DATA STRUCTURES BELOW. If you're interested in how I designed
  13. the emulator, you're welcome to look at the code - but again, you should have
  14. to, and you defeinitely should not have to modify
  15. ******************************************************************/
  16.  
  17. #include "Stud.h"
  18.  
  19.  
  20. struct event {
  21. float evtime; /* event time */
  22. int evtype; /* event type code */
  23. int eventity; /* entity where event occurs */
  24. struct pkt *pktptr; /* ptr to packet (if any) assoc w/ this event */
  25. struct event *prev;
  26. struct event *next;
  27. };
  28.  
  29. struct event *evlist = NULL; /* the event list */
  30.  
  31. /* possible events: */
  32. #define TIMER_INTERRUPT 0
  33. #define FROM_LAYER5 1
  34. #define FROM_LAYER3 2
  35.  
  36. #define OFF 0
  37. #define ON 1
  38. #define A 0
  39. #define B 1
  40.  
  41.  
  42.  
  43. int TRACE = 1; /* for my debugging */
  44. int nsim = 0; /* number of messages from 5 to 4 so far */
  45. int nsimmax = 0; /* number of msgs to generate, then stop */
  46. float time = 0.000;
  47. float lossprob; /* probability that a packet is dropped */
  48. float corruptprob; /* probability that one bit is packet is flipped */
  49. float lambda; /* arrival rate of messages from layer 5 */
  50. int ntolayer3; /* number sent into layer 3 */
  51. int nlost; /* number lost in media */
  52. int ncorrupt; /* number corrupted by media*/
  53.  
  54. main()
  55. {
  56. struct event *eventptr;
  57. struct msg msg2give;
  58. struct pkt pkt2give;
  59.  
  60. int i, j;
  61. char c;
  62.  
  63. init();
  64. A_init();
  65. B_init();
  66.  
  67. while (1) {
  68. eventptr = evlist; /* get next event to simulate */
  69. if (eventptr == NULL)
  70. goto terminate;
  71. evlist = evlist->next; /* remove this event from event list */
  72. if (evlist != NULL)
  73. evlist->prev = NULL;
  74. if (TRACE >= 2) {
  75. printf("\nEVENT time: %f,", eventptr->evtime);
  76. printf(" type: %d", eventptr->evtype);
  77. if (eventptr->evtype == 0)
  78. printf(", timerinterrupt ");
  79. else if (eventptr->evtype == 1)
  80. printf(", fromlayer5 ");
  81. else
  82. printf(", fromlayer3 ");
  83. printf(" entity: %d\n", eventptr->eventity);
  84. }
  85. time = eventptr->evtime; /* update time to next event time */
  86. if (nsim == nsimmax)
  87. break; /* all done with simulation */
  88. if (eventptr->evtype == FROM_LAYER5) {
  89. generate_next_arrival(); /* set up future arrival */
  90. /* fill in msg to give with string of same letter */
  91. j = nsim % 26;
  92. for (i = 0; i<20; i++)
  93. msg2give.data[i] = 97 + j;
  94. if (TRACE>2) {
  95. printf(" MAINLOOP: data given to student: ");
  96. for (i = 0; i<20; i++)
  97. printf("%c", msg2give.data[i]);
  98. printf("\n");
  99. }
  100. nsim++;
  101. if (eventptr->eventity == A)
  102. A_output(msg2give);
  103. else
  104. B_output(msg2give);
  105. }
  106. else if (eventptr->evtype == FROM_LAYER3) {
  107. pkt2give.seqnum = eventptr->pktptr->seqnum;
  108. pkt2give.acknum = eventptr->pktptr->acknum;
  109. pkt2give.checksum = eventptr->pktptr->checksum;
  110. for (i = 0; i<20; i++)
  111. pkt2give.payload[i] = eventptr->pktptr->payload[i];
  112. if (eventptr->eventity == A) /* deliver packet by calling */
  113. A_input(pkt2give); /* appropriate entity */
  114. else
  115. B_input(pkt2give);
  116. free(eventptr->pktptr); /* free the memory for packet */
  117. }
  118. else if (eventptr->evtype == TIMER_INTERRUPT) {
  119. if (eventptr->eventity == A)
  120. A_timerinterrupt();
  121. else
  122. B_timerinterrupt();
  123. }
  124. else {
  125. printf("INTERNAL PANIC: unknown event type \n");
  126. }
  127. free(eventptr);
  128. }
  129.  
  130. terminate:
  131. printf(" Simulator terminated at time %f\n after sending %d msgs from layer5\n", time, nsim);
  132. }
  133.  
  134.  
  135.  
  136. init() /* initialize the simulator */
  137. {
  138. int i;
  139. float sum, avg;
  140. float jimsrand();
  141.  
  142.  
  143. printf("----- Stop and Wait Network Simulator Version 1.1 -------- \n\n");
  144. printf("Enter the number of messages to simulate: ");
  145. scanf_s("%d", &nsimmax);
  146. printf("Enter packet loss probability [enter 0.0 for no loss]:");
  147. scanf_s("%f", &lossprob);
  148. printf("Enter packet corruption probability [0.0 for no corruption]:");
  149. scanf_s("%f", &corruptprob);
  150. printf("Enter average time between messages from sender's layer5 [ > 0.0]:");
  151. scanf_s("%f", &lambda);
  152. printf("Enter TRACE:");
  153. scanf_s("%d", &TRACE);
  154.  
  155. srand(9999); /* init random number generator */
  156. sum = 0.0; /* test random number generator for students */
  157. for (i = 0; i<1000; i++)
  158. sum = sum + jimsrand(); /* jimsrand() should be uniform in [0,1] */
  159. avg = sum / 1000.0;
  160. if (avg < 0.25 || avg > 0.75) {
  161. printf("It is likely that random number generation on your machine\n");
  162. printf("is different from what this emulator expects. Please take\n");
  163. printf("a look at the routine jimsrand() in the emulator code. Sorry. \n");
  164.  
  165. //LAGT TILL 0
  166. exit(0);
  167. }
  168.  
  169. ntolayer3 = 0;
  170. nlost = 0;
  171. ncorrupt = 0;
  172.  
  173. time = 0.0; /* initialize time to 0.0 */
  174. generate_next_arrival(); /* initialize event list */
  175. }
  176.  
  177. /****************************************************************************/
  178. /* jimsrand(): return a float in range [0,1]. The routine below is used to */
  179. /* isolate all random number generation in one location. We assume that the*/
  180. /* system-supplied rand() function return an int in therange [0,mmm] */
  181. /****************************************************************************/
  182. float jimsrand()
  183. {
  184. //ÄNDRAT EFTERSOM DET VERKAR VARA FEL
  185. //double mmm = 2147483647; /* largest int - MACHINE DEPENDENT!!!!!!!! */
  186. double mmm = RAND_MAX;
  187.  
  188. float x; /* individual students may need to change mmm */
  189. x = rand() / mmm; /* x should be uniform in [0,1] */
  190. return(x);
  191. }
  192.  
  193. /********************* EVENT HANDLINE ROUTINES *******/
  194. /* The next set of routines handle the event list */
  195. /*****************************************************/
  196.  
  197. generate_next_arrival()
  198. {
  199. double x, log(), ceil();
  200. struct event *evptr;
  201.  
  202. //TAGIT BORT EFTERSOM DEN SÄGER: REFDEFINITION; DIFFERENT BASIC TYPES
  203. //char *malloc();
  204. float ttime;
  205. int tempint;
  206.  
  207. if (TRACE>2)
  208. printf(" GENERATE NEXT ARRIVAL: creating new arrival\n");
  209.  
  210. x = lambda*jimsrand() * 2; /* x is uniform on [0,2*lambda] */
  211. /* having mean of lambda */
  212. evptr = (struct event *)malloc(sizeof(struct event));
  213. evptr->evtime = time + x;
  214. evptr->evtype = FROM_LAYER5;
  215. if (BIDIRECTIONAL && (jimsrand()>0.5))
  216. evptr->eventity = B;
  217. else
  218. evptr->eventity = A;
  219. insertevent(evptr);
  220. }
  221.  
  222.  
  223. insertevent(p)
  224. struct event *p;
  225. {
  226. struct event *q, *qold;
  227.  
  228. if (TRACE>2) {
  229. printf(" INSERTEVENT: time is %lf\n", time);
  230. printf(" INSERTEVENT: future time will be %lf\n", p->evtime);
  231. }
  232. q = evlist; /* q points to header of list in which p struct inserted */
  233. if (q == NULL) { /* list is empty */
  234. evlist = p;
  235. p->next = NULL;
  236. p->prev = NULL;
  237. }
  238. else {
  239. for (qold = q; q != NULL && p->evtime > q->evtime; q = q->next)
  240. qold = q;
  241. if (q == NULL) { /* end of list */
  242. qold->next = p;
  243. p->prev = qold;
  244. p->next = NULL;
  245. }
  246. else if (q == evlist) { /* front of list */
  247. p->next = evlist;
  248. p->prev = NULL;
  249. p->next->prev = p;
  250. evlist = p;
  251. }
  252. else { /* middle of list */
  253. p->next = q;
  254. p->prev = q->prev;
  255. q->prev->next = p;
  256. q->prev = p;
  257. }
  258. }
  259. }
  260.  
  261. printevlist()
  262. {
  263. struct event *q;
  264. int i;
  265. printf("--------------\nEvent List Follows:\n");
  266. for (q = evlist; q != NULL; q = q->next) {
  267. printf("Event time: %f, type: %d entity: %d\n", q->evtime, q->evtype, q->eventity);
  268. }
  269. printf("--------------\n");
  270. }
  271.  
  272.  
  273.  
  274. /********************** Student-callable ROUTINES ***********************/
  275.  
  276. /* called by students routine to cancel a previously-started timer */
  277. stoptimer(AorB)
  278. int AorB; /* A or B is trying to stop timer */
  279. {
  280. struct event *q, *qold;
  281.  
  282. if (TRACE>2)
  283. printf(" STOP TIMER: stopping timer at %f\n", time);
  284. /* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
  285. for (q = evlist; q != NULL; q = q->next)
  286. if ((q->evtype == TIMER_INTERRUPT && q->eventity == AorB)) {
  287. /* remove this event */
  288. if (q->next == NULL && q->prev == NULL)
  289. evlist = NULL; /* remove first and only event on list */
  290. else if (q->next == NULL) /* end of list - there is one in front */
  291. q->prev->next = NULL;
  292. else if (q == evlist) { /* front of list - there must be event after */
  293. q->next->prev = NULL;
  294. evlist = q->next;
  295. }
  296. else { /* middle of list */
  297. q->next->prev = q->prev;
  298. q->prev->next = q->next;
  299. }
  300. free(q);
  301. return;
  302. }
  303. printf("Warning: unable to cancel your timer. It wasn't running.\n");
  304. }
  305.  
  306.  
  307. starttimer(AorB, increment)
  308. int AorB; /* A or B is trying to stop timer */
  309. float increment;
  310. {
  311.  
  312. struct event *q;
  313. struct event *evptr;
  314. //TAGIT BORT EFTERSOM DEN SÄGER: REFDEFINITION; DIFFERENT BASIC TYPES
  315. //char *malloc();
  316.  
  317. if (TRACE>2)
  318. printf(" START TIMER: starting timer at %f\n", time);
  319. /* be nice: check to see if timer is already started, if so, then warn */
  320. /* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
  321. for (q = evlist; q != NULL; q = q->next)
  322. if ((q->evtype == TIMER_INTERRUPT && q->eventity == AorB)) {
  323. printf("Warning: attempt to start a timer that is already started\n");
  324. return;
  325. }
  326.  
  327. /* create future event for when timer goes off */
  328. evptr = (struct event *)malloc(sizeof(struct event));
  329. evptr->evtime = time + increment;
  330. evptr->evtype = TIMER_INTERRUPT;
  331. evptr->eventity = AorB;
  332. insertevent(evptr);
  333. }
  334.  
  335.  
  336. /************************** TOLAYER3 ***************/
  337. tolayer3(AorB, packet)
  338. int AorB; /* A or B is trying to stop timer */
  339. struct pkt packet;
  340. {
  341. struct pkt *mypktptr;
  342. struct event *evptr, *q;
  343. //TAGIT BORT EFTERSOM DEN SÄGER: REFDEFINITION; DIFFERENT BASIC TYPES
  344. //char *malloc();
  345. float lastime, x, jimsrand();
  346. int i;
  347.  
  348.  
  349. ntolayer3++;
  350.  
  351. /* simulate losses: */
  352. if (jimsrand() < lossprob) {
  353. nlost++;
  354. if (TRACE>0)
  355. printf(" TOLAYER3: packet being lost\n");
  356. return;
  357. }
  358.  
  359. /* make a copy of the packet student just gave me since he/she may decide */
  360. /* to do something with the packet after we return back to him/her */
  361. mypktptr = (struct pkt *)malloc(sizeof(struct pkt));
  362. mypktptr->seqnum = packet.seqnum;
  363. mypktptr->acknum = packet.acknum;
  364. mypktptr->checksum = packet.checksum;
  365. for (i = 0; i<20; i++)
  366. mypktptr->payload[i] = packet.payload[i];
  367. if (TRACE>2) {
  368. printf(" TOLAYER3: seq: %d, ack %d, check: %d ", mypktptr->seqnum,
  369. mypktptr->acknum, mypktptr->checksum);
  370. for (i = 0; i<20; i++)
  371. printf("%c", mypktptr->payload[i]);
  372. printf("\n");
  373. }
  374.  
  375. /* create future event for arrival of packet at the other side */
  376. evptr = (struct event *)malloc(sizeof(struct event));
  377. evptr->evtype = FROM_LAYER3; /* packet will pop out from layer3 */
  378. evptr->eventity = (AorB + 1) % 2; /* event occurs at other entity */
  379. evptr->pktptr = mypktptr; /* save ptr to my copy of packet */
  380. /* finally, compute the arrival time of packet at the other end.
  381. medium can not reorder, so make sure packet arrives between 1 and 10
  382. time units after the latest arrival time of packets
  383. currently in the medium on their way to the destination */
  384. lastime = time;
  385. /* for (q=evlist; q!=NULL && q->next!=NULL; q = q->next) */
  386. for (q = evlist; q != NULL; q = q->next)
  387. if ((q->evtype == FROM_LAYER3 && q->eventity == evptr->eventity))
  388. lastime = q->evtime;
  389. evptr->evtime = lastime + 1 + 9 * jimsrand();
  390.  
  391.  
  392.  
  393. /* simulate corruption: */
  394. if (jimsrand() < corruptprob) {
  395. ncorrupt++;
  396. if ((x = jimsrand()) < .75)
  397. mypktptr->payload[0] = 'Z'; /* corrupt payload */
  398. else if (x < .875)
  399. mypktptr->seqnum = 999999;
  400. else
  401. mypktptr->acknum = 999999;
  402. if (TRACE>0)
  403. printf(" TOLAYER3: packet being corrupted\n");
  404. }
  405.  
  406. if (TRACE>2)
  407. printf(" TOLAYER3: scheduling arrival on other side\n");
  408. insertevent(evptr);
  409. }
  410.  
  411. tolayer5(AorB, datasent)
  412. int AorB;
  413. char datasent[20];
  414. {
  415. int i;
  416. if (TRACE>2) {
  417. printf(" TOLAYER5: data received: ");
  418. for (i = 0; i<20; i++)
  419. printf("%c", datasent[i]);
  420. printf("\n");
  421. }
  422.  
  423. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement