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 
27 #include <mutex>
28 #include <queue>
29 #include <string>
30 
32 #include <ArmarXCore/interface/statechart/StatechartIce.h>
33 
34 namespace armarx
35 {
36  class StateController;
38 
39  class StatechartManager;
41 
42  class StateBase;
44 
45  class Event;
47 
48  /**
49  * @brief The StatechartEventDistributor class is used to distribute
50  * incomming events (via Ice) to the correct states.<br/>
51  *
52  *
53  * The events must be registered before they can be distributed.
54  * The normal process is to register events here, when the condition or
55  * something else is registered on the observer. The registerEvent()-function
56  * returns a unique event-string that is to be passed to the observer.<br/>
57  * A pointer to this class is passed to the observer, so that the observer
58  * can call back.<br/>
59  *
60  * @note This class is an internal statechart-class and is used by the
61  * classes StatechartContext and StateUtility. There is usually only one
62  * instance per statechart (it is located in the StatechartContext).
63  */
65  virtual public EventListenerInterface,
66  virtual public ManagedIceObject
67  {
68  public:
70  {
73  std::string globalEventIdentifier;
75  long visitId;
76  };
77 
79 
80  void setStatechartManager(const StatechartManagerPtr& statechartManager);
81 
82  void
84  {
85  this->statechartName = statechartName;
87  }
88 
89  // inherited from Component
90  std::string
91  getDefaultName() const override
92  {
93  return statechartName + "s_EventDistributor";
94  }
95 
96  using EventListenerMap = std::map<std::string, EventListenerMapEntry>;
97 
98 
99  std::string registerEvent(const EventPtr& event, StateBasePtr eventProcessor, long visitId);
100  void reportEvent(const EventBasePtr& event, const Ice::Current&) override;
101  std::string getUniqueEventIdentifier(const StateBasePtr& eventProcessor,
102  const EventPtr& event,
103  long visitId);
104  void clearEventMap();
105  EventListenerInterfacePrx getListener();
106 
107  protected:
108  std::queue<std::pair<StateControllerPtr, EventPtr>> eventBuffer;
110  std::string statechartName;
111 
112  private:
113  // inherited from Component
114  void onInitComponent() override;
115  void onConnectComponent() override;
116  EventListenerInterfacePrx myProxy;
117  StatechartManagerPtr statechartManager;
118 
119  std::mutex eventMapMutex;
120  std::mutex eventBufferMutex;
121  };
122 } // namespace armarx
armarx::StatechartEventDistributor::setStatechartManager
void setStatechartManager(const StatechartManagerPtr &statechartManager)
Definition: StatechartEventDistributor.cpp:40
armarx::ManagedIceObject::setName
void setName(std::string name)
Override name of well-known object.
Definition: ManagedIceObject.cpp:445
armarx::StatechartEventDistributor::StatechartEventDistributor
StatechartEventDistributor()
Definition: StatechartEventDistributor.cpp:33
armarx::StatechartEventDistributor::getUniqueEventIdentifier
std::string getUniqueEventIdentifier(const StateBasePtr &eventProcessor, const EventPtr &event, long visitId)
Definition: StatechartEventDistributor.cpp:118
armarx::StatechartEventDistributor::registerEvent
std::string registerEvent(const EventPtr &event, StateBasePtr eventProcessor, long visitId)
Definition: StatechartEventDistributor.cpp:46
armarx::StatechartEventDistributor::statechartName
std::string statechartName
Definition: StatechartEventDistributor.h:110
armarx::StatechartEventDistributor::clearEventMap
void clearEventMap()
Definition: StatechartEventDistributor.cpp:130
armarx::StatechartEventDistributor::EventListenerMapEntry::event
EventPtr event
Definition: StatechartEventDistributor.h:72
armarx::StatechartEventDistributor::getListener
EventListenerInterfacePrx getListener()
Definition: StatechartEventDistributor.cpp:136
IceInternal::Handle< StateController >
armarx::Event
An Event is used to communicate between e.g. condition handlers and statecharts.
Definition: Event.h:50
ManagedIceObject.h
armarx::StatechartEventDistributor::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: StatechartEventDistributor.h:91
armarx::StatechartEventDistributor::EventListenerMapEntry::visitId
long visitId
Definition: StatechartEventDistributor.h:75
armarx::StatechartEventDistributor::EventListenerMapEntry::useCounter
int useCounter
Definition: StatechartEventDistributor.h:74
armarx::StatechartEventDistributor::eventBuffer
std::queue< std::pair< StateControllerPtr, EventPtr > > eventBuffer
Definition: StatechartEventDistributor.h:108
armarx::StateBase
Definition: StateBase.h:59
armarx::ManagedIceObject
The ManagedIceObject is the base class for all ArmarX objects.
Definition: ManagedIceObject.h:162
armarx::StatechartEventDistributor::eventMap
EventListenerMap eventMap
Definition: StatechartEventDistributor.h:109
IceUtil::Handle< StatechartManager >
armarx::StatechartEventDistributor::EventListenerMapEntry
Definition: StatechartEventDistributor.h:69
armarx::StatechartEventDistributor::EventListenerMapEntry::eventProcessor
StateControllerPtr eventProcessor
Definition: StatechartEventDistributor.h:71
armarx::StatechartEventDistributor::EventListenerMap
std::map< std::string, EventListenerMapEntry > EventListenerMap
Definition: StatechartEventDistributor.h:96
armarx::StatechartEventDistributor
The StatechartEventDistributor class is used to distribute incomming events (via Ice) to the correct ...
Definition: StatechartEventDistributor.h:64
armarx::StatechartEventDistributor::reportEvent
void reportEvent(const EventBasePtr &event, const Ice::Current &) override
Definition: StatechartEventDistributor.cpp:86
armarx::StatechartEventDistributor::EventListenerMapEntry::globalEventIdentifier
std::string globalEventIdentifier
Definition: StatechartEventDistributor.h:73
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::StatechartEventDistributor::setStatechartName
void setStatechartName(std::string statechartName)
Definition: StatechartEventDistributor.h:83
armarx::StatechartManager
Definition: StatechartManager.h:40