StatechartEventDistributor.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2012
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
28 #include <ArmarXCore/interface/statechart/StatechartIce.h>
29 
30 #include <string>
31 #include <mutex>
32 #include <queue>
33 
34 
35 namespace armarx
36 {
37  class StateController;
39 
40  class StatechartManager;
42 
43  class StateBase;
45 
46  class Event;
48 
49  /**
50  * @brief The StatechartEventDistributor class is used to distribute
51  * incomming events (via Ice) to the correct states.<br/>
52  *
53  *
54  * The events must be registered before they can be distributed.
55  * The normal process is to register events here, when the condition or
56  * something else is registered on the observer. The registerEvent()-function
57  * returns a unique event-string that is to be passed to the observer.<br/>
58  * A pointer to this class is passed to the observer, so that the observer
59  * can call back.<br/>
60  *
61  * @note This class is an internal statechart-class and is used by the
62  * classes StatechartContext and StateUtility. There is usually only one
63  * instance per statechart (it is located in the StatechartContext).
64  */
66  virtual public EventListenerInterface,
67  virtual public ManagedIceObject
68  {
69  public:
70 
72  {
75  std::string globalEventIdentifier;
77  long visitId;
78  };
79 
80 
82 
83  void setStatechartManager(const StatechartManagerPtr& statechartManager);
85  {
86  this->statechartName = statechartName;
88  }
89  // inherited from Component
90  std::string getDefaultName() const override
91  {
92  return statechartName + "s_EventDistributor";
93  }
94 
95  using EventListenerMap = std::map<std::string, EventListenerMapEntry>;
96 
97 
98  std::string registerEvent(const EventPtr& event, StateBasePtr eventProcessor, long visitId);
99  void reportEvent(const EventBasePtr& event, const Ice::Current&) override;
100  std::string getUniqueEventIdentifier(const StateBasePtr& eventProcessor, const EventPtr& event, long visitId);
101  void clearEventMap();
102  EventListenerInterfacePrx getListener();
103  protected:
104  std::queue< std::pair<StateControllerPtr, EventPtr > > eventBuffer;
106  std::string statechartName;
107  private:
108  // inherited from Component
109  void onInitComponent() override;
110  void onConnectComponent() override;
111  EventListenerInterfacePrx myProxy;
112  StatechartManagerPtr statechartManager;
113 
114  std::mutex eventMapMutex;
115  std::mutex eventBufferMutex;
116  };
117 }
armarx::StatechartEventDistributor::setStatechartManager
void setStatechartManager(const StatechartManagerPtr &statechartManager)
Definition: StatechartEventDistributor.cpp:38
armarx::ManagedIceObject::setName
void setName(std::string name)
Override name of well-known object.
Definition: ManagedIceObject.cpp:426
armarx::StatechartEventDistributor::StatechartEventDistributor
StatechartEventDistributor()
Definition: StatechartEventDistributor.cpp:32
armarx::StatechartEventDistributor::getUniqueEventIdentifier
std::string getUniqueEventIdentifier(const StateBasePtr &eventProcessor, const EventPtr &event, long visitId)
Definition: StatechartEventDistributor.cpp:115
armarx::StatechartEventDistributor::registerEvent
std::string registerEvent(const EventPtr &event, StateBasePtr eventProcessor, long visitId)
Definition: StatechartEventDistributor.cpp:43
armarx::StatechartEventDistributor::statechartName
std::string statechartName
Definition: StatechartEventDistributor.h:106
armarx::StatechartEventDistributor::clearEventMap
void clearEventMap()
Definition: StatechartEventDistributor.cpp:127
armarx::StatechartEventDistributor::EventListenerMapEntry::event
EventPtr event
Definition: StatechartEventDistributor.h:74
armarx::StatechartEventDistributor::getListener
EventListenerInterfacePrx getListener()
Definition: StatechartEventDistributor.cpp:132
IceInternal::Handle< StateController >
armarx::Event
An Event is used to communicate between e.g. condition handlers and statecharts.
Definition: Event.h:52
ManagedIceObject.h
armarx::StatechartEventDistributor::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: StatechartEventDistributor.h:90
armarx::StatechartEventDistributor::EventListenerMapEntry::visitId
long visitId
Definition: StatechartEventDistributor.h:77
armarx::StatechartEventDistributor::EventListenerMapEntry::useCounter
int useCounter
Definition: StatechartEventDistributor.h:76
armarx::StateBase
Definition: StateBase.h:61
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:163
armarx::StatechartEventDistributor::eventMap
EventListenerMap eventMap
Definition: StatechartEventDistributor.h:105
armarx::StatechartEventDistributor::eventBuffer
std::queue< std::pair< StateControllerPtr, EventPtr > > eventBuffer
Definition: StatechartEventDistributor.h:104
IceUtil::Handle< StatechartManager >
armarx::StatechartEventDistributor::EventListenerMapEntry
Definition: StatechartEventDistributor.h:71
armarx::StatechartEventDistributor::EventListenerMapEntry::eventProcessor
StateControllerPtr eventProcessor
Definition: StatechartEventDistributor.h:73
armarx::StatechartEventDistributor::EventListenerMap
std::map< std::string, EventListenerMapEntry > EventListenerMap
Definition: StatechartEventDistributor.h:95
armarx::StatechartEventDistributor
The StatechartEventDistributor class is used to distribute incomming events (via Ice) to the correct ...
Definition: StatechartEventDistributor.h:65
armarx::StatechartEventDistributor::reportEvent
void reportEvent(const EventBasePtr &event, const Ice::Current &) override
Definition: StatechartEventDistributor.cpp:81
armarx::StatechartEventDistributor::EventListenerMapEntry::globalEventIdentifier
std::string globalEventIdentifier
Definition: StatechartEventDistributor.h:75
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StatechartEventDistributor::setStatechartName
void setStatechartName(std::string statechartName)
Definition: StatechartEventDistributor.h:84
armarx::StatechartManager
Definition: StatechartManager.h:42