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

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

Some Unix fixes

File size: 2.8 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
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 ;
[163]30}
31
32ProducerExample::~ProducerExample()
33{
[291]34 PACPUS_LOG_FUNCTION();
[163]35}
36
[291]37ComponentBase::COMPONENT_CONFIGURATION ProducerExample::configureComponent(XmlComponentConfig /*config*/)
[163]38{
[176]39 PACPUS_LOG_FUNCTION();
[163]40
[176]41 LOG_INFO("component '" << getName() << "' configured");
[163]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{
[291]57 PACPUS_LOG_FUNCTION();
[163]58
[291]59 using boost::bind;
60 using boost::ref;
61
[301]62 std::fstream mFile(mOutputFileName.c_str(), std::ios_base::out | std::ios_base::app);
[291]63 if (!mFile.is_open()) {
[293]64 LOG_ERROR("file '" << mOutputFileName << "' cannot be opened");
[291]65 return;
66 }
67
68 mImageOutput = getTypedOutput<QImage, ProducerExample>("image");
69
[163]70 setActive(true);
71 setState(MONITOR_OK);
[176]72 LOG_INFO("started component '" << getName() << "'");
[291]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();
[163]78}
79
80void ProducerExample::stopActivity()
81{
[291]82 PACPUS_LOG_FUNCTION();
[163]83
84 setActive(false);
85 setState(STOPPED);
[176]86 LOG_INFO("stopped component '" << getName() << "'");
[291]87
88 mFile.close();
[163]89}
90
[291]91void ProducerExample::produce(int& counter)
[163]92{
[291]93 if (!isActive()) {
94 return;
[163]95 }
[291]96
97 QImage mat(10, 10, QImage::Format_RGB32);
[163]98 //mat.fill( qRgb(189, 149, 39));
[291]99 //mat.setPixel(0,0,i);
100 LOG_INFO("Sending QImage: "
101 << "size = " << mat.size().width()<< " x " << mat.size().height()
102 );
[163]103
[291]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)));
[163]114}
Note: See TracBrowser for help on using the repository browser.