-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.c
106 lines (76 loc) · 2.53 KB
/
listener.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils.h"
void pre_flush(void* address) {
flush(address + RECEIVER_READY_OFFSET);
flush(address + RECEIVER_RECV_OFFSET);
}
int main_loop(void* address, char *filename) {
char message[1024];
char packet = 0;
int index = 0;
int packet_count = 0;
FILE *fp;
fp = fopen(filename, "wb");
if (fp == NULL) {
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
while(1) {
int is_end = 0;
for(index = 0; index < 8; index++) {
// The receiver tells it is ready to read the bit
// until it receives a 0 or a 1
int bit = spam_question(
address,
RECEIVER_READY_OFFSET,
COMM_ONE_OFFSET,
COMM_ZERO_OFFSET);
// adds the data to the packet
packet |= bit << index;
// Confirms that the data is read
is_end = spam_question(address, RECEIVER_RECV_OFFSET, END, NOT_END);
//printf("Received %d (%d)\n", bit, (++index));
}
if(packet_count % 500 == 0) {
printf("\rReceived %d bytes\033[K", packet_count);
fflush(stdout);
}
packet_count++;
fwrite(&packet,1,1,fp);
if (is_end) {
printf("\rReceived %d bytes\033[K", packet_count);
// spam receiver ready so that the sender has a chance to break its loop
for (int i = 0; i < 10; i++) {
maccess(address + RECEIVER_READY_OFFSET);
repeated_sched_yield();
}
fclose(fp);
break;
}
packet = 0;
}
return packet_count;
}
int main(int argc, char** argv) {
if (argc < 3) {
printf("Need more arguments\n");
exit(2);
}
// Memory sharing
char* addr = load_lib(argv[1]);
// Base address for the communication
size_t offset;
sscanf(argv[2], "%lx", &offset);
// Cleans the cache to prevent false positives
pre_flush(addr);
struct timeval before, after;
gettimeofday(&before, NULL);
int bytes_read = main_loop((void*) addr + offset, argv[3]);
gettimeofday(&after, NULL);
int time_delta = after.tv_sec - before.tv_sec;
double speed = ((double) bytes_read) / ((double) time_delta);
printf("\nReceived %d bytes in %d seconds, speed is %0.4f B/s\n", bytes_read, time_delta, speed);
return 0;
}