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 
31 using namespace armarx;
32 
34 {
35  statechartName = "UnknownStatechart";
36  setTag("StatechartEventDistributor");
37 }
38 
39 void
41 {
42  this->statechartManager = statechartManager;
43 }
44 
45 std::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 
74 void
75 StatechartEventDistributor::onInitComponent()
76 {
77 }
78 
79 void
80 StatechartEventDistributor::onConnectComponent()
81 {
82  myProxy = EventListenerInterfacePrx::checkedCast(getProxy());
83 }
84 
85 void
86 StatechartEventDistributor::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 
117 std::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 
129 void
131 {
132  eventMap.clear();
133 }
134 
135 EventListenerInterfacePrx
137 {
138  return myProxy;
139 }
armarx::StatechartEventDistributor::setStatechartManager
void setStatechartManager(const StatechartManagerPtr &statechartManager)
Definition: StatechartEventDistributor.cpp:40
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
str
std::string str(const T &t)
Definition: UserAssistedSegmenterGuiWidgetController.cpp:43
StateBaseImpl.h
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
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
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< Event >
StatechartEventDistributor.h
StateController.h
armarx::Event
An Event is used to communicate between e.g. condition handlers and statecharts.
Definition: Event.h:50
armarx::StatechartEventDistributor::EventListenerMapEntry::visitId
long visitId
Definition: StatechartEventDistributor.h:75
armarx::StatechartEventDistributor::EventListenerMapEntry::useCounter
int useCounter
Definition: StatechartEventDistributor.h:74
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
StatechartManager.h
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::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:108
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:54
armarx::StatechartEventDistributor::reportEvent
void reportEvent(const EventBasePtr &event, const Ice::Current &) override
Definition: StatechartEventDistributor.cpp:86
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:407
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