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

Last change on this file since 300 was 293, checked in by Marek Kurdej, 11 years ago

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