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

Last change on this file since 443 was 443, checked in by Sanahuja Guillaume, 6 weeks ago

update buffering (gcs part)

File size: 3.9 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, QTime startTime)
12    : Layout(NULL, name, "root") {
13  this->socket = socket;
14  this->name = name;
15  this->startTime = startTime;
16}
17
18ConnectionLayout::~ConnectionLayout() {
19}
20
21QTime ConnectionLayout::GetStartTime(void) {
22    return startTime;
23}
24
25void ConnectionLayout::udtSocketDestroyed(QObject *obj){
26  socket=NULL;
27}
28
29void 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_LITTLE_ENDIAN: {
63    uint16_t period;
64    uint16_t nb_buffering;
65    memcpy(&period, buf+sizeof(char), sizeof(uint16_t));
66    memcpy(&nb_buffering,buf+sizeof(char)+sizeof(uint16_t), sizeof(uint16_t));
67    fprintf(stderr,"recu %i, period %i, nb_buff %i\n",size,period,nb_buffering);
68    drawDatas(buf+sizeof(char)+sizeof(uint16_t)+sizeof(uint16_t), size - sizeof(char)+sizeof(uint16_t)+sizeof(uint16_t), period, nb_buffering);
69    break;
70  }
71  case CLOSING_CONNECTION: {
72    deleteLater();
73    break;
74  }
75  default:
76    fprintf(stderr,"trame non supportée %x\n", buf[0]);
77  }
78}
79
80void ConnectionLayout::XmlToSend(QDomDocument doc) {
81   //fprintf(stderr,"xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
82  if(!socket) return;
83 
84  // xml to send a mettre dans le manager
85  if(doc.toString().toLocal8Bit().length()!=0) {
86    socket->write(doc.toString().toLocal8Bit().constData(),doc.toString().toLocal8Bit().length());
87  }
88  /*
89  QMetaObject::invokeMethod(
90      socket, "write", Qt::BlockingQueuedConnection,
91      Q_ARG(const char *, doc.toString().toLocal8Bit().constData()),
92      Q_ARG(qint64, doc.toString().toLocal8Bit().length()));*/
93}
94
95void ConnectionLayout::LoadXml(QDomDocument *to_parse) {
96  QDomElement tmp = to_parse->firstChildElement("root");
97  while (tmp.attribute("name") != name && !tmp.isNull())
98    tmp = to_parse->nextSiblingElement("root");
99
100  if (!tmp.isNull()) {
101    XmlWidget::LoadXml(&tmp);
102  } else {
103    fprintf(stderr,"%s not found in xml file \n", name.toLocal8Bit().constData());
104  }
105}
106
107void ConnectionLayout::removeDataRemote(DataRemote *data) {
108  dataremotes.removeOne(data);
109}
110
111void ConnectionLayout::addDataRemote(DataRemote *data) {
112  dataremotes.append(data);
113}
114
115QString ConnectionLayout::getName() { return name; }
116
117void ConnectionLayout::drawDatas(char *buf, int buf_size, uint16_t period,uint16_t nb_buffering, bool big_endian) {
118    for (int i = 0; i < nb_buffering; i++) {
119        for (int j = 0; j < dataremotes.count(); j++) {
120            dataremotes.at(j)->BufEvent(&buf, &buf_size, period, nb_buffering, big_endian);
121        }
122    }
123}
124
125QString ConnectionLayout::getDocRootName(char* buf, int size) {
126  QString xml;
127  QDomDocument doc;
128  xml = QString((char *)buf);
129  xml.resize(size);
130
131  if (!doc.setContent(xml)) {
132    fprintf(stderr,"prob setContent fichier\n");
133  }
134 
135  return doc.firstChildElement("root").attribute("name");
136}
Note: See TracBrowser for help on using the repository browser.