Commit 55ec359d authored by Andreas Schmidt's avatar Andreas Schmidt

Add clock type. Calculating RTT.

parent f28146f4
add_library(PRRT ../defines.h socket.c block.c block.h packet.c packet.h receiver.c receiver.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 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 <sys/time.h>
#include <stddef.h>
#include "clock.h"
uint32_t PrrtClock_get_current_time(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
uint32_t time_in_micros = (uint32_t) (1000000 * tv.tv_sec + tv.tv_usec);
return time_in_micros;
}
#ifndef PRRT_CLOCK_H
#define PRRT_CLOCK_H
#include <stdint.h>
uint32_t PrrtClock_get_current_time(void);
#endif //PRRT_CLOCK_H
......@@ -8,6 +8,7 @@
#include <stdbool.h>
#include <arpa/inet.h>
#include "packet.h"
#include "clock.h"
void *encode_general_header(void *buf_ptr, const PrrtPacket *packet);
......@@ -417,11 +418,7 @@ PrrtPacket *PrrtPacket_create_data_packet(uint8_t priority, const void *payloadP
check_mem(dataPayload);
packet->payload = dataPayload;
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long time_in_micros = (unsigned long) (1000000 * tv.tv_sec + tv.tv_usec);
dataPayload->timestamp = (uint32_t) time_in_micros;
dataPayload->timestamp = PrrtClock_get_current_time();
dataPayload->group_round_trip_time = 95; // TODO: payload->rtt = CURRENT ESTIMATE
dataPayload->packet_timeout = 110; // TODO: payload->packet_timeout = NOW + maximum delay
dataPayload->decoding_timeout = 150; // TODO: payload->decoding_timeout
......@@ -486,4 +483,9 @@ PrrtPacket *PrrtPacket_create_feedback_packet(uint8_t priority, uint8_t index, u
error:
return NULL;
}
uint32_t PrrtPacket_get_data_timestamp(PrrtPacket* packet) {
PrrtPacketDataPayload* payload = packet->payload;
return payload->timestamp;
}
\ No newline at end of file
......@@ -81,4 +81,6 @@ 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);
#define PrrtPacket_copy_buffer_to_payload(packet, src, header_size) memcpy(packet->payload + header_size, src, packet->payload_len - header_size);
uint32_t PrrtPacket_get_data_timestamp(PrrtPacket* packet);
#endif //PRRT_FRAME_H
#include <netdb.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <src/prrt/clock.h>
#include "../../defines.h"
#include "../../util/dbg.h"
#include "../../util/common.h"
......@@ -61,7 +62,8 @@ bool 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);
uint32_t forwardTripTime = PrrtClock_get_current_time() + sock_ptr->lastSentTimestamp - sock_ptr->lastReceivedTimestamp;
PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 3, forwardTripTime, 50, 4, 6, 8, 9, 5, 1);
uint32_t length = PrrtPacket_size(feedback_pkt_ptr);
void *buf = calloc(1, length);
check_mem(buf);
......@@ -92,12 +94,16 @@ 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), "Sending feedback failed.");
sock_ptr->lastReceivedTimestamp = PrrtClock_get_current_time();
PrrtPacket *packet = (PrrtPacket *) calloc(1, sizeof(PrrtPacket));
check_mem(packet);
check(PrrtPacket_decode(buffer, (uint16_t) n, packet), "Decode failed.");
sock_ptr->lastSentTimestamp = PrrtPacket_get_data_timestamp(packet);
check(send_feedback(sock_ptr, remote), "Sending feedback failed.");
uint8_t packetType = PrrtPacket_type(packet);
if(packetType == PACKET_TYPE_DATA) {
// TODO: packet.timestamp + packet.timeout < now: break
......
......@@ -3,9 +3,10 @@
#include <unistd.h>
#include <string.h>
#include "../../defines.h"
#include "../socket.h"
#include "../block.h"
#include "../clock.h"
#include "../receiver.h"
#include "../socket.h"
#include "../../util/dbg.h"
#include "../../util/common.h"
#include "data_transmitter.h"
......@@ -33,6 +34,7 @@ bool send_packet(PrrtSocket *sock_ptr, PrrtPacket *packet) {
check(hp != NULL, "Could not resolve host '%s'.", recv->host_name)
memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
// TODO: to minimize impact of processing delay to the RTT measurement, the timestamp should be taken and saved here
check(sendto(sock_ptr->dataSocketFd, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) == length, "Sendto failed.");
usleep(1);
}
......@@ -73,6 +75,10 @@ void * send_data_loop(void *ptr) {
}
PrrtPacket *packet = List_shift(sock_ptr->outQueue);
PrrtPacketDataPayload* dataPayload = packet->payload;
dataPayload->timestamp = PrrtClock_get_current_time();
packet->seqno = sock_ptr->sequenceNumberSource++;
packet->index = (uint8_t) (packet->seqno - block->baseSequenceNumber);
......
......@@ -3,6 +3,7 @@
#include "../../defines.h"
#include "../../util/common.h"
#include "../../util/dbg.h"
#include "../clock.h"
#include "../packet.h"
#include "../socket.h"
#include "feedback_receiver.h"
......
......@@ -15,6 +15,7 @@
#include "socket.h"
#include "block.h"
#include "receiver.h"
#include "clock.h"
PrrtSocket* PrrtSocket_create(const uint16_t port, const uint8_t is_sender) {
PrrtSocket *sock_ptr = (PrrtSocket*) calloc(1, sizeof(PrrtSocket));
......@@ -273,6 +274,8 @@ PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, const size_t le
n = recvfrom(sock_ptr->feedbackSocketFd, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
check(n >= 0, "Receiving feedback failed.");
uint32_t receive_time = PrrtClock_get_current_time();
//uint16_t remote_port = ntohs(remote.sin_port);
//char *remote_host = inet_ntoa(remote.sin_addr);
//debug("Received feedback %s:%d", remote_host, remote_port);
......
......@@ -40,6 +40,9 @@ typedef struct prrtSocket {
uint16_t sequenceNumberRedundancy;
uint16_t sequenceNumberFeedback;
uint8_t is_sender;
uint32_t lastSentTimestamp;
uint32_t lastReceivedTimestamp;
} PrrtSocket;
......
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