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

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

REVERTED: Reworked threading in InputInterfaceBase, each slot has its own processing thread.

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