rogue
Loading...
Searching...
No Matches
Slave.cpp
Go to the documentation of this file.
1
19#include "rogue/Directives.h"
20
22
23#include <inttypes.h>
24#include <unistd.h>
25
26#include <cstdio>
27#include <cstring>
28#include <memory>
29#include <string>
30
31#include "rogue/GeneralError.h"
32#include "rogue/GilRelease.h"
33#include "rogue/Logging.h"
34#include "rogue/ScopedGil.h"
40
42
43#ifndef NO_PYTHON
44 #include <boost/python.hpp>
45namespace bp = boost::python;
46#endif
47
49ris::SlavePtr ris::Slave::create() {
50 ris::SlavePtr slv = std::make_shared<ris::Slave>();
51 return (slv);
52}
53
55ris::Slave::Slave() {
56 debug_ = 0;
57 frameCount_ = 0;
58 frameBytes_ = 0;
59}
60
62ris::Slave::~Slave() {}
63
64void ris::Slave::stop() {}
65
67void ris::Slave::setDebug(uint32_t debug, std::string name) {
68 debug_ = debug;
69 log_ = rogue::Logging::create(name.c_str());
70}
71
73void ris::Slave::acceptFrame(ris::FramePtr frame) {
75
76 uint32_t count;
77 uint8_t val;
78
80 ris::FrameLockPtr lock = frame->lock();
81
82 frameCount_++;
83 frameBytes_ += frame->getPayload();
84
85 if (debug_ > 0) {
86 char buffer[1000];
87
88 log_->critical("Got Size=%" PRIu32 ", Error=%" PRIu8 ", Data:", frame->getPayload(), frame->getError());
89 snprintf(buffer, sizeof(buffer), " ");
90
91 count = 0;
92 for (it = frame->begin(); (count < debug_) && (it != frame->end()); ++it) {
93 count++;
94 val = *it;
95
96 snprintf(buffer + strlen(buffer), 1000 - strlen(buffer), " 0x%.2x", val);
97 if (((count + 1) % 8) == 0) {
98 log_->critical(buffer);
99 snprintf(buffer, sizeof(buffer), " ");
100 }
101 }
102
103 if (strlen(buffer) > 5) log_->log(100, buffer);
104 }
105}
106
107#ifndef NO_PYTHON
108
110void ris::SlaveWrap::acceptFrame(ris::FramePtr frame) {
111 // A C++ worker (Fifo, TcpCore, ...) can still deliver a frame while the
112 // interpreter shuts down. Py_IsInitialized() goes false when finalization
113 // starts, well before the interpreter state that PyGILState_Ensure() needs
114 // is torn down, so skip the Python override from then on.
115 if (Py_IsInitialized()) {
117
118 if (boost::python::override pb = this->get_override("_acceptFrame")) {
119 try {
120 pb(frame);
121 return;
122 } catch (...) {
123 PyErr_Print();
124 }
125 }
126 }
127 ris::Slave::acceptFrame(frame);
128}
129
131void ris::SlaveWrap::defAcceptFrame(ris::FramePtr frame) {
132 ris::Slave::acceptFrame(frame);
133}
134
135#endif
136
138uint64_t ris::Slave::getFrameCount() {
139 return (frameCount_);
140}
141
143uint64_t ris::Slave::getByteCount() {
144 return (frameBytes_);
145}
146
147// Ensure passed frame is a single buffer
148bool ris::Slave::ensureSingleBuffer(ris::FramePtr& frame, bool reqEn) {
149 // Frame is a single buffer
150 if (frame->bufferCount() == 1) {
151 return true;
152
153 } else if (!reqEn) {
154 return false;
155
156 } else {
157 uint32_t size = frame->getPayload();
158 ris::FramePtr nFrame = reqLocalFrame(size, true);
159
160 if (nFrame->bufferCount() != 1) {
161 return false;
162
163 } else {
164 nFrame->setPayload(size);
165
166 ris::FrameIterator srcIter = frame->begin();
167 ris::FrameIterator dstIter = nFrame->begin();
168
169 ris::copyFrame(srcIter, size, dstIter);
170 frame = nFrame;
171 return true;
172 }
173 }
174}
175
176// Process a local frame request
177ris::FramePtr ris::Slave::reqLocalFrame(uint32_t size, bool zeroCopyEn) {
178 return ris::Pool::acceptReq(size, zeroCopyEn);
179}
180
181void ris::Slave::setup_python() {
182#ifndef NO_PYTHON
183
184 bp::class_<ris::SlaveWrap, ris::SlaveWrapPtr, boost::noncopyable>("Slave", bp::init<>())
185 .def("setDebug", &ris::Slave::setDebug)
186 .def("_acceptFrame", &ris::Slave::acceptFrame, &ris::SlaveWrap::defAcceptFrame)
187 .def("getFrameCount", &ris::Slave::getFrameCount)
188 .def("getByteCount", &ris::Slave::getByteCount)
189 .def("_stop", &ris::Slave::stop)
190 .def("getAllocCount", &ris::Pool::getAllocCount)
191 .def("getAllocBytes", &ris::Pool::getAllocBytes)
192 .def("setFixedSize", &ris::Pool::setFixedSize)
193 .def("getFixedSize", &ris::Pool::getFixedSize)
194 .def("setPoolSize", &ris::Pool::setPoolSize)
195 .def("getPoolSize", &ris::Pool::getPoolSize)
196 .def("__lshift__", &ris::Slave::lshiftPy);
197
198 bp::implicitly_convertible<ris::SlavePtr, ris::PoolPtr>();
199#endif
200}
201
202#ifndef NO_PYTHON
203
204// Support << operator in python
205bp::object ris::Slave::lshiftPy(bp::object p) {
206 ris::MasterPtr mst;
207
208 // First Attempt to access object as a stream master
209 boost::python::extract<ris::MasterPtr> get_master(p);
210
211 // Test extraction
212 if (get_master.check()) {
213 mst = get_master();
214
215 // Otherwise look for indirect call
216 } else if (PyObject_HasAttrString(p.ptr(), "_getStreamMaster")) {
217 // Attempt to convert returned object to master pointer
218 boost::python::extract<ris::MasterPtr> get_master(p.attr("_getStreamMaster")());
219
220 // Test extraction
221 if (get_master.check()) mst = get_master();
222 }
223
224 if (mst != NULL)
226 else
227 throw(rogue::GeneralError::create("stream::Slave::lshiftPy",
228 "Attempt to use << with incompatable stream master"));
229
230 return p;
231}
232
233#endif
234
236ris::MasterPtr& ris::Slave::operator<<(ris::MasterPtr& other) {
238 return other;
239}
Typed shared-from-this helper for Rogue classes.
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
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
RAII helper that acquires the Python GIL for a scope.
Definition ScopedGil.h:35
Random-access byte iterator across a Frame payload.
std::shared_ptr< rogue::interfaces::stream::Master > MasterPtr
Shared pointer alias for Master.
Definition Master.h:230
std::shared_ptr< rogue::interfaces::stream::Slave > SlavePtr
Shared pointer alias for Slave.
Definition Slave.h:254
static void copyFrame(rogue::interfaces::stream::FrameIterator &srcIter, uint32_t size, rogue::interfaces::stream::FrameIterator &dstIter)
Copies bytes between frame iterators.
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::interfaces::stream::FrameLock > FrameLockPtr
Shared pointer alias for FrameLock.
Definition FrameLock.h:110