Changeset 15 in flair-src for trunk/tools/FlairGCS/src/ConnectionLayout.cpp
- Timestamp:
- 04/08/16 15:40:57 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/FlairGCS/src/ConnectionLayout.cpp
r10 r15 13 13 #define COMPRESS_CHUNK 1024 14 14 15 16 ConnectionLayout::ConnectionLayout(UdtSocket* socket,QString name): Layout(NULL,name,"root") {17 isRemoteNameDefined=false;18 this->socket=socket;15 ConnectionLayout::ConnectionLayout(UdtSocket *socket, QString name) 16 : Layout(NULL, name, "root") { 17 isRemoteNameDefined = false; 18 this->socket = socket; 19 19 } 20 20 21 ConnectionLayout::~ConnectionLayout() { 22 } 21 ConnectionLayout::~ConnectionLayout() {} 23 22 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 30 31 char*uncompressbuf;32 uncompressBuffer(buf,size,&uncompressbuf,&out_size);33 handleUncompressedFrame(uncompressbuf,out_size);34 35 36 37 38 handleUncompressedFrame(buf,size);39 40 23 void 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); 41 40 } 42 41 43 42 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 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())); 52 49 } 53 50 54 51 void ConnectionLayout::LoadXml(QDomDocument to_parse) { 55 if(!isRemoteNameDefined) { 56 printf("load xml: name not defined!\n"); 57 return; 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 68 void 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"); 58 79 } 59 80 60 QDomElement tmp=to_parse.firstChildElement("root"); 61 while(tmp.attribute("name")!=remoteName && !tmp.isNull()) tmp=to_parse.nextSiblingElement("root"); 81 if (!isRemoteNameDefined) { 82 isRemoteNameDefined = true; 83 remoteName = doc.firstChildElement("root").attribute("name"); 84 setRemoteName(remoteName); 85 SetAttribute("name", remoteName); 86 } 62 87 63 if(!tmp.isNull()) { 64 XmlWidget::LoadXml(tmp); 65 } else { 66 printf("%s not found in xml file \n",remoteName.toLocal8Bit().constData()); 67 } 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 } 68 112 } 69 113 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 void ConnectionLayout::removeDataRemote(DataRemote *data) { 115 dataremotes.removeOne(data); 114 116 } 115 117 116 void ConnectionLayout:: removeDataRemote(DataRemote*data) {117 dataremotes.removeOne(data);118 void ConnectionLayout::addDataRemote(DataRemote *data) { 119 dataremotes.append(data); 118 120 } 119 121 120 void ConnectionLayout::addDataRemote(DataRemote* data) { 121 dataremotes.append(data); 122 QString ConnectionLayout::getRemoteName() { return remoteName; } 123 void 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 } 122 128 } 123 129 124 QString ConnectionLayout::getRemoteName() { 125 return remoteName; 130 int 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; 126 173 } 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 state139 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 clobbered159 switch (ret) {160 case Z_NEED_DICT:161 ret = Z_DATA_ERROR; // and fall through162 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 return173 (void)inflateEnd(&strm);174 return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;175 }
Note:
See TracChangeset
for help on using the changeset viewer.