rogue
Loading...
Searching...
No Matches
Application.cpp
Go to the documentation of this file.
1
17#include "rogue/Directives.h"
18
20
21#include <memory>
22
23#include "rogue/GeneralError.h"
24#include "rogue/GilRelease.h"
25#include "rogue/Logging.h"
29
32
33#ifndef NO_PYTHON
34 #include <boost/python.hpp>
35namespace bp = boost::python;
36#endif
37
39rpp::ApplicationPtr rpp::Application::create(uint8_t id) {
40 rpp::ApplicationPtr r = std::make_shared<rpp::Application>(id);
41 return (r);
42}
43
44void rpp::Application::setup_python() {
45#ifndef NO_PYTHON
46
47 bp::class_<rpp::Application, rpp::ApplicationPtr, bp::bases<ris::Master, ris::Slave>, boost::noncopyable>(
48 "Application",
49 bp::init<uint8_t>());
50
51 bp::implicitly_convertible<rpp::ApplicationPtr, ris::MasterPtr>();
52 bp::implicitly_convertible<rpp::ApplicationPtr, ris::SlavePtr>();
53#endif
54}
55
57rpp::Application::Application(uint8_t id) {
58 id_ = id;
59 queue_.setMax(8);
60}
61
63rpp::Application::~Application() {
64 // No-op if setController() never ran.
65 if (thread_) {
66 threadEn_ = false;
68 queue_.stop();
69 thread_->join();
70 }
71}
72
74void rpp::Application::setController(rpp::ControllerPtr cntl) {
75 // One-shot: re-assigning a joinable std::thread triggers std::terminate.
76 if (thread_) return;
77
78 // threadEn_ must precede thread ctor; rollback both on spawn failure.
79 rpp::ControllerPtr prevCntl = cntl_;
80 cntl_ = cntl;
81 threadEn_ = true;
82 try {
83 thread_ = std::make_unique<std::thread>(&rpp::Application::runThread, this);
84 } catch (...) {
85 threadEn_ = false;
86 cntl_ = prevCntl;
87 throw;
88 }
89
90 // Set a thread name
91#ifndef __MACH__
92 pthread_setname_np(thread_->native_handle(), "PackApp");
93#endif
94}
95
97ris::FramePtr rpp::Application::acceptReq(uint32_t size, bool zeroCopyEn) {
98 return (cntl_->reqFrame(size));
99}
100
102void rpp::Application::acceptFrame(ris::FramePtr frame) {
103 cntl_->applicationRx(frame, id_);
104}
105
107void rpp::Application::pushFrame(ris::FramePtr frame) {
108 queue_.push(frame);
109}
110
112void rpp::Application::runThread() {
113 ris::FramePtr frame;
114 Logging log("packetizer.Application");
115 log.logThreadId();
116
117 while (threadEn_) {
118 if ((frame = queue_.pop()) != NULL) sendFrame(frame);
119 }
120}
RAII helper that releases the Python GIL for a scope.
Definition GilRelease.h:36
Structured Rogue logging helper.
Definition Logging.h:59
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::protocols::packetizer::Application > ApplicationPtr
std::shared_ptr< rogue::protocols::packetizer::Controller > ControllerPtr
Definition Controller.h:141