source: pacpusframework/branches/2.0-beta1/src/TestComponents/ShMem/ShMemOutput.cpp@ 96

Last change on this file since 96 was 89, checked in by morasjul, 12 years ago

PACPUS 2.0 Beta deployed in new branch

Major changes:
-Add communication interface between components
-Add examples for communications interface (TestComponents)
-Move to Qt5 support

  • Property svn:executable set to *
File size: 3.2 KB
Line 
1#include "ShMemInterface.h"
2
3#include "kernel/ComponentFactory.h"
4#include "kernel/Log.h"
5#include "PacpusTools/ShMem.h"
6#include "Pacpus/kernel/inputOutputInterface.h"
7#include <Pacpus/kernel/PacpusEvent.h>
8
9
10using namespace std;
11using namespace pacpus;
12
13DECLARE_STATIC_LOGGER("pacpus.base.ShMemOutput");
14
15// Construct the factory
16static ComponentFactory<ShMemOutput> sFactory("ShMemOutput");
17
18void ShMemOutput::addInputOutput()
19{
20 ADD_OUTPUT("data",ShMemOutput,QByteArray);
21}
22
23//////////////////////////////////////////////////////////////////////////
24/// Constructor.
25ShMemOutput::ShMemOutput(QString name)
26 : ComponentBase(name)
27{
28 LOG_TRACE("constructor(" << name << ")");
29 shMemSize_ = sizeof(size_t);
30 shMemName_ = name; // default name
31 timeout_ = 10;
32 addInputOutput();
33}
34
35//////////////////////////////////////////////////////////////////////////
36/// Destructor.
37ShMemOutput::~ShMemOutput()
38{
39 LOG_TRACE("destructor");
40
41}
42
43//////////////////////////////////////////////////////////////////////////
44/// Called by the ComponentManager to start the component
45void ShMemOutput::startActivity()
46{
47 shmemSize_ = new ShMem(shMemSizeName_.toStdString().c_str(), sizeof(size_t));
48 shmem_ = NULL;
49
50 start();
51 THREAD_ALIVE = true;
52 setState(MONITOR_OK);
53 LOG_INFO("started component '" << componentName << "'");
54}
55
56//////////////////////////////////////////////////////////////////////////
57/// Called by the ComponentManager to stop the component
58void ShMemOutput::stopActivity()
59{
60 THREAD_ALIVE = false;
61
62 setState(STOPPED);
63 LOG_INFO("stopped component '" << componentName << "'");
64}
65
66//////////////////////////////////////////////////////////////////////////
67/// Called by the ComponentManager to pass the XML parameters to the
68/// component
69ComponentBase::COMPONENT_CONFIGURATION ShMemOutput::configureComponent(XmlComponentConfig config)
70{
71 if (param.getProperty("memoryName") != QString::null)
72 shMemName_ = param.getProperty("memoryName");
73
74 if (param.getProperty("timeout") != QString::null)
75 timeout_ = param.getProperty("timeout").toInt();
76
77 shMemSizeName_ = shMemName_ + "Size";
78
79 return ComponentBase::CONFIGURED_OK;
80}
81
82void ShMemOutput::run()
83{
84 while(THREAD_ALIVE) {
85
86 if(shmemSize_->wait(timeout_)) {
87 qDebug() << "t[13]= " << road_time();
88
89 shmemSize_->lockMemory();
90 size_t readedSize = *((size_t*)shmemSize_->read());
91 shmemSize_->unlockMemory();
92
93 if(shmem_ == NULL) {
94 shMemSize_ = readedSize;
95 shmem_ = new ShMem(shMemName_.toStdString().c_str(), shMemSize_);
96 }
97
98 if(readedSize != shMemSize_) {
99 LOG_WARN("Shared Memory size incorect");
100 continue;
101 }
102 qDebug() << "t[14]= " << road_time();
103 //LOG_INFO("Shmem size = " << shMemSize_ << " " << readedSize);
104 shmem_->lockMemory();
105 char * ptr = (char*)shmem_->read();
106 GET_OUTPUT("data",ShMemOutput,QByteArray)->sendGenericData(ptr,shMemSize_);
107 shmem_->unlockMemory();
108
109 setState(MONITOR_OK);
110
111 } else {
112 setState(MONITOR_NOK);
113 LOG_WARN("Component " << componentName << " timeout !!!")
114 }
115
116 }
117
118 delete shmemSize_;
119 delete shmem_;
120
121}
122
Note: See TracBrowser for help on using the repository browser.