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