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

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

gcs: do not use socket if connection lost

File size: 3.5 KB
RevLine 
[10]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[10]4// %flair:license}
[9]5#include "ConnectionLayout.h"
6#include "UdtSocket.h"
7#include "DataRemote.h"
8#include <qendian.h>
9#include "communication.h"
10
[15]11ConnectionLayout::ConnectionLayout(UdtSocket *socket, QString name)
12 : Layout(NULL, name, "root") {
13 this->socket = socket;
[234]14 this->name = name;
[9]15}
16
[30]17ConnectionLayout::~ConnectionLayout() {
18}
[9]19
[260]20void ConnectionLayout::udtSocketDestroyed(QObject *obj){
21 socket=NULL;
22}
23
[15]24void ConnectionLayout::receive(char *buf, int size) {
[247]25 //fprintf(stderr,"trame %x\n",buf[0]);
[244]26 // for(int i=0; i<size;i++) fprintf(stderr,"%x ",buf[i]);
27 // fprintf(stderr,"\n");
[15]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);
[9]34
[244]35 // fprintf(stderr,"recu %i\n%s\n",size,xml.toLocal8Bit().constData());
[15]36 if (!doc.setContent(xml)) {
[244]37 fprintf(stderr,"prob setContent fichier\n");
[15]38 }
[9]39
[15]40 ParseXml(doc.firstChildElement("root").firstChildElement());
41 break;
42 }
[253]43 case DATA_BIG_ENDIAN: {
[244]44 // for(int i=0;i<size;i++) fprintf(stderr,"%x ",buf[i]);
45 // fprintf(stderr,"\n");
[15]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 }
[253]52 case DATA_LITTLE_ENDIAN: {
[244]53 // for(int i=0;i<size;i++) fprintf(stderr,"%x ",buf[i]);
54 // fprintf(stderr,"\n");
[15]55 uint16_t period;
56 memcpy(&period, &buf[1], sizeof(uint16_t));
[244]57 // fprintf(stderr,"recu %i period %i\n",size,period);
[15]58 drawDatas(&buf[3], size - 3, period);
59 break;
60 }
[247]61 case CLOSING_CONNECTION: {
62 deleteLater();
63 break;
64 }
[15]65 default:
[244]66 fprintf(stderr,"trame non supportée %x\n", buf[0]);
[15]67 }
[9]68}
69
[234]70void ConnectionLayout::XmlToSend(QDomDocument doc) {
[258]71 //fprintf(stderr,"xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
[260]72 if(!socket) return;
[234]73
74 // xml to send a mettre dans le manager
[258]75 if(doc.toString().toLocal8Bit().length()!=0) {
76 socket->write(doc.toString().toLocal8Bit().constData(),doc.toString().toLocal8Bit().length());
77 }
78 /*
[234]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 {
[244]93 fprintf(stderr,"%s not found in xml file \n", name.toLocal8Bit().constData());
[234]94 }
95}
96
[15]97void ConnectionLayout::removeDataRemote(DataRemote *data) {
98 dataremotes.removeOne(data);
[9]99}
100
[15]101void ConnectionLayout::addDataRemote(DataRemote *data) {
102 dataremotes.append(data);
[9]103}
104
[234]105QString ConnectionLayout::getName() { return name; }
[222]106
[15]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 }
[9]112}
113
[234]114QString ConnectionLayout::getDocRootName(char* buf, int size) {
115 QString xml;
116 QDomDocument doc;
117 xml = QString((char *)buf);
118 xml.resize(size);
[9]119
[234]120 if (!doc.setContent(xml)) {
[244]121 fprintf(stderr,"prob setContent fichier\n");
[15]122 }
[234]123
124 return doc.firstChildElement("root").attribute("name");
125}
Note: See TracBrowser for help on using the repository browser.