source: flair-src/trunk/lib/FlairCore/src/FrameworkManager.cpp@ 426

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

stop all threads then delete them

File size: 7.0 KB
RevLine 
[2]1// %flair:license{
[15]2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
[2]4// %flair:license}
5// created: 2011/08/31
6// filename: FrameworkManager.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Main class of the Framework library
14//
15//
16/*********************************************************************/
17
18#include "FrameworkManager.h"
19#include "FrameworkManager_impl.h"
20#include "IODevice.h"
21#include "config.h"
[45]22#include "compile_info.h"
[2]23
24using std::string;
25using std::vector;
26using namespace flair::gui;
27
[15]28namespace {
[45]29 flair::core::FrameworkManager *frameworkmanager = NULL;
[2]30}
31
[45]32static void constructor() __attribute__((constructor));
33
34void constructor() {
35 compile_info("FlairCore");
36}
37
38
[15]39namespace flair {
40namespace core {
[2]41
[15]42FrameworkManager *getFrameworkManager(void) { return frameworkmanager; }
[2]43
44bool IsBigEndian(void) {
[15]45 union {
46 uint32_t i;
47 char c[4];
48 } bint = {0x01020304};
[2]49
[15]50 if (bint.c[0] == 1) {
51 return true;
52 } else {
53 return false;
54 }
[2]55}
56
[15]57FrameworkManager::FrameworkManager(string name)
58 : Object(NULL, name, XML_ROOT_TYPE) {
59 if (frameworkmanager != NULL) {
60 Err("FrameworkManager should be instanced only one time\n");
61 return;
62 }
[2]63
[15]64 frameworkmanager = this;
65 pimpl_ = new FrameworkManager_impl(this, name);
[2]66}
67
68FrameworkManager::~FrameworkManager() {
[15]69 // Printf("destruction FrameworkManager\n");
[2]70
[15]71 // stop ui_com thread (which sends datas for graphs), we do not need it as
72 // graphs will be destroyed
73 if (getUiCom() != NULL) {
74 getUiCom()->SafeStop();
75 getUiCom()->Join();
76 }
[362]77/*
78//stop and delete each thread has been changed to stop all thread, then delete all threads
79//seems ok but needs more testing
80 *
[15]81 // we start by deleting threads (except pimpl which must be deleted at last)
82 // (before deleting objects that could be used by the threads)
83 // Printf("delete Threads\n");
84 for (vector<const Object *>::iterator it = Childs()->begin();
85 it < Childs()->end(); it++) {
[186]86 //Printf("child %i %s %s\n",Childs()->size(),(*it)->ObjectName().c_str(),(*it)->ObjectType().c_str());
[15]87 if ((*it)->ObjectType() == "Thread") {
88 if (*it != pimpl_) {
89 // Printf("del\n");
[217]90 ((Thread *)(*it))->SafeStop();
91 ((Thread *)(*it))->Join();
[15]92 delete (Thread *)(*it);
93 // Childs() vector has been modified, we start from beginning again
94 // it will be incremented by the loop, so in fact we start again at
95 // begin()+1
96 // it is not a problem since begin is pimpl
97 it = Childs()->begin();
98 // Printf("del ok\n");
99 }
[2]100 }
[15]101 }
[362]102*/
103 // we start by stopping threads (except pimpl which must be deleted at last)
104 // (before deleting objects that could be used by the threads)
105 //Printf("stop Threads\n");
106 for (vector<const Object *>::iterator it = Childs()->begin();it < Childs()->end(); it++) {
107 //Printf("child %i %s %s\n",Childs()->size(),(*it)->ObjectName().c_str(),(*it)->ObjectType().c_str());
108 if ((*it)->ObjectType() == "Thread") {
109 if (*it != pimpl_) {
110 //Printf("stop\n");
111 ((Thread *)(*it))->SafeStop();
112 ((Thread *)(*it))->Join();
113 //Printf("stop ok\n");
114 }
115 }
116 }
117 //then delete threads
118 //Printf("del Threads\n");
119 for (vector<const Object *>::iterator it = Childs()->begin();it < Childs()->end(); it++) {
120 //Printf("child %i %s %s\n",Childs()->size(),(*it)->ObjectName().c_str(),(*it)->ObjectType().c_str());
121 if ((*it)->ObjectType() == "Thread") {
122 if (*it != pimpl_) {
123 // Printf("del\n");
124 delete (Thread *)(*it);
125 // Childs() vector has been modified, we start from beginning again
126 // it will be incremented by the loop, so in fact we start again at
127 // begin()+1
128 // it is not a problem since begin is pimpl
129 it = Childs()->begin();
130 //Printf("del ok\n");
131 }
132 }
133 }
134
[15]135 // next we delete IODevice
136 // (before deleting objects that could be used by the IODevice)
137 // Printf("delete IODevices\n");
138 for (vector<const Object *>::iterator it = Childs()->begin();
139 it < Childs()->end(); it++) {
[186]140 //Printf("child %i %s %s\n",Childs()->size(),(*it)->ObjectName().c_str(),(*it)->ObjectType().c_str());
[15]141 if ((*it)->ObjectType() == "IODevice") {
142 // Printf("del\n");
143 delete (IODevice *)*it;
144 // Printf("del ok\n");
145 // Childs() vector has been modified, we start from beginning again
146 // it will be incremented by the loop, so in fact we start again at
147 // begin()+1
148 // it is not a problem since begin is pimpl (not an IODevice)
149 it = Childs()->begin();
150 // Printf("del ok\n");
[2]151 }
[15]152 }
[2]153
[15]154 // Printf("delete childs\n");
155 // on efface les enfants en commencant par la fin
156 // le ui_com puis FrameworkManager_impl est détruit en dernier
157 // permet de garder l'uicom pour notifer des destructions
158 while (Childs()->size() != 0) {
[186]159 //Printf("child %i %s %s\n",Childs()->size(),Childs()->back()->ObjectName().c_str(),Childs()->back()->ObjectType().c_str());
[15]160 if (Childs()->back() != NULL)
161 delete Childs()->back();
162 }
[2]163
[15]164 // childs.clear();
[2]165
[15]166 // Printf("destruction FrameworkManager ok\n");
[2]167}
168
[55]169void FrameworkManager::SetupConnection(string address, uint16_t port,Time watchdogTimeout,
[15]170 size_t rcv_buf_size) {
[186]171 pimpl_->SetupConnection(address, port, watchdogTimeout,rcv_buf_size);
[2]172}
173
174void FrameworkManager::SetupUserInterface(string xml_file) {
[15]175 pimpl_->SetupUserInterface(xml_file);
[2]176}
177
[15]178gui::TabWidget *FrameworkManager::GetTabWidget(void) const {
[55]179 if(pimpl_->tabwidget==NULL) Err("SetupUserInterface must be called before\n");
[15]180 return pimpl_->tabwidget;
[2]181}
182
183void FrameworkManager::UpdateSendData(const SendData *obj) {
[15]184 if (getUiCom() != NULL)
185 getUiCom()->UpdateSendData(obj);
[2]186}
187
188void FrameworkManager::BlockCom(void) {
[15]189 if (getUiCom() != NULL)
190 getUiCom()->Block();
[2]191}
192
193void FrameworkManager::UnBlockCom(void) {
[15]194 if (getUiCom() != NULL)
195 getUiCom()->UnBlock();
[2]196}
197
198bool FrameworkManager::ConnectionLost(void) const {
[15]199 if (getUiCom() != NULL) {
200 return (getUiCom()->ConnectionLost() | pimpl_->connection_lost);
201 } else {
202 return false;
203 }
[2]204}
205
[213]206void FrameworkManager::SetupLogger(string log_path,uint32_t stackSize) {
207 pimpl_->SetupLogger(log_path,stackSize);
[2]208}
209
[123]210string FrameworkManager::GetLogPath(void) const {
211 return pimpl_->log_path;
212}
[2]213void FrameworkManager::AddDeviceToLog(IODevice *device) {
[15]214 pimpl_->AddDeviceToLog(device);
[2]215}
216
[122]217bool FrameworkManager::IsDeviceLogged(const IODevice *device) const {
218 return pimpl_->IsDeviceLogged(device);
219}
220
[15]221void FrameworkManager::StartLog(void) { pimpl_->StartLog(); }
[2]222
[15]223void FrameworkManager::StopLog(void) { pimpl_->StopLog(); }
[2]224
[15]225bool FrameworkManager::IsLogging(void) const { return pimpl_->is_logging; }
[2]226
227void FrameworkManager::DisableErrorsDisplay(bool value) {
[15]228 pimpl_->disable_errors = value;
[2]229}
230
231bool FrameworkManager::IsDisplayingErrors(void) const {
[15]232 return !(pimpl_->disable_errors);
[2]233}
234
235} // end namespace core
236} // end namespace flair
Note: See TracBrowser for help on using the repository browser.