1 | #ifndef IN_OUT_INTERFACE_H
|
---|
2 | #define IN_OUT_INTERFACE_H
|
---|
3 |
|
---|
4 | #include <Pacpus/kernel/Log.h>
|
---|
5 | #include <Pacpus/kernel/InputOutputBase.h>
|
---|
6 | #include <QApplication>
|
---|
7 | #include <QByteArray>
|
---|
8 | #include <QThread>
|
---|
9 | #include <typeinfo>
|
---|
10 |
|
---|
11 | namespace pacpus {
|
---|
12 |
|
---|
13 | template <typename T, class C>
|
---|
14 | class InputInterface
|
---|
15 | : public InputInterfaceBase
|
---|
16 | {
|
---|
17 | public:
|
---|
18 | InputInterface(QString name, C * component, void (C::*m)(const T&))
|
---|
19 | : InputInterfaceBase(name, component, component)
|
---|
20 | , method(m)
|
---|
21 | {}
|
---|
22 |
|
---|
23 | ~InputInterface()
|
---|
24 | {}
|
---|
25 |
|
---|
26 | size_t getDataSize()
|
---|
27 | {
|
---|
28 | return sizeof(T);
|
---|
29 | }
|
---|
30 |
|
---|
31 | QString getDataType()
|
---|
32 | {
|
---|
33 | return QString(typeid(T).name());
|
---|
34 | }
|
---|
35 |
|
---|
36 | PacpusEvent* getEventTemplate()
|
---|
37 | {
|
---|
38 | return new PacpusTypedEvent<T>(TYPED_EVENT);
|
---|
39 | }
|
---|
40 |
|
---|
41 | void customEvent(QEvent* event)
|
---|
42 | {
|
---|
43 | // TODO check component state started
|
---|
44 | switch (event->type()) {
|
---|
45 | case TYPED_EVENT:
|
---|
46 | {
|
---|
47 | // cast from Component to Component (T->T)
|
---|
48 | PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
|
---|
49 |
|
---|
50 | LOG_DEBUG("Receiver " << getSignature() << " thread " << QThread::currentThread() << " Data & " << & typedEvent->data_);
|
---|
51 |
|
---|
52 | //if(_component) get state
|
---|
53 |
|
---|
54 | if (typedEvent->timerange() < 500 && readingMode() == TimeBounded) {
|
---|
55 | LOG_WARN("Incorrect TimeRange (0), switch to NeverSkip");
|
---|
56 | readingMode() = NeverSkip;
|
---|
57 | }
|
---|
58 |
|
---|
59 | switch (readingMode()) {
|
---|
60 | case TimeBounded:
|
---|
61 | LOG_DEBUG("Input " << this->getSignature().leftJustified(20) << QString("Time bournded").leftJustified(15) << road_time()- typedEvent->t_ << "\t" << typedEvent->tr_);
|
---|
62 |
|
---|
63 | if (road_time() - typedEvent->time() > typedEvent->timerange()) {
|
---|
64 | LOG_DEBUG("Data skip " << this->getSignature());
|
---|
65 | break;
|
---|
66 | }
|
---|
67 |
|
---|
68 | (dynamic_cast<C*>(component())->*method)(typedEvent->data());
|
---|
69 | break;
|
---|
70 |
|
---|
71 | case GetLast:
|
---|
72 | LOG_DEBUG("Input " << this->getSignature().leftJustified(20) << QString("GetLast").leftJustified(15) << road_time() - typedEvent->t_ << "\t" << typedEvent->tr_);
|
---|
73 |
|
---|
74 | (dynamic_cast<C*>(component())->*method)(typedEvent->data());
|
---|
75 | // delete all remaining events
|
---|
76 | QCoreApplication::removePostedEvents(this, TYPED_EVENT);
|
---|
77 | break;
|
---|
78 |
|
---|
79 | case NeverSkip:
|
---|
80 | LOG_DEBUG("Input " << this->getSignature().leftJustified(20) << QString("NeverSkip").leftJustified(15) << road_time() - typedEvent->t_ << "\t" << typedEvent->tr_);
|
---|
81 |
|
---|
82 | default:
|
---|
83 | (dynamic_cast<C*>(component())->*method)(typedEvent->data());
|
---|
84 | }
|
---|
85 | break;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // from Connection interface to Component (G->T)
|
---|
89 | /* case GENERIC_EVENT2: {
|
---|
90 | PacpusTypedEvent<QByteArray> * genericEvent = dynamic_cast<PacpusTypedEvent<QByteArray> *> (event);
|
---|
91 | T data;
|
---|
92 | QByteArray& buf = (QByteArray&) genericEvent->data_;
|
---|
93 | QDataStream in(&buf,QIODevice::ReadOnly);
|
---|
94 |
|
---|
95 | (dynamic_cast<C*>(_component)->*method)(data); // copy 8 X
|
---|
96 | break;
|
---|
97 | }
|
---|
98 |
|
---|
99 | // from Component to Connection interface (T->G) (Typed in QByteArray)
|
---|
100 | case GENERIC_EVENT3: {
|
---|
101 | PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
|
---|
102 | (dynamic_cast<C*>(_component)->*method)(typedEvent->data_); // copy 3 X
|
---|
103 |
|
---|
104 | break;
|
---|
105 | }*/
|
---|
106 |
|
---|
107 | default:
|
---|
108 | LOG_WARN("Unknown event ID " << event->type());
|
---|
109 | break;
|
---|
110 | }
|
---|
111 | event->accept();
|
---|
112 | }
|
---|
113 |
|
---|
114 | T& getData() {
|
---|
115 | T data;
|
---|
116 | // TODO ask output data;
|
---|
117 |
|
---|
118 | //LOG4CXX_INFO(getLogger(), "Hello, World");
|
---|
119 |
|
---|
120 | return data;
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected:
|
---|
124 | void (C::*method)(const T&);
|
---|
125 |
|
---|
126 | /* log4cxx::LoggerPtr& getLogger() {
|
---|
127 | static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger( LOG4CXX_LOCATION.getClassName()));
|
---|
128 | return logger;
|
---|
129 | }*/
|
---|
130 |
|
---|
131 | };
|
---|
132 |
|
---|
133 | template <typename T, class C>
|
---|
134 | class OutputInterface : public OutputInterfaceBase
|
---|
135 | {
|
---|
136 | public:
|
---|
137 | OutputInterface(QString name, C * component):OutputInterfaceBase(name,component,component) {}
|
---|
138 | ~OutputInterface() {}
|
---|
139 |
|
---|
140 | // Used by Components to send data througth typed output
|
---|
141 | void send(const T & data, road_time_t t = road_time(), road_timerange_t tr = 0)
|
---|
142 | {
|
---|
143 | //QSharedPointer<T> sharedPointer = new T(data);
|
---|
144 |
|
---|
145 | for (QList<ConnectionBase>::iterator it = connections().begin(); it != connections().end(); ++it) {
|
---|
146 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<T>(TYPED_EVENT,data,t,tr),it->getPriority()); // Event is delete by the event loop handler
|
---|
147 | //qDebug() << "sender " << it->getInterface()->getSignature() << " thread " << QThread::currentThread() << " Data & " << &data << " ";
|
---|
148 | // TODO Data Shared
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* void send(const T & data, road_time_t t = road_time(), road_timerange_t tr = 0) {
|
---|
153 | for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it){
|
---|
154 |
|
---|
155 | if(it->getInterface()->getDataType() != QString(typeid(QByteArray).name()))
|
---|
156 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<T>(TYPED_EVENT,data,t,tr),it->getPriority());
|
---|
157 | else {
|
---|
158 | QByteArray buf;
|
---|
159 | QDataStream out(&buf,QIODevice::ReadWrite);
|
---|
160 | PacpusTypedEvent<QByteArray> * ev =new PacpusTypedEvent<QByteArray>(GENERIC_EVENT3,buf);
|
---|
161 | QApplication::postEvent(it->getInterface(),ev,it->getPriority()); // Copy 2 (ctor)
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | // Used by Connection Interfaces only to pose generic event
|
---|
167 | void sendGenericData(char * data, size_t size) {
|
---|
168 | QByteArray buf(data,size); // copy 5
|
---|
169 | for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it)
|
---|
170 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<QByteArray>(GENERIC_EVENT2,buf),it->getPriority()); // Copy 6(ctor)
|
---|
171 |
|
---|
172 |
|
---|
173 | }
|
---|
174 | */
|
---|
175 | size_t getDataSize()
|
---|
176 | {
|
---|
177 | return sizeof(T);
|
---|
178 | }
|
---|
179 |
|
---|
180 | QString getDataType()
|
---|
181 | {
|
---|
182 | return QString(typeid(T).name());
|
---|
183 | }
|
---|
184 | };
|
---|
185 |
|
---|
186 |
|
---|
187 | } // namespace pacpus
|
---|
188 |
|
---|
189 | #endif // IN_OUT_INTERFACE_H
|
---|