source: flair-src/trunk/tools/FlairGCS/src/Manager.cpp@ 433

Last change on this file since 433 was 399, checked in by Sanahuja Guillaume, 4 years ago

change color of status bar in gcs if high bandwidth usage is detected

File size: 12.1 KB
RevLine 
[10]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[10]4// %flair:license}
[9]5#include "Manager.h"
6#include "UdtSocket.h"
7#include "ConnectionLayout.h"
8#include "communication.h"
[234]9#include "file_ui.h"
[9]10#include <QDate>
11#include <QFileDialog>
12#include <QMessageBox>
13#include <QPushButton>
14#include <QTabBar>
15#include <QTimer>
16#include <QThread>
17#include <QTextStream>
18#include <QVBoxLayout>
19#include <QModelIndex>
[234]20#include <QStatusBar>
[9]21#include <qendian.h>
22#include <iostream>
23#include <string>
24#include <fstream>
25#include <stdio.h>
26
27#ifndef WIN32
[15]28#include <arpa/inet.h>
[9]29#else
[15]30#include <winsock2.h>
31#include <ws2tcpip.h>
[9]32#endif
33
34using namespace std;
35
[15]36Manager::Manager(QString name, int port) : QWidget() {
37 qRegisterMetaType<QModelIndex>("QModelIndex"); // pour le file ui??
38 this->name = name;
[248]39//fprintf(stderr,stderr,"Manager %x\n",thread());
[234]40 setWindowTitle(name);
[9]41
[15]42 // manager layout
43 managerLayout = new QVBoxLayout;
44 setLayout(managerLayout);
[9]45
[15]46 // tab bar for multiple connections
47 tabBar = new QTabBar();
[247]48 //tabBar->setTabsClosable(true);
[15]49 managerLayout->addWidget(tabBar);
50 connect(tabBar, SIGNAL(currentChanged(int)), this,
51 SLOT(tabBarCurrentChanged(int)));
52 currentTab = 0;
[9]53
[15]54 // layout boutons
55 button_layout = new QGridLayout();
56 managerLayout->addLayout(button_layout);
[9]57
[15]58 // boutons du button_Layout
59 send_button = new QPushButton("apply all");
60 reset_button = new QPushButton("reset all");
61 load_button = new QPushButton("load all locally");
62 save_button = new QPushButton("save all locally");
63 button_layout->addWidget(send_button, 0, 0);
64 button_layout->addWidget(reset_button, 0, 1);
65 button_layout->addWidget(load_button, 0, 2);
66 button_layout->addWidget(save_button, 0, 3);
[9]67
[15]68 connect(send_button, SIGNAL(clicked(bool)), this, SLOT(send()));
69 connect(reset_button, SIGNAL(clicked(bool)), this, SLOT(reset()));
70 connect(load_button, SIGNAL(clicked(bool)), this, SLOT(load()));
71 connect(save_button, SIGNAL(clicked(bool)), this, SLOT(save()));
[9]72
[30]73 status=new QStatusBar();
74 status->setSizeGripEnabled(false);
[399]75 managerLayout->addWidget(status);
[30]76
[15]77 UDT::startup();
78 serv = UDT::socket(AF_INET, SOCK_DGRAM, 0);
[9]79
[15]80 // for non blocking accept
81 bool blocking = false;
82 UDT::setsockopt(serv, 0, UDT_RCVSYN, &blocking, sizeof(bool));
[234]83//UDT::setsockopt(serv, 0, UDT_CC, new CCCFactory<CUDPBlast>, sizeof(CCCFactory<CUDPBlast>));
[15]84 sockaddr_in my_addr;
85 my_addr.sin_family = AF_INET;
86 my_addr.sin_port = htons(port);
87 my_addr.sin_addr.s_addr = INADDR_ANY;
88 memset(&(my_addr.sin_zero), '\0', 8);
[9]89
[15]90 if (UDT::ERROR == UDT::bind(serv, (sockaddr *)&my_addr, sizeof(my_addr))) {
[244]91 fprintf(stderr,"bind error, %s\n", UDT::getlasterror().getErrorMessage());
[15]92 }
[9]93
[15]94 if (UDT::ERROR == UDT::listen(serv, 1)) {
[244]95 fprintf(stderr,"listen error, %s\n", UDT::getlasterror().getErrorMessage());
[15]96 }
[9]97
[247]98
[248]99 icon_green = QIcon(":green.png");
100 icon_red = QIcon(":red.png");
101 icon_orange = QIcon(":orange.png");
[247]102
[15]103 QTimer *timer = new QTimer(this);
104 connect(timer, SIGNAL(timeout()), this, SLOT(acceptConnections()));
[234]105 timer->start(500);
[9]106}
107
108Manager::~Manager() {
[247]109 for (int i = 0; i < connections.count(); i++) {
[248]110 if(connections.at(i).socket!=NULL) {
111 connections.at(i).socket->kill();
112 connections.at(i).socket->thread()->wait();
113 }
[234]114 }
[9]115
[15]116 // delete main_layout;
117 UDT::cleanup();
[9]118}
119
120void Manager::acceptConnections(void) {
[15]121 UDTSOCKET socket;
[9]122
[15]123 sockaddr_in their_addr;
124 int namelen = sizeof(their_addr);
[9]125
[234]126 if (UDT::INVALID_SOCK ==(socket = UDT::accept(serv, (sockaddr *)&their_addr, &namelen))) {
[15]127 if (UDT::getlasterror().getErrorCode() != 6002)
[244]128 fprintf(stderr,"accept error: %s, code %i\n", UDT::getlasterror().getErrorMessage(),UDT::getlasterror().getErrorCode());
[15]129 return;
130 } else {
[234]131 QString name=QString("%1:%2").arg(inet_ntoa(their_addr.sin_addr)).arg(their_addr.sin_port);
[244]132 fprintf(stderr,"connected to %s\n",name.toLocal8Bit().constData());
[234]133
134 QThread *thread = new QThread(this);
135 UdtSocket *udtSocket = new UdtSocket(socket,name);
136 udtSocket->moveToThread(thread);
[247]137
[234]138 connect(udtSocket, SIGNAL(newFileUI(UDTSOCKET)), this, SLOT(newFileUI(UDTSOCKET)));
139 connect(udtSocket, SIGNAL(newConnectionLayout(QString)), this, SLOT(newConnectionLayout(QString)),Qt::BlockingQueuedConnection);
140 connect(thread, SIGNAL(started()), udtSocket, SLOT(receiveData()));
[9]141
[234]142 thread->start();
[15]143 }
[9]144}
145
[234]146void Manager::newConnectionLayout(QString name) {
147 UdtSocket* udtSocket=(UdtSocket *)sender();
148
149 ConnectionLayout *newLayout = new ConnectionLayout(udtSocket, name);
[399]150 connect(udtSocket, SIGNAL(UDTStats(QString,QString,bool)), newLayout, SIGNAL(UDTStats(QString,QString,bool)));//connection in 2 steps to get udtsocket as sender
151 connect(newLayout, SIGNAL(UDTStats(QString,QString,bool)), this, SLOT(printUDTStats(QString,QString,bool)));
[234]152 connect(udtSocket, SIGNAL(dataReady(char *, int)), newLayout,SLOT(receive(char *, int)),Qt::BlockingQueuedConnection);
153 connect(newLayout, SIGNAL(destroyed(QObject *)), this, SLOT(layoutDestroyed(QObject *)));
[244]154 connect(udtSocket, SIGNAL(destroyed(QObject *)), this, SLOT(udtSocketDestroyed(QObject *)));
[260]155 connect(udtSocket, SIGNAL(destroyed(QObject *)), newLayout, SLOT(udtSocketDestroyed(QObject *)));
[234]156
[247]157 // widget
[15]158 QWidget *newWidget = new QWidget();
159 newWidget->setLayout(newLayout->getQGridLayout());
160 managerLayout->insertWidget(1, newWidget);
161 newWidget->hide();
[234]162
[247]163 connections_t connection;
164 connection.layout=newLayout;
165 connection.widget=newWidget;
166 connection.socket=udtSocket;
167 connections.append(connection);
168
[234]169 //tab: avoid having only 1 tab (0, 2 or more)
[247]170 if (connections.count() == 1) { // first connection
171 newWidget->show();
[234]172 hiddenTabName = name;
[15]173 }
[247]174 if (connections.count() == 2) {
[255]175 if(connections[0].socket!=NULL) {
176 tabBar->addTab(icon_green,hiddenTabName);
177 } else {
178 tabBar->addTab(icon_red,hiddenTabName);
179 }
[15]180 currentTab = 0;
181 }
[247]182 if (connections.count() > 1) {
[248]183 tabBar->addTab(icon_green,name);
[15]184 }
[9]185}
186
[244]187void Manager::udtSocketDestroyed(QObject *obj) {
[247]188 //mark socket as not valid, it can be a connection lost
[260]189 //in this case we keep the layout for reading, and mark it as problematic (socket=NULL)
[247]190 for(int i=0;i<connections.count();i++) {
191 if(connections.at(i).socket==(UdtSocket *)obj) {
192 connections[i].socket=NULL;
[248]193 tabBar->setTabIcon(i,icon_red);
[247]194 break;
195 }
196 }
[244]197}
198
[15]199void Manager::layoutDestroyed(QObject *obj) {
[247]200 //remove the connection, it comes from a proper close
201 int index=-1;
202 for(int i=0;i<connections.count();i++) {
203 if(connections.at(i).layout==(ConnectionLayout *)obj) {
204 delete connections.at(i).widget;
205 //connections[i].widget=NULL;
206 //connections[i].layout=NULL;
207 connections.removeAt(i);
208 index=i;
209 break;
210 }
211 }
[244]212
[247]213 if(index==-1) {
214 fprintf(stderr,"layoutDestroyed: error, layout not found!\n");
215 return;
216 }
217
218 //tab: avoid having only 1 tab (only 0, 2 or more)
[15]219 if (tabBar->count() > 1) {
220 tabBar->removeTab(index);
221 }
[9]222
[247]223 if (connections.count() == 1) {
[15]224 hiddenTabName = tabBar->tabText(0);
225 tabBar->removeTab(0);
226 }
[30]227
[247]228 if (connections.count() == 0) {
[30]229 status->showMessage("");
230 }
[9]231}
232
[234]233void Manager::newFileUI(UDTSOCKET socket) {
234 QThread *thread = new QThread(this);
235 file_ui* fileUi = new file_ui(socket,name);
236 fileUi->moveToThread(thread);
237 connect(thread, SIGNAL(started()), fileUi, SLOT(receive()));
238 connect(fileUi, SIGNAL(finished()), this, SLOT(deleteFileUI()));
239 thread->start();
240}
241
242void Manager::deleteFileUI(void) {
243 sender()->thread()->quit();
244 delete sender();
245}
246
[399]247void Manager::printUDTStats(QString stats,QString stylesheet,bool loosingPackets) {
[247]248 int index = -1;
249 for(int i=0;i<connections.count();i++) {
250 if(connections.at(i).layout==(ConnectionLayout *)sender()) {
251 index=i;
252 break;
253 }
254 }
255
[234]256 if(index==-1) return;
257
[248]258 if(!loosingPackets) {
259 tabBar->setTabIcon(index,icon_green);
260 } else {
261 tabBar->setTabIcon(index,icon_orange);
262 }
263
[234]264 if (tabBar->count() == 0) {
[399]265 status->setStyleSheet(stylesheet);
[234]266 status->showMessage(stats);
267 } else if (index==tabBar->currentIndex()) {
[399]268 status->setStyleSheet(stylesheet);
[234]269 status->showMessage(QString("%1: %2").arg(tabBar->tabText(index)).arg(stats));
270 }
271}
272
273void Manager::tabBarCurrentChanged(int index) {
[244]274
[234]275 if (index >= 0) {
[244]276 //if we are coming from layout destroyed
[247]277 if(currentTab<connections.count()) connections.at(currentTab).widget->hide();
278 connections.at(index).widget->show();
[234]279 currentTab = index;
280 } else {
281 currentTab = 0;
[247]282 connections.at(0).widget->show();
[234]283 }
[247]284 QString msg="not connected";
[234]285 if (tabBar->count() == 0) {
[247]286 if(connections.at(0).socket!=NULL) msg=connections.at(0).socket->getUDTStats();
287
[234]288 } else {
[247]289 if(connections.at(index).socket!=NULL) msg=QString("%1: %2").arg(tabBar->tabText(index)).arg(connections.at(index).socket->getUDTStats());
[234]290 }
[247]291 status->showMessage(msg);
[234]292}
293
[9]294void Manager::load(void) {
[15]295 QString dir_name =
296 QFileDialog::getExistingDirectory(this, "Select a directory", 0, 0);
[9]297
[15]298 if (dir_name != "") {
[247]299 for (int i = 0; i < connections.count(); i++) {
[15]300 QFile *file;
301 file = new QFile(dir_name + "/" +
[247]302 connections.at(i).layout->getName() + ".xml");
[15]303 if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
304 QMessageBox::warning(this, "Warning",
305 "Enable to load " +
[247]306 connections.at(i).layout->getName() +
[15]307 ".xml");
308 continue;
309 }
[9]310
[15]311 QDomDocument doc;
312 QString errorMsg;
313 int errorLine;
314 int errorColumn;
315 if (!doc.setContent(file, &errorMsg, &errorLine, &errorColumn)) {
316 QMessageBox::critical(
317 this, "Error",
[247]318 "unable to read " + connections.at(i).layout->getName() +
[15]319 ".xml" + " (" + errorMsg + " at " + QString::number(errorLine) +
320 "," + QString::number(errorColumn) + ")");
321 } else {
[269]322 connections.at(i).layout->LoadXml(&doc);
[15]323 }
324 delete file;
[9]325 }
[15]326 }
[9]327}
328
329void Manager::save(void) {
[15]330 bool isUptodate = true;
[9]331
[247]332 for (int i = 0; i < connections.count(); i++) {
333 if (!connections.at(i).layout->IsUptodate()) {
[15]334 isUptodate = false;
335 break;
[9]336 }
[15]337 }
[9]338
[15]339 if (!isUptodate) {
340 QMessageBox msgBox;
341 msgBox.setText("There are pending modifications");
342 msgBox.setInformativeText("Apply and save?");
343 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
344 msgBox.setDefaultButton(QMessageBox::Yes);
345 int ret = msgBox.exec();
[9]346
[15]347 switch (ret) {
348 case QMessageBox::Yes:
349 send();
350 break;
351 case QMessageBox::Cancel:
352 return;
353 break;
354 default:
355 // should never be reached
356 break;
[9]357 }
[15]358 }
[9]359
[15]360 // create dirctory for storage
361 QDateTime dateTime = QDateTime::currentDateTime();
362 QString dir_name =
363 "configs_" + dateTime.toString("yyyyMMdd_hhmm") + "_" + name;
364 if (QDir().exists(dir_name) == true) {
365 dir_name = "configs_" + dateTime.toString("yyyyMMdd_hhmm_ss") + "_" + name;
366 }
367 QDir().mkdir(dir_name);
[9]368
[247]369 for (int i = 0; i < connections.count(); i++) {
[15]370 QDomDocument *xml = new QDomDocument("remote_ui_xml");
[9]371
[247]372 connections.at(i).layout->GetFullXml((QDomElement *)xml);
[9]373
[247]374 QFile fichier(dir_name + "/" + connections.at(i).layout->getName() +
[15]375 ".xml");
376 QString write_doc = (xml->ownerDocument()).toString();
[9]377
[15]378 if (!fichier.open(QIODevice::WriteOnly)) {
379 fichier.close();
380 QMessageBox::critical(this, "Error", "Enable to write XML");
381 continue;
[9]382 }
[15]383 QTextStream stream(&fichier);
[234]384 stream << write_doc;
[15]385 fichier.close();
[9]386
[15]387 delete xml;
388 }
389
390 QMessageBox::information(this, "save all", "saved to ./" + dir_name);
[9]391}
392
393void Manager::send(void) {
[247]394 for (int i = 0; i < connections.count(); i++) {
[260]395 if(connections.at(i).socket!=NULL) {
396 QDomDocument doc("remote_ui_xml");
397 connections.at(i).layout->GetUpdateXml((QDomElement *)&doc);
398 // fprintf(stderr,"merge\n%s\n",doc.toString().toLocal8Bit().constData());
[9]399
[260]400 connections.at(i).layout->XmlToSend(doc);
401 }
[15]402 }
[9]403}
404
405void Manager::reset() {
[247]406 for (int i = 0; i < connections.count(); i++)
407 connections.at(i).layout->ResetAllChilds();
[9]408}
Note: See TracBrowser for help on using the repository browser.