/
loop-mt.c
242 lines (213 loc) · 6.41 KB
/
loop-mt.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
/*
* Copyright (c) 2010-2011 Luca Abeni
* Copyright (c) 2010-2011 Csaba Kiraly
*
* This file is part of PeerStreamer.
*
* PeerStreamer is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* PeerStreamer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with PeerStreamer. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <sys/time.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <net_helper.h>
#include <grapes_msg_types.h>
#include <peerset.h>
#include <peer.h>
#include "dbg.h"
#include "chunk_signaling.h"
#include "streaming.h"
#include "topology.h"
#include "loop.h"
#define BUFFSIZE 512 * 1024
#define FDSSIZE 16
static int chunks_per_period = 1;
static int period = 500000;
static int done;
pthread_mutex_t cb_mutex;
pthread_mutex_t topology_mutex;
static struct nodeID *s;
static void *chunk_forging(void *dummy)
{
suseconds_t d;
struct chunk *c;
while(!done) {
c = generated_chunk(&d);
if (c) {
pthread_mutex_lock(&cb_mutex);
add_chunk(c);
pthread_mutex_unlock(&cb_mutex);
}
usleep(d); //TODO: handle inputs with fd instead of timer
}
return NULL;
}
static void *source_receive(void *dummy)
{
while (!done) {
int len;
struct nodeID *remote;
static uint8_t buff[BUFFSIZE];
len = recv_from_peer(s, &remote, buff, BUFFSIZE);
if (len < 0) {
fprintf(stderr,"Error receiving message. Maybe larger than %d bytes\n", BUFFSIZE);
nodeid_free(remote);
continue;
}
switch (buff[0] /* Message Type */) {
case MSG_TYPE_TMAN:
case MSG_TYPE_STREAMER_TOPOLOGY:
case MSG_TYPE_TOPOLOGY:
pthread_mutex_lock(&topology_mutex);
update_peers(remote, buff, len);
pthread_mutex_unlock(&topology_mutex);
break;
case MSG_TYPE_CHUNK:
fprintf(stderr, "Some dumb peer pushed a chunk to me! peer:%s\n",node_addr(remote));
break;
case MSG_TYPE_SIGNALLING:
pthread_mutex_lock(&topology_mutex);
sigParseData(remote, buff, len);
pthread_mutex_unlock(&topology_mutex);
break;
default:
fprintf(stderr, "Unknown Message Type %x\n", buff[0]);
}
nodeid_free(remote);
}
return NULL;
}
static void *receive(void *dummy)
{
while (!done) {
int len;
struct nodeID *remote;
static uint8_t buff[BUFFSIZE];
len = recv_from_peer(s, &remote, buff, BUFFSIZE);
if (len < 0) {
fprintf(stderr,"Error receiving message. Maybe larger than %d bytes\n", BUFFSIZE);
nodeid_free(remote);
continue;
}
dprintf("Received message (%d) from %s\n", buff[0], node_addr(remote));
switch (buff[0] /* Message Type */) {
case MSG_TYPE_TMAN:
case MSG_TYPE_STREAMER_TOPOLOGY:
case MSG_TYPE_TOPOLOGY:
pthread_mutex_lock(&topology_mutex);
update_peers(remote, buff, len);
pthread_mutex_unlock(&topology_mutex);
break;
case MSG_TYPE_CHUNK:
dprintf("Chunk message received:\n");
pthread_mutex_lock(&cb_mutex);
received_chunk(remote, buff, len);
pthread_mutex_unlock(&cb_mutex);
break;
case MSG_TYPE_SIGNALLING:
pthread_mutex_lock(&topology_mutex);
sigParseData(remote, buff, len);
pthread_mutex_unlock(&topology_mutex);
break;
default:
fprintf(stderr, "Unknown Message Type %x\n", buff[0]);
}
nodeid_free(remote);
}
return NULL;
}
static void *topology_sending(void *dummy)
{
int gossiping_period = period * 10;
pthread_mutex_lock(&topology_mutex);
update_peers(NULL, NULL, 0);
pthread_mutex_unlock(&topology_mutex);
while(!done) {
pthread_mutex_lock(&topology_mutex);
update_peers(NULL, NULL, 0);
pthread_mutex_unlock(&topology_mutex);
usleep(gossiping_period);
}
return NULL;
}
static void *chunk_sending(void *dummy)
{
int chunk_period = period / chunks_per_period;
while(!done) {
pthread_mutex_lock(&topology_mutex);
pthread_mutex_lock(&cb_mutex);
send_chunk();
pthread_mutex_unlock(&cb_mutex);
pthread_mutex_unlock(&topology_mutex);
usleep(chunk_period);
}
return NULL;
}
static void *chunk_trading(void *dummy)
{
int chunk_period = period / chunks_per_period;
while(!done) {
pthread_mutex_lock(&topology_mutex);
pthread_mutex_lock(&cb_mutex);
send_offer();
pthread_mutex_unlock(&cb_mutex);
pthread_mutex_unlock(&topology_mutex);
usleep(chunk_period);
}
return NULL;
}
void loop(struct nodeID *s1, int csize, int buff_size)
{
pthread_t receive_thread, gossiping_thread, distributing_thread;
period = csize;
s = s1;
peers_init();
stream_init(buff_size, s);
pthread_mutex_init(&cb_mutex, NULL);
pthread_mutex_init(&topology_mutex, NULL);
pthread_create(&receive_thread, NULL, receive, NULL);
pthread_create(&gossiping_thread, NULL, topology_sending, NULL);
pthread_create(&distributing_thread, NULL, chunk_trading, NULL);
pthread_join(receive_thread, NULL);
pthread_join(gossiping_thread, NULL);
pthread_join(distributing_thread, NULL);
}
void source_loop(const char *fname, struct nodeID *s1, int csize, int chunks, int buff_size)
{
pthread_t generate_thread, receive_thread, gossiping_thread, distributing_thread;
period = csize;
chunks_per_period = chunks;
s = s1;
int fds[FDSSIZE];
fds[0] = -1;
// sigInit(s);
peers_init();
if (source_init(fname, s, fds, FDSSIZE, buff_size) < 0) {
fprintf(stderr,"Cannot initialize source, exiting");
return;
}
pthread_mutex_init(&cb_mutex, NULL);
pthread_mutex_init(&topology_mutex, NULL);
pthread_create(&receive_thread, NULL, source_receive, NULL);
pthread_create(&gossiping_thread, NULL, topology_sending, NULL);
pthread_create(&distributing_thread, NULL, chunk_sending, NULL);
pthread_create(&generate_thread, NULL, chunk_forging, NULL);
pthread_join(generate_thread, NULL);
pthread_join(distributing_thread, NULL);
pthread_join(receive_thread, NULL);
pthread_join(gossiping_thread, NULL);
}