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

Last change on this file since 260 was 260, checked in by Sanahuja Guillaume, 4 years ago

gcs: do not use socket if connection lost

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