StatechartListenerSegment.cpp
Go to the documentation of this file.
2
3#include <atomic>
4
7#include <RobotAPI/libraries/armem_skills/aron/Statechart.aron.generated.h>
9
11{
14 Base(iceMemory,
15 "StatechartListener",
16 "Transitions",
17 arondto::Statechart::Transition::ToAronType())
18 {
19 }
20
21 void
23 const std::string& prefix)
24 {
25 // Statechart Logging
26 defs->optional(p.statechartCoreSegmentName,
27 "StatechartCoreSegmentName",
28 "Name of the core segment for statecharts.");
29 defs->optional(p.statechartTransitionsProviderSegmentName,
30 "TransitionsProviderSegmentName",
31 "Name of the provider segment for statechart transitions.");
32 defs->optional(p.statechartTransitionsTopicName,
33 "tpc.sub.ProfilerListener",
34 "Name of the ProfilerListenerInterface topics to subscribe.");
35
37 coreSegment.setDefaultMaxHistorySize(10);
38 Base::defineProperties(defs, prefix);
39 }
40
41 void
46
47 void
49 const ProfilerStatechartTransitionWithParameters& t)
50 {
51 // Ice topic callbacks can fire before init() has set segmentPtr, or
52 // after the segment has been torn down during shutdown. In both cases
53 // dereferencing segmentPtr would crash the component.
54 if (segmentPtr == nullptr)
55 {
56 static std::atomic_flag warned = ATOMIC_FLAG_INIT;
57 if (!warned.test_and_set())
58 {
59 ARMARX_WARNING << "StatechartListenerProviderSegment received a "
60 "transition before init() or after shutdown; "
61 "dropping. This warning is only logged once.";
62 }
63 return;
64 }
65
66 const std::string& entityName = getStatechartName(t.targetStateIdentifier);
67 armem::Time transitionTime = armem::Time(armem::Duration::MicroSeconds(t.timestamp));
68
69 armem::Commit commit;
70 armem::EntityUpdate& update = commit.add();
71 update.entityID = segmentPtr->id().withEntityName(entityName);
72
73 update.referencedTime = transitionTime;
74 skills::arondto::Statechart::Transition data;
76 update.instancesData.push_back(data.toAron());
77
78 try
79 {
80 // Commit through the locking path so the underlying std::map is
81 // modified under an exclusive lock on the core segment. Without
82 // this lock, concurrent queries (which iterate the map under a
83 // shared lock) hit invalidated iterators -> segfault.
84 iceMemory.commitLocking(commit);
85 }
86 catch (const armem::error::ArMemError& e)
87 {
88 ARMARX_WARNING << e.what();
89 }
90 }
91
92 void
94 const ProfilerStatechartTransitionWithParametersList& transitions)
95 {
96 for (const auto& t : transitions)
97 {
99 }
100 }
101
102 std::string
103 StatechartListenerProviderSegment::getStatechartName(std::string stateName)
104 {
105 const std::string delimiter = "->";
106 const int maxLevels = 2;
107
108 size_t pos;
109 int levels = 0;
110 std::string statechartName;
111 while ((pos = stateName.find(delimiter)) != std::string::npos && levels < maxLevels)
112 {
113 if (levels != 0)
114 {
115 statechartName += delimiter;
116 }
117 statechartName += stateName.substr(0, pos);
118 stateName.erase(0, pos + delimiter.length());
119 levels++;
120 }
121
122 return statechartName;
123 }
124} // namespace armarx::skills::segment
Base class for all exceptions thrown by the armem library.
Definition ArMemError.h:19
Helps connecting a Memory server to the Ice interface.
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition Duration.cpp:24
void reportStatechartTransitionWithParameters(const ProfilerStatechartTransitionWithParameters &)
StatechartListenerProviderSegment(armem::server::MemoryToIceAdapter &iceMemory)
void reportStatechartTransitionWithParametersList(const ProfilerStatechartTransitionWithParametersList &)
void defineProperties(PropertyDefinitionsPtr defs, const std::string &prefix)
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
armarx::core::time::DateTime Time
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
A bundle of updates to be sent to the memory.
Definition Commit.h:90
EntityUpdate & add()
Definition Commit.cpp:80
An update of an entity for a specific point in time.
Definition Commit.h:26