receiver.c 8.38 KB
Newer Older
1
#include <malloc.h>
2
#include "../xlap/xlap.h"
3
#include "../util/common.h"
4
#include "../util/dbg.h"
5
#include "stores/inFlightPacketStore.h"
6
7
#include "receiver.h"

8
PrrtReceiver *PrrtReceiver_create(const char *host, uint16_t port) {
9
10
    PrrtReceiver *recv = calloc(1, sizeof(PrrtReceiver));
    check_mem(recv);
Andreas Schmidt's avatar
Andreas Schmidt committed
11
    recv->host_name = strdup(host);
12
    check_mem(recv->host_name);
13
14
    recv->port = port;

15
16
17
    PrrtRateSample *rateSample = calloc(1, sizeof(PrrtRateSample));
    check_mem(rateSample);
    recv->rateSample = rateSample;
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
    PrrtPacketTracking *packetTracking = calloc(1, sizeof(PrrtPacketTracking));
    check_mem(packetTracking);
    recv->packetTracking = packetTracking;

    recv->csi = PrrtChannelStateInformation_create();

    recv->dataPacketStates = PrrtInFlightPacketStore_create();
    recv->redundancyPacketStates = PrrtInFlightPacketStore_create();


    struct addrinfo *info;
    struct addrinfo hints;
    char portstr[sizeof(port) * 8 + 1];
    snprintf(portstr, sizeof(portstr), "%u", (unsigned int) port);
    memset(&hints, 0x0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    check(0 == getaddrinfo(host, portstr, &hints, &info), "getaddrinfo");

    recv->ai = info;

    check(pthread_mutex_init(&recv->lock, NULL) == 0, "lock init failed.");
    check(pthread_cond_init(&recv->pipeNotFullCv, NULL) == 0, "pipeNotFullCv init failed.");
    check(pthread_cond_init(&recv->recordNotFoundCv, NULL) == 0, "recordNotFound init failed.");
44

45
46
47
    return recv;

    error:
48
    if (recv != NULL) {
49
50
51
        free((void *) recv->host_name);
        free(recv);
    }
52
    //PERROR("Memory issue.%s","");
Andreas Schmidt's avatar
Andreas Schmidt committed
53
    return NULL;
54
55
}

56
bool PrrtReceiver_destroy(PrrtReceiver *receiver) {
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
    if (receiver->csi != NULL) {
        PrrtChannelStateInformation_destroy(receiver->csi);
    }

    if (receiver->dataPacketStates != NULL) {
        PrrtInFlightPacketStore_destroy(receiver->dataPacketStates);
    }

    if (receiver->redundancyPacketStates != NULL) {
        PrrtInFlightPacketStore_destroy(receiver->redundancyPacketStates);
    }

    if (receiver->packetTracking != NULL) {
        free(receiver->packetTracking);
    }

    if (receiver->rateSample != NULL) {
        free(receiver->rateSample);
    }


    freeaddrinfo(receiver->ai);
    free((void *) receiver->host_name);
    check(pthread_mutex_destroy(&receiver->lock) == 0, "lock destroy failed.");
    check(pthread_cond_destroy(&receiver->pipeNotFullCv) == 0, "pipeNotFullCv destroy failed.");
    check(pthread_cond_destroy(&receiver->recordNotFoundCv) == 0, "recordNotFoundCv destroy failed.");
    free(receiver);
    return true;

    error:
    PERROR("PrrtReceiver_destroy failed.")
    return false;
}

void PrrtReceiver_updateRateSample(PrrtRateSample *rateSample, PrrtPacket *packet, prrtTimestamp_t receiveTime,
                                   PrrtPacketTracking *packetTracking) {
    if (packet->delivered_time == 0)
        return;

    packetTracking->delivered += packet->payloadLength;
    packetTracking->delivered_time = receiveTime;

    if (packet->delivered > rateSample->prior_delivered) {
        rateSample->prior_delivered = packet->delivered;
        rateSample->prior_time = packet->delivered_time;
        rateSample->is_app_limited = packet->is_app_limited;
        rateSample->send_elapsed = packet->sent_time - packet->first_sent_time;
        rateSample->ack_elapsed = packetTracking->delivered_time - packet->delivered_time;
        packetTracking->first_sent_time = packet->sent_time;
    }
    packet->delivered_time = 0;
}

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
void PrrtReceiver_on_application_write(PrrtReceiver* receiver) {
    check(pthread_mutex_lock(&receiver->lock) == 0, "Lock failed.");
    PrrtPacketTracking *tracking = receiver->packetTracking;
    if(PrrtInFlightPacketStore_get_queue_size(receiver->dataPacketStates) +
       PrrtInFlightPacketStore_get_queue_size(receiver->redundancyPacketStates) == 0) {
        tracking->app_limited = (tracking->delivered + tracking->pipe) ? : 1;
    }
    check(pthread_mutex_unlock(&receiver->lock) == 0, "Unlock failed.");
    return;

    error:
    PERROR("Mutex error.%s", "");

}

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
void PrrtReceiver_generateRateSample(PrrtRateSample *rateSample, PrrtPacketTracking *packetTracking) {
    /* Clear app-limited field */
    if (packetTracking->app_limited && packetTracking->delivered > packetTracking->app_limited)
        packetTracking->app_limited = 0;

    if (rateSample->prior_time == 0) {
        //printf("Prior Time is 0, Cancelling Rate sample calculation\n");
        return;
    }

    rateSample->interval = MAX(rateSample->send_elapsed, rateSample->ack_elapsed);

    rateSample->delivered = packetTracking->delivered - rateSample->prior_delivered;

    if (rateSample->interval != 0) {
        // delivered: bytes; interval: us; convert to bps
        rateSample->delivery_rate =
                rateSample->delivered * 1000 * 1000 * 8 / rateSample->interval;
    }
    debug(DEBUG_FEEDBACK, "RS interval: %u, RS delivered: %u, RS delivery_rate: %u", rateSample->interval,
          rateSample->delivered, rateSample->delivery_rate);

    /* Normally we expect interval >= MinRTT.
     * Note that rate may still be over-estimated when a spuriously
     * retransmitted skb was first (s)acked because "interval"
     * is under-estimated (up to an RTT). However, continuously
     * measuring the delivery rate during loss recovery is crucial
     * for connections suffer heavy or prolonged losses.

    if (packetStore->rateSample->interval <  MinRTT(tp))
        packetStore->rateSample->interval = -1;
    return false  // no reliable sample
    */
}

void PrrtReceiver_updateAndGenerateRateSample(PrrtReceiver *recv, prrtSequenceNumber_t seqnum, uint8_t packetType,
                                              prrtTimestamp_t receiveTime) {
    PrrtInFlightPacketStore *packetStore = NULL;
    if (packetType == PACKET_TYPE_DATA) {
        packetStore = recv->dataPacketStates;
    } else if (packetType == PACKET_TYPE_REDUNDANCY) {
        packetStore = recv->redundancyPacketStates;
    } else return;

    check(pthread_mutex_lock(&recv->lock) == 0, "Lock failed.");
    PrrtPacket *packet = PrrtInFlightPacketStore_get_packet(packetStore, seqnum);
    while (packet == NULL) {
        pthread_cond_wait(&recv->recordNotFoundCv, &recv->lock);
        packet = PrrtInFlightPacketStore_get_packet(packetStore, seqnum);
    }
    PrrtReceiver_updateRateSample(recv->rateSample, packet, receiveTime, recv->packetTracking);
    PrrtReceiver_generateRateSample(recv->rateSample, recv->packetTracking);
177
    recv->packetTracking->pipe -= packet->payloadLength;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    PrrtInFlightPacketStore_remove_outstanding_packet(packetStore, seqnum);
    check(pthread_mutex_unlock(&recv->lock) == 0, "Unlock failed.");
    check(pthread_cond_broadcast(&recv->pipeNotFullCv) == 0, "Signal failed.");

    return;

    error:
    PERROR("Mutex error.%s", "");
}

void PrrtReceiver_add_outstanding_packet_state(PrrtReceiver *recv, PrrtPacket *packet, prrtTimestamp_t sentTime) {
    PrrtInFlightPacketStore *packetStore = NULL;
    if (PrrtPacket_type(packet) == PACKET_TYPE_DATA) {
        packetStore = recv->dataPacketStates;
    } else if (PrrtPacket_type(packet) == PACKET_TYPE_REDUNDANCY) {
        packetStore = recv->redundancyPacketStates;
    } else return;
    //printf("Adding Packet #%u to %u\n", packet->sequenceNumber, PrrtPacket_type(packet));
    check(pthread_mutex_lock(&recv->lock) == 0, "Lock failed.");

    if (PrrtInFlightPacketStore_get_queue_size(recv->dataPacketStates) +
        PrrtInFlightPacketStore_get_queue_size(recv->redundancyPacketStates) == 0) {
        recv->packetTracking->first_sent_time = sentTime;
        recv->packetTracking->delivered_time = sentTime;
    }
203
    recv->packetTracking->pipe += packet->payloadLength;
204
205
206
207
    packet->first_sent_time = recv->packetTracking->first_sent_time;
    packet->sent_time = sentTime;
    packet->delivered_time = recv->packetTracking->delivered_time;
    packet->delivered = recv->packetTracking->delivered;
208
    packet->is_app_limited = (recv->packetTracking->app_limited != 0);
209
210
211
212
213
214
215
216
217
218
219
220
221
222

    PrrtInFlightPacketStore_add_outstanding_packet(packetStore, packet);
    check(pthread_mutex_unlock(&recv->lock) == 0, "Unlock failed.");
    check(pthread_cond_broadcast(&recv->recordNotFoundCv) == 0, "Signal failed.");

    return;

    error:
    PERROR("Lock error.%s", "");
}

void PrrtReceiver_interrupt(PrrtReceiver *recv) {
    pthread_cond_broadcast(&recv->recordNotFoundCv);
    pthread_cond_broadcast(&recv->pipeNotFullCv);
223
}