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

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

Added: OutputInterface::checkedSend.
Added: ComponentManager: prints InputsMap, OutputsMap.

File size: 2.5 KB
RevLine 
[163]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 << ")");
[176]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 ;
[163]29}
30
31ProducerExample::~ProducerExample()
32{
33 LOG_TRACE("destructor");
34}
35
36ComponentBase::COMPONENT_CONFIGURATION
37ProducerExample::configureComponent(XmlComponentConfig /*config*/)
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{
57 LOG_TRACE(Q_FUNC_INFO);
58
59 start();
60 setActive(true);
61 setState(MONITOR_OK);
[176]62 LOG_INFO("started component '" << getName() << "'");
[163]63}
64
65void ProducerExample::stopActivity()
66{
67 LOG_TRACE(Q_FUNC_INFO);
68
69 setActive(false);
70 setState(STOPPED);
[176]71 LOG_INFO("stopped component '" << getName() << "'");
[163]72}
73
74void ProducerExample::run()
75{
[165]76 unsigned int counter = 1;
77 int waitTimeMicros = 150 * 1000;
[163]78
79 std::fstream file(outputFileName, std::ios_base::out | std::ios_base::app);
80 if (!file.is_open()) {
[177]81 LOG_ERROR("file '" << outputFileName << "' cannot be opened");
[163]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);
[165]92 LOG_INFO("Sent QImage: "
93 << "size = " << mat.size().width()<< " x " << mat.size().height()
94 );
[163]95
[202]96 checkedSend(imageOutput, mat);
[163]97
[165]98 LOG_INFO("Sent data=" << counter << ", time=" << road_time());
99 file << counter << " " << road_time() << "\n" << std::flush;
[163]100
[165]101 usleep(waitTimeMicros);
[163]102 ++counter;
103 setState(MONITOR_OK);
104 }
105
106 file.close();
107}
Note: See TracBrowser for help on using the repository browser.