time-receiver.c 3.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <argp.h>
#include "util/dbg.h"
#include "proto/socket.h"

PrrtSocket *s = NULL;
static volatile int keepRunning = true;

static char doc[] = "PRRT Receiver";

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" },
        {"threadpinning", 'T', 0, 0, "Enable thread pinning"},
        { 0 }
};

static struct arguments
{
    uint16_t port;
    uint16_t rounds;
    char* outfile;
    bool thread_pinning;
};

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;
        case 'T':
            arguments->thread_pinning = true;
            break;

        case ARGP_KEY_END:
            break;

        default:
            return ARGP_ERR_UNKNOWN;
    }
    return 0;
}

void intHandler(int dummy) {
    keepRunning = false;
    PrrtSocket_interrupt(s);
}

static long long timedelta(struct timespec *t1, struct timespec *t2)
{
	long long delta = t1->tv_sec - t2->tv_sec;
	delta *= 1000000000;
	delta += t1->tv_nsec - t2->tv_nsec;
	return delta;
}

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

    struct argp argp = { options, parse_opt, args_doc, doc };

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

    bool file_output = false;
    FILE *out_desc = stdout;

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

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

    s = PrrtSocket_create(false, HALF_TIMESTAMP-1);
    check(s != NULL, "Could not create socket.");

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

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

    XlapTimestampTable *tstable_data = malloc(sizeof(XlapTimestampTable));
    XlapTimestampTable *tstable_redundancy = malloc(sizeof(XlapTimestampTable));
    check(tstable_data != NULL, "malloc failed");
    check(tstable_redundancy != NULL, "malloc failed");
    XlapTimestampTableInstall(s, ts_data_packet, tstable_data);
    XlapTimestampTableInstall(s, ts_redundancy_packet, tstable_redundancy);

    uint32_t i = 0;
    while (i < rounds && keepRunning) {
        char buffer[MAX_PAYLOAD_LENGTH + 1];
        debug(DEBUG_RECEIVER, "About to receive.");
Andreas Schmidt's avatar
Andreas Schmidt committed
122
        int n = PrrtSocket_receive_asap_wait(s, buffer);
123 124 125 126 127 128 129 130
        if (n < 0) {
            continue;
        }
		struct timespec now, old;
		clock_gettime(CLOCK_REALTIME, &now);

		memcpy(&old, buffer, sizeof(struct timespec));

131
		fprintf(stderr, "%u: %lli\n", i, timedelta(&old, &now));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

        i++;
    }

    XlapTimestampTableDumpHeader(out_desc);
    XlapTimestampTableDump(out_desc, ts_data_packet, tstable_data);
    XlapTimestampTableDump(out_desc, ts_redundancy_packet, tstable_redundancy);

    if (file_output) {
        fclose(out_desc);
    }

    PrrtSocket_close(s);
    free(s);
    free(tstable_data);
    free(tstable_redundancy);

    pthread_exit(NULL);

    return 0;

    error:
    return -1;
}