Skip to content

Commit

Permalink
Feat/udp2 (#180)
Browse files Browse the repository at this point in the history
* include udp squared submodule

* obcpp binary now compiling w/ udp2 protocol

* pass unit tests

* redisable logging in tests

* update integration test

* update integrationt test after testing with cody

* fix lint
  • Loading branch information
Tyler-Lentz authored May 13, 2024
1 parent 2beade6 commit 4fd7b0a
Show file tree
Hide file tree
Showing 19 changed files with 326 additions and 397 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "protos"]
path = protos
url = https://github.com/tritonuas/protos.git
[submodule "include/udp_squared"]
path = include/udp_squared
url = https://github.com/tritonuas/udp_squared.git
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ if(CPPLINT)
COMMAND cpplint
# Do not require licenses, TODO assignment, Google versions of C++ libs
# also don't check for runtime/references since Google's public style guidelines are behind https://github.com/cpplint/cpplint/issues/148
--exclude=../include/udp_squared
--filter=-legal,-readability/todo,-build/c++11,-runtime/references
--linelength=100
--recursive
Expand Down
119 changes: 0 additions & 119 deletions include/airdrop/packet.h

This file was deleted.

17 changes: 8 additions & 9 deletions include/network/airdrop_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <future>

extern "C" {
#include "airdrop/packet.h"
#include "udp_squared/protocol.h"
#include "network/airdrop_sockets.h"
}
#include "protos/obc.pb.h"
Expand All @@ -23,26 +23,25 @@ class AirdropClient {
explicit AirdropClient(ad_socket_t socket);
~AirdropClient();

bool send(ad_packet_t packet);
bool send(ad_latlng_packet_t packet);
bool send(packet_t packet);
// Receives oldest packet since last receive() call, ignoring any
// HEARTBEAT packets as those are parsed by the client itself
// and exposed through the TODO function.
std::optional<ad_packet_t> receive();
std::optional<packet_t> receive();

// Returns list of all the payloads we have not heard from for more than
// `threshold` seconds, and includes how many seconds it has been since
// we last heard from them.
std::list<std::pair<BottleDropIndex, std::chrono::milliseconds>>
getLostConnections(std::chrono::milliseconds threshold);

std::optional<ad_mode> getMode();
std::optional<drop_mode_t> getMode();

private:
std::optional<ad_mode> mode {};
std::optional<drop_mode_t> mode {};
ad_socket_t socket {};

std::queue<ad_packet_t> recv_queue;
std::queue<packet_t> recv_queue;
std::mutex recv_mut;

std::atomic_bool stop_worker;
Expand All @@ -58,15 +57,15 @@ class AirdropClient {
void _receiveWorker();

// Blocking implementation of receive for internal use
ad_packet_t _receiveBlocking();
packet_t _receiveBlocking();

// Parses packets for heartbeat information and stores it in the
// lastHeartbeat array.
// Returns true if it is a heartbeat packet, in which case
// it should NOT be placed in the recv_queue. Otherwise, returns
// false, meaning that it was NOT a heartbeat and should be exposed
// to the user of the client through the nonblocking receive function.
bool _parseHeartbeats(ad_packet_t packet);
bool _parseHeartbeats(packet_t packet);
};

#endif // INCLUDE_NETWORK_AIRDROP_CLIENT_HPP_
6 changes: 2 additions & 4 deletions include/network/airdrop_sockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdlib.h>

#include "airdrop/packet.h"
#include "udp_squared/protocol.h"

struct ad_socket {
uint16_t send_port;
Expand Down Expand Up @@ -44,7 +44,6 @@ typedef struct ad_int_result ad_int_result_t;
}; \
return result

ad_packet_t make_ad_packet(uint8_t hdr, uint8_t data);

// TODO: write documentation
ad_socket_result_t make_ad_socket(uint16_t recv_port, uint16_t send_port);
Expand All @@ -66,8 +65,7 @@ ad_int_result_t set_socket_nonblocking(int sock_fd);
// Send packet on the network through sock_fd
// Either returns an error string or the number of bytes sent.
// IMPORTANT: must have previously called set_send_thread from curr thread.
ad_int_result_t send_ad_packet(ad_socket_t socket, ad_packet_t packet);
ad_int_result_t send_ad_latlng_packet(ad_socket_t socket, ad_latlng_packet_t packet);
ad_int_result_t send_ad_packet(ad_socket_t socket, packet_t packet);

// Receive packet and place into buf, which is of length buf_len.
// Either returns an error string or the number of bytes read.
Expand Down
12 changes: 6 additions & 6 deletions include/network/mock/packet_queue.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef INCLUDE_NETWORK_MOCK_PACKET_QUEUE_H_
#define INCLUDE_NETWORK_MOCK_PACKET_QUEUE_H_

#include "airdrop/packet.h"

#include <stdlib.h>
#include <semaphore.h>

#include "udp_squared/protocol.h"

/*
* Implementation of a thread-safe queue for the mock airdrop sockets code.
* Essentially, the mock sockets are using these queues in the background to
Expand All @@ -15,7 +15,7 @@
#define MAX_PACKETS 100

typedef struct packet_queue {
ad_packet_t _arr[MAX_PACKETS];
packet_t _arr[MAX_PACKETS];
size_t _front; // index of first element in the queue
size_t _back; // index of next element in the queue
size_t _num_elems;
Expand All @@ -35,13 +35,13 @@ int pqueue_empty(packet_queue_t* queue);
int pqueue_full(packet_queue_t* queue);

// precondition: queue is not empty
void pqueue_push(packet_queue_t* queue, ad_packet_t packet);
void pqueue_push(packet_queue_t* queue, packet_t packet);

// remove front packet from queue and return
ad_packet_t pqueue_pop(packet_queue_t* queue);
packet_t pqueue_pop(packet_queue_t* queue);

// remove front packet from queue, but if the queue is empty
// then wait until there is something to pop
ad_packet_t pqueue_wait_pop(packet_queue_t* queue);
packet_t pqueue_wait_pop(packet_queue_t* queue);

#endif // INCLUDE_NETWORK_MOCK_PACKET_QUEUE_H_
2 changes: 1 addition & 1 deletion include/udp_squared
Submodule udp_squared updated 1 files
+5 −1 internal/enum.h
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ add_library(obcpp_protos STATIC
${PROJECT_SOURCE_DIR}/build/gen_protos/protos/obc.pb.cc
)

add_library(obcpp_udp2 STATIC
${PROJECT_SOURCE_DIR}/include/udp_squared/internal/helper.c
)

add_subdirectory(camera)
add_subdirectory(core)
add_subdirectory(cv)
Expand All @@ -20,6 +24,7 @@ target_link_libraries(obcpp_lib INTERFACE
obcpp_pathing
obcpp_ticks
obcpp_utilities
obcpp_udp2
)

# for use in unit tests
Expand All @@ -33,6 +38,7 @@ target_link_libraries(obcpp_lib_mock INTERFACE
obcpp_pathing
obcpp_ticks
obcpp_utilities
obcpp_udp2
)

add_executable(${PROJECT_NAME} main.cpp)
Expand Down
2 changes: 1 addition & 1 deletion src/core/obc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void OBC::connectAirdrop() {
ad_socket_result_t result;
while (true) {
LOG_F(INFO, "Attempting to create airdrop socket.");
result = make_ad_socket(AD_OBC_PORT, AD_PAYLOAD_PORT);
result = make_ad_socket(UDP2_OBC_PORT, UDP2_PAYLOAD_PORT);
if (!result.is_err) {
LOG_F(INFO, "Established airdrop socket.");
break;
Expand Down
Loading

0 comments on commit 4fd7b0a

Please sign in to comment.