Statistics
| Branch: | Revision:

streamers / loop-mt.c @ 81b601c1

History | View | Annotate | Download (5.29 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_TOPOLOGY:
66
        pthread_mutex_lock(&topology_mutex);
67
        update_peers(remote, buff, len);
68
        pthread_mutex_unlock(&topology_mutex);
69
        break;
70
      case MSG_TYPE_CHUNK:
71
        fprintf(stderr, "Some dumb peer pushed a chunk to me! peer:%s\n",node_addr(remote));
72
        break;
73
      case MSG_TYPE_SIGNALLING:
74
        pthread_mutex_lock(&topology_mutex);
75
        sigParseData(remote, buff, len);
76
        pthread_mutex_unlock(&topology_mutex);
77
        break;
78
      default:
79
        fprintf(stderr, "Unknown Message Type %x\n", buff[0]);
80
    }
81
    nodeid_free(remote);
82
  }
83

    
84
  return NULL;
85
}
86

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

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

    
124
  return NULL;
125
}
126

    
127
static void *topology_sending(void *dummy)
128
{
129
  int gossiping_period = period * 10;
130

    
131
  pthread_mutex_lock(&topology_mutex);
132
  update_peers(NULL, NULL, 0);
133
  pthread_mutex_unlock(&topology_mutex);
134
  while(!done) {
135
    pthread_mutex_lock(&topology_mutex);
136
    update_peers(NULL, NULL, 0);
137
    pthread_mutex_unlock(&topology_mutex);
138
    usleep(gossiping_period);
139
  }
140

    
141
  return NULL;
142
}
143

    
144
static void *chunk_sending(void *dummy)
145
{
146
  int chunk_period = period / chunks_per_period;
147

    
148
  while(!done) {
149
    pthread_mutex_lock(&topology_mutex);
150
    pthread_mutex_lock(&cb_mutex);
151
    send_chunk();
152
    pthread_mutex_unlock(&cb_mutex);
153
    pthread_mutex_unlock(&topology_mutex);
154
    usleep(chunk_period);
155
  }
156

    
157
  return NULL;
158
}
159

    
160
void loop(struct nodeID *s1, int csize, int buff_size)
161
{
162
  pthread_t receive_thread, gossiping_thread, distributing_thread;
163
  
164
  period = csize;
165
  s = s1;
166
 
167
  peers_init();
168
  stream_init(buff_size, s);
169
  pthread_mutex_init(&cb_mutex, NULL);
170
  pthread_mutex_init(&topology_mutex, NULL);
171
  pthread_create(&receive_thread, NULL, receive, NULL); 
172
  pthread_create(&gossiping_thread, NULL, topology_sending, NULL); 
173
  pthread_create(&distributing_thread, NULL, chunk_sending, NULL); 
174

    
175
  pthread_join(receive_thread, NULL);
176
  pthread_join(gossiping_thread, NULL);
177
  pthread_join(distributing_thread, NULL);
178
}
179

    
180
void source_loop(const char *fname, struct nodeID *s1, int csize, int chunks, bool loop)
181
{
182
  pthread_t generate_thread, receive_thread, gossiping_thread, distributing_thread;
183
  
184
  period = csize;
185
  chunks_per_period = chunks;
186
  s = s1;
187
 
188
  int fds[FDSSIZE];
189
  fds[0] = -1;
190

    
191
//  sigInit(s);
192
  peers_init();
193
  if (source_init(fname, s, loop, fds, FDSSIZE) < 0) {
194
    fprintf(stderr,"Cannot initialize source, exiting");
195
    return;
196
  }
197
  pthread_mutex_init(&cb_mutex, NULL);
198
  pthread_mutex_init(&topology_mutex, NULL);
199
  pthread_create(&receive_thread, NULL, source_receive, NULL); 
200
  pthread_create(&gossiping_thread, NULL, topology_sending, NULL); 
201
#ifndef HTTPIO
202
  pthread_create(&distributing_thread, NULL, chunk_sending, NULL); 
203
  pthread_create(&generate_thread, NULL, chunk_forging, NULL); 
204
#endif
205

    
206
#ifndef HTTPIO
207
  pthread_join(generate_thread, NULL);
208
  pthread_join(distributing_thread, NULL);
209
#endif
210
  pthread_join(receive_thread, NULL);
211
  pthread_join(gossiping_thread, NULL);
212
}