receiver.c 4.35 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
        {"hardwaretimestamping", 777, "INTERFACE", 0, "Enable hardware timestamping and bind to interface"},
        { 0 }
};

Andreas Schmidt's avatar
Andreas Schmidt committed
22
typedef struct arguments
23 24 25 26
{
    uint16_t port;
    uint16_t rounds;
    char* outfile;
27
    bool thread_pinning;
28
    char* hardwarestamping_interface;
Andreas Schmidt's avatar
Andreas Schmidt committed
29
} arguments_t;
30 31 32 33 34

static char args_doc[] = "";

static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
Andreas Schmidt's avatar
Andreas Schmidt committed
35
    arguments_t *arguments = state->input;
36 37 38 39 40 41 42 43 44 45 46 47 48

    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

100
    s = PrrtSocket_create(1500, 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) == 0, "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);
rna's avatar
rna committed
132 133 134 135
        prrtTimestamp_t current_time = PrrtClock_get_current_time_us();
        prrtTimestamp_t prrt_time = PrrtClock_get_prrt_time_us(&s->clock);
        printf("C: %d\nP: %d\nD: \n%d\n\n", current_time, prrt_time, prrt_time - current_time);

Stefan Reif's avatar
Stefan Reif committed
136
        debug(DEBUG_RECEIVER, "[B (n: %3d, i: %3d)] %s", n, i, buffer);
137
        i++;
138 139
    }

140 141 142
    XlapTimestampTableDumpHeader(out_desc);
    XlapTimestampTableDump(out_desc, ts_data_packet, tstable_data);
    XlapTimestampTableDump(out_desc, ts_redundancy_packet, tstable_redundancy);
143

Andreas Schmidt's avatar
Andreas Schmidt committed
144
    if (file_output) {
145 146
        fclose(out_desc);
    }
147

148 149
    PrrtSocket_close(s);
    free(s);
Andreas Schmidt's avatar
Andreas Schmidt committed
150 151
    free(tstable_data);
    free(tstable_redundancy);
152 153 154

    pthread_exit(NULL);

155
    return 0;
156 157

    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
158
    return -1;
159
}