Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. /********* STUDENTS WRITE THE NEXT SEVEN ROUTINES *********/
  2.  
  3. #include "Stud.h"
  4.  
  5. #define SIZE 20
  6. #define TIMEOUT 20
  7.  
  8. int A_SEQ;
  9. int B_SEQ;
  10. int FLAG = 0;
  11. int ACK_FLAG = 0;
  12.  
  13. struct pkt backup;
  14.  
  15. int add_sum(struct pkt packet){
  16. int checksum = packet.seqnum + packet.acknum;
  17. for(int i = 0; i < SIZE; i++)
  18. checksum += packet.payload[i];
  19. return checksum;
  20. }
  21.  
  22. int error(char AorB, struct pkt packet)
  23. {
  24. if(AorB == 'A')
  25. {
  26. if(packet.acknum != A_SEQ)
  27. {
  28. printf("A Fel ack: %d\n", packet.seqnum);
  29. return 0;
  30. }
  31. else if(packet.checksum != add_sum(packet))
  32. {
  33. printf("A corrupted: %s\n", packet.payload);
  34. return 0;
  35. }
  36. }
  37. else
  38. {
  39. if(packet.seqnum != B_SEQ || packet.checksum != add_sum(packet))
  40. {
  41. packet.acknum = 1 - B_SEQ;
  42. packet.checksum = add_sum(packet);
  43. printf("B corrupted: %s\n", packet.payload);
  44. tolayer3(1, packet);
  45. return 0;
  46. }
  47.  
  48. }
  49. return 1;
  50. }
  51.  
  52. /* called from layer 5, passed the data to be sent to other side */
  53. void A_output(struct msg message)
  54. {
  55. if(ACK_FLAG || FLAG)
  56. {
  57. printf("Not yet acked dropping message %s \n",message.data);
  58. return;
  59. }
  60.  
  61.  
  62. struct pkt packet;
  63. packet.seqnum = A_SEQ;
  64. memmove(packet.payload, message.data, SIZE);
  65. packet.checksum = add_sum(packet);
  66. backup = packet;
  67.  
  68. ACK_FLAG = 1;
  69. FLAG = 1;
  70. printf("A output %s\n", packet.payload);
  71. tolayer3(0, packet);
  72. starttimer(0, TIMEOUT);
  73.  
  74. }
  75.  
  76. /* called from layer 3, when a packet arrives for layer 4 */
  77. void A_input(struct pkt packet)
  78. {
  79. printf("A input acked: %s\n", packet.payload);
  80. if(!error('A', packet))
  81. return;
  82. stoptimer(0);
  83. FLAG = 0;
  84.  
  85. A_SEQ = 1 - A_SEQ;
  86. B_SEQ = 1 - B_SEQ;
  87. ACK_FLAG = 0;
  88. }
  89.  
  90. /* called when A's timer goes off */
  91. void A_timerinterrupt()
  92. {
  93. printf("Timerinterrupt: %s\n",backup.payload);
  94. tolayer3(0, backup);
  95. starttimer(0, TIMEOUT);
  96. }
  97.  
  98. /* the following routine will be called once (only) before any other */
  99. /* entity A routines are called. You can use it to do any initialization */
  100. void A_init()
  101. {
  102. A_SEQ = 0;
  103. FLAG = 0;
  104. }
  105.  
  106. /* Note that with simplex transfer from a-to-B, there is no B_output() */
  107.  
  108. /* called from layer 3, when a packet arrives for layer 4 at B*/
  109. void B_input(struct pkt packet)
  110. {
  111.  
  112. if(!error('B', packet))
  113. return;
  114. packet.acknum = B_SEQ;
  115. packet.checksum = add_sum(packet);
  116. printf("B input %s\n",packet.payload);
  117. tolayer3(1, packet);
  118. tolayer5(1, packet.payload);
  119.  
  120. }
  121. /* the following rouytine will be called once (only) before any other */
  122. /* entity B routines are called. You can use it to do any initialization */
  123. void B_init()
  124. {
  125. B_SEQ = 0;
  126. }
  127.  
  128. /* called when B's timer goes off */
  129. void B_timerinterrupt()
  130. {}
  131. void B_output(struct msg message) /* need be completed only for extra credit */
  132. {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement