Statistics
| Branch: | Revision:

streamers / streamer.c @ b88c9f4a

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

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

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

    
28
const char *peername = NULL;
29

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

    
52
extern int NEIGHBORHOOD_TARGET_SIZE;
53

    
54
extern struct timeval print_tdiff;
55
extern struct timeval tstartdiff;
56

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

    
108

    
109

    
110
static void cmdline_parse(int argc, char *argv[])
111
{
112
  int o;
113

    
114
  int option_index = 0;
115
  static struct option long_options[] = {
116
        {"chunk_log", no_argument, 0, 0},
117
        {"measure_start", required_argument, 0, 0},
118
        {"measure_every", required_argument, 0, 0},
119
        {0, 0, 0, 0}
120
  };
121

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

    
181
        exit(-1);
182
    }
183
  }
184

    
185
  if (!channel_get_name()) {
186
    channel_set_name("generic");
187
  }
188

    
189
  if (argc <= 1) {
190
    print_usage(argc, argv);
191
    fprintf(stderr, "Trying to start a source with default parameters, reading from %s\n", fname);
192
  }
193
}
194

    
195
static void init_rand(const char * s)
196
{
197
  long i, l, x;
198
  struct timeval t;
199

    
200
  gettimeofday(&t, NULL);
201

    
202
  x = 1;
203
  l = strlen(s);
204
  for (i = 0; i < l; i++) {
205
    x *= s[i];
206
  }
207

    
208
  srand(t.tv_usec * t.tv_sec * x);
209
}
210

    
211
static struct nodeID *init(void)
212
{
213
  int i;
214
  struct nodeID *myID;
215
  char *my_addr;
216

    
217
  if (my_iface) {
218
    my_addr = iface_addr(my_iface);
219
  } else {
220
    my_addr = default_ip_addr();
221
  }
222

    
223
  if (my_addr == NULL) {
224
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
225

    
226
    return NULL;
227
  }
228
  for (i=0;i<2;i++)
229
          bind_msg_type(msgTypes[i]);
230
  myID = net_helper_init(my_addr, port, net_helper_config);
231
  if (myID == NULL) {
232
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
233
    free(my_addr);
234

    
235
    return NULL;
236
  }
237
  free(my_addr);
238
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
239

    
240
  init_rand(node_addr(myID));
241

    
242
  topologyInit(myID, topo_config);
243

    
244
  return myID;
245
}
246

    
247
void leave(int sig) {
248
  end_measures();
249
  exit(sig);
250
}
251

    
252
int main(int argc, char *argv[])
253
{
254
  struct nodeID *my_sock;
255

    
256
  (void) signal(SIGTERM,leave);
257

    
258
  cmdline_parse(argc, argv);
259

    
260
  my_sock = init();
261
  if (my_sock == NULL) {
262
    return -1;
263
  }
264
  if (srv_port != 0) {
265
    struct nodeID *srv;
266

    
267
    output_init(outbuff_size, output_config);
268

    
269
    srv = create_node(srv_ip, srv_port);
270
    if (srv == NULL) {
271
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
272

    
273
      return -1;
274
    }
275
    topoAddNeighbour(srv, NULL, 0);
276

    
277
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
278
  } else {
279
    source_loop(fname, my_sock, period * 1000, multiply, loop_input);
280
  }
281
  return 0;
282
}
283

    
284
int get_cb_size()
285
{
286
  return buff_size;
287
}