receiver.c 4.09 KB
Newer Older
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <signal.h>
4
#include <argp.h>
5
#include "util/dbg.h"
6
#include "proto/socket.h"
7

8
PrrtSocket *s = NULL;
9
static volatile int keepRunning = true;
10

11
static char doc[] = "PRRT Receiver";
12
13
14
15
16

static struct argp_option options[] = {
        {"port",     'p', "PORT",      0,  "Target Port" },
        {"rounds",   'r', "ROUNDS",      0,  "Rounds" },
        {"output",   'o', "FILE", 0, "Output to FILE instead of standard output" },
17
        {"threadpinning", 'T', 0, 0, "Enable thread pinning"},
18
19
20
21
22
23
24
25
26
        {"hardwaretimestamping", 777, "INTERFACE", 0, "Enable hardware timestamping and bind to interface"},
        { 0 }
};

static struct arguments
{
    uint16_t port;
    uint16_t rounds;
    char* outfile;
27
    bool thread_pinning;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    char* hardwarestamping_interface;
};

static char args_doc[] = "";

static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
    struct arguments *arguments = state->input;

    char* pEnd;
    switch (key)
    {
        case 'p':
            arguments->port = (uint16_t) strtol(arg, &pEnd, 10);
            break;
        case 'r':
            arguments->rounds = (uint16_t) strtol(arg, &pEnd, 10);
            break;
        case 'o':
            arguments->outfile = arg;
            break;
49
50
51
        case 'T':
            arguments->thread_pinning = true;
            break;
52
53
54
55
56
57
58
59
60
61
62
63
64
        case 777:
            arguments->hardwarestamping_interface = arg;
            break;

        case ARGP_KEY_END:
            break;

        default:
            return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

65
void intHandler(int dummy) {
66
    keepRunning = false;
67
    PrrtSocket_interrupt(s);
68
69
}

70
int main(int argc, char **argv) {
71
72
    signal(SIGINT, intHandler);

73
74
75
76
77
78
    struct argp argp = { options, parse_opt, args_doc, doc };

    struct arguments arguments = {
            .port = 5000,
            .rounds = 127,
            .outfile = "-",
79
            .thread_pinning = false,
80
81
82
83
            .hardwarestamping_interface = "-"
    };
    argp_parse (&argp, argc, argv, 0, 0, &arguments);

84
    bool file_output = false;
Andreas Schmidt's avatar
Andreas Schmidt committed
85
    FILE *out_desc = stdout;
86

87
    if (strcmp(arguments.outfile, "-") != 0) {
88
        file_output = true;
89
        out_desc = fopen(arguments.outfile, "w");
90
91
    }

92
    uint32_t rounds = arguments.rounds;
93
94
95
96
97
98
    #ifdef XLAP
    if (rounds >= TS_ROWS) {
        printf("Receiver: Too many rounds for use with XLAP (max: %d).\n", TS_ROWS-1);
        return -1;
    }
    #endif
99

Andreas Schmidt's avatar
Andreas Schmidt committed
100
    s = PrrtSocket_create(HALF_TIMESTAMP-1);
101
102
103
104
105
    check(s != NULL, "Could not create socket.");

    if(strcmp(arguments.hardwarestamping_interface, "-") != 0) {
        PrrtSocket_enable_hardware_timestamping(s, arguments.hardwarestamping_interface);
    }
106
107
108
109
110

    if(arguments.thread_pinning) {
        PrrtSocket_enable_thread_pinning(s);
    }

111
    check(PrrtSocket_bind(s, "0.0.0.0", arguments.port), "bind failed");
112

Andreas Schmidt's avatar
Andreas Schmidt committed
113
114
115
116
    XlapTimestampTable *tstable_data = malloc(sizeof(XlapTimestampTable));
    XlapTimestampTable *tstable_redundancy = malloc(sizeof(XlapTimestampTable));
    check(tstable_data != NULL, "malloc failed");
    check(tstable_redundancy != NULL, "malloc failed");
117
118
    XlapTimestampTableInstall(s, ts_data_packet, tstable_data);
    XlapTimestampTableInstall(s, ts_redundancy_packet, tstable_redundancy);
Stefan Reif's avatar
Stefan Reif committed
119

120
    uint32_t i = 0;
121
    struct sockaddr_in addr;
Andreas Schmidt's avatar
Andreas Schmidt committed
122
    while (i < rounds && keepRunning) {
Stefan Reif's avatar
Stefan Reif committed
123
        char buffer[MAX_PAYLOAD_LENGTH + 1];
Stefan Reif's avatar
Stefan Reif committed
124
        debug(DEBUG_RECEIVER, "About to receive.");
125
        int n = PrrtSocket_recv(s, buffer, (struct sockaddr *) &addr);
Andreas Schmidt's avatar
Andreas Schmidt committed
126
        if (n < 0) {
127
128
            continue;
        }
129
        buffer[n] = '\0';
Stefan Reif's avatar
Stefan Reif committed
130
131
        unsigned int seqno;
        sscanf(buffer, "%u", &seqno);
Stefan Reif's avatar
Stefan Reif committed
132
        debug(DEBUG_RECEIVER, "[B (n: %3d, i: %3d)] %s", n, i, buffer);
133
        i++;
134
135
    }

136
137
138
    XlapTimestampTableDumpHeader(out_desc);
    XlapTimestampTableDump(out_desc, ts_data_packet, tstable_data);
    XlapTimestampTableDump(out_desc, ts_redundancy_packet, tstable_redundancy);
139

Andreas Schmidt's avatar
Andreas Schmidt committed
140
    if (file_output) {
141
142
        fclose(out_desc);
    }
143

144
145
    PrrtSocket_close(s);
    free(s);
Andreas Schmidt's avatar
Andreas Schmidt committed
146
147
    free(tstable_data);
    free(tstable_redundancy);
148
149
150

    pthread_exit(NULL);

151
    return 0;
152
153

    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
154
    return -1;
155
}