Statistics
| Branch: | Revision:

streamers / streamer.c @ 4536b69e

History | View | Annotate | Download (7.93 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 <grapes_msg_types.h>
16
#include <net_helper.h>
17

    
18
#ifdef WIN32
19
#include <winsock2.h>
20
#endif
21

    
22
#include "net_helpers.h"
23
#include "loop.h"
24
#include "output.h"
25
#include "channel.h"
26
#include "topology.h"
27
#include "measures.h"
28

    
29
const char *peername = NULL;
30

    
31
static const char *my_iface = NULL;
32
static int port = 6666;
33
static int srv_port;
34
static const char *srv_ip = "";
35
static int period = 40;
36
static int chunks_per_second = 25;
37
#ifdef HTTPIO
38
//input-http.c needs this in order to accomplish the -m multiple send_chunk()
39
int multiply = 3;
40
#else
41
static int multiply = 3;
42
#endif
43
static int buff_size = 50;
44
static int outbuff_size = 50;
45
static const char *fname = "/dev/stdin";
46
static const char *output_config;
47
static bool loop_input = false;
48
static const char *net_helper_config = "";
49
static const char *topo_config = "";
50
unsigned char msgTypes[] = {MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
51
bool chunk_log = false;
52
static int randomize_start = 0;
53

    
54
extern int NEIGHBORHOOD_TARGET_SIZE;
55

    
56
extern struct timeval print_tdiff;
57
extern struct timeval tstartdiff;
58

    
59
static void print_usage(int argc, char *argv[])
60
{
61
  fprintf (stderr,
62
    "Usage:%s [options]\n"
63
    "\n"
64
    "Peer options\n"
65
    "\t[-p port]: port of the remote peer to connect at during bootstrap.\n"
66
    "\t           Usually it is the source peer port.\n"
67
    "\t[-i IP]: IP address of the remote peer to connect at during bootstrap.\n"
68
    "\t         Usually it is the source peer IP\n"
69
    "\t[-C name]: set the channel name to use on the repository.\n"
70
    "\t           All peers should use the same channel name.\n"
71
    "\n"
72
    "\t[-b size]: set the peer Chunk Buffer size.\n"
73
    "\t           This is also the chunk trading window size.\n"
74
    "\t[-o size]: set the Output Buffer size.\n"
75
    "\t[-c chunks]: set the number of chunks a peer can send per seconds.\n"
76
    "\t             it controls the upload capacity of peer as well.\n"
77
    "\t[-M peers]: neighbourhood target size.\n"
78
    "\t[-t config]: topology config.\n"
79
    "\t[-P port]: local UDP port to be used by the peer.\n"
80
    "\t[-I iface]: local netwok interface to be used by the peer.\n"
81
    "\t         Useful if the host has several interfaces/addresses.\n"
82
    "\t[-N name]: set the name of the peer.\n"
83
    "\t         This name will be used when publishing in the repository.\n"
84
    "\t[-n options]: pass configuration options to the net-helper\n"
85
    "\t[--chunk_log]: print a chunk level log on stderr\n"
86
    "\t[-F config]: configure the output module\n"
87
    "\n"
88
    "Special Source Peer options\n"
89
    "\t[-m chunks]: set the number of copies the source injects in the overlay.\n"
90
    "\t[-f filename]: name of the video stream file to transmit.\n"
91
    "\t[-l]: loop the video stream.\n"
92
    "\n"
93
    "Special options\n"
94
    "\t[--randomize_start ms]: random wait before starting in to ms millisecs.\n"
95
    "\n"
96
    "NOTE: by deafult the peer will dump the received video on STDOUT in raw format\n"
97
    "      it can be played by your favourite player simply using a pipe\n"
98
    "      e.g., | cvlc /dev/stdin\n"
99
    "\n"
100
    "Examples:\n"
101
    "\n"
102
    "Start a source peer on port 6600:\n"
103
    "\n"
104
    "%s -l -f foreman.mpg -P 6600\n"
105
    "\n"
106
    "Start a peer connecting to the previous source, and using videolan as player:\n"
107
    "\n"
108
    "%s -i <sourceIP> -p <sourcePort> | cvlc /dev/stdin\n"
109
    "=======================================================\n", argv[0], argv[0], argv[0]
110
    );
111
  }
112

    
113

    
114

    
115
static void cmdline_parse(int argc, char *argv[])
116
{
117
  int o;
118

    
119
  int option_index = 0;
120
  static struct option long_options[] = {
121
        {"chunk_log", no_argument, 0, 0},
122
        {"measure_start", required_argument, 0, 0},
123
        {"measure_every", required_argument, 0, 0},
124
        {"randomize_start", required_argument, 0, 0},
125
        {0, 0, 0, 0}
126
  };
127

    
128
    while ((o = getopt_long (argc, argv, "b:o:c:p:i:P:I:f:F:m:lC:N:n:M:t:",long_options, &option_index)) != -1) { //use this function to manage long options
129
    switch(o) {
130
      case 0: //for long options
131
        if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { chunk_log = true; }
132
        if( strcmp( "measure_start", long_options[option_index].name ) == 0 ) { tstartdiff.tv_sec = atoi(optarg); }
133
        if( strcmp( "measure_every", long_options[option_index].name ) == 0 ) { print_tdiff.tv_sec = atoi(optarg); }
134
        if( strcmp( "randomize_start", long_options[option_index].name ) == 0 ) { randomize_start = atoi(optarg); }
135
        break;
136
      case 'b':
137
        buff_size = atoi(optarg);
138
        break;
139
      case 'o':
140
        outbuff_size = atoi(optarg);
141
        break;
142
        case 'c':
143
        chunks_per_second = atoi(optarg);
144
        break;
145
      case 'm':
146
        multiply = atoi(optarg);
147
        break;
148
      case 'p':
149
        srv_port = atoi(optarg);
150
        break;
151
      case 'i':
152
        srv_ip = strdup(optarg);
153
        break;
154
      case 'P':
155
        port =  atoi(optarg);
156
        break;
157
      case 'I':
158
        my_iface = strdup(optarg);
159
        break;
160
      case 'f':
161
        fname = strdup(optarg);
162
        break;
163
      case 'F':
164
        output_config = strdup(optarg);
165
        break;
166
      case 'l':
167
        loop_input = true;
168
        break;
169
      case 'C':
170
        channel_set_name(optarg);
171
        break;
172
      case 'n':
173
        net_helper_config = strdup(optarg);
174
        break;
175
      case 'N':
176
        peername = strdup(optarg);
177
        break;
178
      case 'M':
179
        NEIGHBORHOOD_TARGET_SIZE = atoi(optarg);
180
        break;
181
      case 't':
182
        topo_config = strdup(optarg);
183
        break;
184
      default:
185
        fprintf(stderr, "Error: unknown option %c\n", o);
186
        print_usage(argc, argv);
187

    
188
        exit(-1);
189
    }
190
  }
191

    
192
  if (!channel_get_name()) {
193
    channel_set_name("generic");
194
  }
195

    
196
  if (argc <= 1) {
197
    print_usage(argc, argv);
198
    fprintf(stderr, "Trying to start a source with default parameters, reading from %s\n", fname);
199
  }
200
}
201

    
202
static void init_rand(const char * s)
203
{
204
  long i, l, x;
205
  struct timeval t;
206

    
207
  gettimeofday(&t, NULL);
208

    
209
  x = 1;
210
  l = strlen(s);
211
  for (i = 0; i < l; i++) {
212
    x *= s[i];
213
  }
214

    
215
  srand(t.tv_usec * t.tv_sec * x);
216
}
217

    
218
static struct nodeID *init(void)
219
{
220
  int i;
221
  struct nodeID *myID;
222
  char *my_addr;
223

    
224
  if (my_iface) {
225
    my_addr = iface_addr(my_iface);
226
  } else {
227
    my_addr = default_ip_addr();
228
  }
229

    
230
  if (my_addr == NULL) {
231
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
232

    
233
    return NULL;
234
  }
235
  for (i=0;i<2;i++)
236
          bind_msg_type(msgTypes[i]);
237
  myID = net_helper_init(my_addr, port, net_helper_config);
238
  if (myID == NULL) {
239
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
240
    free(my_addr);
241

    
242
    return NULL;
243
  }
244
  free(my_addr);
245
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
246

    
247
  init_rand(node_addr(myID));
248

    
249
  topologyInit(myID, topo_config);
250

    
251
  return myID;
252
}
253

    
254
void leave(int sig) {
255
  end_measures();
256
  exit(sig);
257
}
258

    
259
static void random_wait(int max) {
260
    struct timespec t;
261
    uint64_t ms;
262

    
263
    ms = (rand()/(RAND_MAX + 1.0)) * max;
264
    t.tv_sec = ms / 1000000;
265
    t.tv_nsec = (ms % 1000000) * 1000;
266
    nanosleep(&t, NULL);
267
}
268

    
269
int main(int argc, char *argv[])
270
{
271
  struct nodeID *my_sock;
272

    
273
  (void) signal(SIGTERM,leave);
274

    
275
  cmdline_parse(argc, argv);
276

    
277
  my_sock = init();
278
  if (my_sock == NULL) {
279
    return -1;
280
  }
281
  if (srv_port != 0) {
282
    struct nodeID *srv;
283

    
284
    //random wait a bit before starting
285
    if (randomize_start) random_wait(randomize_start);
286

    
287
    output_init(outbuff_size, output_config);
288

    
289
    srv = create_node(srv_ip, srv_port);
290
    if (srv == NULL) {
291
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
292

    
293
      return -1;
294
    }
295
    topoAddNeighbour(srv, NULL, 0);
296

    
297
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
298
  } else {
299
    source_loop(fname, my_sock, period * 1000, multiply, loop_input);
300
  }
301
  return 0;
302
}