Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions main_dsender.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#include<stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "src/include/comm.h"
#include "src/include/datalink.h"

int main(){


int main(int argc, char *argv[]) {

srand(time(NULL));

if (argc >= 2) {
int rate = atoi(argv[1]);
set_loss_rate(rate);
printf("Fault Injection Enabled: drop 1 in %d frames\n", rate);
}

dsender();
return 0;
}
}
3 changes: 2 additions & 1 deletion src/include/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
#include "packet.h"

void send_frame(int fd, frame_t *f);
int receive_frame(int fd, frame_t *f);
int receive_frame(int fd, frame_t *f);
void set_loss_rate(int rate);
12 changes: 12 additions & 0 deletions src/src/comm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@
#include<unistd.h>
#include "../include/packet.h"
#include "../include/comm.h"
#include <stdlib.h>
#include <time.h>

static int loss_rate = 0; // 0 = no loss, N = drop 1 in N frames


void send_frame(int fd, frame_t *f){
if (loss_rate > 0 && rand() % loss_rate == 0) {
printf("FAULT INJECTION: Frame dropped intentionally\n");
return;
}
ssize_t bytes_written = write(fd, f, sizeof(*f));
if(bytes_written > 0){
printf("Sender: Data sent successfully!\n");
Expand All @@ -27,5 +36,8 @@ int receive_frame(int fd, frame_t *f){
printf("Receiver: Error Reading file\n");
return 0;
}
void set_loss_rate(int rate) {
loss_rate = rate;
}