Changeset 234 in flair-src for trunk/tools/FlairGCS/src/Manager.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/Manager.cpp

    r30 r234  
    77#include "ConnectionLayout.h"
    88#include "communication.h"
     9#include "file_ui.h"
    910#include <QDate>
    1011#include <QFileDialog>
     
    1718#include <QVBoxLayout>
    1819#include <QModelIndex>
    19 //#include <qmetatype.h>
     20#include <QStatusBar>
    2021#include <qendian.h>
    2122#include <iostream>
     
    3637  qRegisterMetaType<QModelIndex>("QModelIndex"); // pour le file ui??
    3738  this->name = name;
    38 
     39//fprintf(stderr,"Manager %x\n",thread());
    3940  setWindowTitle(name);
    40 //statusBar()->showMessage(tr("Ready to serve"));
     41
    4142  // manager layout
    4243  managerLayout = new QVBoxLayout;
     
    7980  bool blocking = false;
    8081  UDT::setsockopt(serv, 0, UDT_RCVSYN, &blocking, sizeof(bool));
    81 
     82//UDT::setsockopt(serv, 0, UDT_CC, new CCCFactory<CUDPBlast>, sizeof(CCCFactory<CUDPBlast>));
    8283  sockaddr_in my_addr;
    8384  my_addr.sin_family = AF_INET;
     
    9697  QTimer *timer = new QTimer(this);
    9798  connect(timer, SIGNAL(timeout()), this, SLOT(acceptConnections()));
    98   timer->start(20);
     99  timer->start(500);
    99100}
    100101
    101102Manager::~Manager() {
    102   emit killUdtSockets();
     103  printf("%i\n",udtSockets.count());
     104  for (int i = 0; i < udtSockets.count(); i++) {
     105    udtSockets.at(i)->kill();
     106    udtSockets.at(i)->thread()->wait();
     107  }
    103108
    104109  // delete main_layout;
    105110  UDT::cleanup();
     111  printf("ok\n");
    106112}
    107113
    108114void Manager::acceptConnections(void) {
    109   static UDTSOCKET first_socket = 0;
    110115  UDTSOCKET socket;
    111116
     
    113118  int namelen = sizeof(their_addr);
    114119
    115   if (UDT::INVALID_SOCK ==
    116       (socket = UDT::accept(serv, (sockaddr *)&their_addr, &namelen))) {
     120  if (UDT::INVALID_SOCK ==(socket = UDT::accept(serv, (sockaddr *)&their_addr, &namelen))) {
    117121    if (UDT::getlasterror().getErrorCode() != 6002)
    118       printf("accept: %s, code %i\n", UDT::getlasterror().getErrorMessage(),
    119              UDT::getlasterror().getErrorCode());
     122      printf("accept error: %s, code %i\n", UDT::getlasterror().getErrorMessage(),UDT::getlasterror().getErrorCode());
    120123    return;
    121124  } else {
    122     printf("connected to %s:%i\n", inet_ntoa(their_addr.sin_addr),
    123            their_addr.sin_port);
    124 
    125     if (!first_socket) {
    126       first_socket = socket;
    127       return;
    128     } else {
    129       QThread *thread = new QThread(this);
    130       UdtSocket *new_udt = new UdtSocket(first_socket, socket, name);
    131       new_udt->moveToThread(thread);
    132 
    133       newConnection(new_udt);
    134 
    135       connect(this, SIGNAL(killUdtSockets()), thread, SLOT(quit()));
    136       connect(this, SIGNAL(killUdtSockets()), new_udt, SLOT(kill()),
    137               Qt::BlockingQueuedConnection);
    138 
    139       connect(thread, SIGNAL(started()), new_udt, SLOT(handleConnections()));
    140 
    141       thread->start();
    142       first_socket = 0;
    143     }
    144   }
    145 }
    146 
    147 void Manager::newConnection(UdtSocket *socket) {
    148 
    149   // no tabs to 2 tabs
    150   if (connectionsLayout.count() == 1) {
    151     tabBar->addTab(hiddenTabName);
    152     currentTab = 0;
    153     connectionsWidget.at(0)->show();
    154   }
    155 
    156   // layout utilisateur
    157   ConnectionLayout *newLayout = new ConnectionLayout(socket, "interface");
     125    QString name=QString("%1:%2").arg(inet_ntoa(their_addr.sin_addr)).arg(their_addr.sin_port);
     126    printf("connected to %s\n",name.toLocal8Bit().constData());
     127   
     128    QThread *thread = new QThread(this);
     129    UdtSocket *udtSocket = new UdtSocket(socket,name);
     130    udtSocket->moveToThread(thread);
     131    udtSockets.append(udtSocket);
     132
     133    connect(udtSocket, SIGNAL(newFileUI(UDTSOCKET)), this, SLOT(newFileUI(UDTSOCKET)));
     134    connect(udtSocket, SIGNAL(newConnectionLayout(QString)), this, SLOT(newConnectionLayout(QString)),Qt::BlockingQueuedConnection);
     135    connect(thread, SIGNAL(started()), udtSocket, SLOT(receiveData()));
     136
     137    thread->start(); 
     138  }
     139}
     140
     141void Manager::newConnectionLayout(QString name) {
     142  UdtSocket* udtSocket=(UdtSocket *)sender();
     143 
     144  ConnectionLayout *newLayout = new ConnectionLayout(udtSocket, name);
    158145  connectionsLayout.append(newLayout);
    159   connect(newLayout, SIGNAL(setRemoteName(QString)), this,
    160           SLOT(tabName(QString)));
    161   connect(newLayout, SIGNAL(computedRxRate(float)), this,
    162           SLOT(printRxRates(float)));
    163   connect(socket, SIGNAL(dataReady(char *, int)), newLayout,
    164           SLOT(receive(char *, int)));
    165   connect(newLayout, SIGNAL(destroyed(QObject *)), this,
    166           SLOT(layoutDestroyed(QObject *)));
    167   connect(socket, SIGNAL(destroyed()), newLayout, SLOT(deleteLater()));
    168 
    169   // widget
     146  connect(udtSocket, SIGNAL(UDTStats(QString)), newLayout, SIGNAL(UDTStats(QString)));//connection in 2 steps to get udtsocket as sender
     147  connect(newLayout, SIGNAL(UDTStats(QString)), this, SLOT(printUDTStats(QString)));
     148  connect(udtSocket, SIGNAL(dataReady(char *, int)), newLayout,SLOT(receive(char *, int)),Qt::BlockingQueuedConnection);
     149  connect(newLayout, SIGNAL(destroyed(QObject *)), this, SLOT(layoutDestroyed(QObject *)));
     150  connect(udtSocket, SIGNAL(destroyed()), newLayout, SLOT(deleteLater()));
     151 
     152   // widget
    170153  QWidget *newWidget = new QWidget();
    171154  connectionsWidget.append(newWidget);
     
    173156  managerLayout->insertWidget(1, newWidget);
    174157  newWidget->hide();
    175 
     158 
     159  //tab: avoid having only 1 tab (0, 2 or more)
    176160  if (connectionsLayout.count() == 1) { // first connection
    177     newWidget->show();
    178   } else { // add a tab for the new connection
    179     tabBar->addTab("unknown");
    180   }
    181 }
    182 
    183 void Manager::printRxRates(float rxRate) {
     161    connectionsWidget.at(0)->show();
     162    hiddenTabName = name;
     163  }
     164  if (connectionsLayout.count() == 2) {
     165    tabBar->addTab(hiddenTabName);
     166    currentTab = 0;
     167  }
     168  if (connectionsLayout.count() > 1) {
     169    tabBar->addTab(name);
     170  }
     171}
     172
     173void Manager::layoutDestroyed(QObject *obj) {
     174  int index = connectionsLayout.indexOf((ConnectionLayout *)obj);
     175
     176  //tab: avoid having only 1 tab (0, 2 or more)
     177  if (tabBar->count() > 1) {
     178    tabBar->removeTab(index);
     179  }
     180
     181  delete connectionsWidget.at(index);
     182  connectionsWidget.removeAt(index);
     183  connectionsLayout.removeOne((ConnectionLayout *)obj);
     184
     185  if (connectionsLayout.count() == 1) {
     186    hiddenTabName = tabBar->tabText(0);
     187    tabBar->removeTab(0);
     188  }
     189
     190  if (connectionsLayout.count() == 0) {
     191    status->showMessage("");
     192  }
     193}
     194
     195void Manager::newFileUI(UDTSOCKET socket) {
     196  //remove udtsocket as it will be automatically destroyed
     197  udtSockets.removeOne((UdtSocket *)sender());
     198 
     199  QThread *thread = new QThread(this);
     200  file_ui* fileUi = new file_ui(socket,name);
     201  fileUi->moveToThread(thread);
     202  connect(thread, SIGNAL(started()), fileUi, SLOT(receive()));
     203  connect(fileUi, SIGNAL(finished()), this, SLOT(deleteFileUI()));
     204  thread->start();
     205}
     206
     207void Manager::deleteFileUI(void) {
     208  sender()->thread()->quit();
     209  delete sender();
     210}
     211
     212void Manager::printUDTStats(QString stats) {
    184213  int index = connectionsLayout.indexOf((ConnectionLayout *)sender());
    185 
     214  if(index==-1) return;
     215 
    186216  if (tabBar->count() == 0) {
    187     status->showMessage(tr("rx rate %1 kB/s").arg(rxRate,0,'f',3));
     217    status->showMessage(stats);
    188218  } else if (index==tabBar->currentIndex()) {
    189     status->showMessage(tr("%1 rx rate %2 kB/s").arg(tabBar->tabText(index)).arg(rxRate,0,'f',3));
     219    status->showMessage(QString("%1: %2").arg(tabBar->tabText(index)).arg(stats));
    190220  }
    191221}
     
    200230    connectionsWidget.at(0)->show();
    201231  }
    202 }
    203 
    204 void Manager::tabName(QString name) {
    205   int index = connectionsLayout.indexOf((ConnectionLayout *)sender());
     232 
    206233  if (tabBar->count() == 0) {
    207     hiddenTabName = name;
     234    status->showMessage(connectionsLayout.at(0)->getUDTStats());
    208235  } else {
    209     tabBar->setTabText(index, name);
    210   }
    211 }
    212 
    213 void Manager::layoutDestroyed(QObject *obj) {
    214   int index = connectionsLayout.indexOf((ConnectionLayout *)obj);
    215 
    216   if (tabBar->count() > 1) {
    217     tabBar->removeTab(index);
    218   }
    219 
    220   delete connectionsWidget.at(index);
    221   connectionsWidget.removeAt(index);
    222   connectionsLayout.removeOne((ConnectionLayout *)obj);
    223 
    224   if (connectionsLayout.count() == 1) {
    225     hiddenTabName = tabBar->tabText(0);
    226     tabBar->removeTab(0);
    227   }
    228 
    229   if (connectionsLayout.count() == 0) {
    230     status->showMessage("");
     236    status->showMessage(QString("%1: %2").arg(tabBar->tabText(index)).arg(connectionsLayout.at(index)->getUDTStats()));
    231237  }
    232238}
     
    240246      QFile *file;
    241247      file = new QFile(dir_name + "/" +
    242                        connectionsLayout.at(i)->getRemoteName() + ".xml");
     248                       connectionsLayout.at(i)->getName() + ".xml");
    243249      if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
    244250        QMessageBox::warning(this, "Warning",
    245251                             "Enable to load " +
    246                                  connectionsLayout.at(i)->getRemoteName() +
     252                                 connectionsLayout.at(i)->getName() +
    247253                                 ".xml");
    248254        continue;
     
    256262        QMessageBox::critical(
    257263            this, "Error",
    258             "unable to read " + connectionsLayout.at(i)->getRemoteName() +
     264            "unable to read " + connectionsLayout.at(i)->getName() +
    259265                ".xml" + " (" + errorMsg + " at " + QString::number(errorLine) +
    260266                "," + QString::number(errorColumn) + ")");
     
    312318    connectionsLayout.at(i)->GetFullXml((QDomElement *)xml);
    313319
    314     QFile fichier(dir_name + "/" + connectionsLayout.at(i)->getRemoteName() +
     320    QFile fichier(dir_name + "/" + connectionsLayout.at(i)->getName() +
    315321                  ".xml");
    316322    QString write_doc = (xml->ownerDocument()).toString();
     
    322328    }
    323329    QTextStream stream(&fichier);
    324     stream << write_doc; // On utilise l'opérateur << pour écrire write_doc dans
    325                          // le document XML.
     330    stream << write_doc;
    326331    fichier.close();
    327332
Note: See TracChangeset for help on using the changeset viewer.