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

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

modifs segfault when closing connection

File size: 3.4 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
[234]20QString ConnectionLayout::getUDTStats() {
21  return socket->getUDTStats();
[30]22}
23
[15]24void ConnectionLayout::receive(char *buf, int size) {
[244]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");
[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  }
43  case DATAS_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  }
52  case DATAS_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  }
61  default:
[244]62    fprintf(stderr,"trame non supportée %x\n", buf[0]);
[15]63  }
[9]64}
65
[234]66void ConnectionLayout::XmlToSend(QDomDocument doc) {
[244]67  // fprintf(stderr,"xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
[234]68 
69  // xml to send a mettre dans le manager
70  socket->write(doc.toString().toLocal8Bit().constData(),doc.toString().toLocal8Bit().length());/*
71  QMetaObject::invokeMethod(
72      socket, "write", Qt::BlockingQueuedConnection,
73      Q_ARG(const char *, doc.toString().toLocal8Bit().constData()),
74      Q_ARG(qint64, doc.toString().toLocal8Bit().length()));*/
75}
76
77void ConnectionLayout::LoadXml(QDomDocument to_parse) {
78  QDomElement tmp = to_parse.firstChildElement("root");
79  while (tmp.attribute("name") != name && !tmp.isNull())
80    tmp = to_parse.nextSiblingElement("root");
81
82  if (!tmp.isNull()) {
83    XmlWidget::LoadXml(tmp);
84  } else {
[244]85    fprintf(stderr,"%s not found in xml file \n", name.toLocal8Bit().constData());
[234]86  }
87}
88
[15]89void ConnectionLayout::removeDataRemote(DataRemote *data) {
90  dataremotes.removeOne(data);
[9]91}
92
[15]93void ConnectionLayout::addDataRemote(DataRemote *data) {
94  dataremotes.append(data);
[9]95}
96
[234]97QString ConnectionLayout::getName() { return name; }
[222]98
[15]99void ConnectionLayout::drawDatas(char *buf, int buf_size, uint16_t period,
100                                 bool big_endian) {
101  for (int i = 0; i < dataremotes.count(); i++) {
102    dataremotes.at(i)->BufEvent(&buf, &buf_size, period, big_endian);
103  }
[9]104}
105
[234]106QString ConnectionLayout::getDocRootName(char* buf, int size) {
107  QString xml;
108  QDomDocument doc;
109  xml = QString((char *)buf);
110  xml.resize(size);
[9]111
[234]112  if (!doc.setContent(xml)) {
[244]113    fprintf(stderr,"prob setContent fichier\n");
[15]114  }
[234]115 
116  return doc.firstChildElement("root").attribute("name");
117}
Note: See TracBrowser for help on using the repository browser.