Statistics
| Branch: | Revision:

streamers / loop-mt.c @ 9176d3d1

History | View | Annotate | Download (5.73 KB)

1
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4
 *
5
 *  This is free software; see gpl-3.0.txt
6
 */
7
#include <sys/time.h>
8
#include <unistd.h>
9
#include <stdint.h>
10
#include <stdlib.h>
11
#include <stdio.h>
12
#include <pthread.h>
13
#include <net_helper.h>
14
#include <grapes_msg_types.h>
15
#include <peerset.h>
16
#include <peer.h>
17

    
18
#include "dbg.h"
19
#include "chunk_signaling.h"
20
#include "streaming.h"
21
#include "topology.h"
22
#include "loop.h"
23

    
24
#define BUFFSIZE 512 * 1024
25
#define FDSSIZE 16
26
static int chunks_per_period = 1;
27
static int period = 500000;
28
static int done;
29
pthread_mutex_t cb_mutex;
30
pthread_mutex_t topology_mutex;
31
static struct nodeID *s;
32

    
33
static void *chunk_forging(void *dummy)
34
{
35
  suseconds_t d;
36
  struct chunk *c;
37

    
38
  while(!done) {
39
    c = generated_chunk(&d);
40
    if (c) {
41
      pthread_mutex_lock(&cb_mutex);
42
      add_chunk(c);
43
      pthread_mutex_unlock(&cb_mutex);
44
    }
45
    usleep(d);
46
  }
47

    
48
  return NULL;
49
}
50

    
51
static void *source_receive(void *dummy)
52
{
53
  while (!done) {
54
    int len;
55
    struct nodeID *remote;
56
  static uint8_t buff[BUFFSIZE];
57

    
58
    len = recv_from_peer(s, &remote, buff, BUFFSIZE);
59
    if (len < 0) {
60
      fprintf(stderr,"Error receiving message. Maybe larger than %d bytes\n", BUFFSIZE);
61
      nodeid_free(remote);
62
      continue;
63
    }
64
    switch (buff[0] /* Message Type */) {
65
          case MSG_TYPE_TMAN:
66
      case MSG_TYPE_STREAMER_TOPOLOGY:
67
      case MSG_TYPE_TOPOLOGY:
68
        pthread_mutex_lock(&topology_mutex);
69
        update_peers(remote, buff, len);
70
        pthread_mutex_unlock(&topology_mutex);
71
        break;
72
      case MSG_TYPE_CHUNK:
73
        fprintf(stderr, "Some dumb peer pushed a chunk to me! peer:%s\n",node_addr(remote));
74
        break;
75
      case MSG_TYPE_SIGNALLING:
76
        pthread_mutex_lock(&topology_mutex);
77
        sigParseData(remote, buff, len);
78
        pthread_mutex_unlock(&topology_mutex);
79
        break;
80
      default:
81
        fprintf(stderr, "Unknown Message Type %x\n", buff[0]);
82
    }
83
    nodeid_free(remote);
84
  }
85

    
86
  return NULL;
87
}
88

    
89
static void *receive(void *dummy)
90
{
91
  while (!done) {
92
    int len;
93
    struct nodeID *remote;
94
  static uint8_t buff[BUFFSIZE];
95

    
96
    len = recv_from_peer(s, &remote, buff, BUFFSIZE);
97
    if (len < 0) {
98
      fprintf(stderr,"Error receiving message. Maybe larger than %d bytes\n", BUFFSIZE);
99
      nodeid_free(remote);
100
      continue;
101
    }
102
    dprintf("Received message (%c) from %s\n", buff[0], node_addr(remote));
103
    switch (buff[0] /* Message Type */) {
104
          case MSG_TYPE_TMAN:
105
      case MSG_TYPE_STREAMER_TOPOLOGY:
106
      case MSG_TYPE_TOPOLOGY:
107
        pthread_mutex_lock(&topology_mutex);
108
        update_peers(remote, buff, len);
109
        pthread_mutex_unlock(&topology_mutex);
110
        break;
111
      case MSG_TYPE_CHUNK:
112
        dprintf("Chunk message received:\n");
113
        pthread_mutex_lock(&cb_mutex);
114
        received_chunk(remote, buff, len);
115
        pthread_mutex_unlock(&cb_mutex);
116
        break;
117
      case MSG_TYPE_SIGNALLING:
118
        pthread_mutex_lock(&topology_mutex);
119
        sigParseData(remote, buff, len);
120
        pthread_mutex_unlock(&topology_mutex);
121
        break;
122
      default:
123
        fprintf(stderr, "Unknown Message Type %x\n", buff[0]);
124
    }
125
    nodeid_free(remote);
126
  }
127

    
128
  return NULL;
129
}
130

    
131
static void *topology_sending(void *dummy)
132
{
133
  int gossiping_period = period * 10;
134

    
135
  pthread_mutex_lock(&topology_mutex);
136
  update_peers(NULL, NULL, 0);
137
  pthread_mutex_unlock(&topology_mutex);
138
  while(!done) {
139
    pthread_mutex_lock(&topology_mutex);
140
    update_peers(NULL, NULL, 0);
141
    pthread_mutex_unlock(&topology_mutex);
142
    usleep(gossiping_period);
143
  }
144

    
145
  return NULL;
146
}
147

    
148
static void *chunk_sending(void *dummy)
149
{
150
  int chunk_period = period / chunks_per_period;
151

    
152
  while(!done) {
153
    pthread_mutex_lock(&topology_mutex);
154
    pthread_mutex_lock(&cb_mutex);
155
    send_chunk();
156
    pthread_mutex_unlock(&cb_mutex);
157
    pthread_mutex_unlock(&topology_mutex);
158
    usleep(chunk_period);
159
  }
160

    
161
  return NULL;
162
}
163

    
164
static void *chunk_trading(void *dummy)
165
{
166
  int chunk_period = period / chunks_per_period;
167

    
168
  while(!done) {
169
    pthread_mutex_lock(&topology_mutex);
170
    pthread_mutex_lock(&cb_mutex);
171
    send_offer();
172
    pthread_mutex_unlock(&cb_mutex);
173
    pthread_mutex_unlock(&topology_mutex);
174
    usleep(chunk_period);
175
  }
176

    
177
  return NULL;
178
}
179

    
180
void loop(struct nodeID *s1, int csize, int buff_size)
181
{
182
  pthread_t receive_thread, gossiping_thread, distributing_thread;
183
  
184
  period = csize;
185
  s = s1;
186
 
187
  peers_init();
188
  stream_init(buff_size, s);
189
  pthread_mutex_init(&cb_mutex, NULL);
190
  pthread_mutex_init(&topology_mutex, NULL);
191
  pthread_create(&receive_thread, NULL, receive, NULL); 
192
  pthread_create(&gossiping_thread, NULL, topology_sending, NULL); 
193
  pthread_create(&distributing_thread, NULL, chunk_trading, NULL); 
194

    
195
  pthread_join(receive_thread, NULL);
196
  pthread_join(gossiping_thread, NULL);
197
  pthread_join(distributing_thread, NULL);
198
}
199

    
200
void source_loop(const char *fname, struct nodeID *s1, int csize, int chunks, bool loop)
201
{
202
  pthread_t generate_thread, receive_thread, gossiping_thread, distributing_thread;
203
  
204
  period = csize;
205
  chunks_per_period = chunks;
206
  s = s1;
207
 
208
  int fds[FDSSIZE];
209
  fds[0] = -1;
210

    
211
//  sigInit(s);
212
  peers_init();
213
  if (source_init(fname, s, loop, fds, FDSSIZE) < 0) {
214
    fprintf(stderr,"Cannot initialize source, exiting");
215
    return;
216
  }
217
  pthread_mutex_init(&cb_mutex, NULL);
218
  pthread_mutex_init(&topology_mutex, NULL);
219
  pthread_create(&receive_thread, NULL, source_receive, NULL); 
220
  pthread_create(&gossiping_thread, NULL, topology_sending, NULL); 
221
#ifndef HTTPIO
222
  pthread_create(&distributing_thread, NULL, chunk_sending, NULL); 
223
  pthread_create(&generate_thread, NULL, chunk_forging, NULL); 
224
#endif
225

    
226
#ifndef HTTPIO
227
  pthread_join(generate_thread, NULL);
228
  pthread_join(distributing_thread, NULL);
229
#endif
230
  pthread_join(receive_thread, NULL);
231
  pthread_join(gossiping_thread, NULL);
232
}