Statistics
| Branch: | Revision:

napa-baselibs / tests / Broadcaster / peer.c @ 507372bb

History | View | Annotate | Download (3.49 KB)

1
/*
2
 * % vim: syntax=c ts=4 tw=76 cindent shiftwidth=4
3
 * 
4
 * Peer implementation for the Broadcaster demo application
5
 *
6
 * This simple peer registers itself in the Repository, and then passively wait for chunks to arrive
7
 *
8
 * 
9
 * 
10
 */
11

    
12
#include "peer.h"
13

    
14
extern char *channel;
15

    
16
cfg_opt_t cfg_log[] = {
17
        CFG_INT("level", 3, CFGF_NONE),
18
        CFG_STR("logfile", "stdout", CFGF_NONE),
19
        CFG_STR("filemode", "w", CFGF_NONE),
20
        CFG_END()
21
};
22

    
23
/* Main configuration sections */
24
cfg_opt_t cfg_main[] = {
25
        CFG_SEC("network", cfg_ml, CFGF_NONE),
26
        CFG_SEC("repository", cfg_rep, CFGF_NONE),
27
        CFG_SEC("measurements", cfg_mon, CFGF_NONE),
28
        CFG_SEC("som", cfg_som, CFGF_NONE),
29
        CFG_SEC("source", cfg_source, CFGF_NONE),
30
        CFG_SEC("playout", cfg_playout, CFGF_NONE),
31
        CFG_SEC("log", cfg_log, CFGF_NONE),
32
        CFG_END()
33
};
34

    
35
static void usage(char *argv[]) {
36
        fprintf(stderr, "Peer version %s.\nUsage: %s [-d debuglevel(0-*3*-5)] [-p UDP port]"
37
                        " configfile.cfg\n", version,argv[0]);
38
        exit(-1);
39
}
40

    
41

    
42
int main(int argc, char *argv[]) {
43
        int log_level = -1;
44
        int port = 0;
45
        int i;
46

    
47
        if (argc == 2 && (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help")
48
                                == 0)) usage(argv);
49
        if (!(argc == 2 || argc == 4 || argc == 6)) usage(argv);
50

    
51
        for (i = 1; i < (argc - 1); i += 2) {
52
                if (strcmp(argv[i], "-d") == 0) {
53
                        log_level = atoi(argv[i + 1]);
54
                        if (log_level < 0 || log_level > 5) usage(argv);
55
                        }
56
                else if (strcmp(argv[i], "-p") == 0) {
57
                        port = atoi(argv[i + 1]);
58
                        if (port < 1023 || port > 65535) usage(argv);
59
                }
60
                else usage(argv);
61
        }
62

    
63
        // Read the config file using libconfuse
64
        cfg_t *main_config = cfg_init(cfg_main, CFGF_NONE);
65
        mon_validate_cfg(main_config);
66
        if(cfg_parse(main_config, argv[argc - 1]) == CFG_PARSE_ERROR)
67
                fatal("Unable to parse config file %s", argv[1]);
68

    
69
        cfg_t *log_config = cfg_getsec(main_config, "log");
70

    
71
        // Initialize logging
72
        if (log_level == -1) log_level = cfg_getint(log_config, "level");
73
        napaInitLog(log_level, cfg_getstr(log_config, "logfile"),
74
                        cfg_getstr(log_config, "filemode"));
75
        napaInit(event_base_new());
76

    
77
        // Check config file
78
        int fd = open(argv[argc - 1], 0);
79
        if (fd < 0) usage(argv);
80
        close(fd);
81

    
82
        info("This is peer version %s initializing...", version);
83

    
84
        cfg_t *network_cfg = cfg_getsec(main_config, "network");
85
        ml_init(network_cfg, port);
86

    
87
        cfg_t *rep_cfg = cfg_getsec(main_config, "repository");
88
        rep_init(rep_cfg);
89

    
90
        cfg_t *mon_cfg = cfg_getsec(main_config, "measurements");
91
        mon_init(mon_cfg, channel);
92

    
93
        cfg_t *som_cfg =  cfg_getsec(main_config, "som");
94
        som_init(som_cfg);
95

    
96
        cfg_t *src_cfg = cfg_getsec(main_config, "source");
97
        src_init(src_cfg);
98

    
99
        cfg_t *playout_cfg = cfg_getsec(main_config, "playout");
100
        playout_init(playout_cfg);
101

    
102
        event_base_dispatch(eventbase);
103
        return 0;
104
}
105

    
106
/**
107
    Callback for the "Publish MeasurementRecord" utility function.
108
        */
109
static void publishMeasurementRecord_cb(HANDLE h, HANDLE id, void *arg, int result) {
110
            if (result) 
111
                        warn("Repository publish for \"%s\" returned with result code %d",
112
                                        (char *)arg, result);
113
}
114

    
115
/**
116
    Utility function to publish a MeasurementRecord 
117
*/
118
void publishMeasurementRecord(MeasurementRecord *mr, char *msg) {
119
        repPublish(repository, publishMeasurementRecord_cb, msg, mr);
120
}
121

    
122
/** Utility function to print a string array */
123
const char *print_list(char **list, int n, bool should_free) {
124
        static char buffer[4096];
125
        strcpy(buffer, "[ ");
126
        int i;
127
        for (i = 0; i != n; i++) {
128
                if (i) strcat(buffer, ", ");
129
                strcat(buffer, list[i]);
130
                if (should_free)
131
                        free(list[i]);
132
        }
133
        strcat(buffer, " ]");
134
        if (should_free) free(list);
135
        return buffer;
136
}
137

    
138