Statistics
| Branch: | Revision:

streamers / streamer.c @ 14893aa5

History | View | Annotate | Download (6.76 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
#include "net_helpers.h"
18
#include "loop.h"
19 f0bb64e2 Csaba Kiraly
#include "output.h"
20 581339c4 Csaba Kiraly
#include "channel.h"
21 f088e50e MarcoBiazzini
#include "topology.h"
22 a8a31ce0 Csaba Kiraly
#include "measures.h"
23 89e893e2 Luca
24 7c21b5b1 CsabaKiraly
const char *peername = NULL;
25
26 613263e9 Csaba Kiraly
static const char *my_iface = NULL;
27 89e893e2 Luca
static int port = 6666;
28
static int srv_port;
29 0f23522a Luca
static const char *srv_ip = "";
30 66f50156 Csaba Kiraly
static int period = 40;
31
static int chunks_per_second = 25;
32 6d0c8116 GiuseppeTropea
#ifdef HTTPIO
33
//input-http.c needs this in order to accomplish the -m multiple send_chunk()
34
int multiply = 1;
35
#else
36 ce80b058 Luca Abeni
static int multiply = 1;
37 6d0c8116 GiuseppeTropea
#endif
38 789d6502 Csaba Kiraly
static int buff_size = 50;
39 f0bb64e2 Csaba Kiraly
static int outbuff_size = 25;
40 7442ecb3 Luca
static const char *fname = "input.mpg";
41 463000a4 CsabaKiraly
static const char *output_config;
42 46c24e94 Csaba Kiraly
static bool loop_input = false;
43 193622ac CsabaKiraly
static const char *net_helper_config = "";
44 33d23b91 MarcoBiazzini
unsigned char msgTypes[] = {MSG_TYPE_CHUNK,MSG_TYPE_SIGNALLING};
45 1cd06c26 CsabaKiraly
bool chunk_log = false;
46 89e893e2 Luca
47 20663df3 Csaba Kiraly
extern int NEIGHBORHOOD_TARGET_SIZE;
48
49 75b2b975 Csaba Kiraly
extern struct timeval print_tdiff;
50
extern struct timeval tstartdiff;
51
52 a02fc4cf MarcoMellia
static void print_usage()
53
{
54
  fprintf (stderr,
55 e63b7606 MatteoSammarco
    "Usage:offerstreamer [-bocmtpiPIflCh --chunk_log]\n"
56 a02fc4cf MarcoMellia
    "\n"
57
    "Peer options\n"
58
    "\t[-p port]: port of the remote peer to connect at during bootstrap.\n"
59
    "\t           Usually it is the source peer port.\n"
60
    "\t[-i IP]: IP address of the remote peer to connect at during bootstrap.\n"
61
    "\t         Usually it is the source peer IP\n"
62
    "\t[-C name]: set the channel name to use on the repository.\n"
63
    "\t           All peers should use the same channel name.\n"
64
    "\n"
65
    "\t[-b size]: set the peer Chunk Buffer size.\n"
66
    "\t           This is also the chunk trading window size.\n"
67
    "\t[-o size]: set the Output Buffer size.\n"
68
    "\t[-c chunks]: set the number of chunks a peer can send per seconds.\n"
69
    "\t             it controls the upload capacity of peer as well.\n"
70 20663df3 Csaba Kiraly
    "\t[-M peers]: neighbourhood target size.\n"
71 a02fc4cf MarcoMellia
    "\t[-t time]: chunk emission period. STILL NEEDED??\n"
72
    "\t[-P port]: local UDP port to be used by the peer.\n"
73
    "\t[-I IP]: local IP address to be used by the peer.\n"
74
    "\t         Useful if the host has several interfaces/addresses.\n"
75 7c21b5b1 CsabaKiraly
    "\t[-N name]: set the name of the peer.\n"
76
    "\t         This name will be used when publishing in the repository.\n"
77 193622ac CsabaKiraly
    "\t[-n options]: pass configuration options to the net-helper\n"
78 1cd06c26 CsabaKiraly
    "\t[--chunk_log]: print a chunk level log on stderr\n"
79 a02fc4cf MarcoMellia
    "\n"
80
    "Special Source Peer options\n"
81
    "\t[-m chunks]: set the number of copies the source injects in the overlay.\n"
82
    "\t[-f filename]: name of the video stream file to transmit.\n"
83 1cd06c26 CsabaKiraly
    "\t[-F config]: configure the output module\n"
84 8977e724 CsabaKiraly
    "\t[-l]: loop the video stream.\n"
85 a02fc4cf MarcoMellia
    "\n"
86
    "NOTE: the peer will dump the received video on STDOUT in raw format\n"
87
    "      it can be played by your favourite player simply using a pipe\n"
88
    "      e.g., | vlc -\n"
89
    "\n"
90
    "Examples:\n"
91
    "\n"
92
    "Start a source peer on port 6600:\n"
93
    "\n"
94
    "./offestreamer -m 3 -C MyTest -l -f foreman.avi -P 6600\n"
95
    "\n"
96
    "Start a peer connecting to the previous source, and using videolan as player:\n"
97
    "\n"
98
    "./offerstreamer -i 130.192.9.140 -p 6600 |vlc -\n"
99
    "\n"
100
    
101
    );
102
  }
103
104
105
106 89e893e2 Luca
static void cmdline_parse(int argc, char *argv[])
107
{
108
  int o;
109
110 4aaa8891 MatteoSammarco
  int option_index = 0;
111
  static struct option long_options[] = {
112 e63b7606 MatteoSammarco
        {"chunk_log", no_argument, 0, 0},
113 75b2b975 Csaba Kiraly
        {"measure_start", required_argument, 0, 0},
114
        {"measure_every", required_argument, 0, 0},
115 e63b7606 MatteoSammarco
        {0, 0, 0, 0}
116 4aaa8891 MatteoSammarco
  };
117
118 20663df3 Csaba Kiraly
    while ((o = getopt_long (argc, argv, "b:o:c:t:p:i:P:I:f:F:m:lC:N:n:M:",long_options, &option_index)) != -1) { //use this function to manage long options
119 89e893e2 Luca
    switch(o) {
120 4aaa8891 MatteoSammarco
      case 0: //for long options
121 1cd06c26 CsabaKiraly
        if( strcmp( "chunk_log", long_options[option_index].name ) == 0 ) { chunk_log = true; }
122 75b2b975 Csaba Kiraly
        if( strcmp( "measure_start", long_options[option_index].name ) == 0 ) { tstartdiff.tv_sec = atoi(optarg); }
123
        if( strcmp( "measure_every", long_options[option_index].name ) == 0 ) { print_tdiff.tv_sec = atoi(optarg); }
124 1cd06c26 CsabaKiraly
        break;
125 90f3a290 CsabaKiraly
      case 'b':
126 0a40460a Luca
        buff_size = atoi(optarg);
127
        break;
128 90f3a290 CsabaKiraly
      case 'o':
129 f0bb64e2 Csaba Kiraly
        outbuff_size = atoi(optarg);
130
        break;
131 90f3a290 CsabaKiraly
        case 'c':
132 0a40460a Luca
        chunks_per_second = atoi(optarg);
133
        break;
134 90f3a290 CsabaKiraly
      case 'm':
135 59b7cfc6 Csaba Kiraly
        multiply = atoi(optarg);
136
        break;
137 90f3a290 CsabaKiraly
      case 't':
138 0a40460a Luca
        period = atoi(optarg);
139
        break;
140 90f3a290 CsabaKiraly
      case 'p':
141 89e893e2 Luca
        srv_port = atoi(optarg);
142
        break;
143 90f3a290 CsabaKiraly
      case 'i':
144 89e893e2 Luca
        srv_ip = strdup(optarg);
145
        break;
146 90f3a290 CsabaKiraly
      case 'P':
147 89e893e2 Luca
        port =  atoi(optarg);
148
        break;
149 90f3a290 CsabaKiraly
      case 'I':
150 568bd1bd Luca Abeni
        my_iface = strdup(optarg);
151 89e893e2 Luca
        break;
152 90f3a290 CsabaKiraly
      case 'f':
153 e37511b1 Luca Abeni
        fname = strdup(optarg);
154
        break;
155 463000a4 CsabaKiraly
      case 'F':
156
        output_config = strdup(optarg);
157
        break;
158 90f3a290 CsabaKiraly
      case 'l':
159 46c24e94 Csaba Kiraly
        loop_input = true;
160
        break;
161 90f3a290 CsabaKiraly
      case 'C':
162 581339c4 Csaba Kiraly
        channel_set_name(optarg);
163 8253c8ea CsabaKiraly
        break;
164 193622ac CsabaKiraly
      case 'n':
165
        net_helper_config = strdup(optarg);
166 581339c4 Csaba Kiraly
        break;
167 7c21b5b1 CsabaKiraly
      case 'N':
168
        peername = strdup(optarg);
169
        break;
170 20663df3 Csaba Kiraly
      case 'M':
171
        NEIGHBORHOOD_TARGET_SIZE = atoi(optarg);
172
        break;
173 89e893e2 Luca
      default:
174
        fprintf(stderr, "Error: unknown option %c\n", o);
175 a02fc4cf MarcoMellia
        print_usage();
176 89e893e2 Luca
177
        exit(-1);
178
    }
179
  }
180 581339c4 Csaba Kiraly
181
  if (!channel_get_name()) {
182
    channel_set_name("generic");
183
  }
184 89e893e2 Luca
}
185
186
static struct nodeID *init(void)
187
{
188 bdd19faf Csaba Kiraly
  int i;
189 89e893e2 Luca
  struct nodeID *myID;
190 613263e9 Csaba Kiraly
  char *my_addr;
191
192
  if (my_iface) {
193
    my_addr = iface_addr(my_iface);
194
  } else {
195 371a4639 Csaba Kiraly
    my_addr = default_ip_addr();
196 613263e9 Csaba Kiraly
  }
197 89e893e2 Luca
198 f858e682 Luca Abeni
  if (my_addr == NULL) {
199
    fprintf(stderr, "Cannot find network interface %s\n", my_iface);
200
201
    return NULL;
202
  }
203 33d23b91 MarcoBiazzini
  for (i=0;i<2;i++)
204 bdd19faf Csaba Kiraly
          bind_msg_type(msgTypes[i]);
205 193622ac CsabaKiraly
  myID = net_helper_init(my_addr, port, net_helper_config);
206 4136911a Luca Abeni
  if (myID == NULL) {
207 89e893e2 Luca
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
208 568bd1bd Luca Abeni
    free(my_addr);
209 4136911a Luca Abeni
210 e63ea799 Luca
    return NULL;
211 89e893e2 Luca
  }
212 568bd1bd Luca Abeni
  free(my_addr);
213 5331023a CsabaKiraly
  fprintf(stderr, "My network ID is: %s\n", node_addr(myID));
214 f088e50e MarcoBiazzini
  topologyInit(myID, "");
215 89e893e2 Luca
216
  return myID;
217
}
218
219 5dcfffbc CsabaKiraly
void leave(int sig) {
220 a8a31ce0 Csaba Kiraly
  end_measures();
221 5dcfffbc CsabaKiraly
  exit(sig);
222
}
223 89e893e2 Luca
224
int main(int argc, char *argv[])
225
{
226
  struct nodeID *my_sock;
227
228 5dcfffbc CsabaKiraly
  (void) signal(SIGTERM,leave);
229
230 89e893e2 Luca
  cmdline_parse(argc, argv);
231
232
  my_sock = init();
233 e63ea799 Luca
  if (my_sock == NULL) {
234
    return -1;
235
  }
236 89e893e2 Luca
  if (srv_port != 0) {
237 74a5d4ae CsabaKiraly
    struct nodeID *srv;
238 89e893e2 Luca
239 463000a4 CsabaKiraly
    output_init(outbuff_size, output_config);
240
241 4136911a Luca Abeni
    srv = create_node(srv_ip, srv_port);
242 0f23522a Luca
    if (srv == NULL) {
243
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
244
245
      return -1;
246
    }
247 f088e50e MarcoBiazzini
    topoAddNeighbour(srv, NULL, 0);
248 89e893e2 Luca
249 0a40460a Luca
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
250 98c28f0f CsabaKiraly
  } else {
251 14893aa5 CsabaKiraly
    source_loop(fname, my_sock, period * 1000, multiply, loop_input, buff_size);
252 89e893e2 Luca
  }
253
  return 0;
254
}