feedbackReceiver.c 2.18 KB
Newer Older
1
2
#include <string.h>
#include <unistd.h>
3
4
5
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/poll.h>
6
#include "../../defines.h"
7
8
#include "../../util/common.h"
#include "../../util/dbg.h"
9
#include "../clock.h"
10
#include "../socket.h"
Andreas Schmidt's avatar
Andreas Schmidt committed
11
#include "feedbackReceiver.h"
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
void handle_feedback(PrrtSocket *prrtSocket, const size_t length)
{
    char bufin[MAX_PAYLOAD_LENGTH];
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

    struct pollfd fds;
    int timeout_msecs = 1000;
    fds.fd = prrtSocket->feedbackSocketFd;
    fds.events = POLLIN;

    n = poll(&fds, 1, timeout_msecs);
    check(n >= 0, "Select failed.")
    if(n == 0) {
        return;
    }
    prrtTimestamp_t receiveTime = PrrtClock_get_current_time_us();

    n = recvfrom(prrtSocket->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
    check(n >= 0, "Receiving feedback failed.");

    PrrtPacket *prrtPacket = calloc(1, sizeof(PrrtPacket));
    check_mem(prrtPacket);
    PrrtPacket_decode(bufin, (uint16_t) n, prrtPacket);

    //PrrtPacketFeedbackPayload* payload = prrtPacket->payload;
    //struct in_addr a;
    //a.s_addr = payload->receiverAddress;
    //debug("Receiver Address: %s", inet_ntoa(a));

    prrtTimestamp_t forwardTripTimestamp = ((PrrtPacketFeedbackPayload *) prrtPacket->payload)->forwardTripTimestamp_us;

    PrrtChannelStateInformation_update_rtt(prrtSocket->csi, (prrtTimedelta_t) (receiveTime - forwardTripTimestamp));
    PrrtChannelStateInformation_print(prrtSocket->csi);

    error:
    if(prrtPacket != NULL) { PrrtPacket_destroy(prrtPacket); }
}

void *receive_feedback_loop(void *ptr)
{
55
    PrrtSocket *sock_ptr = ptr;
56

57
    check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
58
    while(sock_ptr->closing == false) {
59
        check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
60
        handle_feedback(sock_ptr, MAX_PAYLOAD_LENGTH);
61
        usleep(1);
62
        check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
63
    }
64
    check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
65
66

    return NULL;
67
68

    error:
69
70
    PERROR("Feedback reception failed.");
    return NULL;
71
}