Commit 624e56aa authored by Andreas Schmidt's avatar Andreas Schmidt

Receiving and encoding/decoding of redundancy packets works.

parent 5da95b6b
......@@ -48,7 +48,6 @@ PrrtPacket *PrrtBlock_get_first_data(PrrtBlock *block_ptr) {
}
void PrrtBlock_code(PrrtBlock *block_ptr) {
printf("CODING\n");
int j = 0, m = 0;
int k = block_ptr->coding_params.k;
int n = block_ptr->coding_params.n;
......@@ -66,8 +65,6 @@ void PrrtBlock_code(PrrtBlock *block_ptr) {
src[j] = malloc(sizeof(gf) * length);
memset(src[j], 0, sizeof(gf) * length);
PrrtPacket* pkt = cur->value;
PrrtPacket_print(pkt);
memcpy(src[j], pkt->payload + PRRT_PACKET_DATA_HEADER_SIZE, pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
j++;
}
......@@ -80,9 +77,8 @@ void PrrtBlock_code(PrrtBlock *block_ptr) {
fec[j] = malloc(sizeof(gf) * length);
memset(fec[j], 0, sizeof(gf) * length);
PrrtCoder_encode(coder, src, fec[j], j+k, length); // gf **src, gf *fec, int index, int sz
print_gf(fec[j], length);
PrrtPacket* red_packet_ptr = PrrtPacket_create_redundancy_packet(0, (void*) fec[j], length, 0, index, base_seqno, block_ptr->coding_params);
PrrtPacket_print(red_packet_ptr);
PrrtBlock_insert_redundancy_packet(block_ptr, red_packet_ptr);
}
......
......@@ -11,10 +11,14 @@ void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
void *encode_data_header(void *buf_ptr, void *payload);
void *encode_redundancy_header(void *buf_ptr, void *payload);
void *encode_feedback_header(void *buf_ptr, void *payload);
void *decode_data_header(void *buf_ptr, const void *payload_ptr);
void *decode_redundancy_header(void *buf_ptr, const void *payload_ptr);
void *decode_feedback_header(void *buf_ptr, void *payload_ptr);
uint8_t PrrtPacket_type(PrrtPacket *packet_ptr) {
......@@ -32,23 +36,27 @@ uint16_t PrrtPacket_size(PrrtPacket *packet_ptr) {
int PrrtPacket_print(PrrtPacket *packet_ptr) {
printf(" 0 1 2 3\n"
" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1\n"
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
"+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
uint8_t type = PrrtPacket_type(packet_ptr);
printf("| %5u | %5u | %13u | %29u |\n", type, PrrtPacket_priority(packet_ptr), packet_ptr->index, packet_ptr->seqno);
printf("| %5u | %5u | %13u | %29u |\n", type, PrrtPacket_priority(packet_ptr), packet_ptr->index,
packet_ptr->seqno);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
if(type == PACKET_TYPE_DATA) {
PrrtPacketDataPayload * payload = packet_ptr->payload;
if (type == PACKET_TYPE_DATA) {
PrrtPacketDataPayload *payload = packet_ptr->payload;
printf("| %61u |\n", payload->timestamp);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %29u | %29u |\n", payload->group_round_trip_time, payload->packet_timeout);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %29u | %29u |\n", payload->decoding_timeout, payload->feedback_timeout);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
}
else if(type == PACKET_TYPE_FEEDBACK) {
PrrtPacketFeedbackPayload * payload = packet_ptr->payload;
} else if (type == PACKET_TYPE_REDUNDANCY) {
PrrtPacketRedundancyPayload *payload = packet_ptr->payload;
printf("| %29u | %13u | %13u |\n", payload->base_seqno, payload->n, payload->k);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
} else if (type == PACKET_TYPE_FEEDBACK) {
PrrtPacketFeedbackPayload *payload = packet_ptr->payload;
printf("| %61u |\n", payload->group_round_trip_time);
printf("+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\n");
printf("| %61u |\n", payload->forward_trip_time);
......@@ -72,9 +80,9 @@ int PrrtPacket_print(PrrtPacket *packet_ptr) {
}
int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
void * payload = packet_ptr->payload;
void *payload = packet_ptr->payload;
if(buf_size < packet_ptr->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE) {
if (buf_size < packet_ptr->payload_len + PRRT_PACKET_GENERAL_HEADER_SIZE) {
perror("BUFFER TO SMALL");
return -1;
}
......@@ -82,12 +90,17 @@ int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
buf_ptr = encode_general_header(buf_ptr, packet_ptr);
uint8_t type = PrrtPacket_type(packet_ptr);
if(type == PACKET_TYPE_DATA) {
if (type == PACKET_TYPE_DATA) {
buf_ptr = encode_data_header(buf_ptr, payload);
memcpy(buf_ptr, payload + PRRT_PACKET_DATA_HEADER_SIZE, packet_ptr->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
} else if (type == PACKET_TYPE_REDUNDANCY) {
buf_ptr = encode_redundancy_header(buf_ptr, payload);
memcpy(buf_ptr, payload + PRRT_PACKET_REDUNDANCY_HEADER_SIZE,
packet_ptr->payload_len - PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
} else if (type == PACKET_TYPE_FEEDBACK) {
buf_ptr = encode_feedback_header(buf_ptr, payload);
memcpy(buf_ptr, payload + PRRT_PACKET_FEEDBACK_HEADER_SIZE, packet_ptr->payload_len - PRRT_PACKET_FEEDBACK_HEADER_SIZE);
memcpy(buf_ptr, payload + PRRT_PACKET_FEEDBACK_HEADER_SIZE,
packet_ptr->payload_len - PRRT_PACKET_FEEDBACK_HEADER_SIZE);
} else {
perror("NOT IMPLEMENTED");
return -1;
......@@ -95,54 +108,72 @@ int PrrtPacket_encode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr)
return 0;
}
void *encode_redundancy_header(void *buf_ptr, void *payload) {
PrrtPacketRedundancyPayload *redundancyPayload = payload;
uint16_t *baseSeqNo = (uint16_t *) buf_ptr;
*baseSeqNo = htons(redundancyPayload->base_seqno);
buf_ptr += 2;
uint8_t *n = (uint8_t *) buf_ptr;
*n = redundancyPayload->n;
buf_ptr += 1;
uint8_t *k = (uint8_t *) buf_ptr;
*k = redundancyPayload->k;
buf_ptr += 1;
return buf_ptr;
}
void *encode_feedback_header(void *buf_ptr, void *payload) {
PrrtPacketFeedbackPayload * feedback_payload = payload;
PrrtPacketFeedbackPayload *feedbackPayload = payload;
uint32_t *receiver_ip = (uint32_t *) buf_ptr;
*receiver_ip = htonl(feedback_payload->receiver_addr);
*receiver_ip = htonl(feedbackPayload->receiver_addr);
buf_ptr += 4;
uint32_t *group_round_trip_time = (uint32_t *) buf_ptr;
*group_round_trip_time = htonl(feedback_payload->group_round_trip_time);
buf_ptr+=4;
*group_round_trip_time = htonl(feedbackPayload->group_round_trip_time);
buf_ptr += 4;
uint32_t *forward_trip_time = (uint32_t *) buf_ptr;
*forward_trip_time = htonl(feedback_payload->forward_trip_time);
buf_ptr+=4;
*forward_trip_time = htonl(feedbackPayload->forward_trip_time);
buf_ptr += 4;
uint32_t *packet_loss_rate = (uint32_t *) buf_ptr;
*packet_loss_rate = htonl(feedback_payload->packet_loss_rate);
buf_ptr+=4;
*packet_loss_rate = htonl(feedbackPayload->packet_loss_rate);
buf_ptr += 4;
uint16_t *gap = (uint16_t *) buf_ptr;
*gap = htons(feedback_payload->gap);
buf_ptr+=2;
*gap = htons(feedbackPayload->gap);
buf_ptr += 2;
uint16_t *ngap = (uint16_t *) buf_ptr;
*ngap = htons(feedback_payload->ngap);
buf_ptr+=2;
*ngap = htons(feedbackPayload->ngap);
buf_ptr += 2;
uint16_t *burst = (uint16_t *) buf_ptr;
*burst = htons(feedback_payload->burst);
buf_ptr+=2;
*burst = htons(feedbackPayload->burst);
buf_ptr += 2;
uint16_t *nburst = (uint16_t *) buf_ptr;
*nburst = htons(feedback_payload->nburst);
buf_ptr+=2;
*nburst = htons(feedbackPayload->nburst);
buf_ptr += 2;
uint32_t *bandwidth_estimate = (uint32_t *) buf_ptr;
*bandwidth_estimate = htonl(feedback_payload->bandwidth_estimate);
buf_ptr+=4;
*bandwidth_estimate = htonl(feedbackPayload->bandwidth_estimate);
buf_ptr += 4;
uint32_t *buffer_feedback = (uint32_t *) buf_ptr;
*buffer_feedback = htonl(feedback_payload->buffer_feedback);
buf_ptr+=4;
*buffer_feedback = htonl(feedbackPayload->buffer_feedback);
buf_ptr += 4;
return buf_ptr;
}
void *encode_data_header(void *buf_ptr, void *payload) {
PrrtPacketDataPayload * data_payload = payload;
PrrtPacketDataPayload *data_payload = payload;
uint32_t *timestamp = (uint32_t *) buf_ptr;
*timestamp = htonl(data_payload->timestamp);
......@@ -171,11 +202,11 @@ void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
*type_priority = packet->type_priority;
buf_ptr += 1;
uint8_t *index =(uint8_t *) buf_ptr;
uint8_t *index = (uint8_t *) buf_ptr;
*index = packet->index;
buf_ptr += 1;
uint16_t *seqno =(uint16_t *) buf_ptr;
uint16_t *seqno = (uint16_t *) buf_ptr;
*seqno = htons(packet->seqno);
buf_ptr += 2;
......@@ -184,32 +215,54 @@ void *encode_general_header(void *buf_ptr, const PrrtPacket *packet) {
int PrrtPacket_decode(void *buf_ptr, uint16_t buf_size, PrrtPacket *packet_ptr) {
uint32_t payload_len = (uint32_t) (buf_size - PRRT_PACKET_GENERAL_HEADER_SIZE);
packet_ptr->type_priority = *(uint8_t*) buf_ptr;
packet_ptr->type_priority = *(uint8_t *) buf_ptr;
buf_ptr += 1;
uint8_t* index_ptr = (uint8_t*) buf_ptr;
uint8_t *index_ptr = (uint8_t *) buf_ptr;
packet_ptr->index = *index_ptr;
buf_ptr += 1;
uint16_t* seqno_prt = (uint16_t*) buf_ptr;
uint16_t *seqno_prt = (uint16_t *) buf_ptr;
packet_ptr->seqno = ntohs(*seqno_prt);
buf_ptr += 2;
void* payload_buffer = malloc(payload_len);
void *payload_buffer = malloc(payload_len);
assert(payload_buffer != NULL);
packet_ptr->payload = payload_buffer;
packet_ptr->payload_len = payload_len;
if(PrrtPacket_type(packet_ptr) == PACKET_TYPE_DATA) {
if (PrrtPacket_type(packet_ptr) == PACKET_TYPE_DATA) {
buf_ptr = decode_data_header(buf_ptr, payload_buffer);
memcpy(payload_buffer + PRRT_PACKET_DATA_HEADER_SIZE, buf_ptr, payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
} else if(PrrtPacket_type(packet_ptr) == PACKET_TYPE_FEEDBACK) {
} else if(PrrtPacket_type(packet_ptr)) {
buf_ptr = decode_redundancy_header(buf_ptr, payload_buffer);
memcpy(payload_buffer + PRRT_PACKET_REDUNDANCY_HEADER_SIZE, buf_ptr, payload_len - PRRT_PACKET_REDUNDANCY_HEADER_SIZE);
} else if (PrrtPacket_type(packet_ptr) == PACKET_TYPE_FEEDBACK) {
buf_ptr = decode_feedback_header(buf_ptr, payload_buffer);
memcpy(payload_buffer + PRRT_PACKET_FEEDBACK_HEADER_SIZE, buf_ptr, payload_len - PRRT_PACKET_FEEDBACK_HEADER_SIZE);
memcpy(payload_buffer + PRRT_PACKET_FEEDBACK_HEADER_SIZE, buf_ptr,
payload_len - PRRT_PACKET_FEEDBACK_HEADER_SIZE);
} else {
printf("NOT IMPLEMENTED\n");
}
return 0;
}
void *decode_redundancy_header(void *buf_ptr, const void *payload_ptr) {
PrrtPacketRedundancyPayload* redundancyPayload = (PrrtPacketRedundancyPayload *) payload_ptr;
uint16_t *baseSeqNo = (uint16_t *) buf_ptr;
redundancyPayload->base_seqno = ntohs(*baseSeqNo);
buf_ptr += 2;
uint8_t *n = (uint8_t *) buf_ptr;
redundancyPayload->n = *n;
buf_ptr += 1;
uint8_t *k = (uint8_t *) buf_ptr;
redundancyPayload->k = *k;
buf_ptr += 1;
return buf_ptr;
}
void *decode_feedback_header(void *buf_ptr, void *payload_ptr) {
PrrtPacketFeedbackPayload *feedback_payload = (PrrtPacketFeedbackPayload *) payload_ptr;
......@@ -257,7 +310,7 @@ void *decode_feedback_header(void *buf_ptr, void *payload_ptr) {
}
void *decode_data_header(void *buf_ptr, const void *payload_ptr) {
PrrtPacketDataPayload * data_payload = (PrrtPacketDataPayload *) payload_ptr;
PrrtPacketDataPayload *data_payload = (PrrtPacketDataPayload *) payload_ptr;
uint32_t *timestamp = (uint32_t *) buf_ptr;
data_payload->timestamp = ntohl(*timestamp);
......@@ -282,7 +335,7 @@ void *decode_data_header(void *buf_ptr, const void *payload_ptr) {
}
int PrrtPacket_destroy(PrrtPacket *packet_ptr) {
if(packet_ptr->payload != NULL) {
if (packet_ptr->payload != NULL) {
free(packet_ptr->payload);
}
free(packet_ptr);
......@@ -302,10 +355,10 @@ int PrrtPacket_create_data_packet(PrrtPacket *packet_ptr, uint8_t priority, cons
void *content_buf = malloc(sizeof(PrrtPacketDataPayload) + data_len);
assert(content_buf != NULL);
PrrtPacketDataPayload * source_payload = content_buf;
PrrtPacketDataPayload *source_payload = content_buf;
struct timeval tv;
gettimeofday(&tv,NULL);
gettimeofday(&tv, NULL);
unsigned long time_in_micros = (unsigned long) (1000000 * tv.tv_sec + tv.tv_usec);
source_payload->timestamp = (uint32_t) time_in_micros;
......@@ -322,12 +375,13 @@ int PrrtPacket_create_data_packet(PrrtPacket *packet_ptr, uint8_t priority, cons
}
PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *data_ptr, uint32_t data_len,
uint16_t seqno, uint8_t index, uint16_t base_seqno, PrrtCodingParams codingParams) {
uint16_t seqno, uint8_t index, uint16_t base_seqno,
PrrtCodingParams codingParams) {
uint32_t payload_length = (uint32_t) (data_len + sizeof(PrrtPacketRedundancyPayload));
PrrtPacket* packet_ptr = malloc(sizeof(PrrtPacket));
PrrtPacket *packet_ptr = malloc(sizeof(PrrtPacket));
assert(packet_ptr != NULL);
packet_ptr->type_priority = PACKET_TYPE_REDUNDANCY;
packet_ptr->type_priority = PACKET_TYPE_REDUNDANCY << 4;
packet_ptr->type_priority |= priority & 0x0F;
packet_ptr->index = index;
packet_ptr->seqno = seqno;
......@@ -338,8 +392,8 @@ PrrtPacket *PrrtPacket_create_redundancy_packet(uint8_t priority, void *data_ptr
PrrtPacketRedundancyPayload *redundancy_payload = content_buf;
redundancy_payload->base_seqno = base_seqno;
redundancy_payload->k =codingParams.k;
redundancy_payload->n =codingParams.n;
redundancy_payload->k = codingParams.k;
redundancy_payload->n = codingParams.n;
packet_ptr->payload = content_buf;
memcpy(content_buf + sizeof(PrrtPacketRedundancyPayload), data_ptr, data_len);
......@@ -360,7 +414,7 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
packet_ptr->index = index;
packet_ptr->seqno = seqno;
PrrtPacketFeedbackPayload * payload = malloc(sizeof(PrrtPacketFeedbackPayload));
PrrtPacketFeedbackPayload *payload = malloc(sizeof(PrrtPacketFeedbackPayload));
assert(payload != NULL);
payload->group_round_trip_time = round_trip_time;
......@@ -373,7 +427,7 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
payload->bandwidth_estimate = bandwidth;
payload->buffer_feedback = buffer_feedback;
packet_ptr->payload = (void*) payload;
packet_ptr->payload = (void *) payload;
packet_ptr->payload_len = (uint32_t) (sizeof(PrrtPacketFeedbackPayload));
return packet_ptr;
......
......@@ -27,11 +27,17 @@ int main(int argc, char* const argv[]) {
perror("Error receiving data");
break;
} else {
// TODO: case distinction over message type
char buffer[pkt->payload_len + 1 - PRRT_PACKET_DATA_HEADER_SIZE];
memcpy(buffer, pkt->payload + PRRT_PACKET_DATA_HEADER_SIZE, pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
buffer[pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE] = '\0';
printf("%s\n", buffer);
switch(PrrtPacket_type(pkt)) {
case PACKET_TYPE_DATA:
memcpy(buffer, pkt->payload + PRRT_PACKET_DATA_HEADER_SIZE, pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
buffer[pkt->payload_len - PRRT_PACKET_DATA_HEADER_SIZE] = '\0';
printf("[B] %s\n", buffer);
break;
default:
PrrtPacket_print(pkt);
break;
}
}
usleep(1);
}
......
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