00001 /*************************************************************************** 00002 tag: Peter Soetens Wed Jan 18 14:11:40 CET 2006 CommandProcessor.cxx 00003 00004 CommandProcessor.cxx - description 00005 ------------------- 00006 begin : Wed January 18 2006 00007 copyright : (C) 2006 Peter Soetens 00008 email : peter.soetens@mech.kuleuven.be 00009 00010 *************************************************************************** 00011 * This library is free software; you can redistribute it and/or * 00012 * modify it under the terms of the GNU General Public * 00013 * License as published by the Free Software Foundation; * 00014 * version 2 of the License. * 00015 * * 00016 * As a special exception, you may use this file as part of a free * 00017 * software library without restriction. Specifically, if other files * 00018 * instantiate templates or use macros or inline functions from this * 00019 * file, or you compile this file and link it with other files to * 00020 * produce an executable, this file does not by itself cause the * 00021 * resulting executable to be covered by the GNU General Public * 00022 * License. This exception does not however invalidate any other * 00023 * reasons why the executable file might be covered by the GNU General * 00024 * Public License. * 00025 * * 00026 * This library is distributed in the hope that it will be useful, * 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00029 * Lesser General Public License for more details. * 00030 * * 00031 * You should have received a copy of the GNU General Public * 00032 * License along with this library; if not, write to the Free Software * 00033 * Foundation, Inc., 59 Temple Place, * 00034 * Suite 330, Boston, MA 02111-1307 USA * 00035 * * 00036 ***************************************************************************/ 00037 00038 00039 #include "CommandProcessor.hpp" 00040 #include <CommandInterface.hpp> 00041 #include <AtomicQueue.hpp> 00042 #include <Logger.hpp> 00043 00044 namespace RTT 00045 { 00046 00047 using namespace std; 00048 00049 using namespace OS; 00050 00051 CommandProcessor::CommandProcessor(int queue_size) 00052 :a_queue( new AtomicQueue<CommandInterface*>(queue_size) ), 00053 coms_processed(0), 00054 accept(false) 00055 { 00056 } 00057 00058 CommandProcessor::~CommandProcessor() 00059 { 00060 delete a_queue; 00061 } 00062 00063 bool CommandProcessor::initialize() 00064 { 00065 a_queue->clear(); 00066 accept = true; 00067 return true; 00068 } 00069 00070 void CommandProcessor::finalize() 00071 { 00072 accept = false; 00073 } 00074 00075 void CommandProcessor::step() 00076 { 00077 // execute one command from the AtomicQueue. 00078 CommandInterface* com(0); 00079 int res = a_queue->dequeueCounted( com ); 00080 if ( com ) { 00081 // note : the command's result is discarded. 00082 // Wrap your command (ie CommandDispatch) to keep track 00083 // of the result of enqueued commands. 00084 com->execute(); 00085 // let the user know this command was processed. 00086 coms_processed = res; 00087 } 00088 } 00089 00090 int CommandProcessor::process( CommandInterface* c ) 00091 { 00092 if (accept && c) { 00093 int result = a_queue->enqueueCounted( c ); 00094 if ( this->getActivity() ) 00095 this->getActivity()->trigger(); 00096 return result; 00097 } 00098 return 0; 00099 } 00100 00101 bool CommandProcessor::isProcessed( int nr ) const 00102 { 00103 return ( nr <= coms_processed ); 00104 } 00105 } 00106
1.5.6