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