Changeset 234 in flair-src for trunk/tools/FlairGCS/src/Manager.cpp
- Timestamp:
- 04/10/18 17:05:27 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/FlairGCS/src/Manager.cpp
r30 r234 7 7 #include "ConnectionLayout.h" 8 8 #include "communication.h" 9 #include "file_ui.h" 9 10 #include <QDate> 10 11 #include <QFileDialog> … … 17 18 #include <QVBoxLayout> 18 19 #include <QModelIndex> 19 //#include <qmetatype.h>20 #include <QStatusBar> 20 21 #include <qendian.h> 21 22 #include <iostream> … … 36 37 qRegisterMetaType<QModelIndex>("QModelIndex"); // pour le file ui?? 37 38 this->name = name; 38 39 //fprintf(stderr,"Manager %x\n",thread()); 39 40 setWindowTitle(name); 40 //statusBar()->showMessage(tr("Ready to serve")); 41 41 42 // manager layout 42 43 managerLayout = new QVBoxLayout; … … 79 80 bool blocking = false; 80 81 UDT::setsockopt(serv, 0, UDT_RCVSYN, &blocking, sizeof(bool)); 81 82 //UDT::setsockopt(serv, 0, UDT_CC, new CCCFactory<CUDPBlast>, sizeof(CCCFactory<CUDPBlast>)); 82 83 sockaddr_in my_addr; 83 84 my_addr.sin_family = AF_INET; … … 96 97 QTimer *timer = new QTimer(this); 97 98 connect(timer, SIGNAL(timeout()), this, SLOT(acceptConnections())); 98 timer->start( 20);99 timer->start(500); 99 100 } 100 101 101 102 Manager::~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 } 103 108 104 109 // delete main_layout; 105 110 UDT::cleanup(); 111 printf("ok\n"); 106 112 } 107 113 108 114 void Manager::acceptConnections(void) { 109 static UDTSOCKET first_socket = 0;110 115 UDTSOCKET socket; 111 116 … … 113 118 int namelen = sizeof(their_addr); 114 119 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))) { 117 121 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()); 120 123 return; 121 124 } 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 141 void Manager::newConnectionLayout(QString name) { 142 UdtSocket* udtSocket=(UdtSocket *)sender(); 143 144 ConnectionLayout *newLayout = new ConnectionLayout(udtSocket, name); 158 145 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 170 153 QWidget *newWidget = new QWidget(); 171 154 connectionsWidget.append(newWidget); … … 173 156 managerLayout->insertWidget(1, newWidget); 174 157 newWidget->hide(); 175 158 159 //tab: avoid having only 1 tab (0, 2 or more) 176 160 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 173 void 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 195 void 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 207 void Manager::deleteFileUI(void) { 208 sender()->thread()->quit(); 209 delete sender(); 210 } 211 212 void Manager::printUDTStats(QString stats) { 184 213 int index = connectionsLayout.indexOf((ConnectionLayout *)sender()); 185 214 if(index==-1) return; 215 186 216 if (tabBar->count() == 0) { 187 status->showMessage( tr("rx rate %1 kB/s").arg(rxRate,0,'f',3));217 status->showMessage(stats); 188 218 } 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)); 190 220 } 191 221 } … … 200 230 connectionsWidget.at(0)->show(); 201 231 } 202 } 203 204 void Manager::tabName(QString name) { 205 int index = connectionsLayout.indexOf((ConnectionLayout *)sender()); 232 206 233 if (tabBar->count() == 0) { 207 hiddenTabName = name;234 status->showMessage(connectionsLayout.at(0)->getUDTStats()); 208 235 } 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())); 231 237 } 232 238 } … … 240 246 QFile *file; 241 247 file = new QFile(dir_name + "/" + 242 connectionsLayout.at(i)->get RemoteName() + ".xml");248 connectionsLayout.at(i)->getName() + ".xml"); 243 249 if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) { 244 250 QMessageBox::warning(this, "Warning", 245 251 "Enable to load " + 246 connectionsLayout.at(i)->get RemoteName() +252 connectionsLayout.at(i)->getName() + 247 253 ".xml"); 248 254 continue; … … 256 262 QMessageBox::critical( 257 263 this, "Error", 258 "unable to read " + connectionsLayout.at(i)->get RemoteName() +264 "unable to read " + connectionsLayout.at(i)->getName() + 259 265 ".xml" + " (" + errorMsg + " at " + QString::number(errorLine) + 260 266 "," + QString::number(errorColumn) + ")"); … … 312 318 connectionsLayout.at(i)->GetFullXml((QDomElement *)xml); 313 319 314 QFile fichier(dir_name + "/" + connectionsLayout.at(i)->get RemoteName() +320 QFile fichier(dir_name + "/" + connectionsLayout.at(i)->getName() + 315 321 ".xml"); 316 322 QString write_doc = (xml->ownerDocument()).toString(); … … 322 328 } 323 329 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; 326 331 fichier.close(); 327 332
Note:
See TracChangeset
for help on using the changeset viewer.