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

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

Added: addParameters() method in ComponentBase using Boost.Program_Options.
Each component can declare its parameters and they will be read automatically before configureComponent() method.
See example ProducerConsumerExample constructors.

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