-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
228 lines (201 loc) · 6.22 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <math.h>
#include <sys/time.h>
#include "common.h"
#include "util.h"
#include "input.h"
#include "communicate.h"
#include "receiver.h"
#include "sender.h"
#include "crc32.h"
int main(int argc, char *argv[])
{
//fprintf(stderr, "size of frame = %lu\n", sizeof(Frame));
pthread_t stdin_thread;
pthread_t * sender_threads;
pthread_t * receiver_threads;
int i;
unsigned char print_usage = 0;
//DO NOT CHANGE THIS
//Set the number of bits to corrupt
CORRUPTION_BITS = (int) MAX_FRAME_SIZE/2;
//DO NOT CHANGE THIS
//Prepare the glb_sysconfig object
glb_sysconfig.drop_prob = 0;
glb_sysconfig.corrupt_prob = 0;
glb_sysconfig.automated = 0;
memset(glb_sysconfig.automated_file,
0,
AUTOMATED_FILENAME);
//DO NOT CHANGE THIS
//Prepare other variables and seed the psuedo random number generator
glb_receivers_array_length = -1;
glb_senders_array_length = -1;
srand(time(NULL));
//Parse out the command line arguments
for(i=1; i < argc;)
{
if(strcmp(argv[i], "-s") == 0)
{
sscanf(argv[i+1],
"%d",
&glb_senders_array_length);
i += 2;
}
else if(strcmp(argv[i], "-r") == 0)
{
sscanf(argv[i+1],
"%d",
&glb_receivers_array_length);
i += 2;
}
else if(strcmp(argv[i], "-d") == 0)
{
sscanf(argv[i+1],
"%f",
&glb_sysconfig.drop_prob);
i += 2;
}
else if(strcmp(argv[i], "-c") == 0)
{
sscanf(argv[i+1],
"%f",
&glb_sysconfig.corrupt_prob);
i += 2;
}
else if(strcmp(argv[i], "-a") == 0)
{
int filename_len = strlen(argv[i+1]);
if (filename_len < AUTOMATED_FILENAME)
{
glb_sysconfig.automated = 1;
strcpy(glb_sysconfig.automated_file,
argv[i+1]);
}
i += 2;
}
else if(strcmp(argv[i], "-h") == 0)
{
print_usage=1;
i++;
}
else
{
i++;
}
}
//Spot check the input variables
if (glb_senders_array_length <= 0 ||
glb_receivers_array_length <= 0 ||
(glb_sysconfig.drop_prob < 0 || glb_sysconfig.drop_prob > 1) ||
(glb_sysconfig.corrupt_prob < 0 || glb_sysconfig.corrupt_prob > 1) ||
print_usage)
{
fprintf(stderr, "USAGE: etherchat \n -r int [# of receivers] \n -s int [# of senders] \n -c float [0 <= corruption prob <= 1] \n -d float [0 <= drop prob <= 1]\n");
exit(1);
}
if(glb_senders_array_length > glb_receivers_array_length)
MAX_COM_ID = glb_senders_array_length;
else
MAX_COM_ID = glb_receivers_array_length;
fprintf(stderr, "MAX_COM_ID = %d\n", MAX_COM_ID);
//DO NOT CHANGE THIS
//Init the pthreads data structure
sender_threads = (pthread_t *) malloc(sizeof(pthread_t) * glb_senders_array_length);
receiver_threads = (pthread_t *) malloc(sizeof(pthread_t) * glb_receivers_array_length);
//Init the global senders array
glb_senders_array = (Sender *) malloc(glb_senders_array_length * sizeof(Sender));
glb_receivers_array = (Receiver *) malloc(glb_receivers_array_length * sizeof(Receiver));
fprintf(stderr, "Messages will be dropped with probability=%f\n", glb_sysconfig.drop_prob);
fprintf(stderr, "Messages will be corrupted with probability=%f\n", glb_sysconfig.corrupt_prob);
fprintf(stderr, "Available sender id(s):\n");
//Init sender objects, assign ids
//NOTE: Do whatever initialization you want here or inside the init_sender function
for (i = 0;
i < glb_senders_array_length;
i++)
{
init_sender(&glb_senders_array[i], i);
fprintf(stderr, " send_id=%d\n", i);
}
//Init receiver objects, assign ids
//NOTE: Do whatever initialization you want here or inside the init_receiver function
fprintf(stderr, "Available receiver id(s):\n");
for (i = 0;
i < glb_receivers_array_length;
i++)
{
init_receiver(&glb_receivers_array[i], i);
fprintf(stderr, " recv_id=%d\n", i);
}
//DO NOT CHANGE THIS
//Create the standard input thread
int rc = pthread_create(&stdin_thread,
NULL,
run_stdinthread,
(void *) 0);
if (rc)
{
fprintf(stderr, "ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
//Spawn sender threads
for (i = 0;
i < glb_senders_array_length;
i++)
{
rc = pthread_create(sender_threads+i,
NULL,
run_sender,
(void *) &glb_senders_array[i]);
if (rc){
fprintf(stderr, "ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
//Spawn receiver threads
for (i = 0;
i < glb_receivers_array_length;
i++)
{
rc = pthread_create(receiver_threads+i,
NULL,
run_receiver,
(void *) &glb_receivers_array[i]);
if (rc){
fprintf(stderr, "ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
fprintf(stderr, "\n");
pthread_join(stdin_thread, NULL);
for (i = 0;
i < glb_senders_array_length;
i++)
{
destroy_sender(&glb_senders_array[i]);
pthread_cancel(sender_threads[i]);
pthread_join(sender_threads[i], NULL);
}
for (i = 0;
i < glb_receivers_array_length;
i++)
{
destroy_receiver(&glb_receivers_array[i]);
pthread_cancel(receiver_threads[i]);
pthread_join(receiver_threads[i], NULL);
}
free(sender_threads);
free(receiver_threads);
free(glb_senders_array);
free(glb_receivers_array);
return 0;
}