User Manual, Developers Guide and API Documentation

FunctionalUnit.hpp

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. 5, 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 #ifndef WNS_LDK_FSM_FUNCTIONALUNIT_HPP
00029 #define WNS_LDK_FSM_FUNCTIONALUNIT_HPP
00030 
00031 #include <WNS/ldk/fsm/CompoundHandlerSignalInterface.hpp>
00032 #include <WNS/ldk/FunctionalUnit.hpp>
00033 
00034 #include <WNS/fsm/FSM.hpp>
00035 
00036 #include <WNS/Exception.hpp>
00037 
00038 namespace wns { namespace ldk { namespace fsm {
00039 
00088     template <typename VARIABLES>
00089     class FunctionalUnit :
00090         virtual public ldk::FunctionalUnit,
00091         public wns::fsm::FSM<wns::ldk::fsm::CompoundHandlerSignalInterface, VARIABLES>
00092     {
00093     public:
00094         typedef wns::fsm::FSM<wns::ldk::fsm::CompoundHandlerSignalInterface, VARIABLES> BaseFSM;
00095         class StateInterface :
00096             public BaseFSM::StateInterface
00097         {
00098         public:
00102             explicit
00103             StateInterface(BaseFSM* _fu, const std::string& stateName) :
00104                 BaseFSM::StateInterface(_fu, stateName),
00105                 fu(dynamic_cast<FunctionalUnit*>(_fu))
00106             {
00107                 assureType(_fu, FunctionalUnit*);
00108             } // StateInterface
00109 
00113             virtual FunctionalUnit*
00114             getFU() const
00115             {
00116                 return fu;
00117             } // getFU
00118 
00122             virtual void
00123             initState()
00124             {} // initState
00125 
00129             virtual void
00130             exitState()
00131             {} // exitState
00132 
00133         private:
00137             FunctionalUnit* fu;
00138         };
00139 
00146         class UnhandledSignals :
00147             public StateInterface
00148         {
00149         protected:
00150             UnhandledSignals(BaseFSM* t, const std::string& stateName) :
00151                 StateInterface(t, stateName)
00152             {} // UnhandledSignals
00153 
00154         private:
00155             virtual void
00156             initState()
00157             {
00158                 doInitState();
00159             } // initState
00160 
00161             virtual void
00162             doInitState()
00163             {} // doInitState
00164 
00165             virtual void
00166             exitState()
00167             {
00168                 assure(!this->getFU()->inAction, "Trying to change state although current state is still in use.");
00169 
00170                 doExitState();
00171             } // exitState
00172 
00173             virtual void
00174             doExitState()
00175             {} // doExitState
00176 
00177             virtual typename BaseFSM::StateInterface*
00178             doSendData(const CompoundPtr&)
00179             {
00180                 Exception e;
00181                 e << "Can't handle signal (DATAReq) in state: "
00182                   << wns::TypeInfo::create(*this);
00183                 throw e;
00184 
00185                 return this;
00186             } // doSendData
00187 
00188             virtual typename BaseFSM::StateInterface*
00189             doOnData(const CompoundPtr&)
00190             {
00191                 Exception e;
00192                 e << "Can't handle invalid signal (DATAInd) in state: "
00193                   << wns::TypeInfo::create(*this);
00194                 throw e;
00195 
00196                 return this;
00197             } // doOnData
00198 
00199 
00200             virtual typename BaseFSM::StateInterface*
00201             doWakeup()
00202             {
00203                 Exception e;
00204                 e << "Can't handle signal (wakeup) in state: "
00205                   << wns::TypeInfo::create(*this);
00206                 throw e;
00207 
00208                 return this;
00209             } // doWakeup
00210 
00211 
00212             virtual void
00213             doIsAccepting(const CompoundPtr&, bool&) const
00214             {
00215                 Exception e;
00216                 e << "Can't handle signal (isAccepting) in state: "
00217                   << wns::TypeInfo::create(*this);
00218                 throw e;
00219             } // doIsAccepting
00220         };
00221 
00222         explicit
00223         FunctionalUnit(const VARIABLES& v) :
00224             BaseFSM(v),
00225             inAction(0),
00226             sendDataCompound(),
00227             furtherSendDataCompounds(0),
00228             wakeupFU(0),
00229             inWakeup(false),
00230             onDataCompounds()
00231         {} // FunctionalUnit
00232 
00233         void
00234         queueSendData(const CompoundPtr& compound)
00235         {
00236             sendDataCompound = compound;
00237         } // queueSendData
00238 
00239         void
00240         queueWakeup()
00241         {
00242             ++wakeupFU;
00243         } // queueWakeup
00244 
00245         void
00246         queueOnData(const CompoundPtr& compound)
00247         {
00248             onDataCompounds.push_back(compound);
00249         } // queueOnData
00250 
00251         bool
00252         isAccepting(const CompoundPtr& compound)
00253         {
00254             if (sendDataCompound)
00255             {
00256                 ++furtherSendDataCompounds;
00257                 return false;
00258             }
00259 
00260             return getConnector()->hasAcceptor(compound);
00261         } // isAccepting
00262 
00263         virtual void
00264         doSendData(const CompoundPtr& compound)
00265         {
00266             ++inAction;
00267             CompoundHandlerSignalInterface* stateInterface = this->getState()->doSendData(compound);
00268             --inAction;
00269             this->changeState(stateInterface);
00270 
00271             if (linkHandler())
00272                 doWakeup();
00273 
00274             return;
00275         } // doSendData
00276 
00277         virtual void
00278         doOnData(const CompoundPtr& compound)
00279         {
00280             ++inAction;
00281             CompoundHandlerSignalInterface* stateInterface = this->getState()->doOnData(compound);
00282             --inAction;
00283             this->changeState(stateInterface);
00284 
00285             if (linkHandler())
00286                 doWakeup();
00287 
00288             return;
00289         } // doOnData
00290 
00291     private:
00292         virtual void
00293         doWakeup()
00294         {
00295             do
00296             {
00297                 ++inAction;
00298                 CompoundHandlerSignalInterface* stateInterface = this->getState()->doWakeup();
00299                 --inAction;
00300                 this->changeState(stateInterface);
00301             }
00302             while (linkHandler());
00303 
00304             return;
00305         } // doWakeup
00306 
00307         virtual bool
00308         doIsAccepting(const CompoundPtr& compound) const
00309         {
00310             bool accepting = false;
00311             this->getState()->doIsAccepting(compound, accepting);
00312             return accepting;
00313         } // doIsAccepting
00314 
00315         typedef std::list<CompoundPtr> CompoundContainer;
00316 
00317     protected:
00318         bool
00319         linkHandler()
00320         {
00321             if (sendDataCompound)
00322             {
00323                 CompoundPtr sendDataCompoundHelp = sendDataCompound;
00324                 sendDataCompound = CompoundPtr();
00325 
00326                 getConnector()->getAcceptor(sendDataCompoundHelp)->sendData(sendDataCompoundHelp);
00327             }
00328 
00329             if (furtherSendDataCompounds)
00330             {
00331                 furtherSendDataCompounds = 0;
00332                 return true;
00333             }
00334 
00335             while (!onDataCompounds.empty())
00336             {
00337                 CompoundPtr onDataCompoundHelp = onDataCompounds.front();
00338                 onDataCompounds.pop_front();
00339 
00340                 getDeliverer()->getAcceptor(onDataCompoundHelp)->onData(onDataCompoundHelp);
00341             }
00342 
00343             if (wakeupFU && !inWakeup)
00344             {
00345                 inWakeup = true;
00346                 getReceptor()->wakeup();
00347                 inWakeup = false;
00348                 wakeupFU = 0;
00349             }
00350 
00351             return false;
00352         } // linkHandler
00353 
00354     protected:
00355         int inAction;
00356 
00357     private:
00358         CompoundPtr sendDataCompound;
00359         int furtherSendDataCompounds;
00360         int wakeupFU;
00361         bool inWakeup;
00362         CompoundContainer onDataCompounds;
00363 
00364     }; // FunctionalUnit
00365 
00366 } // fsm
00367 } // ldk
00368 } // wns
00369 
00370 #endif // NOT defined WNS_LDK_FSM_FUNCTIONALUNIT_HPP
00371 
00372 

Generated on Sun May 27 03:31:43 2012 for openWNS by  doxygen 1.5.5