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

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

ComponentBase: added addParameter.

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