Statistics
| Branch: | Revision:

streamers / dumbstreamer.c @ 0a40460a

History | View | Annotate | Download (1.97 KB)

1
/*
2
 *  Copyright (c) 2009 Luca Abeni
3
 *
4
 *  This is free software; see GPL.txt
5
 */
6
#include <stdlib.h>
7
#include <stdint.h>
8
#include <stdio.h>
9
#include <string.h>
10
#include <getopt.h>
11

    
12
#include <net_helper.h>
13
#include <topmanager.h>
14

    
15
#include "net_helpers.h"
16
#include "loop.h"
17

    
18
static const char *my_addr = "127.0.0.1";
19
static int port = 6666;
20
static int srv_port;
21
static const char *srv_ip = "";
22
static int period = 500;
23
static int chunks_per_second = 4;
24
static int buff_size = 8;
25

    
26
static void cmdline_parse(int argc, char *argv[])
27
{
28
  int o;
29

    
30
  while ((o = getopt(argc, argv, "b:c:t:p:i:P:I:")) != -1) {
31
    switch(o) {
32
      case 'b':
33
        buff_size = atoi(optarg);
34
        break;
35
      case 'c':
36
        chunks_per_second = atoi(optarg);
37
        break;
38
      case 't':
39
        period = atoi(optarg);
40
        break;
41
      case 'p':
42
        srv_port = atoi(optarg);
43
        break;
44
      case 'i':
45
        srv_ip = strdup(optarg);
46
        break;
47
      case 'P':
48
        port =  atoi(optarg);
49
        break;
50
      case 'I':
51
        my_addr = iface_addr(optarg);
52
        break;
53
      default:
54
        fprintf(stderr, "Error: unknown option %c\n", o);
55

    
56
        exit(-1);
57
    }
58
  }
59
}
60

    
61
static struct nodeID *init(void)
62
{
63
  struct nodeID *myID;
64

    
65
  myID = create_socket(my_addr, port);
66
  if (myID == NULL || getFD(myID) == -1) {
67
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
68
    return NULL;
69
  }
70
  topInit(myID);
71

    
72
  return myID;
73
}
74

    
75

    
76
int main(int argc, char *argv[])
77
{
78
  struct nodeID *my_sock;
79

    
80
  cmdline_parse(argc, argv);
81

    
82
  my_sock = init();
83
  if (my_sock == NULL) {
84
    return -1;
85
  }
86
  if (srv_port != 0) {
87
    struct nodeID *srv;
88

    
89
    srv = create_socket(srv_ip, srv_port);
90
    if (srv == NULL) {
91
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
92

    
93
      return -1;
94
    }
95
    topAddNeighbour(srv);
96

    
97
    loop(my_sock, 1000000 / chunks_per_second, buff_size);
98
  }
99

    
100
  source_loop(my_sock, period * 1000, chunks_per_second * period / 1000);
101

    
102
  return 0;
103
}