File size: 11,061 Bytes
68ce2fc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
/*
Author(s): Ankur Kapoor, Peter Kazanzides, Anton Deguet
Created on: 2005-05-02
(C) Copyright 2005-2014 Johns Hopkins University (JHU), All Rights
Reserved.
--- begin cisst license - do not edit ---
This software is provided "as is" under an open source license, with
no warranty. The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.
--- end cisst license ---
*/
/*!
\file
\brief Define an internal command for cisstMultiTask
*/
#ifndef _mtsCommandQueuedWrite_h
#define _mtsCommandQueuedWrite_h
#include <cisstMultiTask/mtsCommandQueuedWriteBase.h>
/*!
\ingroup cisstMultiTask
*/
/*! Write queued command using templated _argumentType parameter. Currently, this is only used
for adding event handlers, and could perhaps be replaced by mtsCommandQueuedWriteGeneric. */
template <class _argumentType>
class mtsCommandQueuedWrite: public mtsCommandQueuedWriteBase
{
public:
typedef mtsCommandQueuedWriteBase BaseType;
typedef _argumentType ArgumentType;
typedef typename mtsGenericTypes<ArgumentType>::FinalType ArgumentQueueType;
typedef typename mtsGenericTypesUnwrap<ArgumentQueueType>::BaseType ArgumentQueueBaseType;
/*! This type. */
typedef mtsCommandQueuedWrite<ArgumentType> ThisType;
protected:
/*! Queue to store arguments */
mtsQueue<ArgumentQueueType> ArgumentsQueue;
private:
/*! Private copy constructor to prevent copies */
inline mtsCommandQueuedWrite(const ThisType & CMN_UNUSED(other));
public:
inline mtsCommandQueuedWrite(void):
BaseType(),
ArgumentsQueue(0, ArgumentQueueType())
{}
inline mtsCommandQueuedWrite(mtsCommandWriteBase * actualCommand):
BaseType(0, actualCommand, 0),
ArgumentsQueue(0, ArgumentQueueType())
{}
inline mtsCommandQueuedWrite(mtsMailBox * mailBox, mtsCommandWriteBase * actualCommand, size_t size):
BaseType(mailBox, actualCommand, size),
ArgumentsQueue(0, ArgumentQueueType())
{
const ArgumentQueueType * argumentPrototype = dynamic_cast<const ArgumentQueueType *>(this->GetArgumentPrototype());
if (argumentPrototype) {
ArgumentsQueue.SetSize(size, *argumentPrototype);
BlockingFlagQueue.SetSize(size, MTS_NOT_BLOCKING);
mtsCommandWriteBase *cmd = 0;
FinishedEventQueue.SetSize(size, cmd);
} else {
CMN_LOG_INIT_ERROR << "Class mtsCommandQueuedWrite: constructor: can't find argument prototype from actual command."
<< std::endl;
}
}
// ArgumentsQueue destructor should get called
inline virtual ~mtsCommandQueuedWrite() {}
inline virtual mtsCommandQueuedWriteBase * Clone(mtsMailBox * mailBox, size_t size) const {
return new mtsCommandQueuedWrite(mailBox, this->ActualCommand, size);
}
// Allocate should be called when a task calls GetMethodXXX().
inline virtual void Allocate(size_t size) {
if (ArgumentsQueue.GetSize() != size) {
if (ArgumentsQueue.GetSize() > 0) {
// Probably should never happen
CMN_LOG_INIT_WARNING << "Class mtsCommandQueuedWrite: Allocate(): changing ArgumentsQueue size from " << ArgumentsQueue.GetSize()
<< " to " << size << std::endl;
}
const ArgumentQueueType * argumentPrototype = dynamic_cast<const ArgumentQueueType *>(this->GetArgumentPrototype());
if (argumentPrototype) {
ArgumentsQueue.SetSize(size, *argumentPrototype);
BlockingFlagQueue.SetSize(size, MTS_NOT_BLOCKING);
mtsCommandWriteBase *cmd = 0;
FinishedEventQueue.SetSize(size, cmd);
} else {
CMN_LOG_INIT_ERROR << "Class mtsCommandQueuedWrite: constructor: can't find argument prototype from actual command."
<< std::endl;
}
}
}
mtsExecutionResult Execute(const mtsGenericObject & argument,
mtsBlockingType blocking,
mtsCommandWriteBase * finishedEventHandler) {
// check if this command is enabled
if (!this->IsEnabled()) {
return mtsExecutionResult::COMMAND_DISABLED;
}
// check if there is a mailbox (i.e. if the command is associated to an interface
if (!MailBox) {
CMN_LOG_RUN_ERROR << "Class mtsCommandQueuedWrite: Execute: no mailbox for \""
<< this->Name << "\"" << std::endl;
return mtsExecutionResult::COMMAND_HAS_NO_MAILBOX;
}
// try to cast the input type
const ArgumentQueueBaseType * argumentTyped = dynamic_cast<const ArgumentQueueBaseType*>(&argument);
if (!argumentTyped) {
return mtsExecutionResult::INVALID_INPUT_TYPE;
}
// check if all queues have some space
if (ArgumentsQueue.IsFull() || BlockingFlagQueue.IsFull() || FinishedEventQueue.IsFull() || MailBox->IsFull()) {
CMN_LOG_RUN_WARNING << "Class mtsCommandQueuedWrite: Execute: Queue full for \""
<< this->Name << "\" ["
<< ArgumentsQueue.IsFull() << "|"
<< BlockingFlagQueue.IsFull() << "|"
<< FinishedEventQueue.IsFull() << "|"
<< MailBox->IsFull() << "]"
<< std::endl;
return mtsExecutionResult::COMMAND_ARGUMENT_QUEUE_FULL;
}
// copy the argument to the local storage.
if (!ArgumentsQueue.Put(*argumentTyped)) {
CMN_LOG_RUN_ERROR << "Class mtsCommandQueuedWrite: Execute: ArgumentsQueue full for \""
<< this->Name << "\"" << std::endl;
cmnThrow("mtsCommandQueuedWrite: Execute: ArgumentsQueue.Put failed");
return mtsExecutionResult::UNDEFINED;
}
// copy the blocking flag to the local storage.
if (!BlockingFlagQueue.Put(blocking)) {
CMN_LOG_RUN_ERROR << "Class mtsCommandQueuedWrite: Execute: BlockingFlagQueue full for \""
<< this->Name << "\"" << std::endl;
ArgumentsQueue.Get(); // pop argument
cmnThrow("mtsCommandQueuedWrite: Execute: BlockingFlagQueue.Put failed");
return mtsExecutionResult::UNDEFINED;
}
// copy the finished event handler to the local storage.
if (!FinishedEventQueue.Put(finishedEventHandler)) {
CMN_LOG_RUN_ERROR << "Class mtsCommandQueuedWrite: Execute: FinishedEventQueue.Put failed for \""
<< this->Name << "\"" << std::endl;
ArgumentsQueue.Get(); // Remove the argument that was already queued
BlockingFlagQueue.Get(); // Remove the blocking flag that was already queued
cmnThrow("mtsCommandQueuedWrite: Execute: FinishedEventQueue.Put failed");
return mtsExecutionResult::UNDEFINED;
}
// finally try to queue to mailbox
if (!MailBox->Write(this)) {
CMN_LOG_RUN_ERROR << "Class mtsCommandQueuedWrite: Execute: mailbox full for \""
<< this->Name << "\"" << std::endl;
ArgumentsQueue.Get(); // pop argument, blocking flag, and finished event from local storage
BlockingFlagQueue.Get();
FinishedEventQueue.Get();
cmnThrow("mtsCommandQueuedWrite: Execute: MailBox.Write failed");
return mtsExecutionResult::UNDEFINED;
}
return mtsExecutionResult::COMMAND_QUEUED;
}
/* commented in base class */
const mtsGenericObject * GetArgumentPrototype(void) const {
return this->ActualCommand->GetArgumentPrototype();
}
inline virtual const mtsGenericObject * ArgumentPeek(void) const {
return ArgumentsQueue.Peek();
}
inline virtual mtsGenericObject * ArgumentGet(void) {
return ArgumentsQueue.Get();
}
};
/*! Write queued command using mtsGenericObject parameter. This is used for all
queued write commands. It is also used to create a generic event
observer (combined with mtsMulticastCommandWriteBase) that can
accept any payload (derived from mtsGenericObject). */
// PK: methods are defined in mtsCommandQueuedWriteBase.cpp
class CISST_EXPORT mtsCommandQueuedWriteGeneric: public mtsCommandQueuedWriteBase
{
protected:
typedef mtsCommandQueuedWriteBase BaseType;
typedef mtsCommandQueuedWriteGeneric ThisType;
size_t ArgumentQueueSize; // size used for queue
/*! Queue to store arguments */
mtsQueueGeneric ArgumentsQueue;
private:
/*! Private default constructor to prevent use. */
inline mtsCommandQueuedWriteGeneric(void);
/*! Private copy constructor to prevent copies */
inline mtsCommandQueuedWriteGeneric(const ThisType & CMN_UNUSED(other));
public:
/*! Constructor, requires a mailbox to queue commands, a pointer
on actual command and size used to create the argument queue.
If the actual command doesn't provide an argument prototype, the
argument queue is not allocated. Queue allocation will
potentially occur later, i.e. when SetArgumentPrototype is
used. This is useful when the queued command is added to a
multicast command. */
mtsCommandQueuedWriteGeneric(mtsMailBox * mailBox, mtsCommandWriteBase * actualCommand, size_t size);
/*! Destructor */
inline virtual ~mtsCommandQueuedWriteGeneric() {}
virtual void ToStream(std::ostream & outputStream) const;
inline virtual mtsCommandQueuedWriteGeneric * Clone(mtsMailBox * mailBox, size_t size) const {
return new mtsCommandQueuedWriteGeneric(mailBox, this->ActualCommand, size);
}
// Allocate should be called when a task calls GetMethodXXX().
virtual void Allocate(size_t size);
inline virtual void SetArgumentPrototype(const mtsGenericObject * argumentPrototype) {
this->ActualCommand->SetArgumentPrototype(argumentPrototype);
this->Allocate(this->ArgumentQueueSize);
}
mtsExecutionResult Execute(const mtsGenericObject & argument,
mtsBlockingType blocking,
mtsCommandWriteBase *finishedEventHandler);
/* commented in base class */
const mtsGenericObject * GetArgumentPrototype(void) const {
return this->ActualCommand->GetArgumentPrototype();
}
inline virtual const mtsGenericObject * ArgumentPeek(void) const {
return ArgumentsQueue.Peek();
}
inline virtual mtsGenericObject * ArgumentGet(void) {
return ArgumentsQueue.Get();
}
};
#endif // _mtsCommandQueuedWrite_h
|