Statistics
| Branch: | Revision:

streamers / dumbstreamer.c @ 0f23522a

History | View | Annotate | Download (1.73 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

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

    
29
  while ((o = getopt(argc, argv, "p:i:P:I:")) != -1) {
30
    switch(o) {
31
      case 'p':
32
        srv_port = atoi(optarg);
33
        break;
34
      case 'i':
35
        srv_ip = strdup(optarg);
36
        break;
37
      case 'P':
38
        port =  atoi(optarg);
39
        break;
40
      case 'I':
41
        my_addr = iface_addr(optarg);
42
        break;
43
      default:
44
        fprintf(stderr, "Error: unknown option %c\n", o);
45

    
46
        exit(-1);
47
    }
48
  }
49
}
50

    
51
static struct nodeID *init(void)
52
{
53
  struct nodeID *myID;
54

    
55
  myID = create_socket(my_addr, port);
56
  if (myID == NULL || getFD(myID) == -1) {
57
    fprintf(stderr, "Error creating my socket (%s:%d)!\n", my_addr, port);
58
    return NULL;
59
  }
60
  topInit(myID);
61

    
62
  return myID;
63
}
64

    
65

    
66
int main(int argc, char *argv[])
67
{
68
  struct nodeID *my_sock;
69

    
70
  cmdline_parse(argc, argv);
71

    
72
  my_sock = init();
73
  if (my_sock == NULL) {
74
    return -1;
75
  }
76
  if (srv_port != 0) {
77
    struct nodeID *srv;
78

    
79
    srv = create_socket(srv_ip, srv_port);
80
    if (srv == NULL) {
81
      fprintf(stderr, "Cannot resolve remote address %s:%d\n", srv_ip, srv_port);
82

    
83
      return -1;
84
    }
85
    topAddNeighbour(srv);
86

    
87
    loop(my_sock, 1000000 / chunks_per_second);
88
  }
89

    
90
  source_loop(my_sock, period * 1000, chunks_per_second * period / 1000);
91

    
92
  return 0;
93
}