Statistics
| Branch: | Revision:

streamers / streamer.c @ 70921454

History | View | Annotate | Download (7.39 KB)

1 89e893e2 Luca
/*
2 8fed7779 CsabaKiraly
 *  Copyright (c) 2010 Luca Abeni
3
 *  Copyright (c) 2010 Csaba Kiraly
4 89e893e2 Luca
 *
5 8fed7779 CsabaKiraly
 *  This is free software; see gpl-3.0.txt
6 89e893e2 Luca
 */
7
#include <stdlib.h>
8
#include <stdint.h>
9
#include <stdio.h>
10 46c24e94 Csaba Kiraly
#include <stdbool.h>
11 89e893e2 Luca
#include <string.h>
12
#include <getopt.h>
13 5dcfffbc CsabaKiraly
#include <signal.h>
14 e28a1487 CsabaKiraly
#include <grapes_msg_types.h>
15 89e893e2 Luca
#include <net_helper.h>
16
17 95e6fe96 CsabaKiraly
#ifdef WIN32
18
#include <winsock2.h>
19
#endif
20
21 89e893e2 Luca
#include "net_helpers.h"
22
#include "loop.h"
23 f0bb64e2 Csaba Kiraly
#include "output.h"
24 581339c4 Csaba Kiraly
#include "channel.h"
25 f088e50e MarcoBiazzini
#include "topology.h"
26 a8a31ce0 Csaba Kiraly
#include "measures.h"
27 89e893e2 Luca
28 7c21b5b1 CsabaKiraly
const char *peername = NULL;
29
30 613263e9 Csaba Kiraly
static const char *my_iface = NULL;
31 89e893e2 Luca
static int port = 6666;
32
static int srv_port;
33 0f23522a Luca
static const char *srv_ip = "";
34 66f50156 Csaba Kiraly
static int period = 40;
35
static int chunks_per_second = 25;
36 6d0c8116 GiuseppeTropea
#ifdef HTTPIO
37
//input-http.c needs this in order to accomplish the -m multiple send_chunk()
38 66f84460 CsabaKiraly
int multiply = 3;
39 6d0c8116 GiuseppeTropea
#else
40 66f84460 CsabaKiraly
static int multiply = 3;
41 6d0c8116 GiuseppeTropea
#endif
42 789d6502 Csaba Kiraly
static int buff_size = 50;
43 66f84460 CsabaKiraly
static int outbuff_size = 50;
44 adb2a9b2 CsabaKiraly
static const char *fname = "/dev/stdin";
45 463000a4 CsabaKiraly
static const char *output_config;
46 46c24e94 Csaba Kiraly
static bool loop_input = false;
47 193622ac CsabaKiraly
static const char *net_helper_config = "";
48 5f72f6b3 Csaba Kiraly
static const char *topo_config = "";
49 33d23b91 MarcoBiazzini
unsigned char msgTypes[] = {MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
50 1cd06c26 CsabaKiraly
bool chunk_log = false;
51 89e893e2 Luca
52 20663df3 Csaba Kiraly
extern int NEIGHBORHOOD_TARGET_SIZE;
53
54 75b2b975 Csaba Kiraly
extern struct timeval print_tdiff;
55
extern struct timeval tstartdiff;
56
57 43c2d7d6 CsabaKiraly
static void print_usage(int argc, char *argv[])
58 a02fc4cf MarcoMellia
{
59
  fprintf (stderr,
60 60784aa1 CsabaKiraly
    "Usage:%s [options]\n"
61 a02fc4cf MarcoMellia
    "\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 20663df3 Csaba Kiraly
    "\t[-M peers]: neighbourhood target size.\n"
76 5f72f6b3 Csaba Kiraly
    "\t[-t config]: topology config.\n"
77 a02fc4cf MarcoMellia
    "\t[-P port]: local UDP port to be used by the peer.\n"
78 60784aa1 CsabaKiraly
    "\t[-I iface]: local netwok interface to be used by the peer.\n"
79 a02fc4cf MarcoMellia
    "\t         Useful if the host has several interfaces/addresses.\n"
80 7c21b5b1 CsabaKiraly
    "\t[-N name]: set the name of the peer.\n"
81
    "\t         This name will be used when publishing in the repository.\n"
82 193622ac CsabaKiraly
    "\t[-n options]: pass configuration options to the net-helper\n"
83 1cd06c26 CsabaKiraly
    "\t[--chunk_log]: print a chunk level log on stderr\n"
84 60784aa1 CsabaKiraly
    "\t[-F config]: configure the output module\n"
85 a02fc4cf MarcoMellia
    "\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 8977e724 CsabaKiraly
    "\t[-l]: loop the video stream.\n"
90 a02fc4cf MarcoMellia
    "\n"
91 60784aa1 CsabaKiraly
    "NOTE: by deafult the peer will dump the received video on STDOUT in raw format\n"
92 a02fc4cf MarcoMellia
    "      it can be played by your favourite player simply using a pipe\n"
93 60784aa1 CsabaKiraly
    "      e.g., | cvlc /dev/stdin\n"
94 a02fc4cf MarcoMellia
    "\n"
95
    "Examples:\n"
96
    "\n"
97
    "Start a source peer on port 6600:\n"
98
    "\n"
99 60784aa1 CsabaKiraly
    "%s -l -f foreman.mpg -P 6600\n"
100 a02fc4cf MarcoMellia
    "\n"
101
    "Start a peer connecting to the previous source, and using videolan as player:\n"
102
    "\n"
103 60784aa1 CsabaKiraly
    "%s -i <sourceIP> -p <sourcePort> | cvlc /dev/stdin\n"
104
    "=======================================================\n", argv[0], argv[0], argv[0]
105 a02fc4cf MarcoMellia
    );
106
  }
107
108
109
110 89e893e2 Luca
static void cmdline_parse(int argc, char *argv[])
111
{
112
  int o;
113
114 4aaa8891 MatteoSammarco
  int option_index = 0;
115
  static struct option long_options[] = {
116 e63b7606 MatteoSammarco
        {"chunk_log", no_argument, 0, 0},
117 75b2b975 Csaba Kiraly
        {"measure_start", required_argument, 0, 0},
118
        {"measure_every", required_argument, 0, 0},
119 e63b7606 MatteoSammarco
        {0, 0, 0, 0}
120 4aaa8891 MatteoSammarco
  };
121
122 5f72f6b3 Csaba Kiraly
    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 89e893e2 Luca
    switch(o) {
124 4aaa8891 MatteoSammarco
      case 0: //for long options
125 1cd06c26 CsabaKiraly
        if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { chunk_log = true; }
126 75b2b975 Csaba Kiraly
        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 1cd06c26 CsabaKiraly
        break;
129 90f3a290 CsabaKiraly
      case 'b':
130 0a40460a Luca
        buff_size = atoi(optarg);
131
        break;
132 90f3a290 CsabaKiraly
      case 'o':
133 f0bb64e2 Csaba Kiraly
        outbuff_size = atoi(optarg);
134
        break;
135 90f3a290 CsabaKiraly
        case 'c':
136 0a40460a Luca
        chunks_per_second = atoi(optarg);
137
        break;
138 90f3a290 CsabaKiraly
      case 'm':
139 59b7cfc6 Csaba Kiraly
        multiply = atoi(optarg);
140
        break;
141 90f3a290 CsabaKiraly
      case 'p':
142 89e893e2 Luca
        srv_port = atoi(optarg);
143
        break;
144 90f3a290 CsabaKiraly
      case 'i':
145 89e893e2 Luca
        srv_ip = strdup(optarg);
146
        break;
147 90f3a290 CsabaKiraly
      case 'P':
148 89e893e2 Luca
        port =  atoi(optarg);
149
        break;
150 90f3a290 CsabaKiraly
      case 'I':
151 568bd1bd Luca Abeni
        my_iface = strdup(optarg);
152 89e893e2 Luca
        break;
153 90f3a290 CsabaKiraly
      case 'f':
154 e37511b1 Luca Abeni
        fname = strdup(optarg);
155
        break;
156 463000a4 CsabaKiraly
      case 'F':
157
        output_config = strdup(optarg);
158
        break;
159 90f3a290 CsabaKiraly
      case 'l':
160 46c24e94 Csaba Kiraly
        loop_input = true;
161
        break;
162 90f3a290 CsabaKiraly
      case 'C':
163 581339c4 Csaba Kiraly
        channel_set_name(optarg);
164 8253c8ea CsabaKiraly
        break;
165 193622ac CsabaKiraly
      case 'n':
166
        net_helper_config = strdup(optarg);
167 581339c4 Csaba Kiraly
        break;
168 7c21b5b1 CsabaKiraly
      case 'N':
169
        peername = strdup(optarg);
170
        break;
171 20663df3 Csaba Kiraly
      case 'M':
172
        NEIGHBORHOOD_TARGET_SIZE = atoi(optarg);
173
        break;
174 5f72f6b3 Csaba Kiraly
      case 't':
175
        topo_config = strdup(optarg);
176
        break;
177 89e893e2 Luca
      default:
178
        fprintf(stderr, "Error: unknown option %c\n", o);
179 43c2d7d6 CsabaKiraly
        print_usage(argc, argv);
180 89e893e2 Luca
181
        exit(-1);
182
    }
183
  }
184 581339c4 Csaba Kiraly
185
  if (!channel_get_name()) {
186
    channel_set_name("generic");
187
  }
188 37b749c8 CsabaKiraly
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 89e893e2 Luca
}
194
195 7a673aa5 Csaba Kiraly
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 89e893e2 Luca
static struct nodeID *init(void)
212
{
213 bdd19faf Csaba Kiraly
  int i;
214 89e893e2 Luca
  struct nodeID *myID;
215 613263e9 Csaba Kiraly
  char *my_addr;
216
217
  if (my_iface) {
218
    my_addr = iface_addr(my_iface);
219
  } else {
220 371a4639 Csaba Kiraly
    my_addr = default_ip_addr();
221 613263e9 Csaba Kiraly
  }
222 89e893e2 Luca
223 f858e682 Luca Abeni
  if (my_addr == NULL) {
224
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
225
226
    return NULL;
227
  }
228 33d23b91 MarcoBiazzini
  for (i=0;i<2;i++)
229 bdd19faf Csaba Kiraly
          bind_msg_type(msgTypes[i]);
230 193622ac CsabaKiraly
  myID = net_helper_init(my_addr, port, net_helper_config);
231 4136911a Luca Abeni
  if (myID == NULL) {
232 89e893e2 Luca
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
233 568bd1bd Luca Abeni
    free(my_addr);
234 4136911a Luca Abeni
235 e63ea799 Luca
    return NULL;
236 89e893e2 Luca
  }
237 568bd1bd Luca Abeni
  free(my_addr);
238 5331023a CsabaKiraly
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
239 7a673aa5 Csaba Kiraly
240
  init_rand(node_addr(myID));
241
242 5f72f6b3 Csaba Kiraly
  topologyInit(myID, topo_config);
243 89e893e2 Luca
244
  return myID;
245
}
246
247 5dcfffbc CsabaKiraly
void leave(int sig) {
248 a8a31ce0 Csaba Kiraly
  end_measures();
249 5dcfffbc CsabaKiraly
  exit(sig);
250
}
251 89e893e2 Luca
252
int main(int argc, char *argv[])
253
{
254
  struct nodeID *my_sock;
255
256 5dcfffbc CsabaKiraly
  (void) signal(SIGTERM,leave);
257
258 89e893e2 Luca
  cmdline_parse(argc, argv);
259
260
  my_sock = init();
261 e63ea799 Luca
  if (my_sock == NULL) {
262
    return -1;
263
  }
264 89e893e2 Luca
  if (srv_port != 0) {
265 74a5d4ae CsabaKiraly
    struct nodeID *srv;
266 89e893e2 Luca
267 463000a4 CsabaKiraly
    output_init(outbuff_size, output_config);
268
269 4136911a Luca Abeni
    srv = create_node(srv_ip, srv_port);
270 0f23522a Luca
    if (srv == NULL) {
271
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
272
273
      return -1;
274
    }
275 f088e50e MarcoBiazzini
    topoAddNeighbour(srv, NULL, 0);
276 89e893e2 Luca
277 0a40460a Luca
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
278 98c28f0f CsabaKiraly
  } else {
279
    source_loop(fname, my_sock, period * 1000, multiply, loop_input);
280 89e893e2 Luca
  }
281
  return 0;
282
}
283 b88c9f4a Csaba Kiraly
284 70921454 Csaba Kiraly
int am_i_source()
285
{
286
  return (srv_port == 0);
287
}
288
289 b88c9f4a Csaba Kiraly
int get_cb_size()
290
{
291
  return buff_size;
292
}