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 |
|
---|
11 | ConnectionLayout::ConnectionLayout(UdtSocket *socket, QString name)
|
---|
12 | : Layout(NULL, name, "root") {
|
---|
13 | this->socket = socket;
|
---|
14 | this->name = name;
|
---|
15 | }
|
---|
16 |
|
---|
17 | ConnectionLayout::~ConnectionLayout() {
|
---|
18 | }
|
---|
19 |
|
---|
20 | void ConnectionLayout::receive(char *buf, int size) {
|
---|
21 | //fprintf(stderr,"trame %x\n",buf[0]);
|
---|
22 | // for(int i=0; i<size;i++) fprintf(stderr,"%x ",buf[i]);
|
---|
23 | // fprintf(stderr,"\n");
|
---|
24 | switch ((unsigned char)buf[0]) {
|
---|
25 | case XML_HEADER: {
|
---|
26 | QString xml;
|
---|
27 | QDomDocument doc;
|
---|
28 | xml = QString((char *)buf);
|
---|
29 | xml.resize(size);
|
---|
30 |
|
---|
31 | // fprintf(stderr,"recu %i\n%s\n",size,xml.toLocal8Bit().constData());
|
---|
32 | if (!doc.setContent(xml)) {
|
---|
33 | fprintf(stderr,"prob setContent fichier\n");
|
---|
34 | }
|
---|
35 |
|
---|
36 | ParseXml(doc.firstChildElement("root").firstChildElement());
|
---|
37 | break;
|
---|
38 | }
|
---|
39 | case DATA_BIG_ENDIAN: {
|
---|
40 | // for(int i=0;i<size;i++) fprintf(stderr,"%x ",buf[i]);
|
---|
41 | // fprintf(stderr,"\n");
|
---|
42 | uint16_t period;
|
---|
43 | memcpy(&period, &buf[1], sizeof(uint16_t));
|
---|
44 | period = qFromBigEndian(period);
|
---|
45 | drawDatas(&buf[3], size - 3, period, true);
|
---|
46 | break;
|
---|
47 | }
|
---|
48 | case DATA_LITTLE_ENDIAN: {
|
---|
49 | // for(int i=0;i<size;i++) fprintf(stderr,"%x ",buf[i]);
|
---|
50 | // fprintf(stderr,"\n");
|
---|
51 | uint16_t period;
|
---|
52 | memcpy(&period, &buf[1], sizeof(uint16_t));
|
---|
53 | // fprintf(stderr,"recu %i period %i\n",size,period);
|
---|
54 | drawDatas(&buf[3], size - 3, period);
|
---|
55 | break;
|
---|
56 | }
|
---|
57 | case CLOSING_CONNECTION: {
|
---|
58 | deleteLater();
|
---|
59 | break;
|
---|
60 | }
|
---|
61 | default:
|
---|
62 | fprintf(stderr,"trame non supportée %x\n", buf[0]);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | void ConnectionLayout::XmlToSend(QDomDocument doc) {
|
---|
67 | // fprintf(stderr,"xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
|
---|
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 |
|
---|
77 | void 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 {
|
---|
85 | fprintf(stderr,"%s not found in xml file \n", name.toLocal8Bit().constData());
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | void ConnectionLayout::removeDataRemote(DataRemote *data) {
|
---|
90 | dataremotes.removeOne(data);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void ConnectionLayout::addDataRemote(DataRemote *data) {
|
---|
94 | dataremotes.append(data);
|
---|
95 | }
|
---|
96 |
|
---|
97 | QString ConnectionLayout::getName() { return name; }
|
---|
98 |
|
---|
99 | void 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 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | QString ConnectionLayout::getDocRootName(char* buf, int size) {
|
---|
107 | QString xml;
|
---|
108 | QDomDocument doc;
|
---|
109 | xml = QString((char *)buf);
|
---|
110 | xml.resize(size);
|
---|
111 |
|
---|
112 | if (!doc.setContent(xml)) {
|
---|
113 | fprintf(stderr,"prob setContent fichier\n");
|
---|
114 | }
|
---|
115 |
|
---|
116 | return doc.firstChildElement("root").attribute("name");
|
---|
117 | } |
---|