User Manual, Developers Guide and API Documentation

CommandQueue.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/events/scheduler/CommandQueue.hpp>
00029 
00030 using namespace wns::events::scheduler;
00031 
00032 CommandQueue::CommandQueue() :
00033     queue_(),
00034     mutex_()
00035 {
00036     pthread_mutex_init(&mutex_, 0);
00037 }
00038 
00039 CommandQueue::~CommandQueue()
00040 {
00041 }
00042 
00043 void
00044 CommandQueue::runCommands()
00045 {
00046     pthread_mutex_lock(&mutex_);
00047     try
00048     {
00049         for(CommandContainer::iterator itr = queue_.begin();
00050             itr != queue_.end();
00051             ++itr)
00052         {
00053             (**itr).state_ = Command::Running;
00054             (**itr)();
00055             (**itr).state_ = Command::Finished;
00056         }
00057         queue_.clear();
00058     }
00059     catch(...)
00060     {
00061         pthread_mutex_unlock(&mutex_);
00062         throw;
00063     }
00064     pthread_mutex_unlock(&mutex_);
00065 }
00066 
00067 void
00068 CommandQueue::reset()
00069 {
00070     // clear Command queue
00071     pthread_mutex_lock(&mutex_);
00072     queue_.clear();
00073     pthread_mutex_unlock(&mutex_);
00074 }
00075 
00076 wns::events::scheduler::ICommandPtr
00077 CommandQueue::queueCommand(const Callable& callee)
00078 {
00079     pthread_mutex_lock(&mutex_);
00080     CommandPtr command(new Command(callee));
00081     try
00082     {
00083         queue_.push_back(command);
00084         command->state_ = Command::Queued;
00085     }
00086     catch(...)
00087     {
00088         pthread_mutex_unlock(&mutex_);
00089         throw;
00090     }
00091     pthread_mutex_unlock(&mutex_);
00092     return command;
00093 }
00094 
00095 void
00096 CommandQueue::dequeueCommand(const ICommandPtr& command)
00097 {
00098     pthread_mutex_lock(&mutex_);
00099     CommandPtr commandPtr = dynamicCast<Command>(command);
00100     if (command->isRunning())
00101     {
00102         throw ICommand::CancelException("Command is currently being executed");
00103     }
00104     else if (command->isCanceled())
00105     {
00106         throw ICommand::CancelException("Command is already canceled");
00107     }
00108     else if (command->isFinished())
00109     {
00110         throw ICommand::CancelException("Command has already been called");
00111     }
00112     else if (command->isNotSubmitted())
00113     {
00114         throw ICommand::CancelException("Should never happen");
00115     }
00116     try
00117     {
00118         queue_.remove(commandPtr);
00119         commandPtr->state_ = Command::Canceled;
00120     }
00121     catch(...)
00122     {
00123         pthread_mutex_unlock(&mutex_);
00124         throw;
00125     }
00126     pthread_mutex_unlock(&mutex_);
00127 }

Generated on Tue May 22 03:31:35 2012 for openWNS by  doxygen 1.5.5