data_transmitter.c 3.42 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
68
69
                block = NULL;

                pthread_mutex_unlock(&sock_ptr->closingMutex);
                pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex);
                return NULL;
            }
            pthread_mutex_unlock(&sock_ptr->closingMutex);
70
            pthread_cond_wait(&sock_ptr->outQueueFilledCv, &sock_ptr->outQueueFilledMutex);
71
        }
72

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

Andreas Schmidt's avatar
Andreas Schmidt committed
77
            PrrtBlock_create(block, cpar, sock_ptr->sequenceNumberRedundancy);
78
79
        }

80
        PrrtPacket *packet = List_shift(sock_ptr->outQueue);
81

82
        PrrtBlock_insert_data_packet(block, packet);
83

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

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

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

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

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

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