/
bang-com.c
425 lines (362 loc) · 9.59 KB
/
bang-com.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
/**
* \file bang-com.c
* \author Nikhil Bysani
* \date December 20, 2009
*
* \brief Implements "the master of slave peer threads" model.
*
*/
#include"bang-com.h"
#include"bang-net.h"
#include"bang-signals.h"
#include"bang-types.h"
#include<poll.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<unistd.h>
/**
* \param node appends node to list started at head
* \param head start of the request node list
*
* \brief Appends a node to the head linked list.
*/
void append_request(request_node **head, request_node *node);
/**
* \param head The head of the list to be freed.
*
* \brief Frees resources used by a request list started at
* head.
*/
void free_requestList(request_node *head);
/*
* \param requests The requests to be freed.
*
* \brief Frees a BANGRequests struct.
*/
void free_BANGRequests(BANG_requests *requests);
/*
* \return Returns an initialized BANGRequest pointer.
*/
BANG_requests* allocate_BANGRequests();
/**
* The peers structure is a reader-writer structure. That is, multiple threads
* can be reading data from the structure, but only one process can change the
* size at once.
*/
/**
* This is a lock on readers
*/
pthread_mutex_t peers_read_lock;
/**
* The number of threads currently reading for the peers structure
*/
int peers_readers = 0;
/**
* A lock on changing the size of peers
*/
pthread_mutex_t peers_change_lock;
/**
* The total number of peers we get through the length of the program.
*/
unsigned int peer_count = 0;
/**
* The current number of peers connected to the program.
*/
unsigned int current_peers = 0;
/**
* Information and threads for each peer connected to the program
*/
peer **peers = NULL;
/**TODO: make this structure not take linear time when sending a request
* to a specific peer
*/
int *keys = NULL;
void acquire_peers_read_lock() {
pthread_mutex_lock(&peers_read_lock);
if (peers_readers == 0)
pthread_mutex_lock(&peers_change_lock);
++peers_readers;
pthread_mutex_unlock(&peers_read_lock);
}
void release_peers_read_lock() {
pthread_mutex_lock(&peers_read_lock);
--peers_readers;
if (peers_readers == 0)
pthread_mutex_lock(&peers_change_lock);
pthread_mutex_unlock(&peers_read_lock);
}
void clear_peer(int pos) {
#ifdef BDEBUG_1
fprintf(stderr,"Clearing a peer at %d.\n",pos);
#endif
///This should quickly make the two threads stop.
pthread_cancel(peers[pos]->receive_thread);
pthread_cancel(peers[pos]->send_thread);
///TODO: find a way to stop this thread.
//pthread_join(peers[pos]->receive_thread,NULL);
pthread_join(peers[pos]->send_thread,NULL);
free_BANGRequests(peers[pos]->requests);
close(peers[pos]->socket);
free(peers[pos]);
peers[pos] = NULL;
}
request_node* pop_request(request_node **head) {
request_node *temp = *head;
if (temp->next == NULL) {
*head = NULL;
return temp;
} else {
*head = temp->next;
temp->next = NULL;
return temp;
}
}
void append_request(request_node **head, request_node *node) {
if (*head == NULL) {
*head = node;
} else {
request_node *cur;
for (cur = *head; cur != NULL; cur = cur->next);
cur->next = node;
}
}
BANG_requests* allocate_BANGRequests() {
BANG_requests *requests = (BANG_requests*) calloc(1,sizeof(BANG_requests));
requests->head = NULL;
pthread_mutex_init(&(requests->lock),NULL);
sem_init(&(requests->num_requests),0,0);
return requests;
}
void free_BANGRequests(BANG_requests *requests) {
pthread_mutex_destroy(&(requests->lock));
sem_destroy(&(requests->num_requests));
free_requestList(requests->head);
}
/**
* \param self The current peer.
*
* \brief Closes one of the two peer threads, after the connection has formally stopped
* and the mess has to been cleaned up..
*/
void peer_self_close(peer *self) {
BANG_sigargs args;
args.args = calloc(1,sizeof(int));
*((int*)args.args) = self->peer_id;
args.length = sizeof(int);
acquire_peers_read_lock();
BANG_send_signal(BANG_PEER_DISCONNECTED,args);
free(args.args);
release_peers_read_lock();
pthread_exit(NULL);
}
/**
* \param sock An open socket.
* \brief Extracts a char* message from the socket per the specifications of all messages.
* (unsigned long length)(message of that length)
*/
char* extract_message(peer *self) {
char *message = NULL;
unsigned int length;
int check_read, read = 0;
char extracting = 1;
while (extracting) {
check_read = read(self->socket,&length,LENGTH_OF_LENGTH);
if (poll(&(self->pfd),1,-1) != -1 && self->pfd.revents & POLLIN) {
if (check_read <= 0) {
message = NULL;
extracting = 0;
} else {
extracting = 0;
}
} else {
extracting = 0;
}
}
return message;
}
void peer_respond_hello(peer *self) {
char responding = 1;
int check_read;
double version;
while (responding) {
if (poll(&(self->pfd),1,-1) != -1 && self->pfd.revents & POLLIN) {
check_read = read(self->socket,&version,LENGTH_OF_VERSION);
if (check_read <= 0) {
responding = 0;
} else {
if (version == BANG_VERSION) {
/**
* TODO: Get the peer name.
*/
} else {
/**
* TODO: File a mismatch version request.
* or do something.
*/
responding = 0;
}
}
} else {
responding = 0;
}
}
}
void* BANG_read_peer_thread(void *self_info) {
peer *self = (peer*)self_info;
memset(&(self->pfd),0,sizeof(struct pollfd));
self->pfd.fd = self->socket;
self->pfd.events = POLLIN | POLLOUT | POLLERR | POLLHUP | POLLNVAL;
unsigned int header;
int check_read;
char reading = 1;
while (reading) {
if (poll(&(self->pfd),1,-1) != -1 && self->pfd.revents & POLLIN) {
check_read = read(self->socket,&header,LENGTH_OF_HEADERS);
/**
* Lookup header message and act accordingly.
*/
if (check_read <= 0) {
reading = 0;
} else {
switch (header) {
case BANG_HELLO:
peer_respond_hello(self);
break;
case BANG_DEBUG_MESSAGE:
break;
case BANG_MISMATCH_VERSION:
case BANG_BYE:
reading = 0;
break;
default:
/**
* Protocol mismatch, hang up.
*/
reading = 0;
break;
}
}
} else {
reading = 0;
}
}
peer_self_close(self);
return NULL;
}
void* BANG_write_peer_thread(void *self_info) {
peer *self = (peer*)self_info;
request_node *current;
while (1) {
sem_wait(&(self->requests->num_requests));
pthread_mutex_lock(&(self->requests->lock));
current = pop_request(&(self->requests->head));
pthread_mutex_unlock(&(self->requests->lock));
///TODO: act on current request
}
return NULL;
}
void BANG_peer_added(int signal,int sig_id,void *socket) {
BANG_add_peer(*((int*)socket));
free(socket);
}
int BANG_get_key_with_peer_id(int peer_id) {
int i = 0;
for (i = 0; i < current_peers; ++i) {
if (peers[i]->peer_id == peer_id) {
return i;
}
}
return -1;
}
void BANG_add_peer(int socket) {
pthread_mutex_lock(&peers_change_lock);
++current_peers;
int current_key = current_peers - 1;
int current_id = peer_count++;
keys = (int*) realloc(keys,current_peers * sizeof(int));
keys[current_key] = current_id;
peers = (peer**) realloc(peers,current_peers * sizeof(peer*));
peers[current_key] = (peer*) calloc(1,sizeof(peer));
peers[current_key]->peer_id = current_id;
peers[current_key]->socket = socket;
peers[current_key]->requests = allocate_BANGRequests();
#ifdef BDEBUG_1
fprintf(stderr,"Threads being started at %d\n",current_key);
#endif
pthread_create(&(peers[current_key]->receive_thread),NULL,BANG_read_peer_thread,peers[current_key]);
pthread_create(&(peers[current_key]->send_thread),NULL,BANG_write_peer_thread,peers[current_key]);
pthread_mutex_unlock(&peers_change_lock);
/**
* Send out that we successfully started the peer threads.
*/
BANG_sigargs args;
args.args = calloc(1,sizeof(int));
*((int*)args.args) = current_id;
args.length = sizeof(int);
BANG_send_signal(BANG_PEER_ADDED,args);
free(args.args);
}
void BANG_peer_removed(int signal,int sig_id,void *peer_id) {
BANG_remove_peer(*((int*)peer_id));
free(peer_id);
}
void free_requestList(request_node *head) {
if (head == NULL) return;
if (head->next != NULL)
free_requestList(head->next);
free(head);
}
void BANG_remove_peer(int peer_id) {
///this lock is needed when trying to change the
///peers structure
#ifdef BDEBUG_1
fprintf(stderr,"Removing peer %d.\n",peer_id);
#endif
pthread_mutex_lock(&peers_change_lock);
int pos = BANG_get_key_with_peer_id(peer_id);
if (pos == -1) return;
clear_peer(pos);
for (;pos < current_peers - 1; ++pos) {
peers[pos] = peers[pos + 1];
keys[pos] = keys[pos + 1];
}
--current_peers;
peers = (peer**) realloc(peers,current_peers * sizeof(peer*));
keys = (int*) realloc(keys,current_peers * sizeof(int));
pthread_mutex_unlock(&peers_change_lock);
BANG_sigargs peer_send;
peer_send.args = calloc(1,sizeof(int));
*((int*)peer_send.args) = peer_id;
peer_send.length = sizeof(int);
BANG_send_signal(BANG_PEER_REMOVED,peer_send);
free(peer_send.args);
}
void BANG_com_init() {
BANG_install_sighandler(BANG_PEER_CONNECTED,&BANG_peer_added);
BANG_install_sighandler(BANG_PEER_DISCONNECTED,&BANG_peer_removed);
pthread_mutex_init(&peers_change_lock,NULL);
pthread_mutex_init(&peers_read_lock,NULL);
}
void BANG_com_close() {
///All of threads should of got hit by a global BANG_CLOSE_ALL
///Should it be sent here?
///Anyway, we'll just wait for each thread now
fprintf(stderr,"BANG com closing.\n");
int i = 0;
pthread_mutex_unlock(&peers_change_lock);
for (i = 0; i < current_peers; ++i) {
clear_peer(i);
}
pthread_mutex_destroy(&peers_change_lock);
pthread_mutex_destroy(&peers_read_lock);
free(peers);
free(keys);
keys = NULL;
peers = NULL;
current_peers = 0;
peer_count = 0;
}