source: flair-src/trunk/tools/FlairGCS/src/ConnectionLayout.cpp@ 307

Last change on this file since 307 was 269, checked in by Sanahuja Guillaume, 6 years ago

flairgcs:
speed up processing time when receiving datas from uav
triger watchdog while receiving datas from uav
(avoids connection lost in uav)

File size: 3.5 KB
Line 
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
11ConnectionLayout::ConnectionLayout(UdtSocket *socket, QString name)
12 : Layout(NULL, name, "root") {
13 this->socket = socket;
14 this->name = name;
15}
16
17ConnectionLayout::~ConnectionLayout() {
18}
19
20void ConnectionLayout::udtSocketDestroyed(QObject *obj){
21 socket=NULL;
22}
23
24void ConnectionLayout::receive(char *buf, int size) {
25 //fprintf(stderr,"trame %x\n",buf[0]);
26 // for(int i=0; i<size;i++) fprintf(stderr,"%x ",buf[i]);
27 // fprintf(stderr,"\n");
28 switch ((unsigned char)buf[0]) {
29 case XML_HEADER: {
30 QString xml;
31 QDomDocument doc;
32 xml = QString((char *)buf);
33 xml.resize(size);
34
35 // fprintf(stderr,"recu %i\n%s\n",size,xml.toLocal8Bit().constData());
36 if (!doc.setContent(xml)) {
37 fprintf(stderr,"prob setContent fichier\n");
38 }
39
40 QDomElement dom=doc.firstChildElement("root").firstChildElement();
41 ParseXml(&dom);
42 break;
43 }
44 case DATA_BIG_ENDIAN: {
45 // for(int i=0;i<size;i++) fprintf(stderr,"%x ",buf[i]);
46 // fprintf(stderr,"\n");
47 uint16_t period;
48 memcpy(&period, &buf[1], sizeof(uint16_t));
49 period = qFromBigEndian(period);
50 drawDatas(&buf[3], size - 3, period, true);
51 break;
52 }
53 case DATA_LITTLE_ENDIAN: {
54 // for(int i=0;i<size;i++) fprintf(stderr,"%x ",buf[i]);
55 // fprintf(stderr,"\n");
56 uint16_t period;
57 memcpy(&period, &buf[1], sizeof(uint16_t));
58 // fprintf(stderr,"recu %i period %i\n",size,period);
59 drawDatas(&buf[3], size - 3, period);
60 break;
61 }
62 case CLOSING_CONNECTION: {
63 deleteLater();
64 break;
65 }
66 default:
67 fprintf(stderr,"trame non supportée %x\n", buf[0]);
68 }
69}
70
71void ConnectionLayout::XmlToSend(QDomDocument doc) {
72 //fprintf(stderr,"xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
73 if(!socket) return;
74
75 // xml to send a mettre dans le manager
76 if(doc.toString().toLocal8Bit().length()!=0) {
77 socket->write(doc.toString().toLocal8Bit().constData(),doc.toString().toLocal8Bit().length());
78 }
79 /*
80 QMetaObject::invokeMethod(
81 socket, "write", Qt::BlockingQueuedConnection,
82 Q_ARG(const char *, doc.toString().toLocal8Bit().constData()),
83 Q_ARG(qint64, doc.toString().toLocal8Bit().length()));*/
84}
85
86void ConnectionLayout::LoadXml(QDomDocument *to_parse) {
87 QDomElement tmp = to_parse->firstChildElement("root");
88 while (tmp.attribute("name") != name && !tmp.isNull())
89 tmp = to_parse->nextSiblingElement("root");
90
91 if (!tmp.isNull()) {
92 XmlWidget::LoadXml(&tmp);
93 } else {
94 fprintf(stderr,"%s not found in xml file \n", name.toLocal8Bit().constData());
95 }
96}
97
98void ConnectionLayout::removeDataRemote(DataRemote *data) {
99 dataremotes.removeOne(data);
100}
101
102void ConnectionLayout::addDataRemote(DataRemote *data) {
103 dataremotes.append(data);
104}
105
106QString ConnectionLayout::getName() { return name; }
107
108void ConnectionLayout::drawDatas(char *buf, int buf_size, uint16_t period,
109 bool big_endian) {
110 for (int i = 0; i < dataremotes.count(); i++) {
111 dataremotes.at(i)->BufEvent(&buf, &buf_size, period, big_endian);
112 }
113}
114
115QString ConnectionLayout::getDocRootName(char* buf, int size) {
116 QString xml;
117 QDomDocument doc;
118 xml = QString((char *)buf);
119 xml.resize(size);
120
121 if (!doc.setContent(xml)) {
122 fprintf(stderr,"prob setContent fichier\n");
123 }
124
125 return doc.firstChildElement("root").attribute("name");
126}
Note: See TracBrowser for help on using the repository browser.