SensorActorUnit.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarXCore::untis
17* @author Kai Welke (welke@kit.edu)
18* @date 2011
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "SensorActorUnit.h"
24
25#include <Ice/Initialize.h>
26#include <Ice/ObjectAdapter.h>
27
28using namespace armarx;
29
30#define CALLINFO //ARMARX_INFO <<__FILE__ << ":" << __PRETTY_FUNCTION__ << flush;
31
33{
34 executionState = eUnitConstructed;
35}
36
40
41void
42SensorActorUnit::init(const Ice::Current& c)
43{
44 std::string currentName = c.adapter->getName();
45 Ice::Identity currentId = Ice::stringToIdentity(currentName);
46
47 if (ownerId == currentId)
48 {
50
51 if (executionState == eUnitConstructed)
52 {
53 executionState = eUnitInitialized;
54 onInit();
55 }
56 else
57 {
58 ARMARX_WARNING << "Unit " << getName() << " is already initialized." << armarx::flush;
59 }
60 }
61 else
62 {
63 ARMARX_WARNING << "Unit " << getName() << " can not be initialized while unrequested."
65 }
66}
67
68void
69SensorActorUnit::start(const Ice::Current& c)
70{
71 std::string currentName = c.adapter->getName();
72 Ice::Identity currentId = Ice::stringToIdentity(currentName);
73
74 if (ownerId == currentId)
75 {
77
78 if ((executionState == eUnitInitialized) || (executionState == eUnitStopped))
79 {
80 executionState = eUnitStarted;
81 onStart();
82 }
83 else
84 {
85 ARMARX_WARNING << "Unit " << getName() << " could not be started." << armarx::flush;
86 }
87 }
88 else
89 {
90 ARMARX_WARNING << "Unit " << getName() << " can not be started while unrequested."
92 }
93}
94
95void
96SensorActorUnit::stop(const Ice::Current& c)
97{
98 std::string currentName = c.adapter->getName();
99 Ice::Identity currentId = Ice::stringToIdentity(currentName);
100
101 if (ownerId == currentId)
102 {
104
105 if (executionState == eUnitStarted)
106 {
107 executionState = eUnitStopped;
108 onStop();
109 }
110 else
111 {
112 ARMARX_WARNING << "Unit " << getName() << " could not be stopped." << armarx::flush;
113 }
114 }
115 else
116 {
117 ARMARX_WARNING << "Unit " << getName() << " can not be stopped while unrequested. "
118 << armarx::flush;
119 }
120}
121
122UnitExecutionState
124{
126 return executionState;
127}
128
129void
130SensorActorUnit::request(const Ice::Current& c)
131{
133
134 // try to lock unit
135 if (!unitMutex.try_lock())
136 {
137 ARMARX_ERROR << "Trying to request already owned unit " << getName() << "\n";
138 throw ResourceUnavailableException("Trying to request already owned unit");
139 }
140
141 // retrieve owner id from current connection
142 std::string ownerName = c.adapter->getName();
143 ownerId = Ice::stringToIdentity(ownerName);
144
145 ARMARX_INFO << "unit requested by " << ownerName << flush;
146}
147
148void
149SensorActorUnit::release(const Ice::Current& c)
150{
152 // retrieve owner id from current connection
153 std::string callerName = c.adapter->getName();
154 Ice::Identity callerId = Ice::stringToIdentity(callerName);
155
156 if (!(ownerId == callerId))
157 {
158 ARMARX_ERROR << "Trying to release unit owned by another component";
159 throw ResourceNotOwnedException("Trying to release unit owned by another component");
160 }
161
162 // unlock mutex
163 ownerId = Ice::stringToIdentity(" ");
164 unitMutex.unlock();
165
166 ARMARX_INFO << "unit released" << flush;
167}
168
169void
171{
172 if (unitMutex.try_lock()) // try to lock, to ensure that it is locked on unlock call
173 {
174 unitMutex.unlock();
175 }
176}
#define CALLINFO
constexpr T c
std::string getName() const
Retrieve name of object.
void start(const Ice::Current &c=Ice::emptyCurrent) override
Set execution state to eStarted.
void release(const Ice::Current &c=Ice::emptyCurrent) override
Release exclusive access to current unit.
UnitExecutionState getExecutionState(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve current execution state.
void init(const Ice::Current &c=Ice::emptyCurrent) override
Set execution state to eInitialized.
virtual void onStart()
callback onStart for subclass hook.
void stop(const Ice::Current &c=Ice::emptyCurrent) override
Set execution state to eStopped.
~SensorActorUnit() override
Destructor of SensorActorUnit.
SensorActorUnit()
Constructs a SensorActorUnit.
virtual void onInit()
callback onInit for subclass hook.
virtual void onStop()
callback onStop for subclass hook.
void request(const Ice::Current &c=Ice::emptyCurrent) override
Request exclusive access to current unit.
void onExitComponent() override
Hook for subclass.
UnitExecutionState executionState
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
const LogSender::manipulator flush
Definition LogSender.h:251