source: pacpusframework/trunk/examples/ProducerConsumerExample/ConsumerExample.cpp@ 256

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

Major: cleaned connection interfaces.

File size: 2.5 KB
RevLine 
[163]1#include "ConsumerExample.h"
2
3#include <Pacpus/kernel/ComponentFactory.h>
4#include <Pacpus/kernel/Log.h>
5#include <Pacpus/kernel/InputOutputInterface.h>
6
7using namespace pacpus;
8using namespace std;
9
10DECLARE_STATIC_LOGGER("pacpus.cityvip.test.ConsumerExample");
11
12/// Construct the factory
13static ComponentFactory<ConsumerExample> sFactory("ConsumerExample");
14
15ConsumerExample::ConsumerExample(QString name)
16 : ComponentBase(name)
17{
18 LOG_TRACE("constructor(" << name << ")");
19
20 LOG_INFO("Thread " << thread.currentThread());
21 LOG_INFO("Current Thread " << QThread::currentThread());
[176]22
23 namespace po = boost::program_options;
24 addParameters()
25 ("output-path", po::value<std::string>(&mOutputFileName)->default_value("consumer.txt"), "set output file path")
26 ;
[163]27}
28
29ConsumerExample::~ConsumerExample()
30{
31 LOG_TRACE("destructor");
32}
33
34void ConsumerExample::addInputs()
35{
36 addInput<QImage, ConsumerExample>("image", &ConsumerExample::process);
37}
38
39void ConsumerExample::addOutputs()
40{
41 // empty: no outputs
42}
43
44ComponentBase::COMPONENT_CONFIGURATION
45ConsumerExample::configureComponent(XmlComponentConfig /*config*/)
46{
[176]47 PACPUS_LOG_FUNCTION();
[163]48
[176]49 // load XML parameters -- NOT NEEDED - loaded by boost::program_options (used addParameters())
[163]50
[176]51 LOG_INFO("component '" << getName() << "' configured");
[163]52 return ComponentBase::CONFIGURED_OK;
53}
54
55void ConsumerExample::startActivity()
56{
57 LOG_TRACE(Q_FUNC_INFO);
58 moveToThread(&thread);
[165]59 m_counter = 0;
[163]60
[176]61 m_file.open(mOutputFileName.c_str(), std::ios_base::out | std::ios_base::app);
[163]62 if (!m_file.is_open()) {
[176]63 LOG_ERROR("file '" << mOutputFileName << "' cannot be opened");
[163]64 }
65
66 thread.start();
67 setState(MONITOR_OK);
[176]68 LOG_INFO("started component '" << getName() << "'");
[163]69}
70
71void ConsumerExample::stopActivity()
72{
73 LOG_TRACE(Q_FUNC_INFO);
74
75 QMetaObject::invokeMethod(&thread, "quit");
76 m_file.close();
77 setState(STOPPED);
[176]78 LOG_INFO("stopped component '" << getName() << "'");
[163]79}
80
81void ConsumerExample::process(const QImage& matrix)
82{
[165]83 m_file << ++m_counter << " " << road_time() << "\n" << std::flush;
[163]84
[165]85 LOG_INFO("Received QIMage: "
86 << "size = " << matrix.size().width()<< " x " << matrix.size().height()
87 );
[163]88
[165]89 // DO PROCESSING
90
[163]91 setState(MONITOR_OK);
92}
[206]93
94void testPacpusTypedEventConverts()
95{
96 PacpusTypedEvent<float> floatEvent(TYPED_EVENT, 1.0f);
97 PacpusTypedEvent<double> doubleEvent = floatEvent;
98
99 PacpusTypedEvent<int> intEvent(TYPED_EVENT, 2);
100 //PacpusTypedEvent<QImage> imageEvent = intEvent;
101}
Note: See TracBrowser for help on using the repository browser.