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