Ignore:
Timestamp:
04/10/18 17:05:27 (6 years ago)
Author:
Sanahuja Guillaume
Message:

create file oscket only when necessary

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/FlairGCS/src/ConnectionLayout.cpp

    r222 r234  
    88#include <qendian.h>
    99#include "communication.h"
    10 #include <zlib.h>
    11 #include <assert.h>
    12 
    13 #define COMPRESS_CHUNK 1024
    14 #define RX_TIME 5000
    1510
    1611ConnectionLayout::ConnectionLayout(UdtSocket *socket, QString name)
    1712    : Layout(NULL, name, "root") {
    18   isRemoteNameDefined = false;
    1913  this->socket = socket;
    20 
    21   total_received=0;
    22   receive_timer = new QTimer(this);
    23   connect(receive_timer, SIGNAL(timeout()), this, SLOT(computeRxRate()));
    24   receive_timer->start(RX_TIME);
     14  this->name = name;
    2515}
    2616
    2717ConnectionLayout::~ConnectionLayout() {
    28   receive_timer->stop();
    2918}
    3019
    31 void ConnectionLayout::computeRxRate(void) {
    32   float receive_rate=((float)total_received/(RX_TIME/1000))/1000;//in Ko/s
    33   total_received=0;
    34   computedRxRate(receive_rate);
     20QString ConnectionLayout::getUDTStats() {
     21  return socket->getUDTStats();
    3522}
    3623
    3724void ConnectionLayout::receive(char *buf, int size) {
    38   total_received+=size-1;
    3925  // printf("trame %x\n",buf[0]);
    4026  // for(int i=0; i<size;i++) printf("%x ",buf[i]);
    4127  // printf("\n");
    42   switch (buf[0]) {
    43   case ZLIB_HEADER: {
    44     ssize_t out_size;
    45     char *uncompressbuf;
    46     uncompressBuffer(buf, size, &uncompressbuf, &out_size);
    47     handleUncompressedFrame(uncompressbuf, out_size);
    48     free(uncompressbuf);
    49     break;
    50   }
    51   default:
    52     handleUncompressedFrame(buf, size);
    53   }
    54   free(buf);
    55 }
    56 
    57 void ConnectionLayout::XmlToSend(QDomDocument doc) {
    58   // printf("xml to send\n%s\n",doc.toString().toLocal8Bit().constData());
    59   // xml to send a mettre dans le manager
    60   QMetaObject::invokeMethod(
    61       socket, "write", Qt::BlockingQueuedConnection,
    62       Q_ARG(const char *, doc.toString().toLocal8Bit().constData()),
    63       Q_ARG(qint64, doc.toString().toLocal8Bit().length()));
    64 }
    65 
    66 void ConnectionLayout::LoadXml(QDomDocument to_parse) {
    67   if (!isRemoteNameDefined) {
    68     printf("load xml: name not defined!\n");
    69     return;
    70   }
    71 
    72   QDomElement tmp = to_parse.firstChildElement("root");
    73   while (tmp.attribute("name") != remoteName && !tmp.isNull())
    74     tmp = to_parse.nextSiblingElement("root");
    75 
    76   if (!tmp.isNull()) {
    77     XmlWidget::LoadXml(tmp);
    78   } else {
    79     printf("%s not found in xml file \n", remoteName.toLocal8Bit().constData());
    80   }
    81 }
    82 
    83 void ConnectionLayout::handleUncompressedFrame(char *buf, ssize_t size) {
    8428  switch ((unsigned char)buf[0]) {
    8529  case XML_HEADER: {
     
    9236    if (!doc.setContent(xml)) {
    9337      printf("prob setContent fichier\n");
    94     }
    95 
    96     if (!isRemoteNameDefined) {
    97       isRemoteNameDefined = true;
    98       remoteName = doc.firstChildElement("root").attribute("name");
    99       setRemoteName(remoteName);
    100       SetAttribute("name", remoteName);
    10138    }
    10239
     
    12764}
    12865
     66void ConnectionLayout::XmlToSend(QDomDocument doc) {
     67  // printf("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
     77void 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    printf("%s not found in xml file \n", name.toLocal8Bit().constData());
     86  }
     87}
     88
    12989void ConnectionLayout::removeDataRemote(DataRemote *data) {
    13090  dataremotes.removeOne(data);
     
    13595}
    13696
    137 QString ConnectionLayout::getRemoteName() { return remoteName; }
     97QString ConnectionLayout::getName() { return name; }
    13898
    13999void ConnectionLayout::drawDatas(char *buf, int buf_size, uint16_t period,
     
    144104}
    145105
    146 int ConnectionLayout::uncompressBuffer(char *in, ssize_t in_size, char **out,
    147                                        ssize_t *out_size) {
    148   int ret;
    149   unsigned have;
    150   z_stream strm;
     106QString ConnectionLayout::getDocRootName(char* buf, int size) {
     107  QString xml;
     108  QDomDocument doc;
     109  xml = QString((char *)buf);
     110  xml.resize(size);
    151111
    152   // allocate inflate state
    153   strm.zalloc = Z_NULL;
    154   strm.zfree = Z_NULL;
    155   strm.opaque = Z_NULL;
    156   strm.avail_in = 0;
    157   strm.next_in = Z_NULL;
    158   ret = inflateInit(&strm);
    159   if (ret != Z_OK)
    160     return ret;
    161 
    162   *out = (char *)malloc(COMPRESS_CHUNK);
    163   if (!(*out))
    164     return Z_BUF_ERROR;
    165 
    166   strm.avail_in = in_size;
    167   strm.next_in = (unsigned char *)in;
    168   strm.avail_out = COMPRESS_CHUNK;
    169   strm.next_out = (unsigned char *)*out;
    170 
    171   ret = inflate(&strm, Z_NO_FLUSH);
    172   assert(ret != Z_STREAM_ERROR); // state not clobbered
    173   switch (ret) {
    174   case Z_NEED_DICT:
    175     ret = Z_DATA_ERROR; // and fall through
    176   case Z_DATA_ERROR:
    177   case Z_MEM_ERROR:
    178     (void)inflateEnd(&strm);
    179     return ret;
     112  if (!doc.setContent(xml)) {
     113    printf("prob setContent fichier\n");
    180114  }
    181   have = COMPRESS_CHUNK - strm.avail_out;
    182   *out_size = have;
    183 
    184   // printf("%i -> %i\n",in_size,have);
    185   // printf("%s\n",*out);
    186   // clean up and return
    187   (void)inflateEnd(&strm);
    188   return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
     115 
     116  return doc.firstChildElement("root").attribute("name");
    189117}
Note: See TracChangeset for help on using the changeset viewer.