Statistics
| Branch: | Revision:

streamers / loop-mt.c @ cab9a9fb

History | View | Annotate | Download (4.31 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

    
14
#include <net_helper.h>
15
#include <topmanager.h>
16
#include <msg_types.h>
17

    
18
#include "dbg.h"
19
#include "streaming.h"
20
#include "loop.h"
21

    
22
#define BUFFSIZE 512 * 1024
23
static int chunks_per_period = 1;
24
static int period = 500000;
25
static int done;
26
static pthread_mutex_t cb_mutex;
27
static pthread_mutex_t topology_mutex;
28
static struct nodeID *s;
29

    
30
static void *chunk_forging(void *dummy)
31
{
32
  suseconds_t d;
33

    
34
  while(!done) {
35
    pthread_mutex_lock(&cb_mutex);
36
    generated_chunk(&d);
37
    pthread_mutex_unlock(&cb_mutex);
38
    usleep(d);
39
  }
40

    
41
  return NULL;
42
}
43

    
44
static void *source_receive(void *dummy)
45
{
46
  while (!done) {
47
    int len;
48
    struct nodeID *remote;
49
  static uint8_t buff[BUFFSIZE];
50

    
51
    len = recv_from_peer(s, &remote, buff, BUFFSIZE);
52
    switch (buff[0] /* Message Type */) {
53
      case MSG_TYPE_TOPOLOGY:
54
        pthread_mutex_lock(&topology_mutex);
55
        topParseData(buff, len);
56
        pthread_mutex_unlock(&topology_mutex);
57
        break;
58
      case MSG_TYPE_CHUNK:
59
        fprintf(stderr, "Some dumb peer pushed a chunk to me!\n");
60
        break;
61
      default:
62
        fprintf(stderr, "Unknown Message Type %x\n", buff[0]);
63
    }
64
    nodeid_free(remote);
65
  }
66

    
67
  return NULL;
68
}
69

    
70
static void *receive(void *dummy)
71
{
72
  while (!done) {
73
    int len;
74
    struct nodeID *remote;
75
  static uint8_t buff[BUFFSIZE];
76

    
77
    len = recv_from_peer(s, &remote, buff, BUFFSIZE);
78
    dprintf("Received message (%c) from %s\n", buff[0], node_addr(remote));
79
    switch (buff[0] /* Message Type */) {
80
      case MSG_TYPE_TOPOLOGY:
81
        pthread_mutex_lock(&topology_mutex);
82
        topParseData(buff, len);
83
        pthread_mutex_unlock(&topology_mutex);
84
        break;
85
      case MSG_TYPE_CHUNK:
86
        dprintf("Chunk message received:\n");
87
        pthread_mutex_lock(&cb_mutex);
88
        received_chunk(buff, len);
89
        pthread_mutex_unlock(&cb_mutex);
90
        break;
91
      default:
92
        fprintf(stderr, "Unknown Message Type %x\n", buff[0]);
93
    }
94
    nodeid_free(remote);
95
  }
96

    
97
  return NULL;
98
}
99

    
100
static void *topology_sending(void *dummy)
101
{
102
  int gossiping_period = period * 10;
103

    
104
  pthread_mutex_lock(&topology_mutex);
105
  topParseData(NULL, 0);
106
  pthread_mutex_unlock(&topology_mutex);
107
  while(!done) {
108
    pthread_mutex_lock(&topology_mutex);
109
    topParseData(NULL, 0);
110
    pthread_mutex_unlock(&topology_mutex);
111
    usleep(gossiping_period);
112
  }
113

    
114
  return NULL;
115
}
116

    
117
static void *chunk_sending(void *dummy)
118
{
119
  int chunk_period = period / chunks_per_period;
120

    
121
  while(!done) {
122
    const struct nodeID **neighbours;
123
    int n;
124

    
125
    pthread_mutex_lock(&topology_mutex);
126
    neighbours = topGetNeighbourhood(&n);
127
    pthread_mutex_lock(&cb_mutex);
128
    send_chunk(neighbours, n);
129
    pthread_mutex_unlock(&cb_mutex);
130
    pthread_mutex_unlock(&topology_mutex);
131
    usleep(chunk_period);
132
  }
133

    
134
  return NULL;
135
}
136

    
137
void loop(struct nodeID *s1, int csize, int buff_size)
138
{
139
  pthread_t receive_thread, gossiping_thread, distributing_thread;
140
  
141
  period = csize;
142
  s = s1;
143
 
144
  stream_init(buff_size, s);
145
  pthread_mutex_init(&cb_mutex, NULL);
146
  pthread_mutex_init(&topology_mutex, NULL);
147
  pthread_create(&receive_thread, NULL, receive, NULL); 
148
  pthread_create(&gossiping_thread, NULL, topology_sending, NULL); 
149
  pthread_create(&distributing_thread, NULL, chunk_sending, NULL); 
150

    
151
  pthread_join(receive_thread, NULL);
152
  pthread_join(gossiping_thread, NULL);
153
  pthread_join(distributing_thread, NULL);
154
}
155

    
156
void source_loop(const char *fname, struct nodeID *s1, int csize, int chunks, bool loop)
157
{
158
  pthread_t generate_thread, receive_thread, gossiping_thread, distributing_thread;
159
  
160
  period = csize;
161
  chunks_per_period = chunks;
162
  s = s1;
163
 
164
  source_init(fname, s, loop);
165
  pthread_mutex_init(&cb_mutex, NULL);
166
  pthread_mutex_init(&topology_mutex, NULL);
167
  pthread_create(&receive_thread, NULL, source_receive, NULL); 
168
  pthread_create(&gossiping_thread, NULL, topology_sending, NULL); 
169
  pthread_create(&generate_thread, NULL, chunk_forging, NULL); 
170
  pthread_create(&distributing_thread, NULL, chunk_sending, NULL); 
171

    
172
  pthread_join(generate_thread, NULL);
173
  pthread_join(receive_thread, NULL);
174
  pthread_join(gossiping_thread, NULL);
175
  pthread_join(distributing_thread, NULL);
176
}