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

Last change on this file since 15 was 15, checked in by Bayard Gildas, 5 years ago

sources reformatted with flair-format-dir script

File size: 4.6 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#include <zlib.h>
11#include <assert.h>
12
13#define COMPRESS_CHUNK 1024
14
15ConnectionLayout::ConnectionLayout(UdtSocket *socket, QString name)
16    : Layout(NULL, name, "root") {
17  isRemoteNameDefined = false;
18  this->socket = socket;
19}
20
21ConnectionLayout::~ConnectionLayout() {}
22
23void ConnectionLayout::receive(char *buf, int size) {
24  // printf("trame %x\n",buf[0]);
25  // for(int i=0; i<size;i++) printf("%x ",buf[i]);
26  // printf("\n");
27  switch (buf[0]) {
28  case ZLIB_HEADER: {
29    ssize_t out_size;
30    char *uncompressbuf;
31    uncompressBuffer(buf, size, &uncompressbuf, &out_size);
32    handleUncompressedFrame(uncompressbuf, out_size);
33    free(uncompressbuf);
34    break;
35  }
36  default:
37    handleUncompressedFrame(buf, size);
38  }
39  free(buf);
40}
41
42void ConnectionLayout::XmlToSend(QDomDocument doc) {
43  // printf("xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
44  // xml to send a mettre dans le manager
45  QMetaObject::invokeMethod(
46      socket, "write", Qt::BlockingQueuedConnection,
47      Q_ARG(const char *, doc.toString().toLocal8Bit().constData()),
48      Q_ARG(qint64, doc.toString().toLocal8Bit().length()));
49}
50
51void ConnectionLayout::LoadXml(QDomDocument to_parse) {
52  if (!isRemoteNameDefined) {
53    printf("load xml: name not defined!\n");
54    return;
55  }
56
57  QDomElement tmp = to_parse.firstChildElement("root");
58  while (tmp.attribute("name") != remoteName && !tmp.isNull())
59    tmp = to_parse.nextSiblingElement("root");
60
61  if (!tmp.isNull()) {
62    XmlWidget::LoadXml(tmp);
63  } else {
64    printf("%s not found in xml file \n", remoteName.toLocal8Bit().constData());
65  }
66}
67
68void ConnectionLayout::handleUncompressedFrame(char *buf, ssize_t size) {
69  switch ((unsigned char)buf[0]) {
70  case XML_HEADER: {
71    QString xml;
72    QDomDocument doc;
73    xml = QString((char *)buf);
74    xml.resize(size);
75
76    // printf("recu %i\n%s\n",size,xml.toLocal8Bit().constData());
77    if (!doc.setContent(xml)) {
78      printf("prob setContent fichier\n");
79    }
80
81    if (!isRemoteNameDefined) {
82      isRemoteNameDefined = true;
83      remoteName = doc.firstChildElement("root").attribute("name");
84      setRemoteName(remoteName);
85      SetAttribute("name", remoteName);
86    }
87
88    ParseXml(doc.firstChildElement("root").firstChildElement());
89    break;
90  }
91  case DATAS_BIG_ENDIAN: {
92    // for(int i=0;i<size;i++) printf("%x ",buf[i]);
93    // printf("\n");
94    uint16_t period;
95    memcpy(&period, &buf[1], sizeof(uint16_t));
96    period = qFromBigEndian(period);
97    drawDatas(&buf[3], size - 3, period, true);
98    break;
99  }
100  case DATAS_LITTLE_ENDIAN: {
101    // for(int i=0;i<size;i++) printf("%x ",buf[i]);
102    // printf("\n");
103    uint16_t period;
104    memcpy(&period, &buf[1], sizeof(uint16_t));
105    // printf("recu %i period %i\n",size,period);
106    drawDatas(&buf[3], size - 3, period);
107    break;
108  }
109  default:
110    printf("trame non supportée %x\n", buf[0]);
111  }
112}
113
114void ConnectionLayout::removeDataRemote(DataRemote *data) {
115  dataremotes.removeOne(data);
116}
117
118void ConnectionLayout::addDataRemote(DataRemote *data) {
119  dataremotes.append(data);
120}
121
122QString ConnectionLayout::getRemoteName() { return remoteName; }
123void ConnectionLayout::drawDatas(char *buf, int buf_size, uint16_t period,
124                                 bool big_endian) {
125  for (int i = 0; i < dataremotes.count(); i++) {
126    dataremotes.at(i)->BufEvent(&buf, &buf_size, period, big_endian);
127  }
128}
129
130int ConnectionLayout::uncompressBuffer(char *in, ssize_t in_size, char **out,
131                                       ssize_t *out_size) {
132  int ret;
133  unsigned have;
134  z_stream strm;
135
136  // allocate inflate state
137  strm.zalloc = Z_NULL;
138  strm.zfree = Z_NULL;
139  strm.opaque = Z_NULL;
140  strm.avail_in = 0;
141  strm.next_in = Z_NULL;
142  ret = inflateInit(&strm);
143  if (ret != Z_OK)
144    return ret;
145
146  *out = (char *)malloc(COMPRESS_CHUNK);
147  if (!(*out))
148    return Z_BUF_ERROR;
149
150  strm.avail_in = in_size;
151  strm.next_in = (unsigned char *)in;
152  strm.avail_out = COMPRESS_CHUNK;
153  strm.next_out = (unsigned char *)*out;
154
155  ret = inflate(&strm, Z_NO_FLUSH);
156  assert(ret != Z_STREAM_ERROR); // state not clobbered
157  switch (ret) {
158  case Z_NEED_DICT:
159    ret = Z_DATA_ERROR; // and fall through
160  case Z_DATA_ERROR:
161  case Z_MEM_ERROR:
162    (void)inflateEnd(&strm);
163    return ret;
164  }
165  have = COMPRESS_CHUNK - strm.avail_out;
166  *out_size = have;
167
168  // printf("%i -> %i\n",in_size,have);
169  // printf("%s\n",*out);
170  // clean up and return
171  (void)inflateEnd(&strm);
172  return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
173}
Note: See TracBrowser for help on using the repository browser.