-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent2B.c
92 lines (74 loc) · 2.85 KB
/
student2B.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "project2.h"
/* ***************************************************************************
ALTERNATING BIT AND GO-BACK-N NETWORK EMULATOR: VERSION 1.1 J.F.Kurose
This code should be used for Project 2, unidirectional or bidirectional
data transfer protocols from A to B and B to A.
Network properties:
- one way network delay averages five time units (longer if there
are other messages in the channel for GBN), but can be larger
- packets can be corrupted (either the header or the data portion)
or lost, according to user-defined probabilities
- packets may be delivered out of order.
Compile as gcc -g project2.c student2.c -o p2
**********************************************************************/
/********* STUDENTS WRITE THE NEXT SEVEN ROUTINES *********/
/*
* The routines you will write are detailed below. As noted above,
* such procedures in real-life would be part of the operating system,
* and would be called by other procedures in the operating system.
* All these routines are in layer 4.
*/
// expected sequence number
int rev_seqnum;
/*
* Just like A_output, but residing on the B side. USED only when the
* implementation is bi-directional.
*/
void B_output(struct msg message) {
}
/*
* Note that with simplex transfer from A-to-B, there is no routine B_output()
*/
/*
* B_input(packet),where packet is a structure of type pkt. This routine
* will be called whenever a packet sent from the A-side (i.e., as a result
* of a tolayer3() being done by a A-side procedure) arrives at the B-side.
* packet is the (possibly corrupted) packet sent from the A-side.
*/
void B_input(struct pkt packet) {
struct msg message; // Store message retrived from network
struct pkt ack; // Store ack packet
// Pass message to layer 5 when it not corrupted and when it has expected seqnum
if (not_corrupted(packet) && (packet.seqnum == rev_seqnum)) {
strncpy(message.data, packet.payload, MESSAGE_LENGTH);
tolayer5(BEntity, message);
// Make ack message
ack = prepare_pkt(1, rev_seqnum, NULL);
// flip expected sequence number of packet
rev_seqnum = !rev_seqnum;
} else {
// Make ack message
ack = prepare_pkt(1, !rev_seqnum, NULL);
}
// Send ack message
tolayer3(BEntity, ack);
}
/*
* B_timerinterrupt() This routine will be called when B's timer expires
* (thus generating a timer interrupt). You'll probably want to use this
* routine to control the retransmission of packets. See starttimer()
* and stoptimer() in the writeup for how the timer is started and stopped.
*/
void B_timerinterrupt() {
}
/*
* The following routine will be called once (only) before any other
* entity B routines are called. You can use it to do any initialization
*/
void B_init() {
// Initial sequence number is 0
rev_seqnum = 0;
}