Changeset 234 in flair-src for trunk/tools/FlairGCS/src/file_ui.cpp


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/file_ui.cpp

    r214 r234  
    44// %flair:license}
    55#include "file_ui.h"
     6#include "communication.h"
     7
    68#include <stdio.h>
    7 
    89#include <cstring>
    910#include <cstdlib>
     
    2223#include <QStringList>
    2324#include <QFormLayout>
    24 
    25 #ifndef WIN32
    26 #include <arpa/inet.h>
    27 #else
    28 #include <winsock2.h>
    29 #include <ws2tcpip.h>
    30 #endif
    31 
     25#include <QThread>
     26
     27#include <unistd.h>
    3228using namespace std;
    3329
    34 file_ui::file_ui() {
     30file_ui::file_ui(UDTSOCKET socket,QString name): QObject() {
     31  this->socket=socket;
     32  this->name=name;
     33 
     34  bool blocking = true;
     35  if (UDT::setsockopt(socket, 0, UDT_SNDSYN, &blocking, sizeof(bool))!= 0) {
     36    printf("UDT::setsockopt error (UDT_SNDSYN) %s\n",UDT::getlasterror().getErrorMessage());
     37  }
     38   
     39  linger _linger;
     40  _linger.l_onoff=1;
     41  _linger.l_linger=180;
     42 
     43  if (UDT::setsockopt(socket, 0, UDT_LINGER, &_linger, sizeof(struct linger)) != 0)
     44    printf("UDT::setsockopt error (UDT_LINGER) %s\n",UDT::getlasterror().getErrorMessage());
     45   
    3546  dialog = new QDialog();
     47//fprintf(stderr,"creator file ui %x\n",thread());
    3648  dialog->setWindowTitle("log files");
    3749  QGridLayout *main_layout = new QGridLayout(dialog);
     
    4254  input_text->setText("add your log comment here");
    4355  input_cleared = false;
    44 
    4556  ok_button->setEnabled(false);
    4657
     
    5667  main_layout->addWidget(ok_button, 3, 0);
    5768
    58   connect(ok_button, SIGNAL(clicked()), this, SLOT(save()),
    59           Qt::QueuedConnection);
    60   connect(this, SIGNAL(showDialog()), dialog, SLOT(show()),
    61           Qt::QueuedConnection);
    62   connect(this, SIGNAL(appendToLog(QString)), log_text, SLOT(append(QString)),
    63           Qt::QueuedConnection);
    64   connect(input_text, SIGNAL(cursorPositionChanged()), this,
    65           SLOT(clearInputText()), Qt::QueuedConnection);
     69  //connect for multithreaded stuffs
     70  connect(ok_button, SIGNAL(clicked()), this, SLOT(save()),Qt::QueuedConnection);
     71  connect(input_text, SIGNAL(cursorPositionChanged()), this,SLOT(clearInputText()),Qt::DirectConnection);
     72  connect(this, SIGNAL(appendToLog(QString)), log_text, SLOT(append(QString)));
    6673
    6774  file_names = new QStringList();
    68 }
    69 
    70 file_ui::~file_ui() { delete dialog; }
    71 
    72 void file_ui::log(QString text) { appendToLog(text); }
     75 
     76  dialog->show();
     77}
     78
     79file_ui::~file_ui() {
     80  delete dialog;
     81}
     82
     83void file_ui::receive(void) {
     84  char *recv_buf;
     85  int bytesRead;
     86  bool flag_new_seq = true;
     87  QString folder_name;
     88//fprintf(stderr,"file_ui thread %x\n",thread());
     89  while(1) {
     90    // receive file info
     91    recv_buf = (char *)malloc(1024);
     92    bytesRead = UDT::recvmsg(socket, recv_buf, 1024);
     93    if (bytesRead <= 0) {
     94      free(recv_buf);
     95      break;
     96    }
     97
     98    int size;
     99    memcpy(&size, &recv_buf[1], sizeof(int));
     100    if (recv_buf[0] == FILE_INFO_BIG_ENDIAN)
     101      size = qFromBigEndian(size);
     102
     103    // printf("file_ui recu %i %x\n",bytesRead,recv_buf[0]);
     104    if ((recv_buf[0]==FILE_INFO_LITTLE_ENDIAN || recv_buf[0]==FILE_INFO_BIG_ENDIAN) && size>0) {
     105      if (flag_new_seq == true) {
     106        // create directory for storage
     107        QDateTime dateTime = QDateTime::currentDateTime();
     108        folder_name = dateTime.toString("yyyyMMdd_hhmm") + "_" + name;
     109        if (QDir().exists(folder_name) == true) {
     110          folder_name = dateTime.toString("yyyyMMdd_hhmm_ss") + "_" + name;
     111        }
     112        QDir().mkdir(folder_name);
     113
     114        flag_new_seq = false;
     115        appendToLog("Creating directory " + folder_name);
     116      }
     117
     118      QString file_name=QString::fromAscii((const char *)&recv_buf[5], bytesRead - 5);
     119      QString file_path=folder_name+"/"+file_name;
     120      appendToLog(QString("receiving %1 (%2 bytes)").arg(file_name).arg(size));
     121      QFile fichier(file_path);
     122
     123      if (!fichier.open(QIODevice::WriteOnly)) {
     124        appendToLog("      could not write to file!");
     125      } else {
     126        // receive file
     127        recv_buf = (char *)realloc((void *)recv_buf, size);
     128        bytesRead = UDT::recvmsg(socket, recv_buf, size);
     129        if (bytesRead != size) {
     130          appendToLog(QString("      error receiving file! (%1/%2)").arg(bytesRead).arg(size));
     131          free(recv_buf);
     132          break;
     133        } else {
     134          appendToLog("      ok");
     135        }
     136
     137        QDataStream stream(&fichier);
     138        stream.writeRawData(recv_buf, size);
     139        fichier.close();
     140
     141        addFile(file_path);
     142      }
     143
     144      free(recv_buf);
     145    } else if (recv_buf[0] == END_SENDING_FILES) {
     146      //end ack
     147      UDT::sendmsg(socket,&recv_buf[0],1);
     148      endOfFiles();
     149      UDT::close(socket);
     150      printf("disconnected from log files\n");
     151      break;
     152    }
     153  }
     154}
    73155
    74156void file_ui::addFile(QString file_path) {
     
    86168    dbt2csv(file_path.replace(QString(".txt"), QString(".dbt")));
    87169  }
    88 
    89   if (file_names->size() == 1) {
    90     input_cleared = false;
    91     showDialog();
    92   }
    93170}
    94171
     
    101178    if (info.size() > max_file_size) {
    102179      max_file_size = info.size();
    103       csv_combo->setCurrentIndex(i +
    104                                  1); // first item of combobox is already taken
     180      csv_combo->setCurrentIndex(i+1); // first item of combobox is already taken
    105181    }
    106182  }
     
    112188  QStringList data_type;
    113189
    114   QString filename =
    115       file_path.section('/', -1); // remove path for displaying on logs
     190  QString filename =file_path.section('/', -1); // remove path for displaying on logs
    116191  appendToLog(QString("converting %1 to csv").arg(filename));
    117192
     
    145220      break;
    146221    QString txt_line = txt_in.readLine();
    147     data_type.append(txt_line.section(
    148         "(",
    149         -1)); // on part de la fin pour trouver la premiere parenthese ouvrante
     222    data_type.append(txt_line.section("(",-1)); // on part de la fin pour trouver la premiere parenthese ouvrante
    150223    // printf("type %s\n",txt_line.section("(",-1).toLocal8Bit().constData());
    151224  }
     
    241314  }
    242315
    243   log_text->clear();
    244   input_cleared = true; // avoid clearing it with setText
    245   input_text->setText("add your log comment here");
    246   file_names->clear();
    247   csv_combo->clear();
    248   csv_combo->addItem(QString("(no base time)"));
    249 
    250   dialog->setVisible(false);
    251   ok_button->setEnabled(false);
    252316  emit finished();
    253317}
Note: See TracChangeset for help on using the changeset viewer.