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