data_transmitter.c 3.45 KB
Newer Older
Andreas Schmidt's avatar
Andreas Schmidt committed
1
2
#include <stdio.h>
#include <netdb.h>
3
#include <unistd.h>
4
#include <string.h>
Andreas Schmidt's avatar
Andreas Schmidt committed
5
6
7
#include <src/defines.h>
#include <src/prrt/socket.h>
#include <src/prrt/block.h>
8
9
#include <src/util/dbg.h>
#include <src/util/common.h>
10
11
12
#include "data_transmitter.h"


13
int send_packet(PrrtSocket *sock_ptr, PrrtPacket *packet) {
14
    uint8_t buf[MAX_PAYLOAD_LENGTH];
15
16
17
18
19
    memset(buf, 0, sizeof(buf));
    uint32_t length = PrrtPacket_size(packet);

    int encodeResult = PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, packet);
    check(encodeResult >= 0, "Buffer too small.");
20
21

    // SENDING TO ALL RECEIVERS
22
23
    LIST_FOREACH(sock_ptr->receivers, first, next, cur) {
        PrrtReceiver* recv = cur->value;
24
25
26
27
28
        struct hostent *hp;

        struct sockaddr_in targetaddr;
        memset((char *) &targetaddr, 0, sizeof(targetaddr));
        targetaddr.sin_family = AF_INET;
29
        targetaddr.sin_port = htons(recv->port);
30

31
        hp = gethostbyname(recv->host_name);
32
33
        memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);

34
35
        ssize_t sendtoRes = sendto(sock_ptr->dataSocketFd, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr));
        check(sendtoRes >= 0, "Sendto failed.")
36
    }
37

38
39
40
    PrrtPacket_destroy(packet);

    return EXIT_SUCCESS;
41

42
    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
43
        PERROR("Something is wrong%s.", "")
44
        return EXIT_FAILURE;
45
46
}

47
void * send_data_loop(void *ptr) {
48
49
50
    PrrtSocket *sock_ptr = ptr;

    PrrtBlock *block = NULL;
Andreas Schmidt's avatar
Andreas Schmidt committed
51
    PrrtCodingParams *cpar = calloc(1, sizeof(PrrtCodingParams));
52
53
54
    PrrtCodingParams_init(cpar);

    while (1) {
55
56
        pthread_mutex_lock(&sock_ptr->outQueueFilledMutex);
        while (List_count(sock_ptr->outQueue) == 0) {
57
58
59
            pthread_mutex_lock(&sock_ptr->closingMutex);
            if(sock_ptr->closing) {
                free(cpar);
Andreas Schmidt's avatar
Andreas Schmidt committed
60
61
62
                if(block != NULL) {
                    PrrtBlock_destroy(block);
                }
63
64
65
66
67
                pthread_mutex_unlock(&sock_ptr->closingMutex);
                pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex);
                return NULL;
            }
            pthread_mutex_unlock(&sock_ptr->closingMutex);
68
            pthread_cond_wait(&sock_ptr->outQueueFilledCv, &sock_ptr->outQueueFilledMutex);
69
        }
70

71
        if(block == NULL) {
72
73
74
            block = calloc(1, sizeof(PrrtBlock));
            check_mem(block);

75
            PrrtBlock_create(block, cpar, sock_ptr->sequenceNumberSource);
76
77
        }

78
        PrrtPacket *packet = List_shift(sock_ptr->outQueue);
79
        packet->seqno = sock_ptr->sequenceNumberSource++;
80

81
        PrrtBlock_insert_data_packet(block, packet);
82

83
        if (PrrtBlock_encode_ready(block)) {
84
            int j = 0;
85
            PrrtBlock_encode(block, &sock_ptr->sequenceNumberRedundancy);
86

87
            uint32_t redundancyBlocks = List_count(block->redundancyPackets);
88
            for (j = 0; j < redundancyBlocks; j++) {
89
                PrrtPacket *red_pkt = PrrtBlock_get_first_red_data(block);
90
                send_packet(sock_ptr, red_pkt);
91
            }
92

93
            uint32_t dataBlocks = List_count(block->dataPackets);
94
            for (j = 0; j < dataBlocks; j++) {
95
96
97
                PrrtPacket *data_pkt = PrrtBlock_get_first_data(block);
                send_packet(sock_ptr, data_pkt);
            }
98

Andreas Schmidt's avatar
Andreas Schmidt committed
99
            PrrtBlock_destroy(block);
100
            block = NULL;
101
        }
102

103
        pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex);
104
        usleep(1);
105
    }
106
107

    error:
Andreas Schmidt's avatar
Andreas Schmidt committed
108
        PERROR("Failed to send packet %s", "");
109
        return NULL;
110
}