Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* STUDENTS WRITE THE NEXT SEVEN ROUTINES *********/
- #include "Stud.h"
- #define SIZE 20
- #define TIMEOUT 20
- int A_SEQ;
- int B_SEQ;
- int FLAG = 0;
- int ACK_FLAG = 0;
- struct pkt backup;
- int add_sum(struct pkt packet){
- int checksum = packet.seqnum + packet.acknum;
- for(int i = 0; i < SIZE; i++)
- checksum += packet.payload[i];
- return checksum;
- }
- int error(char AorB, struct pkt packet)
- {
- if(AorB == 'A')
- {
- if(packet.acknum != A_SEQ)
- {
- printf("A Fel ack: %d\n", packet.seqnum);
- return 0;
- }
- else if(packet.checksum != add_sum(packet))
- {
- printf("A corrupted: %s\n", packet.payload);
- return 0;
- }
- }
- else
- {
- if(packet.seqnum != B_SEQ || packet.checksum != add_sum(packet))
- {
- packet.acknum = 1 - B_SEQ;
- packet.checksum = add_sum(packet);
- printf("B corrupted: %s\n", packet.payload);
- tolayer3(1, packet);
- return 0;
- }
- }
- return 1;
- }
- /* called from layer 5, passed the data to be sent to other side */
- void A_output(struct msg message)
- {
- if(ACK_FLAG || FLAG)
- {
- printf("Not yet acked dropping message %s \n",message.data);
- return;
- }
- struct pkt packet;
- packet.seqnum = A_SEQ;
- memmove(packet.payload, message.data, SIZE);
- packet.checksum = add_sum(packet);
- backup = packet;
- ACK_FLAG = 1;
- FLAG = 1;
- printf("A output %s\n", packet.payload);
- tolayer3(0, packet);
- starttimer(0, TIMEOUT);
- }
- /* called from layer 3, when a packet arrives for layer 4 */
- void A_input(struct pkt packet)
- {
- printf("A input acked: %s\n", packet.payload);
- if(!error('A', packet))
- return;
- stoptimer(0);
- FLAG = 0;
- A_SEQ = 1 - A_SEQ;
- B_SEQ = 1 - B_SEQ;
- ACK_FLAG = 0;
- }
- /* called when A's timer goes off */
- void A_timerinterrupt()
- {
- printf("Timerinterrupt: %s\n",backup.payload);
- tolayer3(0, backup);
- starttimer(0, TIMEOUT);
- }
- /* the following routine will be called once (only) before any other */
- /* entity A routines are called. You can use it to do any initialization */
- void A_init()
- {
- A_SEQ = 0;
- FLAG = 0;
- }
- /* Note that with simplex transfer from a-to-B, there is no B_output() */
- /* called from layer 3, when a packet arrives for layer 4 at B*/
- void B_input(struct pkt packet)
- {
- if(!error('B', packet))
- return;
- packet.acknum = B_SEQ;
- packet.checksum = add_sum(packet);
- printf("B input %s\n",packet.payload);
- tolayer3(1, packet);
- tolayer5(1, packet.payload);
- }
- /* the following rouytine will be called once (only) before any other */
- /* entity B routines are called. You can use it to do any initialization */
- void B_init()
- {
- B_SEQ = 0;
- }
- /* called when B's timer goes off */
- void B_timerinterrupt()
- {}
- void B_output(struct msg message) /* need be completed only for extra credit */
- {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement