Statistics
| Branch: | Revision:

streamers / streamer.c @ 648aa768

History | View | Annotate | Download (11.6 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 <stdlib.h>
8
#include <stdint.h>
9
#include <stdio.h>
10
#include <stdbool.h>
11
#include <string.h>
12
#include <getopt.h>
13
#include <signal.h>
14
#include <time.h>
15
#include <errno.h>
16
#include <math.h>
17
#ifndef NAN        //NAN is missing in some old math.h versions
18
#define NAN            (0.0/0.0)
19
#endif
20

    
21
#include <grapes_msg_types.h>
22
#include <net_helper.h>
23

    
24
#ifdef WIN32
25
#include <winsock2.h>
26
#endif
27

    
28
#include "net_helpers.h"
29
#include "loop.h"
30
#include "output.h"
31
#include "channel.h"
32
#include "topology.h"
33
#include "measures.h"
34
#include "streamer.h"
35

    
36
#ifndef EXTRAVERSION
37
#define EXTRAVERSION "Unknown"
38
#endif
39

    
40
static struct nodeID *my_sock;
41

    
42
const char *peername = NULL;
43

    
44
static const char *my_iface = NULL;
45
static int port = 6666;
46
static int srv_port;
47
static const char *srv_ip = "";
48
static int period = 40;
49
static int chunks_per_second = 25;
50
static double capacity_override = NAN;
51
#ifdef HTTPIO
52
//input-http.c needs this in order to accomplish the -m multiple send_chunk()
53
int multiply = 3;
54
#else
55
static int multiply = 3;
56
#endif
57
static int buff_size = 50;
58
static int outbuff_size = 50;
59
static const char *fname = "/dev/stdin";
60
static const char *output_config;
61
static bool loop_input = false;
62
static const char *net_helper_config = "";
63
static const char *topo_config = "";
64
unsigned char msgTypes[] = {MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
65
bool chunk_log = false;
66
static int randomize_start = 0;
67
int start_id = -1;
68
int end_id = -1;
69
int initial_id = -1;
70

    
71
extern int NEIGHBORHOOD_TARGET_SIZE;
72
extern uint64_t CB_SIZE_TIME;
73
extern double desired_bw;
74
extern double desired_rtt;
75
extern double alpha_target;
76
extern double topo_mem;
77
extern bool topo_out;
78
extern bool topo_in;
79

    
80
#ifndef MONL
81
extern struct timeval print_tdiff;
82
extern struct timeval tstartdiff;
83
#endif
84

    
85
static void print_usage(int argc, char *argv[])
86
{
87
  fprintf (stderr,
88
    "Usage:%s [options]\n"
89
    "\n"
90
    "Peer options\n"
91
    "\t[-p port]: port of the remote peer to connect at during bootstrap.\n"
92
    "\t           Usually it is the source peer port.\n"
93
    "\t[-i IP]: IP address of the remote peer to connect at during bootstrap.\n"
94
    "\t         Usually it is the source peer IP\n"
95
    "\t[-C name]: set the channel name to use on the repository.\n"
96
    "\t           All peers should use the same channel name.\n"
97
    "\n"
98
    "\t[-b size]: set the peer Chunk Buffer size.\n"
99
    "\t           This is also the chunk trading window size.\n"
100
    "\t[-o size]: set the Output Buffer size.\n"
101
    "\t[-c chunks]: set the number of chunks a peer can send per seconds.\n"
102
    "\t             it controls the upload capacity of peer as well.\n"
103
    "\t[-M peers]: neighbourhood target size.\n"
104
    "\t[-t config]: topology config.\n"
105
    "\t[-P port]: local UDP port to be used by the peer.\n"
106
    "\t[-I iface]: local netwok interface to be used by the peer.\n"
107
    "\t         Useful if the host has several interfaces/addresses.\n"
108
    "\t[-N name]: set the name of the peer.\n"
109
    "\t         This name will be used when publishing in the repository.\n"
110
    "\t[-n options]: pass configuration options to the net-helper\n"
111
    "\t[--chunk_log]: print a chunk level log on stderr\n"
112
    "\t[-F config]: configure the output module\n"
113
    "\t[-a alpha]: set the topology alpha value (from 0 to 100)\n"
114
    "\t[-r rtt]: set the RTT threshold (in ms) for desired neighbours\n"
115
    "\t[--desired_bw bw]: set the BW threshold (in bits/s) for desired neighbours. Use of K(ilo), M(ega) allowed, e.g 0.8M\n"
116
    "\t[--topo_mem p]: keep p (0..1) portion of peers between topology operations\n"
117
    "\t[--topo_out]: peers only choose out-neighbours\n"
118
    "\t[--topo_in]: peers only choose in-neighbours\n"
119
    "\t[--topo_bidir]: peers choose both in- and out-neighbours (bidir)\n"
120
    "\n"
121
    "Special Source Peer options\n"
122
    "\t[-m chunks]: set the number of copies the source injects in the overlay.\n"
123
    "\t[-f filename]: name of the video stream file to transmit.\n"
124
    "\t[-l]: loop the video stream.\n"
125
    "\t[-S]: set initial chunk_id (source only).\n"
126
    "\t[-s]: set start_id from which to start output.\n"
127
    "\t[-e]: set end_id at which to end output.\n"
128
    "\n"
129
    "Special options\n"
130
    "\t[--randomize_start ms]: random wait before starting in to ms millisecs.\n"
131
    "\t[-v]: print version.\n"
132
    "\n"
133
    "NOTE: by deafult the peer will dump the received video on STDOUT in raw format\n"
134
    "      it can be played by your favourite player simply using a pipe\n"
135
    "      e.g., | cvlc /dev/stdin\n"
136
    "\n"
137
    "Examples:\n"
138
    "\n"
139
    "Start a source peer on port 6600:\n"
140
    "\n"
141
    "%s -l -f foreman.mpg -P 6600\n"
142
    "\n"
143
    "Start a peer connecting to the previous source, and using videolan as player:\n"
144
    "\n"
145
    "%s -i <sourceIP> -p <sourcePort> | cvlc /dev/stdin\n"
146
    "=======================================================\n", argv[0], argv[0], argv[0]
147
    );
148
  }
149

    
150

    
151
static double atod_kmg(const char *s) {
152
  double d;
153
  char *e;
154

    
155
  errno = 0;
156
  d = strtod(s, &e);
157
  if (errno) {
158
    fprintf(stderr, "Error parsing option: %s\n", s);
159
    exit(-1);
160
  }
161
  switch (*e) {
162
    case 'g':
163
    case 'G':
164
      d *= 1024;
165
    case 'm':
166
    case 'M':
167
      d *= 1024;
168
    case 'k':
169
    case 'K':
170
      d *= 1024;
171
    case 0:
172
      break;
173
    default:
174
      fprintf(stderr, "Error parsing option: %s\n", s);
175
      exit(-1);
176
  }
177

    
178
  return d;
179
}
180

    
181
static void cmdline_parse(int argc, char *argv[])
182
{
183
  int o;
184

    
185
  int option_index = 0;
186
  static struct option long_options[] = {
187
        {"chunk_log", no_argument, 0, 0},
188
        {"measure_start", required_argument, 0, 0},
189
        {"measure_every", required_argument, 0, 0},
190
        {"playout_limit", required_argument, 0, 0},
191
        {"randomize_start", required_argument, 0, 0},
192
        {"capacity_override", required_argument, 0, 0},
193
        {"desired_bw", required_argument, 0, 0},
194
        {"topo_mem", required_argument, 0, 0},
195
        {"topo_in", no_argument, 0, 0},
196
        {"topo_out", no_argument, 0, 0},
197
        {"topo_bidir", no_argument, 0, 0},
198
        {0, 0, 0, 0}
199
  };
200

    
201
    while ((o = getopt_long (argc, argv, "r:a:b:o:c:p:i:P:I:f:F:m:lC:N:n:M:t:s:e:S:v",long_options, &option_index)) != -1) { //use this function to manage long options
202
    switch(o) {
203
      case 0: //for long options
204
        if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { chunk_log = true; }
205
#ifndef MONL
206
        if( strcmp( "measure_start", long_options[option_index].name ) == 0 ) { tstartdiff.tv_sec = atoi(optarg); }
207
        if( strcmp( "measure_every", long_options[option_index].name ) == 0 ) { print_tdiff.tv_sec = atoi(optarg); }
208
#endif
209
        if( strcmp( "playout_limit", long_options[option_index].name ) == 0 ) { CB_SIZE_TIME = atoi(optarg); }
210
        if( strcmp( "randomize_start", long_options[option_index].name ) == 0 ) { randomize_start = atoi(optarg); }
211
        if( strcmp( "capacity_override", long_options[option_index].name ) == 0 ) { capacity_override = atod_kmg(optarg); }
212
        if( strcmp( "desired_bw", long_options[option_index].name ) == 0 ) { desired_bw = atod_kmg(optarg); }
213
        if( strcmp( "topo_mem", long_options[option_index].name ) == 0 ) { topo_mem = atof(optarg); }
214
        else if( strcmp( "topo_in", long_options[option_index].name ) == 0 ) { topo_in = true; topo_out = false; }
215
        else if( strcmp( "topo_out", long_options[option_index].name ) == 0 ) { topo_in = false; topo_out = true; }
216
        else if( strcmp( "topo_bidir", long_options[option_index].name ) == 0 ) { topo_in = true; topo_out = true; }
217
        break;
218
      case 'a':
219
        alpha_target = (double)atoi(optarg) / 100.0;
220
        break;
221
      case 'r':
222
        desired_rtt = (double)atoi(optarg) / 1000.0;
223
        break;
224
      case 'b':
225
        buff_size = atoi(optarg);
226
        break;
227
      case 'o':
228
        outbuff_size = atoi(optarg);
229
        break;
230
        case 'c':
231
        chunks_per_second = atoi(optarg);
232
        break;
233
      case 'm':
234
        multiply = atoi(optarg);
235
        break;
236
      case 'p':
237
        srv_port = atoi(optarg);
238
        break;
239
      case 'i':
240
        srv_ip = strdup(optarg);
241
        break;
242
      case 'P':
243
        port =  atoi(optarg);
244
        break;
245
      case 'I':
246
        my_iface = strdup(optarg);
247
        break;
248
      case 'f':
249
        fname = strdup(optarg);
250
        break;
251
      case 'F':
252
        output_config = strdup(optarg);
253
        break;
254
      case 'l':
255
        loop_input = true;
256
        break;
257
      case 'C':
258
        channel_set_name(optarg);
259
        break;
260
      case 'n':
261
        net_helper_config = strdup(optarg);
262
        break;
263
      case 'N':
264
        peername = strdup(optarg);
265
        break;
266
      case 'M':
267
        NEIGHBORHOOD_TARGET_SIZE = atoi(optarg);
268
        break;
269
      case 't':
270
        topo_config = strdup(optarg);
271
        break;
272
      case 'S':
273
        initial_id = atoi(optarg);
274
        break;
275
      case 's':
276
        start_id = atoi(optarg);
277
        break;
278
      case 'e':
279
        end_id = atoi(optarg);
280
        break;
281
      case 'v':
282
        fprintf(stderr, "Version: %s\n", EXTRAVERSION);
283
            exit(0);
284
      default:
285
        fprintf(stderr, "Error: unknown option %c\n", o);
286
        print_usage(argc, argv);
287

    
288
        exit(-1);
289
    }
290
  }
291

    
292
  if (!channel_get_name()) {
293
    channel_set_name("generic");
294
  }
295

    
296
  if (argc <= 1) {
297
    print_usage(argc, argv);
298
    fprintf(stderr, "Trying to start a source with default parameters, reading from %s\n", fname);
299
  }
300
}
301

    
302
const struct nodeID *get_my_addr(void)
303
{
304
  return my_sock;
305
}
306

    
307
static void init_rand(const char * s)
308
{
309
  long i, l, x;
310
  struct timeval t;
311

    
312
  gettimeofday(&t, NULL);
313

    
314
  x = 1;
315
  l = strlen(s);
316
  for (i = 0; i < l; i++) {
317
    x *= s[i];
318
  }
319

    
320
  srand(t.tv_usec * t.tv_sec * x);
321
}
322

    
323
static struct nodeID *init(void)
324
{
325
  int i;
326
  struct nodeID *myID;
327
  char *my_addr;
328

    
329
  if (my_iface) {
330
    my_addr = iface_addr(my_iface);
331
  } else {
332
    my_addr = default_ip_addr();
333
  }
334

    
335
  if (my_addr == NULL) {
336
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
337

    
338
    return NULL;
339
  }
340
  for (i=0;i<2;i++)
341
          bind_msg_type(msgTypes[i]);
342
  myID = net_helper_init(my_addr, port, net_helper_config);
343
  if (myID == NULL) {
344
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
345
    free(my_addr);
346

    
347
    return NULL;
348
  }
349
  free(my_addr);
350
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
351

    
352
  init_rand(node_addr(myID));
353

    
354
  topologyInit(myID, topo_config);
355

    
356
  return myID;
357
}
358

    
359
void leave(int sig) {
360
  fprintf(stderr, "Received signal %d, exiting!\n", sig);
361
  end_measures();
362
  exit(sig);
363
}
364

    
365
static void random_wait(int max) {
366
    struct timespec t;
367
    uint64_t ms;
368

    
369
    ms = (rand()/(RAND_MAX + 1.0)) * max;
370
    t.tv_sec = ms / 1000000;
371
    t.tv_nsec = (ms % 1000000) * 1000;
372
    nanosleep(&t, NULL);
373
}
374

    
375
int main(int argc, char *argv[])
376
{
377

    
378
  (void) signal(SIGTERM,leave);
379
  (void) signal(SIGINT,leave);
380

    
381
  cmdline_parse(argc, argv);
382

    
383
  my_sock = init();
384
  if (my_sock == NULL) {
385
    fprintf(stderr, "Cannot initialize streamer, exiting!\n");
386
    return -1;
387
  }
388
  if (srv_port != 0) {
389
    struct nodeID *srv;
390

    
391
    //random wait a bit before starting
392
    if (randomize_start) random_wait(randomize_start);
393

    
394
    output_init(outbuff_size, output_config);
395

    
396
    srv = create_node(srv_ip, srv_port);
397
    if (srv == NULL) {
398
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
399

    
400
      return -1;
401
    }
402
    topoAddNeighbour(srv, NULL, 0);
403

    
404
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
405
  } else {
406
    source_loop(fname, my_sock, period * 1000, multiply, loop_input);
407
  }
408
  return 0;
409
}
410

    
411
int am_i_source()
412
{
413
  return (srv_port == 0);
414
}
415

    
416
int get_cb_size()
417
{
418
  return buff_size;
419
}
420

    
421
int get_chunks_per_sec()
422
{
423
  return chunks_per_second;
424
}
425

    
426
//capacity in bits/s, or NAN if unknown
427
double get_capacity()
428
{
429
  return capacity_override;
430
}