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

Last change on this file since 291 was 291, checked in by Marek Kurdej, 10 years ago

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

File size: 2.3 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
64void ConsumerExample::stopActivity()
65{
66 LOG_TRACE(Q_FUNC_INFO);
67
68 m_file.close();
69 setState(STOPPED);
70 LOG_INFO("stopped component '" << getName() << "'");
71}
72
73void ConsumerExample::process(const QImage& matrix)
74{
75 m_file << ++m_counter << " " << road_time() << "\n" << std::flush;
76
77 LOG_INFO("Received QIMage: "
78 << "size = " << matrix.size().width()<< " x " << matrix.size().height()
79 );
80
81 // DO PROCESSING
82
83 setState(MONITOR_OK);
84}
85
86void testPacpusTypedEventConverts()
87{
88 PacpusTypedEvent<float> floatEvent(TYPED_EVENT, 1.0f);
89 PacpusTypedEvent<double> doubleEvent = floatEvent;
90
91 PacpusTypedEvent<int> intEvent(TYPED_EVENT, 2);
92 //PacpusTypedEvent<QImage> imageEvent = intEvent; // should not compile
93}
Note: See TracBrowser for help on using the repository browser.