1 | #include "ShMemInterface.h"
|
---|
2 |
|
---|
3 | #include "kernel/ComponentFactory.h"
|
---|
4 | #include "kernel/Log.h"
|
---|
5 | #include "PacpusTools/ShMem.h"
|
---|
6 | #include "Pacpus/kernel/PacpusEvent.h"
|
---|
7 | #include "Pacpus/kernel/inputOutputInterface.h"
|
---|
8 |
|
---|
9 |
|
---|
10 | using namespace std;
|
---|
11 | using namespace pacpus;
|
---|
12 |
|
---|
13 | DECLARE_STATIC_LOGGER("pacpus.base.ShMemInput");
|
---|
14 |
|
---|
15 | // Construct the factory
|
---|
16 | static ComponentFactory<ShMemInput> sFactory("ShMemInput");
|
---|
17 |
|
---|
18 | void ShMemInput::addInputOutput()
|
---|
19 | {
|
---|
20 | ADD_INPUT("data",ShMemInput,QByteArray,send);
|
---|
21 | }
|
---|
22 |
|
---|
23 | //////////////////////////////////////////////////////////////////////////
|
---|
24 | /// Constructor.
|
---|
25 | ShMemInput::ShMemInput(QString name)
|
---|
26 | : ComponentBase(name)
|
---|
27 | {
|
---|
28 | LOG_TRACE("constructor(" << name << ")");
|
---|
29 | shMemSize_ = sizeof(size_t);
|
---|
30 | shMemName_ = name; // default name
|
---|
31 | addInputOutput();
|
---|
32 | }
|
---|
33 |
|
---|
34 | //////////////////////////////////////////////////////////////////////////
|
---|
35 | /// Destructor.
|
---|
36 | ShMemInput::~ShMemInput()
|
---|
37 | {
|
---|
38 | LOG_TRACE("destructor");
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | //////////////////////////////////////////////////////////////////////////
|
---|
43 | /// Called by the ComponentManager to start the component
|
---|
44 | void ShMemInput::startActivity()
|
---|
45 | {
|
---|
46 | shmemSize_ = new ShMem(shMemSizeName_.toStdString().c_str(), sizeof(size_t));
|
---|
47 | shmem_ = NULL;
|
---|
48 | }
|
---|
49 |
|
---|
50 | //////////////////////////////////////////////////////////////////////////
|
---|
51 | /// Called by the ComponentManager to stop the component
|
---|
52 | void ShMemInput::stopActivity()
|
---|
53 | {
|
---|
54 | //delete shmemSize_;
|
---|
55 | //delete shmem_;
|
---|
56 | }
|
---|
57 |
|
---|
58 | //////////////////////////////////////////////////////////////////////////
|
---|
59 | /// Called by the ComponentManager to pass the XML parameters to the
|
---|
60 | /// component
|
---|
61 | ComponentBase::COMPONENT_CONFIGURATION ShMemInput::configureComponent(XmlComponentConfig config)
|
---|
62 | {
|
---|
63 | if (param.getProperty("memoryName") != QString::null)
|
---|
64 | shMemName_ = param.getProperty("memoryName");
|
---|
65 | // if (param.getProperty("alascaPort") != QString::null)
|
---|
66 | // port_ = param.getProperty("alascaPort").toInt();
|
---|
67 |
|
---|
68 | shMemSizeName_ = shMemName_ + "Size";
|
---|
69 |
|
---|
70 | return ComponentBase::CONFIGURED_OK;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void ShMemInput::send(const QByteArray& data)
|
---|
74 | {
|
---|
75 | qDebug() << "t[10]= " << road_time();
|
---|
76 | if(shmem_ == NULL) {
|
---|
77 | shMemSize_ = data.size();
|
---|
78 | shmem_ = new ShMem(shMemName_.toStdString().c_str(), shMemSize_);
|
---|
79 | LOG_WARN("Shared Memory allocated");
|
---|
80 | }
|
---|
81 |
|
---|
82 | if(data.size() != shMemSize_) {
|
---|
83 | LOG_WARN("Shared Memory size incorect");
|
---|
84 | return;
|
---|
85 | }
|
---|
86 | qDebug() << "t[11]= " << road_time();
|
---|
87 | shmemSize_->write(&shMemSize_,sizeof(size_t));
|
---|
88 | shmem_->write((void*)(data.data()),shMemSize_); // copy 4
|
---|
89 |
|
---|
90 |
|
---|
91 | qDebug() << "t[12]= " << road_time();
|
---|
92 |
|
---|
93 | }
|
---|