User Manual, Developers Guide and API Documentation

FlowGate.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. 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 #include <WNS/ldk/FlowGate.hpp>
00029 
00030 using namespace wns::ldk;
00031 
00032 STATIC_FACTORY_REGISTER_WITH_CREATOR(
00033     FlowGate,
00034     FunctionalUnit,
00035     "wns.FlowGate",
00036     FUNConfigCreator
00037     );
00038 
00039 FlowGate::FlowGate(fun::FUN* fun, const wns::pyconfig::View& config) :
00040     CommandTypeSpecifier<>(fun),
00041     HasReceptor<>(),
00042     HasConnector<>(),
00043     HasDeliverer<>(),
00044     Cloneable<FlowGate>(),
00045 
00046     flowStatus(),
00047     keyBuilder(NULL),
00048     logger(config.get("logger"))
00049 {
00050     pyconfig::View keyBuilderConfig(config, "keyBuilder");
00051     std::string keyBuilderName = keyBuilderConfig.get<std::string>("__plugin__");
00052     keyBuilder = KeyBuilderFactory::creator(keyBuilderName)->create(fun, keyBuilderConfig);
00053 }
00054 
00055 FlowGate::FlowGate(fun::FUN* fun, const wns::pyconfig::View& config, KeyBuilder* builder) :
00056     CommandTypeSpecifier<>(fun),
00057     HasReceptor<>(),
00058     HasConnector<>(),
00059     HasDeliverer<>(),
00060     Cloneable<FlowGate>(),
00061 
00062     flowStatus(),
00063     keyBuilder(builder),
00064     logger(config.get("logger"))
00065 {}
00066 
00067 FlowGate::~FlowGate()
00068 {
00069     delete keyBuilder;
00070     keyBuilder = NULL;
00071 
00072     while(flowStatus.empty() == false)
00073     {
00074         flowStatus.erase(flowStatus.begin());
00075     }
00076 }
00077 
00078 void
00079 FlowGate::onFUNCreated()
00080 {
00081     keyBuilder->onFUNCreated();
00082 }
00083 
00084 void
00085 FlowGate::doSendData(const CompoundPtr& compound)
00086 {
00087     assure(this->isAccepting(compound), "send data called even though not accepting");
00088 
00089     // Forward call to next FU
00090     return getConnector()->getAcceptor(compound)->sendData(compound);
00091 }
00092 
00093 void
00094 FlowGate::doOnData(const CompoundPtr& compound)
00095 {
00096     ConstKeyPtr key = (*keyBuilder)(compound, Direction::INCOMING());
00097 
00098     if (flowIsKnown(key))
00099     {
00100         if ( flowStatus.find(key)->second == true )
00101         {
00102             // Forward call to next FU
00103             getDeliverer()->getAcceptor(compound)->onData(compound);
00104         }
00105 
00106         else
00107         {
00108             // Gate is closed for this flow, drop the compound
00109             MESSAGE_SINGLE(NORMAL, logger, "Dropped incoming compound for closed flow ("<< key->str()<<")!");
00110         }
00111     }
00112     else
00113     {
00114         // Gate is unknown for this flow, drop the compound
00115         MESSAGE_SINGLE(NORMAL, logger, "Dropped incoming compound for unknown flow ("<< key->str()<<")!");
00116     }
00117 }
00118 
00119 bool
00120 FlowGate::doIsAccepting(const CompoundPtr& compound) const
00121 {
00122     ConstKeyPtr key = (*keyBuilder)(compound, Direction::OUTGOING());
00123 
00124     if (flowIsKnown(key))
00125     {
00126         if ( flowStatus.find(key)->second == true )
00127         {
00128             // Forward call to next FU
00129             return getConnector()->hasAcceptor(compound);
00130         }
00131         else
00132         {
00133             // Gate is closed for this flow
00134             return false;
00135         }
00136     }
00137     else
00138     {
00139         std::stringstream ss;
00140         ss << "Compound for unknown flow " << key->str() << " reached FlowGate!\n";
00141         throw wns::Exception(ss.str());
00142     }
00143 }
00144 
00145 void
00146 FlowGate::doWakeup()
00147 {
00148     // pass this call to the next FU
00149     getReceptor()->wakeup();
00150 }
00151 
00152 void
00153 FlowGate::createFlow(const ConstKeyPtr& key)
00154 {
00155     assure(!flowIsKnown(key), "You cannot create a flow twice!");
00156     flowStatus[key] = true;
00157     MESSAGE_SINGLE(NORMAL, logger, "Flow Created for key:" << key->str());
00158 }
00159 
00160 void
00161 FlowGate::destroyFlow(const ConstKeyPtr& key)
00162 {
00163     MESSAGE_SINGLE(NORMAL, logger, "Destroying flow for key:" << key->str());
00164 
00165     FlowStatus::iterator remove = flowStatus.find(key);
00166     assure(remove != flowStatus.end(), "Flow does not exist!");
00167     flowStatus.erase(remove);
00168 }
00169 
00170 void
00171 FlowGate::openFlow(const ConstKeyPtr& key)
00172 {
00173     MESSAGE_SINGLE(NORMAL, logger, "Opening flow for key:" << key->str());
00174     assure(flowIsKnown(key), "Flow does not exist!");
00175     flowStatus[key] = true;
00176 }
00177 
00178 void
00179 FlowGate::closeFlow(const ConstKeyPtr& key)
00180 {
00181     MESSAGE_SINGLE(NORMAL, logger, "Closing flow for key:" << key->str());
00182     assure(flowIsKnown(key), "Flow does not exist!");
00183     flowStatus[key] = false;
00184 }
00185 
00186 bool
00187 FlowGate::flowIsKnown(const ConstKeyPtr& key) const
00188 {
00189     return flowStatus.find(key) != flowStatus.end();
00190 }
00191 
00192 

Generated on Thu May 24 03:31:39 2012 for openWNS by  doxygen 1.5.5