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

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

Added: addParameters() method in ComponentBase using Boost.Program_Options.
Each component can declare its parameters and they will be read automatically before configureComponent() method.
See example ProducerConsumerExample constructors.

File size: 2.6 KB
Line 
1#include "ProducerExample.h"
2
3#include <fstream>
4#include <Pacpus/kernel/ComponentFactory.h>
5#include <Pacpus/kernel/ComponentManager.h>
6#include <Pacpus/kernel/InputOutputInterface.h>
7#include <Pacpus/kernel/Log.h>
8#include <QColor>
9
10using namespace pacpus;
11using namespace std;
12
13DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ProducerExample");
14
15/// Construct the factory
16static ComponentFactory<ProducerExample> sFactory("ProducerExample");
17
18static const char * outputFileName = "producer.txt";
19
20ProducerExample::ProducerExample(QString name)
21 : ComponentBase(name)
22{
23 LOG_TRACE("constructor(" << name << ")");
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 LOG_TRACE("destructor");
35}
36
37ComponentBase::COMPONENT_CONFIGURATION
38ProducerExample::configureComponent(XmlComponentConfig /*config*/)
39{
40 PACPUS_LOG_FUNCTION();
41
42 LOG_INFO("component '" << getName() << "' configured");
43 return ComponentBase::CONFIGURED_OK;
44}
45
46void ProducerExample::addInputs()
47{
48 // empty: no inputs
49}
50
51void ProducerExample::addOutputs()
52{
53 addOutput<QImage, ProducerExample>("image");
54}
55
56void ProducerExample::startActivity()
57{
58 LOG_TRACE(Q_FUNC_INFO);
59
60 //Q_ASSERT(input);
61
62 start();
63 setActive(true);
64 setState(MONITOR_OK);
65 LOG_INFO("started component '" << getName() << "'");
66}
67
68void ProducerExample::stopActivity()
69{
70 LOG_TRACE(Q_FUNC_INFO);
71
72 setActive(false);
73 setState(STOPPED);
74 LOG_INFO("stopped component '" << getName() << "'");
75}
76
77void ProducerExample::run()
78{
79 unsigned int counter = 1;
80 int waitTimeMicros = 150 * 1000;
81
82 std::fstream file(outputFileName, std::ios_base::out | std::ios_base::app);
83 if (!file.is_open()) {
84 LOG_ERROR("file '" << outputFileName << "'cannot be opened");
85 }
86
87 QImage mat(10000, 1000, QImage::Format_RGB32);
88 //mat.fill( qRgb(189, 149, 39));
89
90 OutputInterface<QImage, ProducerExample> * imageOutput =
91 getTypedOutput<QImage, ProducerExample>("image");
92
93 while (isActive()) {
94 //mat.setPixel(0,0,i);
95 LOG_INFO("Sent QImage: "
96 << "size = " << mat.size().width()<< " x " << mat.size().height()
97 );
98
99 if (imageOutput && imageOutput->hasConnection()) {
100 imageOutput->send(mat);
101 }
102
103 LOG_INFO("Sent data=" << counter << ", time=" << road_time());
104 file << counter << " " << road_time() << "\n" << std::flush;
105
106 usleep(waitTimeMicros);
107 ++counter;
108 setState(MONITOR_OK);
109 }
110
111 file.close();
112}
Note: See TracBrowser for help on using the repository browser.