Changeset 15 in flair-src for trunk/tools/FlairGCS/src/Manager.cpp
- Timestamp:
- 04/08/16 15:40:57 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/FlairGCS/src/Manager.cpp
r10 r15 25 25 26 26 #ifndef WIN32 27 27 #include <arpa/inet.h> 28 28 #else 29 30 29 #include <winsock2.h> 30 #include <ws2tcpip.h> 31 31 #endif 32 32 33 33 using namespace std; 34 34 35 Manager::Manager(QString name,int port):QWidget() { 36 qRegisterMetaType<QModelIndex>("QModelIndex");//pour le file ui?? 37 this->name=name; 38 39 setWindowTitle(name); 40 41 //manager layout 42 managerLayout = new QVBoxLayout; 43 setLayout(managerLayout); 44 45 //tab bar for multiple connections 46 tabBar=new QTabBar(); 47 managerLayout->addWidget(tabBar); 48 connect(tabBar, SIGNAL(currentChanged(int)),this, SLOT(tabBarCurrentChanged(int))); 49 currentTab=0; 50 51 //layout boutons 52 button_layout = new QGridLayout(); 53 managerLayout->addLayout(button_layout); 54 55 //boutons du button_Layout 56 send_button = new QPushButton("apply all"); 57 reset_button = new QPushButton("reset all"); 58 load_button = new QPushButton("load all locally"); 59 save_button = new QPushButton("save all locally"); 60 button_layout->addWidget(send_button,0,0); 61 button_layout->addWidget(reset_button,0,1); 62 button_layout->addWidget(load_button,0,2); 63 button_layout->addWidget(save_button,0,3); 64 65 connect(send_button, SIGNAL(clicked(bool)),this, SLOT(send())); 66 connect(reset_button, SIGNAL(clicked(bool)),this, SLOT(reset())); 67 connect(load_button, SIGNAL(clicked(bool)),this, SLOT(load())); 68 connect(save_button, SIGNAL(clicked(bool)),this, SLOT(save())); 69 70 UDT::startup(); 71 serv = UDT::socket(AF_INET, SOCK_DGRAM, 0); 72 73 //for non blocking accept 74 bool blocking = false; 75 UDT::setsockopt(serv, 0, UDT_RCVSYN, &blocking, sizeof(bool)); 76 77 sockaddr_in my_addr; 78 my_addr.sin_family = AF_INET; 79 my_addr.sin_port = htons(port); 80 my_addr.sin_addr.s_addr = INADDR_ANY; 81 memset(&(my_addr.sin_zero), '\0', 8); 82 83 if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&my_addr, sizeof(my_addr))) { 84 printf("bind error, %s\n",UDT::getlasterror().getErrorMessage()); 85 } 86 87 if (UDT::ERROR == UDT::listen(serv, 1)) { 88 printf("listen error, %s\n",UDT::getlasterror().getErrorMessage()); 89 } 90 91 QTimer *timer = new QTimer(this); 92 connect(timer, SIGNAL(timeout()), this, SLOT(acceptConnections())); 93 timer->start(20); 35 Manager::Manager(QString name, int port) : QWidget() { 36 qRegisterMetaType<QModelIndex>("QModelIndex"); // pour le file ui?? 37 this->name = name; 38 39 setWindowTitle(name); 40 41 // manager layout 42 managerLayout = new QVBoxLayout; 43 setLayout(managerLayout); 44 45 // tab bar for multiple connections 46 tabBar = new QTabBar(); 47 managerLayout->addWidget(tabBar); 48 connect(tabBar, SIGNAL(currentChanged(int)), this, 49 SLOT(tabBarCurrentChanged(int))); 50 currentTab = 0; 51 52 // layout boutons 53 button_layout = new QGridLayout(); 54 managerLayout->addLayout(button_layout); 55 56 // boutons du button_Layout 57 send_button = new QPushButton("apply all"); 58 reset_button = new QPushButton("reset all"); 59 load_button = new QPushButton("load all locally"); 60 save_button = new QPushButton("save all locally"); 61 button_layout->addWidget(send_button, 0, 0); 62 button_layout->addWidget(reset_button, 0, 1); 63 button_layout->addWidget(load_button, 0, 2); 64 button_layout->addWidget(save_button, 0, 3); 65 66 connect(send_button, SIGNAL(clicked(bool)), this, SLOT(send())); 67 connect(reset_button, SIGNAL(clicked(bool)), this, SLOT(reset())); 68 connect(load_button, SIGNAL(clicked(bool)), this, SLOT(load())); 69 connect(save_button, SIGNAL(clicked(bool)), this, SLOT(save())); 70 71 UDT::startup(); 72 serv = UDT::socket(AF_INET, SOCK_DGRAM, 0); 73 74 // for non blocking accept 75 bool blocking = false; 76 UDT::setsockopt(serv, 0, UDT_RCVSYN, &blocking, sizeof(bool)); 77 78 sockaddr_in my_addr; 79 my_addr.sin_family = AF_INET; 80 my_addr.sin_port = htons(port); 81 my_addr.sin_addr.s_addr = INADDR_ANY; 82 memset(&(my_addr.sin_zero), '\0', 8); 83 84 if (UDT::ERROR == UDT::bind(serv, (sockaddr *)&my_addr, sizeof(my_addr))) { 85 printf("bind error, %s\n", UDT::getlasterror().getErrorMessage()); 86 } 87 88 if (UDT::ERROR == UDT::listen(serv, 1)) { 89 printf("listen error, %s\n", UDT::getlasterror().getErrorMessage()); 90 } 91 92 QTimer *timer = new QTimer(this); 93 connect(timer, SIGNAL(timeout()), this, SLOT(acceptConnections())); 94 timer->start(20); 94 95 } 95 96 96 97 Manager::~Manager() { 97 98 99 //delete main_layout;100 98 emit killUdtSockets(); 99 100 // delete main_layout; 101 UDT::cleanup(); 101 102 } 102 103 103 104 void Manager::acceptConnections(void) { 104 static UDTSOCKET first_socket=0; 105 UDTSOCKET socket; 106 107 sockaddr_in their_addr; 108 int namelen = sizeof(their_addr); 109 110 if (UDT::INVALID_SOCK == (socket = UDT::accept(serv, (sockaddr*)&their_addr, &namelen))) { 111 if(UDT::getlasterror().getErrorCode()!=6002) printf("accept: %s, code %i\n",UDT::getlasterror().getErrorMessage(),UDT::getlasterror().getErrorCode()); 112 return; 105 static UDTSOCKET first_socket = 0; 106 UDTSOCKET socket; 107 108 sockaddr_in their_addr; 109 int namelen = sizeof(their_addr); 110 111 if (UDT::INVALID_SOCK == 112 (socket = UDT::accept(serv, (sockaddr *)&their_addr, &namelen))) { 113 if (UDT::getlasterror().getErrorCode() != 6002) 114 printf("accept: %s, code %i\n", UDT::getlasterror().getErrorMessage(), 115 UDT::getlasterror().getErrorCode()); 116 return; 117 } else { 118 printf("connected to %s:%i\n", inet_ntoa(their_addr.sin_addr), 119 their_addr.sin_port); 120 121 if (!first_socket) { 122 first_socket = socket; 123 return; 113 124 } else { 114 printf("connected to %s:%i\n",inet_ntoa(their_addr.sin_addr),their_addr.sin_port); 115 116 if(!first_socket) { 117 first_socket=socket; 118 return; 119 } else { 120 QThread* thread = new QThread(this); 121 UdtSocket* new_udt=new UdtSocket(first_socket,socket,name); 122 new_udt->moveToThread(thread); 123 124 newConnection(new_udt); 125 126 connect(this, SIGNAL(killUdtSockets()),thread, SLOT(quit())); 127 connect(this, SIGNAL(killUdtSockets()),new_udt, SLOT(kill()),Qt::BlockingQueuedConnection); 128 129 connect(thread, SIGNAL(started()), new_udt, SLOT(handleConnections())); 130 131 thread->start(); 132 first_socket=0; 133 } 134 } 135 } 136 137 void Manager::newConnection(UdtSocket* socket) { 138 139 //no tabs to 2 tabs 140 if(connectionsLayout.count()==1) { 141 tabBar->addTab(hiddenTabName); 142 currentTab=0; 143 connectionsWidget.at(0)->show(); 144 } 145 146 //layout utilisateur 147 ConnectionLayout* newLayout = new ConnectionLayout(socket,"interface"); 148 connectionsLayout.append(newLayout); 149 connect(newLayout, SIGNAL(setRemoteName(QString)),this, SLOT(tabName(QString))); 150 connect(socket, SIGNAL(dataReady(char*,int)),newLayout, SLOT(receive(char*,int))); 151 connect(newLayout, SIGNAL(destroyed(QObject*)),this, SLOT(layoutDestroyed(QObject*))); 152 connect(socket, SIGNAL(destroyed()),newLayout, SLOT(deleteLater())); 153 154 //widget 155 QWidget* newWidget=new QWidget(); 156 connectionsWidget.append(newWidget); 157 newWidget->setLayout(newLayout->getQGridLayout()); 158 managerLayout->insertWidget(1,newWidget); 159 newWidget->hide(); 160 161 if(connectionsLayout.count()==1) {//first connection 162 newWidget->show(); 163 } else {//add a tab for the new connection 164 tabBar->addTab("unknown"); 165 } 125 QThread *thread = new QThread(this); 126 UdtSocket *new_udt = new UdtSocket(first_socket, socket, name); 127 new_udt->moveToThread(thread); 128 129 newConnection(new_udt); 130 131 connect(this, SIGNAL(killUdtSockets()), thread, SLOT(quit())); 132 connect(this, SIGNAL(killUdtSockets()), new_udt, SLOT(kill()), 133 Qt::BlockingQueuedConnection); 134 135 connect(thread, SIGNAL(started()), new_udt, SLOT(handleConnections())); 136 137 thread->start(); 138 first_socket = 0; 139 } 140 } 141 } 142 143 void Manager::newConnection(UdtSocket *socket) { 144 145 // no tabs to 2 tabs 146 if (connectionsLayout.count() == 1) { 147 tabBar->addTab(hiddenTabName); 148 currentTab = 0; 149 connectionsWidget.at(0)->show(); 150 } 151 152 // layout utilisateur 153 ConnectionLayout *newLayout = new ConnectionLayout(socket, "interface"); 154 connectionsLayout.append(newLayout); 155 connect(newLayout, SIGNAL(setRemoteName(QString)), this, 156 SLOT(tabName(QString))); 157 connect(socket, SIGNAL(dataReady(char *, int)), newLayout, 158 SLOT(receive(char *, int))); 159 connect(newLayout, SIGNAL(destroyed(QObject *)), this, 160 SLOT(layoutDestroyed(QObject *))); 161 connect(socket, SIGNAL(destroyed()), newLayout, SLOT(deleteLater())); 162 163 // widget 164 QWidget *newWidget = new QWidget(); 165 connectionsWidget.append(newWidget); 166 newWidget->setLayout(newLayout->getQGridLayout()); 167 managerLayout->insertWidget(1, newWidget); 168 newWidget->hide(); 169 170 if (connectionsLayout.count() == 1) { // first connection 171 newWidget->show(); 172 } else { // add a tab for the new connection 173 tabBar->addTab("unknown"); 174 } 166 175 } 167 176 168 177 void Manager::tabBarCurrentChanged(int index) { 169 if(index>=0) {170 171 172 currentTab=index;173 174 currentTab=0;175 176 178 if (index >= 0) { 179 connectionsWidget.at(currentTab)->hide(); 180 connectionsWidget.at(index)->show(); 181 currentTab = index; 182 } else { 183 currentTab = 0; 184 connectionsWidget.at(0)->show(); 185 } 177 186 } 178 187 179 188 void Manager::tabName(QString name) { 180 int index=connectionsLayout.indexOf((ConnectionLayout*)sender());181 if(tabBar->count()==0) {182 hiddenTabName=name;183 184 tabBar->setTabText(index,name);185 186 } 187 188 void Manager::layoutDestroyed(QObject *obj) {189 int index=connectionsLayout.indexOf((ConnectionLayout*)obj);190 191 if(tabBar->count()>1) {192 193 194 195 196 197 connectionsLayout.removeOne((ConnectionLayout*)obj);198 199 if(connectionsLayout.count()==1) {200 hiddenTabName=tabBar->tabText(0);201 202 189 int index = connectionsLayout.indexOf((ConnectionLayout *)sender()); 190 if (tabBar->count() == 0) { 191 hiddenTabName = name; 192 } else { 193 tabBar->setTabText(index, name); 194 } 195 } 196 197 void Manager::layoutDestroyed(QObject *obj) { 198 int index = connectionsLayout.indexOf((ConnectionLayout *)obj); 199 200 if (tabBar->count() > 1) { 201 tabBar->removeTab(index); 202 } 203 204 delete connectionsWidget.at(index); 205 connectionsWidget.removeAt(index); 206 connectionsLayout.removeOne((ConnectionLayout *)obj); 207 208 if (connectionsLayout.count() == 1) { 209 hiddenTabName = tabBar->tabText(0); 210 tabBar->removeTab(0); 211 } 203 212 } 204 213 205 214 void Manager::load(void) { 206 QString dir_name= QFileDialog::getExistingDirectory(this, "Select a directory",0,0); 207 208 if(dir_name!="") { 209 for(int i=0;i<connectionsLayout.count();i++) { 210 QFile* file; 211 file = new QFile(dir_name+"/"+connectionsLayout.at(i)->getRemoteName()+".xml"); 212 if(!file->open(QIODevice::ReadOnly | QIODevice::Text)) { 213 QMessageBox::warning(this,"Warning","Enable to load "+connectionsLayout.at(i)->getRemoteName()+".xml"); 214 continue; 215 } 216 217 QDomDocument doc; 218 QString errorMsg; 219 int errorLine; 220 int errorColumn; 221 if(!doc.setContent(file,&errorMsg,&errorLine,&errorColumn)) { 222 QMessageBox::critical(this,"Error","unable to read " +connectionsLayout.at(i)->getRemoteName()+".xml" 223 +" ("+errorMsg + " at " +QString::number(errorLine) +","+QString::number(errorColumn)+")"); 224 } else { 225 connectionsLayout.at(i)->LoadXml(doc); 226 } 227 delete file; 228 } 229 } 215 QString dir_name = 216 QFileDialog::getExistingDirectory(this, "Select a directory", 0, 0); 217 218 if (dir_name != "") { 219 for (int i = 0; i < connectionsLayout.count(); i++) { 220 QFile *file; 221 file = new QFile(dir_name + "/" + 222 connectionsLayout.at(i)->getRemoteName() + ".xml"); 223 if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) { 224 QMessageBox::warning(this, "Warning", 225 "Enable to load " + 226 connectionsLayout.at(i)->getRemoteName() + 227 ".xml"); 228 continue; 229 } 230 231 QDomDocument doc; 232 QString errorMsg; 233 int errorLine; 234 int errorColumn; 235 if (!doc.setContent(file, &errorMsg, &errorLine, &errorColumn)) { 236 QMessageBox::critical( 237 this, "Error", 238 "unable to read " + connectionsLayout.at(i)->getRemoteName() + 239 ".xml" + " (" + errorMsg + " at " + QString::number(errorLine) + 240 "," + QString::number(errorColumn) + ")"); 241 } else { 242 connectionsLayout.at(i)->LoadXml(doc); 243 } 244 delete file; 245 } 246 } 230 247 } 231 248 232 249 void Manager::save(void) { 233 bool isUptodate=true; 234 235 for(int i=0;i<connectionsLayout.count();i++) { 236 if(!connectionsLayout.at(i)->IsUptodate()) { 237 isUptodate=false; 238 break; 239 } 240 } 241 242 if(!isUptodate) { 243 QMessageBox msgBox; 244 msgBox.setText("There are pending modifications"); 245 msgBox.setInformativeText("Apply and save?"); 246 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); 247 msgBox.setDefaultButton(QMessageBox::Yes); 248 int ret = msgBox.exec(); 249 250 switch (ret) { 251 case QMessageBox::Yes: 252 send(); 253 break; 254 case QMessageBox::Cancel: 255 return; 256 break; 257 default: 258 // should never be reached 259 break; 260 } 261 } 262 263 //create dirctory for storage 264 QDateTime dateTime = QDateTime::currentDateTime(); 265 QString dir_name = "configs_"+dateTime.toString("yyyyMMdd_hhmm")+ "_" + name; 266 if(QDir().exists(dir_name)==true) { 267 dir_name = "configs_"+dateTime.toString("yyyyMMdd_hhmm_ss")+ "_" + name; 268 } 269 QDir().mkdir(dir_name); 270 271 for(int i=0;i<connectionsLayout.count();i++) { 272 QDomDocument* xml=new QDomDocument("remote_ui_xml"); 273 274 connectionsLayout.at(i)->GetFullXml((QDomElement*)xml); 275 276 QFile fichier(dir_name+"/"+connectionsLayout.at(i)->getRemoteName()+".xml"); 277 QString write_doc = (xml->ownerDocument()).toString(); 278 279 if(!fichier.open(QIODevice::WriteOnly)) { 280 fichier.close(); 281 QMessageBox::critical(this,"Error","Enable to write XML"); 282 continue; 283 } 284 QTextStream stream(&fichier); 285 stream << write_doc; // On utilise l'opérateur << pour écrire write_doc dans le document XML. 286 fichier.close(); 287 288 delete xml; 289 } 290 291 QMessageBox::information(this,"save all","saved to ./"+ dir_name); 250 bool isUptodate = true; 251 252 for (int i = 0; i < connectionsLayout.count(); i++) { 253 if (!connectionsLayout.at(i)->IsUptodate()) { 254 isUptodate = false; 255 break; 256 } 257 } 258 259 if (!isUptodate) { 260 QMessageBox msgBox; 261 msgBox.setText("There are pending modifications"); 262 msgBox.setInformativeText("Apply and save?"); 263 msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); 264 msgBox.setDefaultButton(QMessageBox::Yes); 265 int ret = msgBox.exec(); 266 267 switch (ret) { 268 case QMessageBox::Yes: 269 send(); 270 break; 271 case QMessageBox::Cancel: 272 return; 273 break; 274 default: 275 // should never be reached 276 break; 277 } 278 } 279 280 // create dirctory for storage 281 QDateTime dateTime = QDateTime::currentDateTime(); 282 QString dir_name = 283 "configs_" + dateTime.toString("yyyyMMdd_hhmm") + "_" + name; 284 if (QDir().exists(dir_name) == true) { 285 dir_name = "configs_" + dateTime.toString("yyyyMMdd_hhmm_ss") + "_" + name; 286 } 287 QDir().mkdir(dir_name); 288 289 for (int i = 0; i < connectionsLayout.count(); i++) { 290 QDomDocument *xml = new QDomDocument("remote_ui_xml"); 291 292 connectionsLayout.at(i)->GetFullXml((QDomElement *)xml); 293 294 QFile fichier(dir_name + "/" + connectionsLayout.at(i)->getRemoteName() + 295 ".xml"); 296 QString write_doc = (xml->ownerDocument()).toString(); 297 298 if (!fichier.open(QIODevice::WriteOnly)) { 299 fichier.close(); 300 QMessageBox::critical(this, "Error", "Enable to write XML"); 301 continue; 302 } 303 QTextStream stream(&fichier); 304 stream << write_doc; // On utilise l'opérateur << pour écrire write_doc dans 305 // le document XML. 306 fichier.close(); 307 308 delete xml; 309 } 310 311 QMessageBox::information(this, "save all", "saved to ./" + dir_name); 292 312 } 293 313 294 314 void Manager::send(void) { 295 for(int i=0;i<connectionsLayout.count();i++) {296 297 connectionsLayout.at(i)->GetUpdateXml((QDomElement*)&doc);298 //printf("merge\n%s\n",doc.toString().toLocal8Bit().constData());299 300 301 315 for (int i = 0; i < connectionsLayout.count(); i++) { 316 QDomDocument doc("remote_ui_xml"); 317 connectionsLayout.at(i)->GetUpdateXml((QDomElement *)&doc); 318 // printf("merge\n%s\n",doc.toString().toLocal8Bit().constData()); 319 320 connectionsLayout.at(i)->XmlToSend(doc); 321 } 302 322 } 303 323 304 324 void Manager::reset() { 305 for(int i=0;i<connectionsLayout.count();i++) connectionsLayout.at(i)->ResetAllChilds(); 306 } 325 for (int i = 0; i < connectionsLayout.count(); i++) 326 connectionsLayout.at(i)->ResetAllChilds(); 327 }
Note:
See TracChangeset
for help on using the changeset viewer.