fw4spl
SStarter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "gui/action/SStarter.hpp"
8 
9 #include <fwCore/base.hpp>
10 
11 #include <fwGui/dialog/MessageDialog.hpp>
12 
13 #include <fwRuntime/Extension.hpp>
14 #include <fwRuntime/helper.hpp>
15 
16 #include <fwServices/macros.hpp>
17 #include <fwServices/op/Get.hpp>
18 
19 #include <fwTools/fwID.hpp>
20 
21 #include <boost/range/adaptor/reversed.hpp>
22 
23 namespace gui
24 {
25 namespace action
26 {
27 
28 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::gui::action::SStarter );
29 
30 //-----------------------------------------------------------------------------
31 
32 SStarter::SStarter() noexcept
33 {
34 }
35 
36 //-----------------------------------------------------------------------------
37 
39 {
40 }
41 
42 //-----------------------------------------------------------------------------
43 
45 {
46  this->actionServiceStarting();
47 }
48 
49 //-----------------------------------------------------------------------------
50 
52 {
53  std::vector< ::fwServices::IService::SharedFutureType > futures;
54 
55  for( VectPairIDActionType::value_type serviceUid : ::boost::adaptors::reverse(m_uuidServices) )
56  {
57  bool srv_exists = ::fwTools::fwID::exist(serviceUid.first );
58  if (srv_exists && (m_idStartedSrvSet.find(serviceUid.first) != m_idStartedSrvSet.end()) )
59  {
60  ::fwServices::IService::sptr service = ::fwServices::get( serviceUid.first );
61  if (service->isStarted())
62  {
63  futures.push_back(service->stop());
64  }
65  }
66  }
67 
68  std::for_each(futures.begin(), futures.end(), std::mem_fn(&::std::shared_future<void>::wait));
69 
70  this->actionServiceStopping();
71 }
72 
73 //-----------------------------------------------------------------------------
74 
75 void SStarter::info(std::ostream& _sstream )
76 {
77  _sstream << "Starter Action" << std::endl;
78 }
79 
80 //-----------------------------------------------------------------------------
81 
83 {
84  for(size_t i = 0; i < m_uuidServices.size(); i++)
85  {
86  ActionType action = m_uuidServices.at(i).second;
87  IDSrvType uid = m_uuidServices.at(i).first;
88  bool srv_exists = ::fwTools::fwID::exist(uid);
89 
90  // Manage special action
91  if ( action == START_IF_EXISTS )
92  {
93  if ( srv_exists )
94  {
95  action = START;
96  }
97  else
98  {
99  action = DO_NOTHING;
100  }
101  }
102  else if( action == STOP_IF_EXISTS )
103  {
104  if ( srv_exists )
105  {
106  action = STOP;
107  }
108  else
109  {
110  action = DO_NOTHING;
111  }
112  }
113 
114  if( action != DO_NOTHING)
115  {
116  ::fwGui::LockAction lock(this->getSptr());
117 
118  ::fwServices::IService::sptr service = ::fwServices::get( uid );
119  SLM_ASSERT("service not found", service);
120  switch ( action )
121  {
122  case START_OR_STOP:
123  {
124  if(service->isStopped())
125  {
126  service->start();
127  service->update();
128  m_idStartedSrvSet.insert(uid);
129  }
130  else
131  {
132  service->stop();
133  m_idStartedSrvSet.erase(uid);
134  }
135  break;
136  }
137  case START_ONLY_OR_STOP:
138  {
139  if(service->isStopped())
140  {
141  service->start();
142  m_idStartedSrvSet.insert(uid);
143  }
144  else
145  {
146  service->stop();
147  m_idStartedSrvSet.erase(uid);
148  }
149  break;
150  }
151  case START:
152  {
153  if(service->isStopped())
154  {
155  service->start();
156  m_idStartedSrvSet.insert(uid);
157  }
158  else
159  {
160  OSLM_WARN("Service " << service->getID() << " is not stopped");
161  }
162  service->update();
163  break;
164  }
165  case STOP:
166  {
167  if(service->isStarted())
168  {
169  service->stop();
170  }
171  else
172  {
173  OSLM_WARN("Service " << service->getID() << " is not started");
174  }
175  break;
176  }
177  case START_ONLY:
178  {
179  if(service->isStopped())
180  {
181  service->start();
182  m_idStartedSrvSet.insert(uid);
183  }
184  else
185  {
186  OSLM_WARN("Service " << service->getID() << " is not stopped");
187  }
188  break;
189  }
190  default:
191  {
192  OSLM_FATAL("There is no action ("<< action
193  <<") type corresponding to the action id requested for " << uid <<
194  ".");
195  break;
196  }
197  }
198  }
199  else
200  {
202  "Service unavailable",
203  "The service is unavailable.",
204  ::fwGui::dialog::IMessageDialog::WARNING);
205 
206  OSLM_INFO("Do nothing for Service " << m_uuidServices.at(i).first);
207  }
208  }
209 }
210 
211 //-----------------------------------------------------------------------------
212 
214 {
215  this->initialize();
216 
217  for(ConfigurationType actionCfg : m_configuration->getElements() )
218  {
219  OSLM_INFO( "SStarter " << actionCfg->getName());
220 
221  std::string actionType = actionCfg->getName();
222  ActionType action;
223  if ( actionType == "start" )
224  {
225  action = START;
226  }
227  else if ( actionType == "stop" )
228  {
229  action = STOP;
230  }
231  else if ( actionType == "start_or_stop" )
232  {
233  action = START_OR_STOP;
234  }
235  else if ( actionType == "start_only_or_stop" )
236  {
237  action = START_ONLY_OR_STOP;
238  }
239  else if ( actionType == "start_if_exists" )
240  {
241  action = START_IF_EXISTS;
242  }
243  else if ( actionType == "stop_if_exists" )
244  {
245  action = STOP_IF_EXISTS;
246  }
247  else if ( actionType == "start_only" )
248  {
249  action = START_ONLY;
250  }
251  else
252  {
253  OSLM_WARN("The \"actionType\":" << actionType <<" is not managed by SStarter");
254  continue;
255  }
256  SLM_ASSERT("Attribute uid missing", actionCfg->hasAttribute("uid"));
257  IDSrvType uuid = actionCfg->getExistingAttributeValue("uid");
258 
259  m_uuidServices.push_back( std::make_pair(uuid, action) );
260  }
261 }
262 
263 //-----------------------------------------------------------------------------
264 
265 } // namespace action
266 } // namespace gui
GUI_API void configuring() override
This method is used to configure the service parameters: specifies which services must be started or ...
Definition: SStarter.cpp:213
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
virtual GUI_API void starting() override
Initialize the service activity.
Definition: SStarter.cpp:44
virtual GUI_API void stopping() override
Uninitialized the service activity. All services started by this action are stopped.
Definition: SStarter.cpp:51
The namespace gui contains the basic services to build the application IHM.
static FWTOOLS_API bool exist(IDType _id)
Definition: fwID.cpp:33
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
#define OSLM_INFO(message)
Definition: spyLog.hpp:252
GUI_API void updating() override
This method starts-updates or stops the specified services.
Definition: SStarter.cpp:82
virtual GUI_API ~SStarter() noexcept
Destructor. Do nothing.
Definition: SStarter.cpp:38
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
virtual GUI_API void info(std::ostream &_sstream) override
This method gives information about the class. Do nothing.
Definition: SStarter.cpp:75
GUI_API SStarter() noexcept
Constructor. Do nothing.
#define OSLM_WARN(message)
Definition: spyLog.hpp:263
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
#define OSLM_FATAL(message)
Definition: spyLog.hpp:285
Apply an action (start, stop, ...) on a service specify by uid.
Definition: SStarter.hpp:89