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
30namespace rpr = rogue::protocols::rssi;
32
33#ifndef NO_PYTHON
34 #include <boost/python.hpp>
35namespace bp = boost::python;
36#endif
37
39rpr::ApplicationPtr rpr::Application::create() {
40 rpr::ApplicationPtr r = std::make_shared<rpr::Application>();
41 return (r);
42}
43
44void rpr::Application::setup_python() {
45#ifndef NO_PYTHON
46
47 bp::class_<rpr::Application, rpr::ApplicationPtr, bp::bases<ris::Master, ris::Slave>, boost::noncopyable>(
48 "Application",
49 bp::init<>());
50
51 bp::implicitly_convertible<rpr::ApplicationPtr, ris::MasterPtr>();
52 bp::implicitly_convertible<rpr::ApplicationPtr, ris::SlavePtr>();
53#endif
54}
55
57rpr::Application::Application() {}
58
60rpr::Application::~Application() {
61 stop();
62}
63
65void rpr::Application::stop() {
66 // No-op on a repeat call, or if setController() never ran.
67 if (threadEn_.exchange(false) && thread_ != nullptr) {
68 // Release the GIL: worker may be mid-sendFrame to a Python slave.
70 // Expired means the worker cannot be blocked in applicationTx(), since
71 // it holds a strong reference for the duration of that call.
72 rpr::ControllerPtr cntl = cntl_.lock();
73 if (cntl) cntl->stopQueue();
74 thread_->join();
75 delete thread_;
76 thread_ = nullptr;
77 }
78}
79
81void rpr::Application::setController(rpr::ControllerPtr cntl) {
82 cntl_ = cntl;
83
84 // Start read thread
85 threadEn_ = true;
86 thread_ = new std::thread(&rpr::Application::runThread, this);
87
88 // Set a thread name
89#ifndef __MACH__
90 pthread_setname_np(thread_->native_handle(), "RssiApp");
91#endif
92}
93
95ris::FramePtr rpr::Application::acceptReq(uint32_t size, bool zeroCopyEn) {
96 rpr::ControllerPtr cntl = cntl_.lock();
97 if (!cntl) throw(rogue::GeneralError::create("rssi::Application::acceptReq", "RSSI controller has been released"));
98 return (cntl->reqFrame(size));
99}
100
102void rpr::Application::acceptFrame(ris::FramePtr frame) {
103 rpr::ControllerPtr cntl = cntl_.lock();
104 if (cntl) cntl->applicationRx(frame);
105}
106
108void rpr::Application::runThread() {
109 ris::FramePtr frame;
110 Logging log("rssi.Application");
111 log.logThreadId();
112
113 while (threadEn_) {
114 rpr::ControllerPtr cntl = cntl_.lock();
115 if (!cntl) break;
116 frame = cntl->applicationTx();
117 cntl.reset();
118 if (frame) sendFrame(frame);
119 }
120}
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
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