Commit a0cd6a2e authored by Andreas Schmidt's avatar Andreas Schmidt

Fix error codes and bool handling.

parent c5936fed
Pipeline #43 passed with stage
......@@ -8,9 +8,6 @@
#define MAX_PAYLOAD_LENGTH 65528 // maximum UDP packet length (2^16 - 8)
#define TRUE 1
#define FALSE 0
#define GF_BITS 8
#define K_START 4
......@@ -22,10 +19,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#ifdef WINDOWS
#define bool char
#define false 0
#define true 1
#endif
#endif //PRRT_DEFINES_H
......@@ -73,55 +73,54 @@ int PrrtBlock_create(PrrtBlock *mblock, const PrrtCodingParams *cpar, uint16_t b
return 0;
}
int PrrtBlock_insert_data_packet(PrrtBlock *prrtBlock, const PrrtPacket *prrtPacket)
bool PrrtBlock_insert_data_packet(PrrtBlock *prrtBlock, const PrrtPacket *prrtPacket)
{
int found = FALSE;
bool found = false;
LIST_FOREACH(prrtBlock->dataPackets, first, next, cur) {
PrrtPacket *pkt = cur->value;
if(pkt->seqno == prrtPacket->seqno) {
found = TRUE;
found = true;
}
}
if(found == FALSE) {
if(found == false) {
List_push(prrtBlock->dataPackets, prrtPacket);
prrtBlock->largestDataLength = (uint32_t) MAX(prrtBlock->largestDataLength,
prrtPacket->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
return EXIT_SUCCESS;
return true;
} else {
return EXIT_FAILURE;
return false;
}
}
int PrrtBlock_insert_redundancy_packet(PrrtBlock *block_ptr, const PrrtPacket *ptr)
bool PrrtBlock_insert_redundancy_packet(PrrtBlock *block_ptr, const PrrtPacket *ptr)
{
int found = FALSE;
bool found = false;
LIST_FOREACH(block_ptr->redundancyPackets, first, next, cur) {
PrrtPacket *pkt = cur->value;
if(pkt->seqno == ptr->seqno) {
found = TRUE;
found = true;
}
}
if(found == FALSE) {
if(found == false) {
List_push(block_ptr->redundancyPackets, ptr);
block_ptr->largestDataLength = (uint32_t) MAX(block_ptr->largestDataLength,
ptr->payload_len - PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
return EXIT_SUCCESS;
return true;
} else {
return EXIT_FAILURE;
return false;
}
}
int PrrtBlock_encode_ready(const PrrtBlock *block_ptr)
bool PrrtBlock_encode_ready(const PrrtBlock *block_ptr)
{
return (List_count(block_ptr->dataPackets) == block_ptr->codingParams.k) ? TRUE : FALSE;
return (List_count(block_ptr->dataPackets) == block_ptr->codingParams.k);
}
int PrrtBlock_decode_ready(const PrrtBlock *block_ptr)
bool PrrtBlock_decode_ready(const PrrtBlock *block_ptr)
{
return (List_count(block_ptr->dataPackets) + List_count(block_ptr->redundancyPackets) == block_ptr->codingParams.k)
? TRUE : FALSE;
return (List_count(block_ptr->dataPackets) + List_count(block_ptr->redundancyPackets) == block_ptr->codingParams.k);
}
PrrtPacket *PrrtBlock_get_first_data(PrrtBlock *block_ptr)
......@@ -166,7 +165,7 @@ void PrrtBlock_encode(PrrtBlock *block_ptr, uint16_t *seqno)
PrrtBlock_insert_redundancy_packet(block_ptr, red_packet_ptr);
}
block_ptr->isCoded = TRUE;
block_ptr->isCoded = true;
PrrtCoder_destroy(coder);
......@@ -212,8 +211,8 @@ void PrrtBlock_decode(PrrtBlock *block_ptr)
for(j = 0; j < k; j++) {
if(idx_p[j] >= k) {
PrrtPacket *packet = PrrtPacket_create_data_packet(0, fec[j], length, (uint16_t) (baseSequenceNumber + j));
int insertRes = PrrtBlock_insert_data_packet(block_ptr, packet);
if(insertRes == EXIT_FAILURE) {
bool insertRes = PrrtBlock_insert_data_packet(block_ptr, packet);
if(insertRes == false) {
debug("Tried to insert unnecessary packet.");
PrrtPacket_destroy(packet);
}
......
......@@ -11,7 +11,7 @@ typedef struct {
uint16_t baseSequenceNumber;
List*dataPackets;
List*redundancyPackets;
uint8_t isCoded;
bool isCoded;
} PrrtBlock;
......@@ -25,11 +25,11 @@ int PrrtBlock_create(PrrtBlock *mblock, const PrrtCodingParams *cpar, uint16_t b
*/
void PrrtBlock_destroy(PrrtBlock *mblock);
int PrrtBlock_insert_data_packet(PrrtBlock *prrtBlock, const PrrtPacket *prrtPacket);
int PrrtBlock_insert_redundancy_packet(PrrtBlock *prrtBlock, const PrrtPacket *prrtPacket);
bool PrrtBlock_insert_data_packet(PrrtBlock *prrtBlock, const PrrtPacket *prrtPacket);
bool PrrtBlock_insert_redundancy_packet(PrrtBlock *prrtBlock, const PrrtPacket *prrtPacket);
int PrrtBlock_encode_ready(const PrrtBlock *block_ptr);
int PrrtBlock_decode_ready(const PrrtBlock *block_ptr);
bool PrrtBlock_encode_ready(const PrrtBlock *block_ptr);
bool PrrtBlock_decode_ready(const PrrtBlock *block_ptr);
PrrtPacket *PrrtBlock_get_first_data(PrrtBlock *block_ptr);
......
......@@ -5,6 +5,7 @@
#include <netinet/in.h>
#include <src/util/dbg.h>
#include <src/util/common.h>
#include <stdbool.h>
#include "packet.h"
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
......@@ -123,7 +124,7 @@ PrrtPacket *create_header(uint8_t priority, uint16_t seqno, uint32_t size, uint8
PERROR("Could not create packet.%s", "");
}
int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
{
void *payload = packet_ptr->payload;
......@@ -143,12 +144,12 @@ int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
PrrtPacket_copy_payload_to_buffer(buf_ptr, packet_ptr, PRRT_PACKET_FEEDBACK_HEADER_SIZE);
} else {
perror("NOT IMPLEMENTED");
return -1;
return false;
}
return 0;
return true;
error:
return -1;
return false;
}
void *encode_redundancy_header(void *buf_ptr, const void *payload)
......@@ -260,7 +261,7 @@ void *encode_general_header(void *buf_ptr, const PrrtPacket *packet)
return buf_ptr;
}
int PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket)
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket)
{
uint32_t payload_len = (uint32_t) (srcBufferSize - PRRT_PACKET_GENERAL_HEADER_SIZE);
targetPacket->type_priority = *(uint8_t *) srcBuffer;
......@@ -290,10 +291,10 @@ int PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targe
} else {
printf("NOT IMPLEMENTED\n");
}
return 0;
return true;
error:
return EXIT_FAILURE;
return false;
}
void *decode_redundancy_header(void *dstBuffer, const void *srcBuffer)
......
......@@ -8,6 +8,7 @@
#include <stdint.h>
#include <src/prrt/coding_params.h>
#include <stdbool.h>
#define PACKET_TYPE_DATA 0
#define PACKET_TYPE_REPETITION 1
......@@ -73,8 +74,8 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *payloadPointer, uint32_t payloadLength,
uint16_t sequenceNumber, uint8_t index, uint16_t baseSequenceNumber, PrrtCodingParams codingParams);
int PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket);
int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr);
bool PrrtPacket_decode(void *srcBuffer, uint16_t srcBufferSize, PrrtPacket *targetPacket);
bool PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr);
int PrrtPacket_destroy(PrrtPacket *packet);
#define PrrtPacket_copy_payload_to_buffer(dst, packet, header_size) memcpy(dst, packet->payload + header_size, packet->payload_len - header_size);
......
......@@ -15,12 +15,11 @@ void retrieve_data_blocks(PrrtSocket *sock_ptr, uint16_t base_seqno, uint8_t k,
LIST_FOREACH(res, first, next, cur) {
PrrtPacket *packetPtr = cur->value;
int insertRes = PrrtBlock_insert_data_packet((PrrtBlock *) block, packetPtr);
check(insertRes == EXIT_SUCCESS, "Insert failed!")
check(PrrtBlock_insert_data_packet((PrrtBlock *) block, packetPtr), "Insert failed!")
sock_ptr->dataStore = BPTree_delete(sock_ptr->dataStore, packetPtr->seqno);
}
error:
List_destroy(res);
List_destroy(res);
}
void decode_block(PrrtSocket *sock_ptr, PrrtBlock *block, uint16_t base_seqno)
......@@ -46,7 +45,7 @@ void decode_block(PrrtSocket *sock_ptr, PrrtBlock *block, uint16_t base_seqno)
}
}
int send_feedback(const PrrtSocket *sock_ptr, struct sockaddr_in remote)
bool send_feedback(const PrrtSocket *sock_ptr, struct sockaddr_in remote)
{
uint16_t remote_port = ntohs(remote.sin_port);
char *remote_host = inet_ntoa(remote.sin_addr);
......@@ -60,23 +59,21 @@ int send_feedback(const PrrtSocket *sock_ptr, struct sockaddr_in remote)
hp = gethostbyname(remote_host);
memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
uint8_t buf[MAX_PAYLOAD_LENGTH];
memset(buf, 0, MAX_PAYLOAD_LENGTH);
uint32_t length = PrrtPacket_size(feedback_pkt_ptr);
check(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) == EXIT_SUCCESS,
"Buffer for encoding feedback is too small");
check(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr), "Buffer for encoding feedback is too small");
check(sendto(sock_ptr->feedbackSocketFd, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) ==
length, "Sending feedback failed.");
PrrtPacket_destroy(feedback_pkt_ptr);
return EXIT_SUCCESS;
return true;
error:
return EXIT_FAILURE;
return false;
}
void *receive_data_loop(void *ptr)
......@@ -91,7 +88,7 @@ void *receive_data_loop(void *ptr)
while(1) {
memset(buffer, 0, MAX_PAYLOAD_LENGTH);
n = recvfrom(sock_ptr->dataSocketFd, buffer, MAX_PAYLOAD_LENGTH, 0, (struct sockaddr *) &remote, &addrlen);
check(send_feedback(sock_ptr, remote) == EXIT_SUCCESS, "Sending feedback failed.");
check(send_feedback(sock_ptr, remote), "Sending feedback failed.");
PrrtPacket *packet = (PrrtPacket *) calloc(1, sizeof(PrrtPacket));
check_mem(packet);
......@@ -102,17 +99,16 @@ void *receive_data_loop(void *ptr)
// TODO: packet.timestamp + packet.timeout < now: break
if(PrrtForwardPacketTable_test_set_is_number_relevant(sock_ptr->forwardPacketTable, packet->seqno) ==
FALSE) {
false) {
PrrtPacket_destroy(packet);
} else {
uint16_t baseSequenceNumber = packet->seqno - packet->index;
PrrtPacket* reference = PrrtPacket_copy(packet);
PrrtPacket *reference = PrrtPacket_copy(packet);
PrrtBlock *block = BPTree_get(sock_ptr->blockStore, baseSequenceNumber);
if(block != NULL) {
int res = PrrtBlock_insert_data_packet(block, reference);
check(res == EXIT_SUCCESS, "Inserting failed")
check(PrrtBlock_insert_data_packet(block, reference), "Inserting failed");
decode_block(sock_ptr, block, baseSequenceNumber);
} else {
// Check for duplicate data packet.
......@@ -156,8 +152,7 @@ void *receive_data_loop(void *ptr)
retrieve_data_blocks(sock_ptr, redundancyPayload->base_seqno, block->codingParams.k, block);
int res = PrrtBlock_insert_redundancy_packet(block, packet);
if(res == EXIT_SUCCESS) {
if(PrrtBlock_insert_redundancy_packet(block, packet)) {
decode_block(sock_ptr, block, redundancyPayload->base_seqno);
} else {
PrrtPacket_destroy(packet);
......
......@@ -10,13 +10,12 @@
#include "data_transmitter.h"
int send_packet(PrrtSocket *sock_ptr, PrrtPacket *packet) {
bool send_packet(PrrtSocket *sock_ptr, PrrtPacket *packet) {
uint8_t buf[MAX_PAYLOAD_LENGTH];
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.");
check(PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, packet), "Buffer too small.");
// SENDING TO ALL RECEIVERS
LIST_FOREACH(sock_ptr->receivers, first, next, cur) {
......@@ -37,11 +36,11 @@ int send_packet(PrrtSocket *sock_ptr, PrrtPacket *packet) {
PrrtPacket_destroy(packet);
return EXIT_SUCCESS;
return true;
error:
PERROR("Something is wrong%s.", "")
return EXIT_FAILURE;
return false;
}
void * send_data_loop(void *ptr) {
......
......@@ -12,7 +12,7 @@ void *receive_feedback_loop(void *ptr) {
PrrtSocket *sock_ptr = ptr;
pthread_mutex_lock(&sock_ptr->closingMutex);
while (sock_ptr->closing == FALSE) {
while (sock_ptr->closing == false) {
pthread_mutex_unlock(&sock_ptr->closingMutex);
memset(bufin, 0, MAX_PAYLOAD_LENGTH);
PrrtPacket *t = PrrtSocket_recv_feedback(sock_ptr, bufin, MAX_PAYLOAD_LENGTH);
......
......@@ -17,7 +17,7 @@
int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender) {
bool PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender) {
sock_ptr->sequenceNumberSource = 1;
sock_ptr->sequenceNumberRedundancy = 1;
......@@ -76,11 +76,11 @@ int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t i
"Cannot create data receiving thread.");
}
return EXIT_SUCCESS;
return true;
error:
PrrtSocket_close(sock_ptr);
return EXIT_FAILURE;
return false;
}
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
......@@ -132,7 +132,7 @@ int32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void *buf_ptr) {
int PrrtSocket_interrupt(PrrtSocket *sock_ptr) {
pthread_mutex_lock(&sock_ptr->closingMutex);
sock_ptr->closing = TRUE;
sock_ptr->closing = true;
pthread_mutex_unlock(&sock_ptr->closingMutex);
void **res = NULL;
......
......@@ -46,7 +46,7 @@ typedef struct {
} PrrtSocket;
int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender);
bool PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender);
int PrrtSocket_interrupt(PrrtSocket *sock_ptr);
int PrrtSocket_close(PrrtSocket *sock_ptr);
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port);
......
......@@ -8,9 +8,9 @@
int is_position_relevant(const PrrtForwardPacketTable *fpt_ptr, uint16_t seqno) {
uint16_t stop = (uint16_t) (SEQNO_SPACE / 2 + fpt_ptr->start);
if (fpt_ptr->start < stop && !(fpt_ptr->start <= seqno && seqno <= stop)) {
return FALSE;
return false;
} else if (!(seqno <= stop || fpt_ptr->start <= seqno)) {
return FALSE;
return false;
} else {
uint16_t which_byte = (uint16_t) (seqno / 32);
uint16_t which_bit = (uint16_t) (seqno % 32);
......@@ -57,9 +57,10 @@ int PrrtForwardPacketTable_test_set_is_number_relevant(PrrtForwardPacketTable *f
return res;
}
int PrrtForwardPacketTable_test_is_block_relevant(PrrtForwardPacketTable * forwardPacketTable, uint16_t start, uint16_t length) {
bool PrrtForwardPacketTable_test_is_block_relevant(PrrtForwardPacketTable *forwardPacketTable, uint16_t start,
uint16_t length) {
// TODO: implement efficiently
int res = FALSE;
bool res = false;
int i;
for (i = 0; i < length; i++) {
......
......@@ -15,6 +15,7 @@ int PrrtForwardPacketTable_create(PrrtForwardPacketTable* fpt_prt);
int PrrtForwardPacketTable_test_set_is_number_relevant(PrrtForwardPacketTable *fpt_ptr, uint16_t seqno);
int PrrtForwardPacketTable_test_is_block_relevant(PrrtForwardPacketTable * forwardPacketTable, uint16_t start, uint16_t length);
bool PrrtForwardPacketTable_test_is_block_relevant(PrrtForwardPacketTable *forwardPacketTable, uint16_t start,
uint16_t length);
#endif //PRRT_FORWARD_PACKET_TABLE_H
......@@ -15,8 +15,7 @@ int main(int argc, char *const argv) {
PrrtSocket* sock = calloc(1, sizeof(PrrtSocket));
debug("PRRT - SENDER\n");
int res = PrrtSocket_create(sock, local_port, TRUE);
check(res == EXIT_SUCCESS, "Socket creation failed.");
check(PrrtSocket_create(sock, local_port, true), "Socket creation failed.");
//char *remote_host = "127.0.0.1";
//uint16_t remote_port = 5000;
......
......@@ -107,7 +107,7 @@ TEST_F(PrrtBlockTest, EncodeDecode)
packets[i] = PrrtPacket_create_data_packet(0, data, (uint32_t) strlen(data), i+1);
packets[i]->index = (uint8_t) i;
refPackets[i] = PrrtPacket_create_data_packet(0, data, (uint32_t) strlen(data), i+1);
ASSERT_TRUE(PrrtBlock_insert_data_packet(encBlock, packets[i]) == EXIT_SUCCESS);
ASSERT_TRUE(PrrtBlock_insert_data_packet(encBlock, packets[i]));
}
// ENCODING
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment