source: pacpusframework/branches/2.0-beta1/examples/ProducerConsumerExample/ConsumerExample.cpp@ 163

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

Added: test component from PacpusCityVIP.
Added: PACPUS_BUILD_EXAMPLES variable.

File size: 2.1 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
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 LOG_TRACE(Q_FUNC_INFO);
43
44 // load XML parameters
45
46 LOG_INFO("component '" << name() << "' configured");
47 return ComponentBase::CONFIGURED_OK;
48}
49
50void ConsumerExample::startActivity()
51{
52 LOG_TRACE(Q_FUNC_INFO);
53 moveToThread(&thread);
54
55 m_counter = 0;
56 static const char * outputFileName = "test2.txt";
57 m_file.open(outputFileName, std::ios_base::out | std::ios_base::app);
58 if (!m_file.is_open()) {
59 LOG_ERROR("file '" << outputFileName << "'cannot be opened");
60 }
61
62 thread.start();
63 setState(MONITOR_OK);
64 LOG_INFO("started component '" << name() << "'");
65}
66
67void ConsumerExample::stopActivity()
68{
69 LOG_TRACE(Q_FUNC_INFO);
70
71 QMetaObject::invokeMethod(&thread, "quit");
72 m_file.close();
73 setState(STOPPED);
74 LOG_INFO("stopped component '" << name() << "'");
75}
76
77void ConsumerExample::process(const QImage& matrix)
78{
79 LOG_INFO("Size " << matrix.size().width()<< " x " << matrix.size().height());
80 unsigned int k = 0;
81
82 // a process
83 for (int i = 0; i < 100; ++i) {
84 ++k;
85 }
86 m_file << ++m_counter << " " << road_time() << "\n";
87
88 setState(MONITOR_OK);
89}
Note: See TracBrowser for help on using the repository browser.