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

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

Update: minor.

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
9using namespace pacpus;
10using namespace std;
11
12DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ProducerExample");
13
14/// Construct the factory
15static ComponentFactory<ProducerExample> sFactory("ProducerExample");
16
17static const char * outputFileName = "producer.txt";
18
19ProducerExample::ProducerExample(QString name)
20 : ComponentBase(name)
21{
22 LOG_TRACE("constructor(" << name << ")");
23
24 namespace po = boost::program_options;
25
26 addParameters()
27 ("output-path", po::value<std::string>(&mOutputFileName)->default_value("producer.txt"), "set output file path")
28 ;
29}
30
31ProducerExample::~ProducerExample()
32{
33 LOG_TRACE("destructor");
34}
35
36ComponentBase::COMPONENT_CONFIGURATION
37ProducerExample::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 LOG_TRACE(Q_FUNC_INFO);
58
59 start();
60 setActive(true);
61 setState(MONITOR_OK);
62 LOG_INFO("started component '" << getName() << "'");
63}
64
65void ProducerExample::stopActivity()
66{
67 LOG_TRACE(Q_FUNC_INFO);
68
69 setActive(false);
70 setState(STOPPED);
71 LOG_INFO("stopped component '" << getName() << "'");
72}
73
74void ProducerExample::run()
75{
76 unsigned int counter = 1;
77 int waitTimeMicros = 150 * 1000;
78
79 std::fstream file(outputFileName, std::ios_base::out | std::ios_base::app);
80 if (!file.is_open()) {
81 LOG_ERROR("file '" << outputFileName << "' cannot be opened");
82 }
83
84 QImage mat(10000, 1000, QImage::Format_RGB32);
85 //mat.fill( qRgb(189, 149, 39));
86
87 OutputInterface<QImage, ProducerExample> * imageOutput =
88 getTypedOutput<QImage, ProducerExample>("image");
89
90 while (isActive()) {
91 //mat.setPixel(0,0,i);
92 LOG_INFO("Sent QImage: "
93 << "size = " << mat.size().width()<< " x " << mat.size().height()
94 );
95
96 if (imageOutput && imageOutput->hasConnection()) {
97 imageOutput->send(mat);
98 }
99
100 LOG_INFO("Sent data=" << counter << ", time=" << road_time());
101 file << counter << " " << road_time() << "\n" << std::flush;
102
103 usleep(waitTimeMicros);
104 ++counter;
105 setState(MONITOR_OK);
106 }
107
108 file.close();
109}
Note: See TracBrowser for help on using the repository browser.