-
Notifications
You must be signed in to change notification settings - Fork 1
/
communicate.c
118 lines (106 loc) · 3.56 KB
/
communicate.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
#include "communicate.h"
//*********************************************************************
//NOTE: We will overwrite this file, so whatever changes you put here
// WILL NOT persist
//*********************************************************************
void send_frame(char * char_buffer,
enum SendFrame_DstType dst_type)
{
int i = 0, j;
char * per_recv_char_buffer;
//Multiply out the probabilities to some degree of precision
int prob_prec = 1000;
int drop_prob = (int) prob_prec * glb_sysconfig.drop_prob;
int corrupt_prob = (int) prob_prec * glb_sysconfig.corrupt_prob;
int num_corrupt_bits = CORRUPTION_BITS;
int corrupt_indices[num_corrupt_bits];
//Pick a random number
int random_num = rand() % prob_prec;
int random_index;
//Drop the packet on the floor
if (random_num < drop_prob)
{
free(char_buffer);
return;
}
//Determine whether to corrupt bits
random_num = rand() % prob_prec;
if (random_num < corrupt_prob)
{
//Pick random indices to corrupt
for (i=0;
i < num_corrupt_bits;
i++)
{
random_index = rand() % MAX_FRAME_SIZE;
corrupt_indices[i] = random_index;
}
}
//Determine the array size of the destination objects
int array_length = 0;
if (dst_type == ReceiverDst)
{
array_length = glb_receivers_array_length;
}
else if (dst_type == SenderDst)
{
array_length = glb_senders_array_length;
}
//Go through the dst array and add the packet to their receive queues
for (i=0;
i < array_length;
i++)
{
//Allocate a per receiver char buffer for the message
per_recv_char_buffer = (char *) malloc(sizeof(char) * MAX_FRAME_SIZE);
memcpy(per_recv_char_buffer,
char_buffer,
MAX_FRAME_SIZE);
//Corrupt the bits (inefficient, should just corrupt one copy and memcpy it
if (random_num < corrupt_prob)
{
//Corrupt bits at the chosen random indices
for (j=0;
j < num_corrupt_bits;
j++)
{
random_index = corrupt_indices[j];
per_recv_char_buffer[random_index] = ~per_recv_char_buffer[random_index];
}
}
if (dst_type == ReceiverDst)
{
Receiver * dst = &glb_receivers_array[i];
pthread_mutex_lock(&dst->buffer_mutex);
ll_append_node(&dst->input_framelist_head,
(void *) per_recv_char_buffer);
pthread_cond_signal(&dst->buffer_cv);
pthread_mutex_unlock(&dst->buffer_mutex);
}
else if (dst_type == SenderDst)
{
Sender * dst = &glb_senders_array[i];
pthread_mutex_lock(&dst->buffer_mutex);
ll_append_node(&dst->input_framelist_head,
(void *) per_recv_char_buffer);
pthread_cond_signal(&dst->buffer_cv);
pthread_mutex_unlock(&dst->buffer_mutex);
}
}
free(char_buffer);
return;
}
//NOTE: You should use the following method to transmit messages from senders to receivers
void send_msg_to_receivers(char * char_buffer)
{
send_frame(char_buffer,
ReceiverDst);
return;
}
//NOTE: You should use the following method to transmit messages from receivers to senders
void send_msg_to_senders(char * char_buffer)
{
send_frame(char_buffer,
SenderDst);
return;
}