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