Commit 8d65f984 authored by Andreas Schmidt's avatar Andreas Schmidt

Extract channel state information

parent ccfecaef
add_library(PRRT ../defines.h socket.c block.c block.h packet.c packet.h receiver.c receiver.h clock.c clock.h processes/feedback_receiver.c processes/feedback_receiver.h processes/data_transmitter.c processes/data_transmitter.h coding_params.c coding_params.h vdmcode/block_code.c vdmcode/block_code.h coding_params.c coding_params.h stores/forward_packet_table.c stores/forward_packet_table.h processes/data_receiver.c processes/data_receiver.h)
\ No newline at end of file
add_library(PRRT ../defines.h socket.c block.c block.h packet.c packet.h receiver.c receiver.h clock.c clock.h channelStateInformation.c channelStateInformation.h processes/feedback_receiver.c processes/feedback_receiver.h processes/data_transmitter.c processes/data_transmitter.h coding_params.c coding_params.h vdmcode/block_code.c vdmcode/block_code.h coding_params.c coding_params.h stores/forward_packet_table.c stores/forward_packet_table.h processes/data_receiver.c processes/data_receiver.h)
\ No newline at end of file
#include <stdlib.h>
#include "../defines.h"
#include "../util/common.h"
#include "../util/dbg.h"
#include "channelStateInformation.h"
void PrrtChannelStateInformation_init(PrrtChannelStateInformation *csi)
{
check(csi != NULL, "Input should not be NULL.");
csi->rttMean = 0;
csi->rttDev = 0;
return;
error:
PERROR("Should not happen.%s","");
}
void PrrtChannelStateInformation_update_rtt(PrrtChannelStateInformation *csi, uint32_t rtt)
{
int32_t delta = rtt - csi->rttMean;
// TODO: ensure that there are no arithemtic problems via rounding etc.
csi->rttMean = (uint32_t) (csi->rttMean + RRT_ALPHA * delta);
csi->rttDev = (uint32_t) (csi->rttDev + RRT_ALPHA * (labs(delta) - csi->rttDev));
}
void PrrtChannelStateInformation_print(PrrtChannelStateInformation *csi)
{
check(csi != NULL, "Input should not be NULL.");
printf("RTT [us]: %d +- %d\n", csi->rttMean, csi->rttDev);
return;
error:
PERROR("Should not happen.%s","");
}
\ No newline at end of file
#ifndef PRRT_CHANNELSTATEINFORMATION_H
#define PRRT_CHANNELSTATEINFORMATION_H
#include <stdint.h>
typedef struct prrtChannelStateInformation {
uint32_t rttMean;
uint32_t rttDev;
} PrrtChannelStateInformation;
void PrrtChannelStateInformation_init(PrrtChannelStateInformation* csi);
void PrrtChannelStateInformation_update_rtt(PrrtChannelStateInformation* csi, uint32_t rtt);
void PrrtChannelStateInformation_print(PrrtChannelStateInformation *csi);
#endif //PRRT_CHANNELSTATEINFORMATION_H
......@@ -26,8 +26,7 @@ PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
sock_ptr->sequenceNumberSource = 1;
sock_ptr->sequenceNumberRedundancy = 1;
sock_ptr->rttMean = 0;
sock_ptr->rttDev = 0;
PrrtChannelStateInformation_init(&sock_ptr->csi);
sock_ptr->dataStore = NULL;
......@@ -256,7 +255,7 @@ int PrrtSocket_close(PrrtSocket *sock_ptr) {
return -1;
}
PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *sock_ptr, const size_t length)
PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *prrtSocket, const size_t length)
{
char bufin[MAX_PAYLOAD_LENGTH];
ssize_t n;
......@@ -265,7 +264,7 @@ PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *sock_ptr, const size_t length)
struct pollfd fds;
int timeout_msecs = 1000;
fds.fd = sock_ptr->feedbackSocketFd;
fds.fd = prrtSocket->feedbackSocketFd;
fds.events = POLLIN;
n = poll(&fds, 1, timeout_msecs);
......@@ -273,22 +272,19 @@ PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *sock_ptr, const size_t length)
if (n == 0) {
return NULL;
}
uint32_t receive_time = PrrtClock_get_current_time();
uint32_t receiveTime = PrrtClock_get_current_time();
n = recvfrom(sock_ptr->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
n = recvfrom(prrtSocket->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
check(n >= 0, "Receiving feedback failed.");
PrrtPacket *packet_ptr = calloc(1, sizeof(PrrtPacket));
check_mem(packet_ptr);
PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
PrrtPacket *prrtPacket = calloc(1, sizeof(PrrtPacket));
check_mem(prrtPacket);
PrrtPacket_decode(bufin, (uint16_t) n, prrtPacket);
PrrtPacketFeedbackPayload* feedbackPayload = packet_ptr->payload;
int32_t delta = (receive_time - feedbackPayload->forward_trip_timestamp) - sock_ptr->rttMean;
// TODO: ensure that there are no arithemtic problems via rounding etc.
sock_ptr->rttMean = (uint32_t) (sock_ptr->rttMean + RRT_ALPHA * delta);
sock_ptr->rttDev = (uint32_t) (sock_ptr->rttDev + RRT_ALPHA * (labs(delta) - sock_ptr->rttDev));
uint32_t forwardTripTimestamp = ((PrrtPacketFeedbackPayload*) prrtPacket->payload)->forward_trip_timestamp;
PrrtChannelStateInformation_update_rtt(&prrtSocket->csi, receiveTime - forwardTripTimestamp);
return packet_ptr;
return prrtPacket;
error:
return NULL;
......
......@@ -6,6 +6,7 @@
#include "stores/forward_packet_table.h"
#include "../util/list.h"
#include "../util/bptree.h"
#include "channelStateInformation.h"
typedef struct prrtSocket {
......@@ -44,6 +45,7 @@ typedef struct prrtSocket {
uint32_t lastSentTimestamp;
uint32_t lastReceivedTimestamp;
PrrtChannelStateInformation csi;
uint32_t rttMean;
uint32_t rttDev;
} PrrtSocket;
......@@ -55,6 +57,6 @@ int PrrtSocket_close(PrrtSocket *sock_ptr);
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port);
int PrrtSocket_send(PrrtSocket *sock_ptr, const uint8_t *data, const size_t data_len);
int32_t PrrtSocket_recv(PrrtSocket *sock_ptr, void *buf_ptr);
PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *sock_ptr, const size_t length);
PrrtPacket *PrrtSocket_recv_feedback(PrrtSocket *prrtSocket, const size_t length);
#endif // PRRT_SOCKET_H
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