-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5863c98
commit a40daf3
Showing
8 changed files
with
218 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2022-2024 Transit Live Mapping Solutions | ||
* All rights reserved. | ||
* | ||
* Authors: | ||
* Marenz Schmidl | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "l2/logical_link_control_packet.hpp" | ||
#include "l2/slot.hpp" | ||
#include "thread_safe_fifo.hpp" | ||
#include <atomic> | ||
#include <thread> | ||
#include <variant> | ||
|
||
class BorzoiSender { | ||
public: | ||
BorzoiSender() = delete; | ||
|
||
/// This class sends the HTTPS Post requests to borzoi. https://github.com/tlm-solutions/borzoi | ||
/// \param queue the queue holds either the parsed packets (std::unique_ptr<LogicalLinkControlPacket>) or Slots that | ||
/// failed to decode | ||
/// \param termination_flag this flag is set when the sender should terminate after finishing all work | ||
BorzoiSender(ThreadSafeFifo<std::variant<std::unique_ptr<LogicalLinkControlPacket>, Slots>>& queue, | ||
std::atomic_bool& termination_flag, unsigned borzoi_port); | ||
|
||
~BorzoiSender(); | ||
|
||
private: | ||
/// The thread function for continously process incomming parsed packets or failed slots. | ||
auto worker() -> void; | ||
|
||
/// The input queue | ||
ThreadSafeFifo<std::variant<std::unique_ptr<LogicalLinkControlPacket>, Slots>>& queue_; | ||
|
||
/// The flag that is set when terminating the program | ||
std::atomic_bool& termination_flag_; | ||
|
||
/// The worker thread | ||
std::thread worker_thread_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (C) 2024 Transit Live Mapping Solutions | ||
* All rights reserved. | ||
* | ||
* Authors: | ||
* Marenz Schmidl | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <deque> | ||
#include <mutex> | ||
#include <optional> | ||
#include <utility> | ||
|
||
template <typename T> class ThreadSafeFifo { | ||
public: | ||
using OptionalT = std::optional<T>; | ||
|
||
ThreadSafeFifo() = default; | ||
~ThreadSafeFifo() = default; | ||
|
||
ThreadSafeFifo(const ThreadSafeFifo&) = delete; | ||
auto operator=(const ThreadSafeFifo&) -> ThreadSafeFifo& = delete; | ||
|
||
ThreadSafeFifo(ThreadSafeFifo&&) = delete; | ||
auto operator=(ThreadSafeFifo&&) -> ThreadSafeFifo& = delete; | ||
|
||
// get a finished item of a nullopt | ||
auto get_or_null() -> OptionalT { | ||
using namespace std::chrono_literals; | ||
|
||
OptionalT result; | ||
|
||
{ | ||
std::lock_guard<std::mutex> lk(mutex_); | ||
if (!queue_.empty()) { | ||
result = std::forward<T>(queue_.front()); | ||
queue_.pop_front(); | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
auto empty() -> bool { | ||
std::lock_guard<std::mutex> lk(mutex_); | ||
return queue_.empty(); | ||
}; | ||
|
||
auto push_back(T&& element) -> void { | ||
std::lock_guard<std::mutex> lk(mutex_); | ||
queue_.push_back(std::forward<T>(element)); | ||
}; | ||
|
||
private: | ||
/// the mutex that is used to access the queue. | ||
std::mutex mutex_; | ||
|
||
/// the wrapped queue | ||
std::deque<T> queue_; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright (C) 2022-2024 Transit Live Mapping Solutions | ||
* All rights reserved. | ||
* | ||
* Authors: | ||
* Marenz Schmidl | ||
*/ | ||
|
||
#include "borzoi_sender.hpp" | ||
#include "l3/circuit_mode_control_entity_packet.hpp" | ||
#include "l3/mobile_link_entity_packet.hpp" | ||
#include "l3/short_data_service_packet.hpp" | ||
|
||
#if defined(__linux__) | ||
#include <pthread.h> | ||
#endif | ||
|
||
BorzoiSender::BorzoiSender(ThreadSafeFifo<std::variant<std::unique_ptr<LogicalLinkControlPacket>, Slots>>& queue, | ||
std::atomic_bool& termination_flag, unsigned borzoi_port) | ||
: queue_(queue) | ||
, termination_flag_(termination_flag) { | ||
worker_thread_ = std::thread(&BorzoiSender::worker, this); | ||
|
||
#if defined(__linux__) | ||
auto handle = worker_thread_.native_handle(); | ||
pthread_setname_np(handle, "BorzoiSender"); | ||
#endif | ||
} | ||
|
||
BorzoiSender::~BorzoiSender() { worker_thread_.join(); } | ||
|
||
void BorzoiSender::worker() { | ||
for (;;) { | ||
const auto return_value = queue_.get_or_null(); | ||
|
||
if (!return_value) { | ||
if (termination_flag_.load() && queue_.empty()) { | ||
break; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
std::visit( | ||
[](auto&& arg) { | ||
using T = std::decay_t<decltype(arg)>; | ||
if constexpr (std::is_same_v<T, std::unique_ptr<LogicalLinkControlPacket>>) { | ||
/// process the parsed packet | ||
if (auto* llc = dynamic_cast<LogicalLinkControlPacket*>(arg.get())) { | ||
if (llc->basic_link_information_ && | ||
(llc->basic_link_information_->basic_link_type_ == BasicLinkType::kBlAckWithoutFcs || | ||
llc->basic_link_information_->basic_link_type_ == BasicLinkType::kBlAckWithFcs)) { | ||
return; | ||
} | ||
std::cout << *llc; | ||
if (auto* mle = dynamic_cast<MobileLinkEntityPacket*>(llc)) { | ||
std::cout << *mle; | ||
if (auto* cmce = dynamic_cast<CircuitModeControlEntityPacket*>(llc)) { | ||
std::cout << *cmce; | ||
if (auto* sds = dynamic_cast<ShortDataServicePacket*>(llc)) { | ||
std::cout << *sds; | ||
} | ||
} | ||
std::cout << std::endl; | ||
} | ||
} | ||
} else if constexpr (std::is_same_v<T, Slots>) { | ||
/// send out the slots which had an error while parsing | ||
} | ||
}, | ||
*return_value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters