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 |
|
---|
10 | using namespace std;
|
---|
11 | using namespace pacpus;
|
---|
12 |
|
---|
13 | DECLARE_STATIC_LOGGER("pacpus.base.ShMemOutput");
|
---|
14 |
|
---|
15 | // Construct the factory
|
---|
16 | static ComponentFactory<ShMemOutput> sFactory("ShMemOutput");
|
---|
17 |
|
---|
18 | void ShMemOutput::addInputOutput()
|
---|
19 | {
|
---|
20 | ADD_OUTPUT("data",ShMemOutput,QByteArray);
|
---|
21 | }
|
---|
22 |
|
---|
23 | //////////////////////////////////////////////////////////////////////////
|
---|
24 | /// Constructor.
|
---|
25 | ShMemOutput::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.
|
---|
37 | ShMemOutput::~ShMemOutput()
|
---|
38 | {
|
---|
39 | LOG_TRACE("destructor");
|
---|
40 |
|
---|
41 | }
|
---|
42 |
|
---|
43 | //////////////////////////////////////////////////////////////////////////
|
---|
44 | /// Called by the ComponentManager to start the component
|
---|
45 | void 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
|
---|
58 | void 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
|
---|
69 | ComponentBase::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 |
|
---|
82 | void 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 |
|
---|