-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcast.c
239 lines (194 loc) · 5.69 KB
/
mcast.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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
#include "mp1.h"
#include <gnu/libc-version.h>
#include <glib.h>
// Structs
typedef struct timestamp {
int pid;
int ts;
}timestamp;
typedef struct mcast_message {
int size;
int pid;
timestamp* timestamp;
char* payload;
} mcast_message;
// Global Variables
int *process_health; // List of failed and correct processes
GSList *holdback_queue; // Queue for messages not yet ready to be delivered
GSList *deliverables; // List of messages ready to be delivered
timestamp *vector_timestamp; // Vector of timestamps to ensure causal ordering
int sequence_number; // Sequence number for this process
GArray *cached_messages; // List of all cached messages for resending
// Initializes data structures upon process creation
void multicast_init(void) {
unicast_init();
int i;
// Initialize failure detection structure and spawn failure detector thread
process_health = malloc(sizeof(int)*mcast_num_members);
for (i = 0; i < mcast_num_members; i++) {
process_health[i] = 1;
}
pthread_t fd;
// Create array for cached messages
g_array_sized_new((gint)0, (gint)0, sizeof(char*), sizeof(char*)*mcast_num_members);
// Create holdback queue for unordered messages
holdback_queue = g_slist_alloc();
// Create list of deliverable messages (may not be required)
deliverables = g_slist_alloc();
// Initialize current process sequence number
sequence_number = 0;
// Create timestamp vector
vector_timestamp = malloc(sizeof(timestamp)*mcast_num_members);
for(i = 0; i < mcast_num_members; i++) {
vector_timestamp[i].pid = mcast_members[i];
vector_timestamp[i].ts = 0;
}
}
/* Basic multicast implementation */
void multicast(const char *message) {
// Add this message to our cached list of messages
g_slist_append(cached_messages,message);
int size = 2*sizeof(int) + (mcast_num_members*sizeof(timestamp)) + strlen(message)+1;
mcast_message* m = malloc(size);
m->size = mcast_num_members;
m->pid = my_id;
char* manipulate = m; // Used for moving around the datastructure to remove headers
manipulate += 2*sizeof(int);
sequence_number++;
int i;
for(i = 0; i < mcast_num_members; i++) {
if(vector_timestamp[i].pid = my_id) {
vector_timestamp[i].ts += 1;
break;
}
}
void* mptr = manipulate;
// Copy over all timestamps
memcpy(mptr,(void*)vector_timestamp,sizeof(timestamp)*mcast_num_members);
mptr = manipulate+(mcast_num_members*sizeof(timestamp));
// Copy over actual message
strncpy(mptr, (void*)(message), strlen(message));
((char*)mptr)[strlen(message)] = '\0';
// Lock up during usend in case of race conditions
pthread_mutex_lock(&member_lock);
for (i = 0; i < mcast_num_members; i++) {
usend(mcast_members[i], (char*)m, size);
}
pthread_mutex_unlock(&member_lock);
}
/* Iterates over holdback queue finding messages ready to be delivered */
void checkAllDeliverables() {
/* int size = g_slist_length(holdback_queue);
if ( size == 0) {
return;
}
int ctr = 0;
int i;
GSList* curr = holdback_queue;
debugprintf("HQ SIZE IS %d", size);
for ( i = 0; i < size; i++) {
if(curr->data != NULL && isDeliverable(curr->data) ) {
debugprintf("isDeliverable %d : %d", i, isDeliverable(curr->data));
//Remove from queue
gconstpointer g = curr->data;
deliver_wrapper(curr->data);
g_slist_remove(curr,g);
ctr++;
}
curr = curr->next;
}
if (ctr) {
checkAllDeliverables();
}
*/
GSList* curr = holdback_queue;
while (curr != NULL) {
if (curr->data != NULL && isDeliverable(curr->data)) {
gconstpointer g = curr->data;
deliver_wrapper(curr->data);
curr = g_slist_remove(curr,g);
}
else {
curr = curr->next;
}
}
//FOREACH CHECK IF DELIVERABLE
//IF SO SET VECTOR FOR J ++ and REMOVE FROM HBACK AND DELIVER
// deliver source pfinal
}
void deliver_wrapper(char* message) {
mcast_message* m = message;
int size = m->size;
int pid = m->pid;
char* delivery = m;
delivery+=2*sizeof(int) + sizeof(timestamp)*size;
deliver(pid, delivery);
}
/* Returns a non-zero value if a given message is ready to be delivered */
int isDeliverable(void* message) {
mcast_message* m = message;
int size = m->size;
int pid = m->pid;
char* ts = m;
ts+=2*sizeof(int);
timestamp* v = ts;
char* pload = ts + sizeof(timestamp)*size;
int i,j;
int expected_ts = 0;
for(i = 0; i < size; i++) {
if (v[i].pid == pid)
expected_ts = v[i].ts;
break;
}
for( i = 0; i < mcast_num_members; i++) {
if ( vector_timestamp[i].pid == pid ) {
if (expected_ts-1 != vector_timestamp[i].ts) {
return 0;
}
}
}
for( i = 0; i < mcast_num_members; i++) {
for( j = 0; j < size; j++) {
if( v[j].pid == pid) {
continue;
}
if( vector_timestamp[i].pid == v[j].pid && v[j].ts > vector_timestamp[i].ts ) {
return 0;
}
}
}
return 1;
}
void receive(int source, const char *message, int len) {
if( source == my_id) {
const char *pfinal = message + 2*sizeof(int) + (mcast_num_members*sizeof(timestamp));
deliver(source,pfinal);
return;
}
gpointer data = message;
g_slist_append(holdback_queue,data);
checkAllDeliverables(message);
//const char *pfinal = message + 2*sizeof(int) + (mcast_num_members*sizeof(timestamp));
//deliver(source, pfinal);
}
void mcast_join(int member) {
debugprintf("MCASTNUM = %d\n",mcast_num_members);
timestamp* new_ts = malloc(sizeof(timestamp)*mcast_num_members);
int* new_fd = malloc(sizeof(int)*mcast_num_members);
int i;
for(i=0; i < mcast_num_members-1; i++) {
new_ts[i] = vector_timestamp[i];
new_fd[i] = process_health[i];
}
new_ts[mcast_num_members-1].pid = member;
new_ts[mcast_num_members-1].ts = 0;
new_fd[mcast_num_members-1] = 1;
free(process_health);
free(vector_timestamp);
process_health = new_fd;
vector_timestamp = new_ts;
}