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 #include <Ice/ObjectAdapter.h>
25 #include <Ice/Initialize.h>
26 
27 using namespace armarx;
28 
29 #define CALLINFO //ARMARX_INFO <<__FILE__ << ":" << __PRETTY_FUNCTION__ << flush;
30 
32 {
33  executionState = eUnitConstructed;
34 }
35 
37 {
38 
39 }
40 
41 void SensorActorUnit::init(const Ice::Current& c)
42 {
43  std::string currentName = c.adapter->getName();
44  Ice::Identity currentId = Ice::stringToIdentity(currentName);
45 
46  if (ownerId == currentId)
47  {
48  CALLINFO
49 
50  if (executionState == eUnitConstructed)
51  {
52  executionState = eUnitInitialized;
53  onInit();
54  }
55  else
56  {
57  ARMARX_WARNING << "Unit " << getName() << " is already initialized." << armarx::flush;
58  }
59  }
60  else
61  {
62  ARMARX_WARNING << "Unit " << getName() << " can not be initialized while unrequested." << armarx::flush;
63  }
64 }
65 
66 void SensorActorUnit::start(const Ice::Current& c)
67 {
68  std::string currentName = c.adapter->getName();
69  Ice::Identity currentId = Ice::stringToIdentity(currentName);
70 
71  if (ownerId == currentId)
72  {
73  CALLINFO
74 
75  if ((executionState == eUnitInitialized) || (executionState == eUnitStopped))
76  {
77  executionState = eUnitStarted;
78  onStart();
79  }
80  else
81  {
82  ARMARX_WARNING << "Unit " << getName() << " could not be started." << armarx::flush;
83  }
84  }
85  else
86  {
87  ARMARX_WARNING << "Unit " << getName() << " can not be started while unrequested." << armarx::flush;
88  }
89 }
90 
91 void SensorActorUnit::stop(const Ice::Current& c)
92 {
93  std::string currentName = c.adapter->getName();
94  Ice::Identity currentId = Ice::stringToIdentity(currentName);
95 
96  if (ownerId == currentId)
97  {
98  CALLINFO
99 
100  if (executionState == eUnitStarted)
101  {
102  executionState = eUnitStopped;
103  onStop();
104  }
105  else
106  {
107  ARMARX_WARNING << "Unit " << getName() << " could not be stopped." << armarx::flush;
108  }
109  }
110  else
111  {
112  ARMARX_WARNING << "Unit " << getName() << " can not be stopped while unrequested. " << armarx::flush;
113  }
114 }
115 
116 UnitExecutionState SensorActorUnit::getExecutionState(const Ice::Current& c)
117 {
118  CALLINFO
119  return executionState;
120 }
121 
122 
123 void SensorActorUnit::request(const Ice::Current& c)
124 {
125  CALLINFO
126 
127  // try to lock unit
128  if (!unitMutex.try_lock())
129  {
130  ARMARX_ERROR << "Trying to request already owned unit " << getName() << "\n";
131  throw ResourceUnavailableException("Trying to request already owned unit");
132  }
133 
134  // retrieve owner id from current connection
135  std::string ownerName = c.adapter->getName();
136  ownerId = Ice::stringToIdentity(ownerName);
137 
138  ARMARX_INFO << "unit requested by " << ownerName << flush;
139 }
140 
141 void SensorActorUnit::release(const Ice::Current& c)
142 {
143  CALLINFO
144  // retrieve owner id from current connection
145  std::string callerName = c.adapter->getName();
146  Ice::Identity callerId = Ice::stringToIdentity(callerName);
147 
148  if (!(ownerId == callerId))
149  {
150  ARMARX_ERROR << "Trying to release unit owned by another component";
151  throw ResourceNotOwnedException("Trying to release unit owned by another component");
152  }
153 
154  // unlock mutex
155  ownerId = Ice::stringToIdentity(" ");
156  unitMutex.unlock();
157 
158  ARMARX_INFO << "unit released" << flush;
159 }
160 
162 {
163  if (unitMutex.try_lock()) // try to lock, to ensure that it is locked on unlock call
164  {
165  unitMutex.unlock();
166  }
167 }
armarx::SensorActorUnit::onInit
virtual void onInit()
callback onInit for subclass hook.
Definition: SensorActorUnit.h:116
armarx::SensorActorUnit::~SensorActorUnit
~SensorActorUnit() override
Destructor of SensorActorUnit.
Definition: SensorActorUnit.cpp:36
armarx::SensorActorUnit::release
void release(const Ice::Current &c=Ice::emptyCurrent) override
Release exclusive access to current unit.
Definition: SensorActorUnit.cpp:141
armarx::SensorActorUnit::start
void start(const Ice::Current &c=Ice::emptyCurrent) override
Set execution state to eStarted.
Definition: SensorActorUnit.cpp:66
armarx::SensorActorUnit::SensorActorUnit
SensorActorUnit()
Constructs a SensorActorUnit.
Definition: SensorActorUnit.cpp:31
armarx::SensorActorUnit::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: SensorActorUnit.cpp:161
armarx::SensorActorUnit::request
void request(const Ice::Current &c=Ice::emptyCurrent) override
Request exclusive access to current unit.
Definition: SensorActorUnit.cpp:123
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::SensorActorUnit::getExecutionState
UnitExecutionState getExecutionState(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve current execution state.
Definition: SensorActorUnit.cpp:116
armarx::SensorActorUnit::onStop
virtual void onStop()
callback onStop for subclass hook.
Definition: SensorActorUnit.h:126
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::SensorActorUnit::ownerId
Ice::Identity ownerId
Definition: SensorActorUnit.h:129
armarx::SensorActorUnit::stop
void stop(const Ice::Current &c=Ice::emptyCurrent) override
Set execution state to eStopped.
Definition: SensorActorUnit.cpp:91
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
SensorActorUnit.h
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::SensorActorUnit::executionState
UnitExecutionState executionState
Definition: SensorActorUnit.h:130
armarx::SensorActorUnit::init
void init(const Ice::Current &c=Ice::emptyCurrent) override
Set execution state to eInitialized.
Definition: SensorActorUnit.cpp:41
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
CALLINFO
#define CALLINFO
Definition: SensorActorUnit.cpp:29
armarx::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::SensorActorUnit::onStart
virtual void onStart()
callback onStart for subclass hook.
Definition: SensorActorUnit.h:121
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::SensorActorUnit::unitMutex
std::mutex unitMutex
Definition: SensorActorUnit.h:126