socket.c 10.9 KB
Newer Older
1
2
3
4
5
6
#include <arpa/inet.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
7
#include <sys/poll.h>
8
9
10
11
12
13
14
#include "../defines.h"
#include "packet.h"
#include "../util/dbg.h"
#include "../util/common.h"
#include "processes/feedback_receiver.h"
#include "processes/data_transmitter.h"
#include "processes/data_receiver.h"
15
#include "socket.h"
16
#include "block.h"
17
#include "receiver.h"
18
#include "clock.h"
19

20
21
PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
    PrrtSocket *sock_ptr = (PrrtSocket*) calloc(1, sizeof(PrrtSocket));
22
    check_mem(sock_ptr);
23

24
    sock_ptr->is_sender = is_sender;
25

26
27
    sock_ptr->sequenceNumberSource = 1;
    sock_ptr->sequenceNumberRedundancy = 1;
28

29
30
    sock_ptr->dataStore = NULL;

31
32
    check(sock_ptr->dataSocketFd = socket(AF_INET, SOCK_DGRAM, 0), "Cannot create data socket.")
    check(sock_ptr->feedbackSocketFd = socket(AF_INET, SOCK_DGRAM, 0), "Cannot create feedback socket.");
33

34
    pthread_mutexattr_t attr;
35
36
37
    check(pthread_mutexattr_init(&attr) == 0, "Mutex attr init failed.");
    check(pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0, "Setting type failed.");
    check(pthread_mutex_init(&sock_ptr->closingMutex, &attr) == 0, "Mutex init failed.");
38

39
40
41
42
43
44
45
46
47
48
    // Bind Data Socket
    struct sockaddr_in address;
    memset((char *) &address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port = htons(port);

    check(bind(sock_ptr->dataSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
          "Cannot bind data socket.");

49
    if (is_sender) {
50
51
        // Bind Feedback Socket
        struct sockaddr_in address;
52
        memset((char *) &address, 0, sizeof(address));
53
54
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = htonl(INADDR_ANY);
55
56
        check(port <= 65534, "Port %d cannot be bound to.", port);
        address.sin_port = htons((uint16_t) (port + 1));
57

58
59
        check(bind(sock_ptr->feedbackSocketFd, (struct sockaddr *) &address, sizeof(address)) == EXIT_SUCCESS,
              "Cannot bind feedback socket.");
60

61
62
        check(pthread_mutex_init(&sock_ptr->outQueueFilledMutex, NULL) == 0, "Mutex init failed.");
        check(pthread_cond_init(&sock_ptr->outQueueFilledCv, NULL) == 0, "Cond init failed.");
63
        sock_ptr->outQueue = List_create();
64

65
66
67
68
        check(pthread_create(&sock_ptr->receiveFeedbackThread, NULL, receive_feedback_loop, (void *) sock_ptr) ==
              EXIT_SUCCESS, "Cannot create receive feedback thread.");
        check(pthread_create(&sock_ptr->sendThread, NULL, send_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
              "Cannot create send thread.");
69
70

        sock_ptr->receivers = List_create();
71
    } else {
Andreas Schmidt's avatar
Andreas Schmidt committed
72
        sock_ptr->forwardPacketTable = calloc(1, sizeof(PrrtForwardPacketTable));
73
        check_mem(sock_ptr->forwardPacketTable)
74
        check(PrrtForwardPacketTable_create(sock_ptr->forwardPacketTable) == 0, "Creating table failed.");
75

76
77
        check(pthread_mutex_init(&sock_ptr->inQueueFilledMutex, NULL) == 0, "Mutex init failed.");
        check(pthread_cond_init(&sock_ptr->inQueueFilledCv, NULL) == 0, "Cond init failed.");
78
79
        sock_ptr->inQueue = List_create();

80
81
        check(pthread_create(&sock_ptr->receiveDataThread, NULL, receive_data_loop, (void *) sock_ptr) == EXIT_SUCCESS,
              "Cannot create data receiving thread.");
82
83
    }

84
    return sock_ptr;
85
86

    error:
87
        PrrtSocket_close(sock_ptr);
88
        return NULL;
89
90
}

91
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
92
    PrrtReceiver *recv = PrrtReceiver_create(host, port);
93
    List_push(sock_ptr->receivers, recv);
94
95
96
    return 0;
}

97
int PrrtSocket_send(PrrtSocket *sock_ptr, const uint8_t *data, const size_t data_len) {
98
    check(sock_ptr->is_sender, "Cannot send on receiver socket.")
99
    check(pthread_mutex_lock(&sock_ptr->outQueueFilledMutex) == 0, "Lock failed.");
100

101
    PrrtPacket *packet = PrrtPacket_create_data_packet(5, data, (uint32_t) data_len, 0);
102

103
    List_push(sock_ptr->outQueue, packet);
104
105
    check(pthread_cond_signal(&sock_ptr->outQueueFilledCv) == 0, "Signal failed.");
    check(pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex) == 0, "Unlock failed");
106

107
    return 0;
108
    error:
109
        PERROR("There was a failure while sending from socket.%s", "");
110
        return -1;
111
112
}

113
int32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void *buf_ptr) {
114
    check(sock_ptr->is_sender == false, "Cannot receive on sender socket.")
115
    while (1) {
116
        check(pthread_mutex_lock(&sock_ptr->inQueueFilledMutex) == 0, "Lock failed.");
117
        while (List_count(sock_ptr->inQueue) == 0) {
118
            check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
119
            if (sock_ptr->closing) {
120
121
                check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
                check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed.");
122
123
                return -1;
            }
124
125
            check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
            check(pthread_cond_wait(&sock_ptr->inQueueFilledCv, &sock_ptr->inQueueFilledMutex) == 0, "Wait failed.");
126
        }
127
        PrrtPacket *packet = List_shift(sock_ptr->inQueue);
128

129
        uint32_t len = (uint32_t) (packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
130
        PrrtPacket_copy_payload_to_buffer(buf_ptr, packet, PRRT_PACKET_DATA_HEADER_SIZE);
131
132
        PrrtPacket_destroy(packet);

133
        check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed.");
134
        return len;
135
    }
136
    error:
137
        PERROR("There was a failure while receiving from socket.%s", "");
138
        return -1;
139
140
}

141
int PrrtSocket_interrupt(PrrtSocket *sock_ptr) {
142
    check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
143
    sock_ptr->closing = true;
144
    check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
145

146
    void **res = NULL;
147
    if (sock_ptr->sendThread != 0) {
148
149
150
        check(pthread_mutex_lock(&sock_ptr->outQueueFilledMutex) == 0, "Lock failed.");
        check(pthread_cond_broadcast(&sock_ptr->outQueueFilledCv) == 0, "Broadcast failed.");
        check(pthread_mutex_unlock(&sock_ptr->outQueueFilledMutex) == 0, "Unlock failed.");
151

152
        check(pthread_join(sock_ptr->sendThread, res) == 0, "Join failed.");
153
154
155
        sock_ptr->sendThread = 0;
    }

156
    if (sock_ptr->receiveDataThread != 0) {
157
158
159
        check(pthread_mutex_lock(&sock_ptr->inQueueFilledMutex) == 0, "Lock failed.");
        check(pthread_cond_broadcast(&sock_ptr->inQueueFilledCv) == 0, "Broadcast failed");
        check(pthread_mutex_unlock(&sock_ptr->inQueueFilledMutex) == 0, "Unlock failed");
160

161
162
        check(pthread_cancel(sock_ptr->receiveDataThread) == 0, "Cancel failed.");
        check(pthread_join(sock_ptr->receiveDataThread, res) == 0, "Join failed.");
163
164
165
166

        sock_ptr->receiveDataThread = 0;
    }

167
    if (sock_ptr->receiveFeedbackThread != 0) {
168
169
        check(pthread_cancel(sock_ptr->receiveFeedbackThread) == 0, "Cancel failed.");
        check(pthread_join(sock_ptr->receiveFeedbackThread, res) == 0, "Join failed.");
170
171
172

        sock_ptr->receiveFeedbackThread = 0;
    }
Andreas Schmidt's avatar
Andreas Schmidt committed
173
    return EXIT_SUCCESS;
174
175
176
177

    error:
        PERROR("Interruping socket failed.%s","");
        return EXIT_FAILURE;
178
179
180
}

int PrrtSocket_close(PrrtSocket *sock_ptr) {
181
    debug("Closing socket.");
182
    check(pthread_mutex_lock(&sock_ptr->closingMutex) == 0, "Lock failed.");
183
    if (!sock_ptr->closing) {
184
185
        check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
        check(PrrtSocket_interrupt(sock_ptr) == EXIT_SUCCESS, "Interrupt failed.");
186
    } else {
187
        check(pthread_mutex_unlock(&sock_ptr->closingMutex) == 0, "Unlock failed.");
188
189
    }

190
191
192
193
194
    if (sock_ptr->dataStore != NULL) {
        List *dataList = List_create();
        BPTree_get_range(sock_ptr->dataStore, dataList, 0, SEQNO_SPACE - 1);
        while (List_count(dataList) > 0) {
            PrrtPacket *packet = List_shift(dataList);
195
196
197
198
199
            PrrtPacket_destroy(packet);
        }

        List_destroy(dataList);

200
201
202
        sock_ptr->dataStore = BPTree_destroy(sock_ptr->dataStore);
    }

203
204
205
206
207
    if (sock_ptr->blockStore != NULL) {
        List *blockList = List_create();
        BPTree_get_range(sock_ptr->blockStore, blockList, 0, SEQNO_SPACE - 1);
        while (List_count(blockList) > 0) {
            PrrtBlock *block = List_shift(blockList);
Andreas Schmidt's avatar
Andreas Schmidt committed
208
            PrrtBlock_destroy(block);
209
210
211
212
        }

        List_destroy(blockList);

213
        sock_ptr->blockStore = BPTree_destroy(sock_ptr->blockStore);
214
215
    }

216
    if (sock_ptr->receivers != NULL) {
217
        while (List_count(sock_ptr->receivers) > 0) {
Andreas Schmidt's avatar
Andreas Schmidt committed
218
            PrrtReceiver *recv = List_shift(sock_ptr->receivers);
219
            PrrtReceiver_destroy(recv);
220
221
        }
        List_destroy(sock_ptr->receivers);
222
        sock_ptr->receivers = NULL;
223
224
    }

225
    if (sock_ptr->outQueue != NULL) {
226
227
        check(pthread_mutex_destroy(&sock_ptr->outQueueFilledMutex) == 0, "Mutex destroy failed.");
        check(pthread_cond_destroy(&sock_ptr->outQueueFilledCv) == 0, "Cond destroy failed.");
228
        List_destroy(sock_ptr->outQueue);
229
        sock_ptr->outQueue = NULL;
230
231
    }

232
    if (sock_ptr->inQueue != NULL) {
233
234
        check(pthread_mutex_destroy(&sock_ptr->inQueueFilledMutex) == 0, "Mutex destroy failed.");
        check(pthread_cond_destroy(&sock_ptr->inQueueFilledCv) == 0, "Cond destroy failed.");
235
        List_destroy(sock_ptr->inQueue);
236
237
238
        sock_ptr->inQueue = NULL;
    }

239
    if (sock_ptr->forwardPacketTable != NULL) {
240
        check(PrrtForwardPacketTable_destroy(sock_ptr->forwardPacketTable), "Destroy failed.");
241
        sock_ptr->forwardPacketTable = NULL;
242
    }
243

244
    check(pthread_mutex_destroy(&sock_ptr->closingMutex) == 0, "Mutex destroy failed.");
245

246
247
    close(sock_ptr->dataSocketFd);
    close(sock_ptr->feedbackSocketFd);
248
    debug("Socket closed.");
249
    return 0;
250
251
252
253

    error:
        PERROR("Closing socket failed.%s", "");
        return -1;
254
255
}

256
257
258
PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, const size_t length)
{
    char bufin[MAX_PAYLOAD_LENGTH];
259
260
261
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);
262
263
264
265
266
267
268
269
270
271
272
273

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

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

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

277
278
    uint32_t receive_time = PrrtClock_get_current_time();

279
280
    //uint16_t remote_port = ntohs(remote.sin_port);
    //char *remote_host = inet_ntoa(remote.sin_addr);
Andreas Schmidt's avatar
Andreas Schmidt committed
281
    //debug("Received feedback %s:%d", remote_host, remote_port);
282

Andreas Schmidt's avatar
Andreas Schmidt committed
283
    PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
284
    check_mem(packet_ptr);
285
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
286
    return packet_ptr;
287
288

    error:
289
    return NULL;
290
}