StatechartEventDistributor.cpp
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 
26 #include "StateController.h"
27 #include "StatechartManager.h"
28 #include "StateBaseImpl.h"
29 
30 using namespace armarx;
31 
33 {
34  statechartName = "UnknownStatechart";
35  setTag("StatechartEventDistributor");
36 }
37 
39 {
40  this->statechartManager = statechartManager;
41 }
42 
43 std::string StatechartEventDistributor::registerEvent(const EventPtr& event, StateBasePtr eventProcessor, long visitId)
44 {
45  std::string uniqueEventIdentifier = getUniqueEventIdentifier(eventProcessor, event, visitId);
47  entry.event = new Event(*event);
48  entry.eventProcessor = StateControllerPtr::dynamicCast(eventProcessor);
49  entry.globalEventIdentifier = uniqueEventIdentifier;
50  entry.useCounter = 1;
51  entry.visitId = visitId;
52 
53  std::unique_lock lock(eventMapMutex);
54  StatechartEventDistributor::EventListenerMap::iterator it = eventMap.find(uniqueEventIdentifier);
55 
56  if (it != eventMap.end())
57  {
58  it->second.useCounter++;
59  }
60  else
61  {
62  eventMap[uniqueEventIdentifier] = entry;
63  }
64 
65  return uniqueEventIdentifier;
66 }
67 
68 void StatechartEventDistributor::onInitComponent()
69 {
70 
71 }
72 
73 void StatechartEventDistributor::onConnectComponent()
74 {
75  myProxy = EventListenerInterfacePrx::checkedCast(getProxy());
76 }
77 
78 
79 
80 
81 void StatechartEventDistributor::reportEvent(const EventBasePtr& event, const Ice::Current& c)
82 {
83  {
84  std::unique_lock lock(eventMapMutex);
85  EventListenerMap::iterator it = eventMap.find(event->eventName);
86 
87  if (it == eventMap.end())
88  {
89  ARMARX_ERROR << "Could not find Event in EventListenerMap" << event->eventName << " receiver: " << event->eventReceiverName;
90  throw LocalException("Could not find Event in EventListenerMap - eventName") << event->eventName << " receiver: " << event->eventReceiverName;
91  }
92 
93  EventListenerMapEntry& entry = it->second;
94  entry.event->properties = event->properties;
95 
96  if (!statechartManager->addEvent(entry.event, entry.eventProcessor))
97  {
98  return;
99  }
100 
101  entry.useCounter--;
102 
103  if (entry.useCounter <= 0)
104  {
105  eventMap.erase(it);
106  }
107  }
108 }
109 
110 
111 
112 
113 
114 
115 std::string StatechartEventDistributor::getUniqueEventIdentifier(const StateBasePtr& eventProcessor, const EventPtr& event, long visitId)
116 {
117  std::stringstream str;
118  str << getName() << "___" <<
119  eventProcessor->stateName <<
120  "(" << eventProcessor->impl->localUniqueId <<
121  ")___Event:" << event->eventName <<
122  "___EventRecv:" << event->eventReceiverName <<
123  "__visitId:" << visitId;
124  return str.str();
125 }
126 
128 {
129  eventMap.clear();
130 }
131 
132 EventListenerInterfacePrx StatechartEventDistributor::getListener()
133 {
134  return myProxy;
135 }
136 
armarx::StatechartEventDistributor::setStatechartManager
void setStatechartManager(const StatechartManagerPtr &statechartManager)
Definition: StatechartEventDistributor.cpp:38
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
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:42
StateBaseImpl.h
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
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
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< Event >
StatechartEventDistributor.h
StateController.h
armarx::Event
An Event is used to communicate between e.g. condition handlers and statecharts.
Definition: Event.h:52
armarx::StatechartEventDistributor::EventListenerMapEntry::visitId
long visitId
Definition: StatechartEventDistributor.h:77
armarx::StatechartEventDistributor::EventListenerMapEntry::useCounter
int useCounter
Definition: StatechartEventDistributor.h:76
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
StatechartManager.h
armarx::StatechartEventDistributor::eventMap
EventListenerMap eventMap
Definition: StatechartEventDistributor.h:105
IceUtil::Handle< StatechartManager >
armarx::StatechartEventDistributor::EventListenerMapEntry
Definition: StatechartEventDistributor.h:71
armarx::StatechartEventDistributor::EventListenerMapEntry::eventProcessor
StateControllerPtr eventProcessor
Definition: StatechartEventDistributor.h:73
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
armarx::StatechartEventDistributor::reportEvent
void reportEvent(const EventBasePtr &event, const Ice::Current &) override
Definition: StatechartEventDistributor.cpp:81
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:393
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