[89] | 1 | #ifndef IN_OUT_INTERFACE_H
|
---|
| 2 | #define IN_OUT_INTERFACE_H
|
---|
| 3 |
|
---|
[95] | 4 | #include <Pacpus/kernel/Log.h>
|
---|
[138] | 5 | #include <Pacpus/kernel/InputOutputBase.h>
|
---|
[110] | 6 | #include <QApplication>
|
---|
| 7 | #include <typeinfo>
|
---|
[89] | 8 |
|
---|
| 9 | #include <QDebug>
|
---|
[110] | 10 | #include <QThread>
|
---|
[89] | 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))
|
---|
[110] | 16 |
|
---|
[89] | 17 | #define GET_OUTPUT(name,ComponentType, DataType) dynamic_cast<OutputInterface<DataType,ComponentType> *> (output.value(name))
|
---|
[110] | 18 | #define GET_INPUT(name,ComponentType, DataType) dynamic_cast<InputInterface<DataType,ComponentType> *> (input.value(name))
|
---|
[89] | 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 |
|
---|
[110] | 31 | protected:
|
---|
[89] | 32 |
|
---|
[110] | 33 | public:
|
---|
[89] | 34 |
|
---|
[110] | 35 | PacpusEvent* getEventTemplate() {return new PacpusTypedEvent<T>(TYPED_EVENT); }
|
---|
[96] | 36 |
|
---|
[89] | 37 | void customEvent(QEvent* event) {
|
---|
[125] | 38 | // TODO check compoennt state started
|
---|
[89] | 39 | switch (event->type()) {
|
---|
| 40 |
|
---|
[110] | 41 | // from Component to Component (T->T)
|
---|
| 42 | case TYPED_EVENT: {
|
---|
| 43 | PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
|
---|
[89] | 44 |
|
---|
[122] | 45 | //qDebug() << "Reciever " << getSignature() << " thread " << QThread::currentThread() << " Data & " << & typedEvent->data_;
|
---|
[110] | 46 |
|
---|
[119] | 47 | //if(_component) get state
|
---|
| 48 |
|
---|
[110] | 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:
|
---|
[122] | 56 | //qDebug() << "Input " << this->getSignature().leftJustified(20) << QString("Time bournded").leftJustified(15) << road_time()- typedEvent->t_ << "\t" << typedEvent->tr_;
|
---|
[110] | 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_);
|
---|
[89] | 62 | break;
|
---|
[110] | 63 |
|
---|
| 64 | case GetLast:
|
---|
[122] | 65 | //qDebug() << "Input " << this->getSignature().leftJustified(20) << QString("GetLast").leftJustified(15) << road_time() - typedEvent->t_ << "\t" << typedEvent->tr_;
|
---|
[110] | 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:
|
---|
[122] | 72 | //qDebug() << "Input " << this->getSignature().leftJustified(20) << QString("NeverSkip").leftJustified(15) << road_time() - typedEvent->t_ << "\t" << typedEvent->tr_;
|
---|
[110] | 73 |
|
---|
| 74 | default:
|
---|
| 75 | (dynamic_cast<C*>(_component)->*method)(typedEvent->data_);
|
---|
[89] | 76 | }
|
---|
[110] | 77 | break;
|
---|
| 78 | }
|
---|
[89] | 79 |
|
---|
[110] | 80 | // from Connection interface to Component (G->T)
|
---|
| 81 | /* case GENERIC_EVENT2: {
|
---|
[89] | 82 | PacpusTypedEvent<QByteArray> * genericEvent = dynamic_cast<PacpusTypedEvent<QByteArray> *> (event);
|
---|
| 83 | T data;
|
---|
[110] | 84 | QByteArray& buf = (QByteArray&) genericEvent->data_;
|
---|
[89] | 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);
|
---|
[110] | 94 | (dynamic_cast<C*>(_component)->*method)(typedEvent->data_); // copy 3 X
|
---|
[96] | 95 |
|
---|
[89] | 96 | break;
|
---|
[96] | 97 | }*/
|
---|
[89] | 98 |
|
---|
[110] | 99 | default:
|
---|
[89] | 100 |
|
---|
[96] | 101 | qDebug() << "Unknown event ID " << event->type();
|
---|
[110] | 102 | break;
|
---|
[89] | 103 | }
|
---|
[110] | 104 | event->accept();
|
---|
| 105 | }
|
---|
[89] | 106 |
|
---|
[110] | 107 | T& getData() {
|
---|
| 108 | T data;
|
---|
| 109 | // TODO ask output data;
|
---|
[89] | 110 |
|
---|
[110] | 111 | //LOG4CXX_INFO(getLogger(), "Hello, World");
|
---|
| 112 |
|
---|
| 113 | return data;
|
---|
[89] | 114 | }
|
---|
| 115 |
|
---|
| 116 | protected:
|
---|
| 117 | void (C::*method)(const T&);
|
---|
| 118 |
|
---|
[110] | 119 | /* log4cxx::LoggerPtr& getLogger() {
|
---|
| 120 | static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger( LOG4CXX_LOCATION.getClassName()));
|
---|
| 121 | return logger;
|
---|
| 122 | }*/
|
---|
| 123 |
|
---|
[89] | 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
|
---|
[102] | 134 | void send(const T & data, road_time_t t = road_time(), road_timerange_t tr = 0) {
|
---|
[110] | 135 |
|
---|
| 136 | //QSharedPointer<T> sharedPointer = new T(data);
|
---|
| 137 |
|
---|
[89] | 138 | for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it){
|
---|
[110] | 139 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<T>(TYPED_EVENT,data,t,tr),it->getPriority()); // Event is delete by the event loop handler
|
---|
[122] | 140 | //qDebug() << "sender " << it->getInterface()->getSignature() << " thread " << QThread::currentThread() << " Data & " << &data << " ";
|
---|
| 141 | // TODO Data Shared
|
---|
[110] | 142 | }
|
---|
| 143 | }
|
---|
[89] | 144 |
|
---|
[110] | 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()))
|
---|
[102] | 149 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<T>(TYPED_EVENT,data,t,tr),it->getPriority());
|
---|
[110] | 150 | else {
|
---|
[89] | 151 | QByteArray buf;
|
---|
| 152 | QDataStream out(&buf,QIODevice::ReadWrite);
|
---|
[96] | 153 | PacpusTypedEvent<QByteArray> * ev =new PacpusTypedEvent<QByteArray>(GENERIC_EVENT3,buf);
|
---|
| 154 | QApplication::postEvent(it->getInterface(),ev,it->getPriority()); // Copy 2 (ctor)
|
---|
[110] | 155 | }
|
---|
[89] | 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | // Used by Connection Interfaces only to pose generic event
|
---|
[110] | 160 | void sendGenericData(char * data, size_t size) {
|
---|
[89] | 161 | QByteArray buf(data,size); // copy 5
|
---|
| 162 | for(QList<ConnectionBase>::iterator it = _connection.begin(); it!=_connection.end(); ++it)
|
---|
[96] | 163 | QApplication::postEvent(it->getInterface(),new PacpusTypedEvent<QByteArray>(GENERIC_EVENT2,buf),it->getPriority()); // Copy 6(ctor)
|
---|
| 164 |
|
---|
| 165 |
|
---|
[89] | 166 | }
|
---|
[96] | 167 | */
|
---|
[89] | 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
|
---|