source: pacpusframework/trunk/examples/ProducerConsumerExample/ProducerExample.cpp@ 291

Last change on this file since 291 was 291, checked in by Marek Kurdej, 10 years ago

Reworked threading in InputInterfaceBase, each slot has its own processing thread.

File size: 2.8 KB
Line 
1#include "ProducerExample.h"
2
3#include <Pacpus/kernel/ComponentFactory.h>
4#include <Pacpus/kernel/ComponentManager.h>
5#include <Pacpus/kernel/InputOutputInterface.h>
6#include <Pacpus/kernel/Log.h>
7
8#include <boost/bind.hpp>
9#include <boost/thread.hpp>
10
11using namespace pacpus;
12using namespace std;
13
14DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ProducerExample");
15
16PACPUS_REGISTER_COMPONENT(ProducerExample);
17
18static const char * outputFileName = "producer.txt";
19
20ProducerExample::ProducerExample(QString name)
21 : ComponentBase(name)
22 , mInterval(boost::posix_time::milliseconds(150))
23 , mTimer(mIo, mInterval)
24{
25 PACPUS_LOG_FUNCTION();
26
27 namespace po = boost::program_options;
28
29 addParameters()
30 ("output-path", po::value<std::string>(&mOutputFileName)->default_value("producer.txt"), "set output file path")
31 ;
32}
33
34ProducerExample::~ProducerExample()
35{
36 PACPUS_LOG_FUNCTION();
37}
38
39ComponentBase::COMPONENT_CONFIGURATION ProducerExample::configureComponent(XmlComponentConfig /*config*/)
40{
41 PACPUS_LOG_FUNCTION();
42
43 LOG_INFO("component '" << getName() << "' configured");
44 return ComponentBase::CONFIGURED_OK;
45}
46
47void ProducerExample::addInputs()
48{
49 // empty: no inputs
50}
51
52void ProducerExample::addOutputs()
53{
54 addOutput<QImage, ProducerExample>("image");
55}
56
57void ProducerExample::startActivity()
58{
59 PACPUS_LOG_FUNCTION();
60
61 using boost::bind;
62 using boost::ref;
63
64 std::fstream mFile(outputFileName, std::ios_base::out | std::ios_base::app);
65 if (!mFile.is_open()) {
66 LOG_ERROR("file '" << outputFileName << "' cannot be opened");
67 return;
68 }
69
70 mImageOutput = getTypedOutput<QImage, ProducerExample>("image");
71
72 setActive(true);
73 setState(MONITOR_OK);
74 LOG_INFO("started component '" << getName() << "'");
75
76 int counter = 0;
77 mTimer.async_wait(bind(&ProducerExample::produce, this, ref(counter)));
78 boost::thread t(bind(&boost::asio::io_service::run, ref(mIo)));
79 //mIo.run();
80}
81
82void ProducerExample::stopActivity()
83{
84 PACPUS_LOG_FUNCTION();
85
86 setActive(false);
87 setState(STOPPED);
88 LOG_INFO("stopped component '" << getName() << "'");
89
90 mFile.close();
91}
92
93void ProducerExample::produce(int& counter)
94{
95 if (!isActive()) {
96 return;
97 }
98
99 QImage mat(10, 10, QImage::Format_RGB32);
100 //mat.fill( qRgb(189, 149, 39));
101 //mat.setPixel(0,0,i);
102 LOG_INFO("Sending QImage: "
103 << "size = " << mat.size().width()<< " x " << mat.size().height()
104 );
105
106 checkedSend(mImageOutput, mat);
107 LOG_INFO("Sent data=" << counter << ", time=" << road_time());
108 mFile << counter << " " << road_time() << "\n" << std::flush;
109 ++counter;
110 setState(MONITOR_OK);
111
112 using boost::bind;
113 using boost::ref;
114 mTimer.expires_at(mTimer.expires_at() + mInterval);
115 mTimer.async_wait(bind(&ProducerExample::produce, this, ref(counter)));
116}
Note: See TracBrowser for help on using the repository browser.