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

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

ComponentBase: added addParameter.

File size: 2.7 KB
RevLine 
[163]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
[291]8#include <boost/bind.hpp>
9#include <boost/thread.hpp>
10
[163]11using namespace pacpus;
12using namespace std;
13
14DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ProducerExample");
15
[288]16PACPUS_REGISTER_COMPONENT(ProducerExample);
[163]17
18ProducerExample::ProducerExample(QString name)
19 : ComponentBase(name)
[291]20 , mInterval(boost::posix_time::milliseconds(150))
21 , mTimer(mIo, mInterval)
[163]22{
[291]23 PACPUS_LOG_FUNCTION();
[176]24
[312]25 addParameter("output-path", value<std::string>(&mOutputFileName)->default_value("producer.txt"), "set output file path");
[163]26}
27
28ProducerExample::~ProducerExample()
29{
[291]30 PACPUS_LOG_FUNCTION();
[163]31}
32
[291]33ComponentBase::COMPONENT_CONFIGURATION ProducerExample::configureComponent(XmlComponentConfig /*config*/)
[163]34{
[176]35 PACPUS_LOG_FUNCTION();
[163]36
[176]37 LOG_INFO("component '" << getName() << "' configured");
[163]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{
[291]53 PACPUS_LOG_FUNCTION();
[163]54
[291]55 using boost::bind;
56 using boost::ref;
57
[301]58 std::fstream mFile(mOutputFileName.c_str(), std::ios_base::out | std::ios_base::app);
[291]59 if (!mFile.is_open()) {
[293]60 LOG_ERROR("file '" << mOutputFileName << "' cannot be opened");
[291]61 return;
62 }
63
64 mImageOutput = getTypedOutput<QImage, ProducerExample>("image");
65
[163]66 setActive(true);
67 setState(MONITOR_OK);
[176]68 LOG_INFO("started component '" << getName() << "'");
[291]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();
[163]74}
75
76void ProducerExample::stopActivity()
77{
[291]78 PACPUS_LOG_FUNCTION();
[163]79
80 setActive(false);
81 setState(STOPPED);
[176]82 LOG_INFO("stopped component '" << getName() << "'");
[291]83
84 mFile.close();
[163]85}
86
[291]87void ProducerExample::produce(int& counter)
[163]88{
[291]89 if (!isActive()) {
90 return;
[163]91 }
[291]92
93 QImage mat(10, 10, QImage::Format_RGB32);
[163]94 //mat.fill( qRgb(189, 149, 39));
[291]95 //mat.setPixel(0,0,i);
96 LOG_INFO("Sending QImage: "
97 << "size = " << mat.size().width()<< " x " << mat.size().height()
98 );
[163]99
[291]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)));
[163]110}
Note: See TracBrowser for help on using the repository browser.