0% found this document useful (0 votes)
139 views

Selective Repeat

This document contains C code that implements a simple data link layer protocol for reliable data transmission between two nodes. It includes functions for sending and receiving frames, starting and stopping timers, and waiting for events from the network layer or physical layer. The main function initializes variables and contains a loop that waits for events, handles them by calling appropriate functions, and updates frame transmission and reception state.

Uploaded by

Sugandh Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Selective Repeat

This document contains C code that implements a simple data link layer protocol for reliable data transmission between two nodes. It includes functions for sending and receiving frames, starting and stopping timers, and waiting for events from the network layer or physical layer. The main function initializes variables and contains a loop that waits for events, handles them by calling appropriate functions, and updates frame transmission and reception state.

Uploaded by

Sugandh Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream.h> #include<stdio.h> #include<conio.

h> #define MAX_SEQ 7 #define NR_BUFS ((MAX_SEQ+1)/2) #define MAX_PKT 1024 typedef enum{data,ack,nak}frame_kind; typedef struct{ unsigned char data[MAX_PKT]; }packet; typedef struct { frame_kind kind; int seq; int ack; packet info; }frame; void to_physical_layer(frame *s) { printf("\n to phy. layer"); } void from_physical_layer(frame *s) { printf("\n from phy. layer"); } void start_timer(int n) { printf("\n start timer"); } void stop_timer(int n) { printf("\n stop timer"); } void stop_ack_timer() { printf("\n stop timer"); } void start_ack_timer() { printf("\n start timer"); } int wait_for_event() { int n; printf("\n 1. network layer ready\n 2. frame arrival \n 3.cksum error \n 4.tim eout\n enter your choice"); scanf("%d",&n); return n; } typedef unsigned int seq_nr; typedef enum{frame_arrival,cksum_err,timeout,network_layer_ready,ack_timeout}eve nt_type; int no_nak=1; seq_nr oldest_frame=MAX_SEQ+1; int between(seq_nr a,seq_nr b, seq_nr c) { return((a<=b)&&(b<c))||((c<a)&&(a<=b))||((b<c)&&(c<a)); }

void send_frame(frame_kind fk,seq_nr frame_nr,seq_nr frame_expected,packet buffe r[]) { frame s; printf("\n this function construct and send data, ack,nak frame"); s.kind=fk; if(fk==data) s.info=buffer[frame_nr % NR_BUFS]; s.seq=frame_nr; s.ack=(frame_expected+MAX_SEQ)%(MAX_SEQ+1); if(fk==nak) no_nak=0; to_physical_layer(&s); printf("\n transmitting the frame"); if(fk==data) start_timer(frame_nr%NR_BUFS); stop_ack_timer(); } void from_network_layer(packet s[]) { printf("\n from network layer"); } void to_network_layer(packet s[]) { printf("\n to network layer"); } void enable_network_layer() { printf("\n network layer enabled"); } void disable_network_layer() { printf("\n network layer disabled"); } void wait_for_event(event_type *s) { printf("\n wait for the event"); } void main() { clrscr(); int choice; seq_nr ack_expected; seq_nr next_frame_to_send; seq_nr frame_expected; seq_nr too_far; int i,ch; frame r; packet out_buf[NR_BUFS]; packet in_buf[NR_BUFS]; int arrived[NR_BUFS]; seq_nr nbuffered; event_type event; enable_network_layer(); ack_expected=0; next_frame_to_send=0; frame_expected=0; too_far=NR_BUFS; nbuffered=0; for(i=0;i<NR_BUFS;i++) arrived[i]=0; printf("\n do u wanna enter more frames:1(yes) and 0(no)"); fflush(stdin);

scanf("%d",&ch); while(ch==1) { choice = wait_for_event(); switch(choice) { case 1: nbuffered=nbuffered+1; from_network_layer(&out_buf[next_frame_to_send%NR_BUFS]) ; send_frame(data,next_frame_to_send,frame_expected,out_bu f); next_frame_to_send++; break; case 2: from_physical_layer(&r); if(r.kind==data) { if((r.seq!=frame_expected)&&(no_nak)) send_frame(nak,0,frame_expected,out_buf); else start_ack_timer(); if(between(frame_expected,r.seq,too_far)&&(arrived[r.seq %NR_BUFS]==0)) { arrived[r.seq%NR_BUFS]=1; in_buf[r.seq%NR_BUFS]=r.info; while(arrived[frame_expected%NR_BUFS]) { to_network_layer(&in_buf[frame_expected%NR_BUFS]); no_nak=1; arrived[frame_expected%NR_BUFS]=0; frame_expected++; too_far++; start_ack_timer(); } } } if((r.kind==nak)&& between(ack_expected,(r.ack+1)%(MAX_SEQ+1),ne xt_frame_to_send)) send_frame(data,(r.ack+1)%(MAX_SEQ+1),frame_expected,out _buf); while(between(ack_expected,r.ack,next_frame_to_send)) { nbuffered=nbuffered-1; stop_timer(ack_expected%NR_BUFS); ack_expected++; } break; case 3: if(no_nak) send_frame(nak,0,frame_expected,out_buf); break; case 4: send_frame(data,oldest_frame,frame_expected,out_buf); break; case 5: send_frame(ack,0,frame_expected,out_buf); } if(nbuffered<NR_BUFS)

enable_network_layer(); else disable_network_layer(); } getch(); }

You might also like