-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.c
465 lines (392 loc) · 12.6 KB
/
emulator.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Michael lammens
* XLW945
* 11335630
* */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <list.h>
#include <protocol.h>
#define BUFFER_SIZE 2048
#define B_SEND_TIME 1
#define C_SEND_TIME 1
#define QUEUE_SIZE 500
//#define BPS 100
#define PSIZE 300
double first_start;
int BPS;
int token_bucket;
pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t dequeue_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t token_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t fwd_mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct {
LIST *data;
int cur_size;
int max_size;
}Queue;
/*
typedef struct {
int pack_id;
int source_port;
int num_bytes;
char start_time[30];
}Segment;
*/
typedef struct {
int throughput;
int token_bucket;
double min_rtt;
double max_additional_rtt;
int em_port, serv_port;
Queue *inf_queue;
}Emulator;
typedef struct {
Emulator* emulator;
Segment* dq_packet;
pthread_cond_t* cond;
pthread_mutex_t* mutex;
int is_done; // Flag to indicate if forward_after_prop_routine has finished
} ForwardArgs;
void init_queue(Emulator *eminem) {
eminem->inf_queue = malloc(sizeof(Queue));
if (eminem->inf_queue == NULL || eminem->inf_queue == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
eminem->inf_queue->cur_size = 0;
eminem->inf_queue->max_size = 1000;
eminem->inf_queue->data = ListCreate();
if (eminem->inf_queue->data == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
}
/*Create restriction methods on list for queue*/
/*Function to check queue is empty
*returns 1 if empty
0 if not empty
* */
int isEmpty(Queue* queue){
return (ListCount(queue->data)==0);
}
// -1 if failed
// 0 else
// Mallocs a clone of seg. Adds clone to List. Frees original
int enqueue(Queue* queue, Segment* seg){
/* heap our new segments memory so receiver_a doesnt overwrite it.
* Then queue.
*/
int prepend_status;
Segment* temp = (Segment*)malloc(sizeof(Segment));
temp->pack_id = seg->pack_id;
temp->source_port = seg->source_port;
temp->ws = seg->ws;
temp->num_bytes = seg->num_bytes;
strcpy(temp->start_time,seg->start_time);
// printf("Timer Val=%s\n", temp->start_time);
prepend_status = ListPrepend(queue->data,temp);
if(prepend_status==-1){
printf("\nPrepend failed\n");
exit(-1);
}
// free(seg->start_time);
free(seg);
queue->cur_size+=1;
return 0;
}
void* dequeue(Queue* queue){
Segment* deleted;
if(isEmpty(queue)){
return NULL;
}
deleted = ListTrim(queue->data);/*Freed in caller*/
queue->cur_size-=1;
return deleted;
}
void* queue_head(Queue* queue){
if(isEmpty(queue)){
return NULL;
}
return ListFirst(queue->data);
}
/*
int calc_total_delay(Emulator * emulator){
srand(time(NULL));
int additional = rand() % (emulator->max_additional_rtt + 1);
return emulator->min_rtt + additional;
}
*/
// Mallocs memory. Returns pointer to it.
// Callers responsibility to free
Segment* receive_packet_deserialize(int socket) {
char data[512];
struct sockaddr_in client_addr;
int bytes_received;
Segment* temp;
socklen_t addr_len = sizeof(client_addr);
memset(data, '\0', sizeof(data));
bytes_received = recvfrom(socket, data, sizeof(data), 0,
(struct sockaddr *)&client_addr,
&addr_len);
if (bytes_received == -1) {
perror("recvfrom");
exit(EXIT_FAILURE);
}
temp = (Segment*)malloc(sizeof(Segment)); // Allocate memory for temp
if (temp == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
deserialize(data, temp);
//current_unix_timestamp()
strcpy(temp->start_time, current_unix_timestamp());
return temp;
}
void increase_BPS(){
char* curTimeStamp_str = current_unix_timestamp();
double curTimeStamp = strtod(curTimeStamp_str, NULL);
if((int)(curTimeStamp - first_start) % 5 == 0){
BPS = BPS+50;
token_bucket=token_bucket+50;
}
printf("\nBPS:%d, Time: %s", BPS, curTimeStamp_str);
}
void *forward_after_prop_routine(void *arg){
/*MUTEXED AREA*/
ForwardArgs* delay_args = (ForwardArgs*)arg;
Emulator* emulator = delay_args->emulator;
/*DEQUEUE*/
pthread_mutex_lock(&queue_mutex);
Segment* dq_packet = dequeue(emulator->inf_queue);
pthread_mutex_unlock(&queue_mutex);
int pack_size = dq_packet->num_bytes;
/*WAIT FOR TOKENS*/
while(token_bucket - pack_size < 0){
;/*Busy wait until we have enough tokens*/
}
/*REMOVE x TOKENS FROM BUCKET*/
pthread_mutex_lock(&token_mutex);
token_bucket = token_bucket - pack_size;
pthread_mutex_unlock(&token_mutex);
delay_args->is_done = 1;
pthread_cond_signal(delay_args->cond);
/*MUTEXED AREA STOPS*/
/*DATARATE DELAY*/
double delay = (double)pack_size*10 / BPS; //calc_total_delay(emulator);
if(nano_second_sleep(delay)==0){
;
}else{perror("nanosleep");}
/*RELEASE TOKENS AFTER DATA DELAY*/
pthread_mutex_lock(&token_mutex);
token_bucket = token_bucket + pack_size;
pthread_mutex_unlock(&token_mutex);
/*MIN RTT(Rm) DELAY*/
nano_second_sleep(emulator->min_rtt);/*Non congestive delay min rtt*/
/*ADDITIONAL DELAY 0-D*/
double d_delay = generate_random_value(emulator->max_additional_rtt, (double)dq_packet->pack_id);
nano_second_sleep(d_delay);
/*Send to Server*/
int sockfd_b;
struct sockaddr_in dest_addr_b;
if ((sockfd_b = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
dest_addr_b.sin_family = AF_INET;
dest_addr_b.sin_addr.s_addr = inet_addr("127.0.0.1");
dest_addr_b.sin_port = htons(emulator->serv_port);
fflush(stdout);
char buffer_out[512] = {'\0'};
int bytes = serialize((Segment*)dq_packet, buffer_out);
sendto(sockfd_b, buffer_out,bytes+1, 0,
(struct sockaddr *)&dest_addr_b, sizeof(dest_addr_b));
/*FREE MEMORY AND CLOSE*/
free(dq_packet);
free(delay_args);
close(sockfd_b);
pthread_exit(NULL);
}
// Function that creates threa(forward_after_prop_routine)
// args(Emulator, Packet most recently Dequeued)
//
// Creates thread and finishes
void launch_forward_after_prop_thread(ForwardArgs* args) {
pthread_t forward_thread_pid;
int result;
result = pthread_create(&forward_thread_pid, NULL, forward_after_prop_routine, args);
if (result != 0) {
fprintf(stderr, "Error creating sleeper thread: %s\n", strerror(result));
exit(EXIT_FAILURE); // Exit the program in case of error
}
// Detach the sleeper thread
result = pthread_detach(forward_thread_pid);
if (result != 0) {
fprintf(stderr, "Error detaching sleeper thread: %s\n", strerror(result));
}
}
/* Always running. If theres a packet in queue, wait the relative time PackSize/BPS
* Before dequeue
*
* After dequeue -> send packet to thread for forwarding
* */
void *dequeue_background_routine(void *arg){
Emulator *emulator = (Emulator *)arg;
int pack_size;
pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_fwd = PTHREAD_COND_INITIALIZER;
while(1){
if(!isEmpty(emulator->inf_queue)){
ForwardArgs *args = malloc(sizeof(ForwardArgs));
if (args == NULL) {
// Handle memory allocation failure
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
args->emulator = emulator;
args->cond = &cond_fwd;
args->mutex = &cond_mutex;
args->is_done = 0; // Initialize the flag
/* WAIT HERE UNTIL SIGNALLED */
pthread_mutex_lock(&cond_mutex);
launch_forward_after_prop_thread(args);
while(!args->is_done){
pthread_cond_wait(&cond_fwd, &cond_mutex);
}
pthread_mutex_unlock(&cond_mutex);
}
}
return NULL;
}
// Function to launch the dequeue loop
// Creates it and returns
void launch_dequeue_background_thread(Emulator* emulator) {
pthread_t dequeue_thread_pid;
int result;
result = pthread_create(&dequeue_thread_pid, NULL, dequeue_background_routine, emulator);
if (result != 0) {
fprintf(stderr, "Error creating sleeper thread: %s\n", strerror(result));
exit(EXIT_FAILURE); // Exit the program in case of error
}
result = pthread_detach(dequeue_thread_pid);
if (result != 0) {
fprintf(stderr, "Error detaching sleeper thread: %s\n", strerror(result));
// Handle the error (e.g., log it), but continue execution
}
}
/*
* Waits to receive packet from B or C
* if packet from B:
* add to queue C
* launch thread that sleeps for d then sends packet to C + dequeue from C
* Vice Versa
* */
void *receive_and_process(void *arg) {
Emulator *emulator = (Emulator *)arg;
int sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd == -1) {
perror("Socket create failed");
}
int enqueue_status;
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(emulator->em_port);
if (bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(server_addr))
== -1) {
perror("Bind failed");
close(sock_fd);
exit(-1);
}
/*Every Emulator has a dequeue process running concurrently*/
launch_dequeue_background_thread(emulator);/*TODO See if we will have this thread manage individual
queues per client port? one queue total? have router determine server?*/
Segment* packet_from_client;
while (1) {
packet_from_client = receive_packet_deserialize(sock_fd); /*Returns Malloc'd Segment*/
pthread_mutex_lock(&queue_mutex);
enqueue_status = enqueue(emulator->inf_queue, packet_from_client);/*Clones packet. Frees packet_from_client*/
if(enqueue_status != 0){
printf("\nEnqueue Failed\n");
exit(EXIT_FAILURE);
}
pthread_mutex_unlock(&queue_mutex);
}
return NULL;
}
/* Emulator Initialization. Make this a module. */
void initialize_emulator_queue(Emulator *eminem) {
/* Queue object pointer */
eminem->inf_queue = malloc(sizeof(Queue));
if (eminem->inf_queue == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
eminem->inf_queue->cur_size = 0;
eminem->inf_queue->max_size = 500;
eminem->inf_queue->data = ListCreate();
/*Queue struct LIST Object Memory*/
if (eminem->inf_queue->data == NULL) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
}
}
void initialize_emulator(Emulator* emulator, char* argv[]) {
emulator->throughput = atoi(argv[1]);
// emulator->min_rtt = atoi(argv[2]);
// emulator->max_additional_rtt = atoi(argv[3]);
emulator->em_port = atoi(argv[4]);
emulator->serv_port = atoi(argv[5]);
/*Initialize queue.*/
init_queue(emulator);
}
// Launch emulator thread with emulator data provided
int launch_emulator_blocking(Emulator* emulator_data) {
pthread_t receiver_thread;
//realistically we should validate emulator fields here but im an optimist
if (pthread_create(&receiver_thread, NULL, receive_and_process, emulator_data) != 0) {
fprintf(stderr, "Error creating thread\n");
exit(EXIT_FAILURE);
}
if (pthread_join(receiver_thread, NULL) != 0) {
fprintf(stderr, "Error joining thread\n");
exit(EXIT_FAILURE);
}
return 0;
}
int main(int argc, char *argv[]) {
if (argc != 6) {
fprintf(stderr, "Usage: %s <data-rate(Bytes/s)>, Rm, D(Max)"
"<our_port> <server_port>\n",
argv[0]);
exit(EXIT_FAILURE);
}
printf("\n *** TO ADJUST SENDERS INTERVAL TIME. "
"CHANGE MACRO AT FILE TOP\n\n\n");
/*Create. Initialize. Launch Emulator. (Not up to date)*/
Emulator* emulator;
emulator = (Emulator*)malloc(sizeof(Emulator));
emulator->throughput = atoi(argv[1]);
emulator->min_rtt = strtod(argv[2],NULL);
emulator->max_additional_rtt = strtod(argv[3],NULL);
emulator->em_port = atoi(argv[4]);
emulator->serv_port = atoi(argv[5]);
/*Initialize queue.*/
init_queue(emulator);
BPS = atoi(argv[1]);
token_bucket = BPS;
char* curTimeStamp_str = current_unix_timestamp();
first_start = strtod(curTimeStamp_str, NULL);
// initialize_emulator(emulator, argv);
if(launch_emulator_blocking(emulator))
printf("Emulator Finished\n");
return 0;
}