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

Last change on this file since 9 was 9, checked in by Sanahuja Guillaume, 7 years ago

gcs

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