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

Last change on this file since 255 was 255, checked in by Sanahuja Guillaume, 3 years ago

maj

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