fw4spl
IService.hpp
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 #pragma once
8 
9 #include "fwServices/config.hpp"
10 #include "fwServices/factory/new.hpp"
11 #include "fwServices/helper/ProxyConnections.hpp"
12 
13 #include <fwCom/helper/SigSlotConnection.hpp>
14 #include <fwCom/HasSignals.hpp>
15 #include <fwCom/HasSlots.hpp>
16 #include <fwCom/Slot.hpp>
17 #include <fwCom/Slots.hpp>
18 
19 #include <fwData/Object.hpp>
20 
21 #include <fwRuntime/ConfigurationElement.hpp>
22 
23 #include <fwTools/Failed.hpp>
24 #include <fwTools/Object.hpp>
25 
26 #include <boost/property_tree/ptree.hpp>
27 
28 #include <cstdint>
29 
30 namespace fwServices
31 {
32 namespace registry
33 {
34 class ObjectService;
35 }
36 namespace fwThread
37 {
38 class Worker;
39 }
40 
41 #define KEY_GROUP_NAME(key, index) (key + "#" + std::to_string(index) )
42 
61 class FWSERVICES_CLASS_API IService : public ::fwTools::Object,
62  public ::fwCom::HasSlots,
63  public ::fwCom::HasSignals
64 {
65 
66 // to give to OSR an access on IService.m_associatedObject;
67 friend class registry::ObjectService;
68 friend class AppConfigManager;
69 
70 public:
73 
74  typedef ::boost::property_tree::ptree ConfigType;
75 
76  typedef std::string IdType;
77  typedef std::string KeyType;
78  typedef std::map< KeyType, CWPTR( ::fwData::Object )> InputMapType;
79  typedef std::map< KeyType, WPTR( ::fwData::Object )> InOutMapType;
80  typedef std::map< KeyType, SPTR( ::fwData::Object )> OutputMapType;
81 
82  enum class AccessType : std::uint8_t
83  {
84  INPUT,
85  OUTPUT,
86  INOUT,
87  };
88 
91  {
92  std::string m_uid;
93  std::string m_key;
94  AccessType m_access;
95  bool m_autoConnect { false };
96  bool m_optional { false };
97  };
98 
100  struct Config
101  {
102  std::string m_uid;
103  std::string m_type;
104  bool m_globalAutoConnect { false };
105  std::string m_worker;
106  std::vector<ObjectServiceConfig> m_objects;
107  std::map<std::string, size_t> m_groupSize;
109  };
110 
113  FWSERVICES_API static const std::string s_DEFAULT_OBJECT;
114 
120 
122  typedef enum
123  {
128  STOPPING
129  } GlobalStatus;
130 
132  typedef enum
133  {
135  NOTUPDATING
136  } UpdatingStatus;
137 
139  typedef enum
140  {
143  UNCONFIGURED
145 
147 
152  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_STARTED_SIG;
153  typedef ::fwCom::Signal<void ()> StartedSignalType;
154 
155  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_UPDATED_SIG;
156  typedef ::fwCom::Signal<void ()> UpdatedSignalType;
157 
158  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_STOPPED_SIG;
159  typedef ::fwCom::Signal<void ()> StoppedSignalType;
160 
162 
167  typedef std::shared_future< void > SharedFutureType;
168  typedef std::packaged_task< void ()> PackagedTaskType;
169  typedef std::future< void > UniqueFutureType;
170 
171  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_START_SLOT;
172  typedef ::fwCom::Slot<SharedFutureType()> StartSlotType;
173 
174  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_STOP_SLOT;
175  typedef ::fwCom::Slot<SharedFutureType()> StopSlotType;
176 
177  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_UPDATE_SLOT;
178  typedef ::fwCom::Slot<SharedFutureType()> UpdateSlotType;
179 
180  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_SWAP_SLOT;
181  typedef ::fwCom::Slot<SharedFutureType(::fwData::Object::sptr)> SwapSlotType;
182 
183  FWSERVICES_API static const ::fwCom::Slots::SlotKeyType s_SWAPKEY_SLOT;
184  typedef ::fwCom::Slot<SharedFutureType(const KeyType&, ::fwData::Object::sptr)> SwapKeySlotType;
185 
187  FWSERVICES_API void setWorker( SPTR(::fwThread::Worker) worker );
188 
190  FWSERVICES_API SPTR(::fwThread::Worker) getWorker() const;
191 
193 
199 
205  FWSERVICES_API void setConfiguration( const ::fwRuntime::ConfigurationElement::sptr _cfgElement );
206 
212  FWSERVICES_API void setConfiguration( const Config& _configuration);
213 
219  FWSERVICES_API void setConfiguration( const ConfigType& ptree );
220 
228  FWSERVICES_API void configure();
229 
234  FWSERVICES_API SharedFutureType start();
235 
242  FWSERVICES_API SharedFutureType stop();
243 
248  FWSERVICES_API SharedFutureType update();
249 
263  FWSERVICES_API SharedFutureType swap( ::fwData::Object::sptr _obj );
264 
277  FWSERVICES_API SharedFutureType swapKey( const KeyType& _key, ::fwData::Object::sptr _obj );
279 
285 
290  FWSERVICES_API GlobalStatus getStatus() const noexcept;
291 
296  FWSERVICES_API bool isStarted() const noexcept;
297 
302  FWSERVICES_API bool isStopped() const noexcept;
303 
308  FWSERVICES_API ConfigurationStatus getConfigurationStatus() const noexcept;
309 
314  FWSERVICES_API UpdatingStatus getUpdatingStatus() const noexcept;
316 
322 
327  FWSERVICES_API ::fwRuntime::ConfigurationElement::sptr getConfiguration() const;
328 
332  FWSERVICES_API ConfigType getConfigTree() const;
334 
339 
347  FWSERVICES_API ::fwData::Object::sptr getObject();
348 
357  template< class DATATYPE > SPTR(DATATYPE) getObject();
358 
363  FWSERVICES_API const InputMapType& getInputs() const;
364 
369  FWSERVICES_API const InOutMapType& getInOuts() const;
370 
375  FWSERVICES_API const OutputMapType& getOutputs() const;
376 
383  FWSERVICES_API std::vector< ::fwData::Object::csptr > getObjects() const;
384 
390  template< class DATATYPE > CSPTR(DATATYPE) getInput(const KeyType& key) const;
391 
397  template< class DATATYPE > SPTR(DATATYPE) getInOut(const KeyType& key) const;
398 
404  template< class DATATYPE > SPTR(DATATYPE) getOutput(const KeyType& key) const;
405 
411  template< class DATATYPE > CSPTR(DATATYPE) getInput(const KeyType& keybase, size_t index) const;
412 
418  template< class DATATYPE > SPTR(DATATYPE) getInOut(const KeyType& keybase, size_t index) const;
419 
425  template< class DATATYPE > SPTR(DATATYPE) getOutput(const KeyType& keybase, size_t index) const;
426 
433  FWSERVICES_API void setOutput(const ::fwServices::IService::KeyType& key, const ::fwData::Object::sptr& object,
434  size_t index = 0);
435 
441  size_t getKeyGroupSize(const KeyType& keybase) const;
443 
448 
449  typedef ::fwCom::helper::SigSlotConnection::KeyConnectionsType KeyConnectionsType;
450 
455  {
456  public:
457  //------------------------------------------------------------------------------
458 
459  void push (const KeyType& key,
460  const ::fwCom::Signals::SignalKeyType& sig,
461  const ::fwCom::Slots::SlotKeyType& slot)
462  {
463  m_keyConnectionsMap[key].push_back(std::make_pair(sig, slot));
464  }
465 
466  typedef std::map< KeyType, KeyConnectionsType> KeyConnectionsMapType;
467 
468  //------------------------------------------------------------------------------
469 
470  KeyConnectionsMapType::const_iterator find(const KeyType& key) const
471  {
472  return m_keyConnectionsMap.find(key);
473  }
474  //------------------------------------------------------------------------------
475 
476  KeyConnectionsMapType::const_iterator end() const
477  {
478  return m_keyConnectionsMap.cend();
479  }
480  //------------------------------------------------------------------------------
481 
482  bool empty() const
483  {
484  return m_keyConnectionsMap.empty();
485  }
486  //------------------------------------------------------------------------------
487 
488  size_t size() const
489  {
490  return m_keyConnectionsMap.size();
491  }
492 
493  private:
494  std::map< KeyType, KeyConnectionsType> m_keyConnectionsMap;
495  };
496 
502  FWSERVICES_API virtual KeyConnectionsType getObjSrvConnections() const;
503 
505 
511 
517  FWSERVICES_API friend std::ostream& operator<<(std::ostream& _sstream, IService& _service);
518 
523  FWSERVICES_API bool hasObjectId(const KeyType& _key) const;
524 
528  FWSERVICES_API IdType getObjectId(const KeyType& _key) const;
529 
533  FWSERVICES_API void setObjectId(const KeyType& _key, const IdType& _id);
535 
544  FWSERVICES_API void registerInput(const ::fwData::Object::csptr& obj, const std::string& key,
545  const bool autoConnect = false, const bool optional = false);
546 
555  FWSERVICES_API void registerInOut(const ::fwData::Object::sptr& obj, const std::string& key,
556  const bool autoConnect = false, const bool optional = false);
557 
558 protected:
559 
565 
572  FWSERVICES_API IService();
573 
579  FWSERVICES_API virtual ~IService();
580 
582 
588 
596  FWSERVICES_API virtual void starting() = 0;
597 
603  FWSERVICES_API virtual void stopping() = 0;
604 
613  virtual void swapping()
614  {
615  }
616 
627  virtual void swapping(const KeyType& key)
628  {
629  }
630 
635  FWSERVICES_API virtual void configuring() = 0;
636 
642  FWSERVICES_API virtual void reconfiguring();
643 
649  FWSERVICES_API virtual void updating() = 0;
650 
657  FWSERVICES_API virtual void info( std::ostream& _sstream );
658 
663  FWSERVICES_API virtual KeyConnectionsMap getAutoConnections() const;
665 
670  ::fwRuntime::ConfigurationElement::sptr m_configuration;
671 
676  ::fwData::Object::wptr m_associatedObject;
677 
682 
684  StartSlotType::sptr m_slotStart;
685 
687  StopSlotType::sptr m_slotStop;
688 
690  UpdateSlotType::sptr m_slotUpdate;
691 
693  SwapSlotType::sptr m_slotSwap;
694 
696  SwapKeySlotType::sptr m_slotSwapKey;
697 
699  SPTR(::fwThread::Worker) m_associatedWorker;
700 
702 
703 private:
704 
705  // Slot: start the service
706  SharedFutureType startSlot();
707  SharedFutureType internalStart(bool _async);
708 
709  // Slot: stop the service
710  SharedFutureType stopSlot();
711  SharedFutureType internalStop(bool _async);
712 
713  // Slot: swap the object
714  SharedFutureType swapSlot(::fwData::Object::sptr _obj);
715  SharedFutureType internalSwap(::fwData::Object::sptr _obj, bool _async);
716 
717  // Slot: swap an object
718  SharedFutureType swapKeySlot(const KeyType& _key, ::fwData::Object::sptr _obj);
719  SharedFutureType internalSwapKey(const KeyType& _key, ::fwData::Object::sptr _obj, bool _async);
720 
721  // Slot: update the service
722  SharedFutureType updateSlot();
723  SharedFutureType internalUpdate(bool _async);
724 
726  FWSERVICES_API void connectToConfig();
727 
729  FWSERVICES_API void disconnectFromConfig();
730 
732  FWSERVICES_API void autoConnect();
733 
735  FWSERVICES_API void autoDisconnect();
736 
738  FWSERVICES_API void addProxyConnection(const helper::ProxyConnections& info);
739 
743  InputMapType m_inputsMap;
744 
748  InOutMapType m_inOutsMap;
749 
753  OutputMapType m_outputsMap;
754 
758  std::map<KeyType, IdType> m_idsMap;
759 
763  std::map<std::string, size_t> m_keyGroupSize;
764 
768  GlobalStatus m_globalState;
769 
773  UpdatingStatus m_updatingState;
774 
778  ConfigurationStatus m_configurationState;
779 
783  ::fwServices::IService::Config m_serviceConfig;
784 
786  ::fwCom::helper::SigSlotConnection m_srvConnections;
787 
789  ::fwCom::helper::SigSlotConnection m_autoConnections;
790 
792  std::map<std::string, helper::ProxyConnections> m_proxies;
793 };
794 
795 } // namespace fwServices
796 
797 #include "fwServices/IService.hxx"
Base class for all services.
Definition: IService.hpp:61
#define SPTR(_cls_)
#define CSPTR(_cls_)
Contains fwAtomsFilter::registry details.
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
GlobalStatus
Defines all possible global status for a service, including transitions.
Definition: IService.hpp:122
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_SWAPKEY_SLOT
Slot to call start method.
Definition: IService.hpp:183
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_STOP_SLOT
Slot to call start method.
Definition: IService.hpp:174
static FWSERVICES_API const std::string s_DEFAULT_OBJECT
Definition: IService.hpp:113
Define Base class for FW4SPL objects and services.
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_START_SLOT
Slot to call start method.
Definition: IService.hpp:171
Namespace containing fw4spl communication tools.
Definition: DumpEditor.hpp:30
std::shared_future< void > SharedFutureType
Slot to call start method.
Definition: IService.hpp:167
SwapKeySlotType::sptr m_slotSwapKey
Slot to call swap method.
Definition: IService.hpp:696
STL namespace.
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
virtual void swapping(const KeyType &key)
Swap the service from an associated object to another object. The key in parameter indicates allows t...
Definition: IService.hpp:627
This class provides an API to manage config template.
This class proposes a mapping between a SlotKeyType and a SlotBase.
Definition: HasSlots.hpp:22
virtual void swapping()
Swap the service from associated object to another object.
Definition: IService.hpp:613
::fwCom::Slot< SharedFutureType()> UpdateSlotType
Slot to call start method.
Definition: IService.hpp:178
SwapSlotType::sptr m_slotSwap
Slot to call swap method.
Definition: IService.hpp:693
Defines the configuration element class.
maintain the relation between objects and services
::fwCom::Slot< SharedFutureType(::fwData::Object::sptr)> SwapSlotType
Slot to call start method.
Definition: IService.hpp:181
Used to store object configuration in a service.
Definition: IService.hpp:90
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
UpdateSlotType::sptr m_slotUpdate
Slot to call update method.
Definition: IService.hpp:690
std::packaged_task< void()> PackagedTaskType
Slot to call start method.
Definition: IService.hpp:168
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
::fwCom::helper::SigSlotConnection::KeyConnectionsType KeyConnectionsType
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: IService.hpp:449
::fwData::Object::wptr m_associatedObject
associated object of service
Definition: IService.hpp:676
UpdatingStatus
Defines all possible status for an update process.
Definition: IService.hpp:132
#define fwCoreAllowSharedFromThis()
Generate getSptr and getConstSptr methods.
#define fwCoreServiceClassDefinitionsMacro(_classinfo_)
Generate common code for services classes.
std::future< void > UniqueFutureType
Slot to call start method.
Definition: IService.hpp:169
ConfigurationStatus
Defines all possible status for a configuration process.
Definition: IService.hpp:139
Contains the representation of the data objects used in the framework.
StopSlotType::sptr m_slotStop
Slot to call stop method.
Definition: IService.hpp:687
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_SWAP_SLOT
Slot to call start method.
Definition: IService.hpp:180
::fwCom::Slot< SharedFutureType()> StopSlotType
Slot to call start method.
Definition: IService.hpp:175
This class creates and manages a task loop. The default implementation create a loop in a new thread...
Definition: Worker.hpp:32
This namespace fwThread provides few tools to execute asynchronous tasks on different threads...
std::ostream & operator<<(std::ostream &_ostream, IService &_service)
Streaming a service.
Definition: IService.cpp:936
StartSlotType::sptr m_slotStart
Slot to call start method.
Definition: IService.hpp:684
::fwCom::Slot< SharedFutureType()> StartSlotType
Slot to call start method.
Definition: IService.hpp:172
::fwCom::Slot< SharedFutureType(const KeyType &,::fwData::Object::sptr)> SwapKeySlotType
Slot to call start method.
Definition: IService.hpp:184
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177
Used to store a service configuration.
Definition: IService.hpp:100
This class proposes a mapping between a SignalKeyType and a SignalBase.
Definition: HasSignals.hpp:21