| 1 | // %flair:license{
|
---|
| 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
| 4 | // %flair:license}
|
---|
| 5 | #include "ConnectionLayout.h"
|
---|
| 6 | #include "UdtSocket.h"
|
---|
| 7 | #include "DataRemote.h"
|
---|
| 8 | #include <qendian.h>
|
---|
| 9 | #include "communication.h"
|
---|
| 10 |
|
---|
| 11 | ConnectionLayout::ConnectionLayout(UdtSocket *socket, QString name, QTime startTime)
|
---|
| 12 | : Layout(NULL, name, "root") {
|
---|
| 13 | this->socket = socket;
|
---|
| 14 | this->name = name;
|
---|
| 15 | this->startTime = startTime;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | ConnectionLayout::~ConnectionLayout() {
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | QTime ConnectionLayout::GetStartTime(void) {
|
---|
| 22 | return startTime;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | void ConnectionLayout::udtSocketDestroyed(QObject *obj){
|
---|
| 26 | socket=NULL;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | void ConnectionLayout::receive(char *buf, int size) {
|
---|
| 30 | //fprintf(stderr,"trame %x\n",buf[0]);
|
---|
| 31 | // for(int i=0; i<size;i++) fprintf(stderr,"%x ",buf[i]);
|
---|
| 32 | // fprintf(stderr,"\n");
|
---|
| 33 | switch ((unsigned char)buf[0]) {
|
---|
| 34 | case XML_HEADER: {
|
---|
| 35 | QString xml;
|
---|
| 36 | QDomDocument doc;
|
---|
| 37 | xml = QString((char *)buf);
|
---|
| 38 | xml.resize(size);
|
---|
| 39 |
|
---|
| 40 | // fprintf(stderr,"recu %i\n%s\n",size,xml.toLocal8Bit().constData());
|
---|
| 41 | if (!doc.setContent(xml)) {
|
---|
| 42 | fprintf(stderr,"prob setContent fichier\n");
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | QDomElement dom=doc.firstChildElement("root").firstChildElement();
|
---|
| 46 | ParseXml(&dom);
|
---|
| 47 | break;
|
---|
| 48 | }
|
---|
| 49 | case DATA_BIG_ENDIAN: {
|
---|
| 50 | uint16_t period;
|
---|
| 51 | memcpy(&period, &buf[1], sizeof(uint16_t));
|
---|
| 52 | period = qFromBigEndian(period);
|
---|
| 53 | drawDatas(&buf[3], size - 3, period, 1,true);
|
---|
| 54 | break;
|
---|
| 55 | }
|
---|
| 56 | case DATA_LITTLE_ENDIAN: {
|
---|
| 57 | uint16_t period;
|
---|
| 58 | memcpy(&period, &buf[1], sizeof(uint16_t));
|
---|
| 59 | drawDatas(&buf[3], size - 3, period,1);
|
---|
| 60 | break;
|
---|
| 61 | }
|
---|
| 62 | case MULTIPLE_DATA_BIG_ENDIAN: {
|
---|
| 63 | uint16_t period;
|
---|
| 64 | uint16_t nb_buffering;
|
---|
| 65 | memcpy(&period, buf+sizeof(char), sizeof(uint16_t));
|
---|
| 66 | period = qFromBigEndian(period);
|
---|
| 67 | memcpy(&nb_buffering,buf+sizeof(char)+sizeof(uint16_t), sizeof(uint16_t));
|
---|
| 68 | nb_buffering = qFromBigEndian(nb_buffering);
|
---|
| 69 | //fprintf(stderr,"recu %i, period %i, nb_buff %i\n",size,period,nb_buffering);
|
---|
| 70 | drawDatas(buf+sizeof(char)+sizeof(uint16_t)+sizeof(uint16_t), size - sizeof(char)+sizeof(uint16_t)+sizeof(uint16_t), period, nb_buffering,true);
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 | case MULTIPLE_DATA_LITTLE_ENDIAN: {
|
---|
| 74 | uint16_t period;
|
---|
| 75 | uint16_t nb_buffering;
|
---|
| 76 | memcpy(&period, buf+sizeof(char), sizeof(uint16_t));
|
---|
| 77 | memcpy(&nb_buffering,buf+sizeof(char)+sizeof(uint16_t), sizeof(uint16_t));
|
---|
| 78 | //fprintf(stderr,"recu %i, period %i, nb_buff %i\n",size,period,nb_buffering);
|
---|
| 79 | drawDatas(buf+sizeof(char)+sizeof(uint16_t)+sizeof(uint16_t), size - sizeof(char)+sizeof(uint16_t)+sizeof(uint16_t), period, nb_buffering);
|
---|
| 80 | break;
|
---|
| 81 | }
|
---|
| 82 | case CLOSING_CONNECTION: {
|
---|
| 83 | deleteLater();
|
---|
| 84 | break;
|
---|
| 85 | }
|
---|
| 86 | default:
|
---|
| 87 | fprintf(stderr,"trame non supportée %x\n", buf[0]);
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | void ConnectionLayout::XmlToSend(QDomDocument doc) {
|
---|
| 92 | //fprintf(stderr,"xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
|
---|
| 93 | if(!socket) return;
|
---|
| 94 |
|
---|
| 95 | // xml to send a mettre dans le manager
|
---|
| 96 | if(doc.toString().toLocal8Bit().length()!=0) {
|
---|
| 97 | socket->write(doc.toString().toLocal8Bit().constData(),doc.toString().toLocal8Bit().length());
|
---|
| 98 | }
|
---|
| 99 | /*
|
---|
| 100 | QMetaObject::invokeMethod(
|
---|
| 101 | socket, "write", Qt::BlockingQueuedConnection,
|
---|
| 102 | Q_ARG(const char *, doc.toString().toLocal8Bit().constData()),
|
---|
| 103 | Q_ARG(qint64, doc.toString().toLocal8Bit().length()));*/
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void ConnectionLayout::LoadXml(QDomDocument *to_parse) {
|
---|
| 107 | QDomElement tmp = to_parse->firstChildElement("root");
|
---|
| 108 | while (tmp.attribute("name") != name && !tmp.isNull())
|
---|
| 109 | tmp = to_parse->nextSiblingElement("root");
|
---|
| 110 |
|
---|
| 111 | if (!tmp.isNull()) {
|
---|
| 112 | XmlWidget::LoadXml(&tmp);
|
---|
| 113 | } else {
|
---|
| 114 | fprintf(stderr,"%s not found in xml file \n", name.toLocal8Bit().constData());
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | void ConnectionLayout::removeDataRemote(DataRemote *data) {
|
---|
| 119 | dataremotes.removeOne(data);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void ConnectionLayout::addDataRemote(DataRemote *data) {
|
---|
| 123 | dataremotes.append(data);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | QString ConnectionLayout::getName() { return name; }
|
---|
| 127 |
|
---|
| 128 | void ConnectionLayout::drawDatas(char *buf, int buf_size, uint16_t period,uint16_t nb_buffering, bool big_endian) {
|
---|
| 129 | for (int i = 0; i < nb_buffering; i++) {
|
---|
| 130 | for (int j = 0; j < dataremotes.count(); j++) {
|
---|
| 131 | dataremotes.at(j)->BufEvent(&buf, &buf_size, period, nb_buffering,big_endian);
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | QString ConnectionLayout::getDocRootName(char* buf, int size) {
|
---|
| 137 | QString xml;
|
---|
| 138 | QDomDocument doc;
|
---|
| 139 | xml = QString((char *)buf);
|
---|
| 140 | xml.resize(size);
|
---|
| 141 |
|
---|
| 142 | if (!doc.setContent(xml)) {
|
---|
| 143 | fprintf(stderr,"prob setContent fichier\n");
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | return doc.firstChildElement("root").attribute("name");
|
---|
| 147 | } |
---|