User Manual, Developers Guide and API Documentation

Wire.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-2009
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 COPPER_WIRE_HPP
00029 #define COPPER_WIRE_HPP
00030 
00031 #include <COPPER/Transmission.hpp>
00032 #include <COPPER/ReceiverInterface.hpp>
00033 
00034 #include <WNS/events/CanTimeout.hpp>
00035 #include <WNS/pyconfig/View.hpp>
00036 #include <WNS/Broker.hpp>
00037 #include <WNS/Singleton.hpp>
00038 #include <WNS/RoundRobin.hpp>
00039 #include <WNS/logger/Logger.hpp>
00040 #include <WNS/osi/PDU.hpp>
00041 #include <WNS/service/dll/Address.hpp>
00042 #include <WNS/events/scheduler/Interface.hpp>
00043 #include <WNS/osi/PDU.hpp>
00044 
00045 #include <list>
00046 #include <map>
00047 
00048 namespace copper
00049 {
00059     class WireInterface
00060     {
00061     public:
00065         virtual
00066         ~WireInterface()
00067         {}
00068 
00075         virtual void
00076         sendData(const UnicastTransmissionPtr& ut, simTimeType duration) = 0;
00077 
00084         virtual void
00085         sendData(const BroadcastTransmissionPtr& bt, simTimeType duration) = 0;
00086 
00095         virtual void
00096         stopTransmission(const wns::osi::PDUPtr& pdu) = 0;
00097 
00113         virtual simTimeType
00114         blockedSince() const = 0;
00115 
00119         virtual void
00120         addReceiver(
00121             ReceiverInterface* r,
00122             const wns::service::dll::UnicastAddress& macAddress) = 0;
00123     };
00124 
00128     class Wire :
00129         public virtual WireInterface
00130     {
00131     public:
00135         explicit
00136         Wire(const wns::pyconfig::View& config);
00137 
00142         void
00143         sendData(const UnicastTransmissionPtr& ut, simTimeType duration);
00144 
00145         void
00146         sendData(const BroadcastTransmissionPtr& bt, simTimeType duration);
00147 
00151         void
00152         stopTransmission(const wns::osi::PDUPtr& pdu);
00153 
00154         simTimeType
00155         blockedSince() const;
00156 
00157         void
00158         addReceiver(
00159             ReceiverInterface* r,
00160             const wns::service::dll::UnicastAddress& macAddress);
00162 
00163     private:
00168         class TransmissionEndEventBase
00169         {
00170         public:
00174             virtual
00175             ~TransmissionEndEventBase()
00176             {}
00177 
00182             virtual TransmissionPtr
00183             getTransmission() const = 0;
00184         };
00185 
00190         template<typename TRANSMISSIONTYPE>
00191         class TransmissionEndEvent :
00192             public TransmissionEndEventBase
00193         {
00194         public:
00200             TransmissionEndEvent(Wire* w, const TRANSMISSIONTYPE& t) :
00201                 wire(w),
00202                 transmission(t)
00203             {
00204                 assure(this->wire, "must be non-NULL");
00205                 assure(this->transmission, "must be non-NULL");
00206             }
00207 
00212             void
00213             operator()()
00214             {
00215                 this->wire->stopTransmission(transmission);
00216             }
00217 
00221             virtual TransmissionPtr
00222             getTransmission() const
00223             {
00224                 return this->transmission;
00225             }
00226 
00227         private:
00231             Wire* wire;
00232 
00245             TRANSMISSIONTYPE transmission;
00246         };
00247 
00248         typedef std::map< wns::osi::PDUPtr, wns::events::scheduler::IEventPtr >
00249         TransmissionEndEventContainer;
00250 
00251         typedef std::map< wns::osi::PDUPtr, TransmissionPtr >
00252         Transmissions;
00253 
00254         typedef std::map<wns::service::dll::UnicastAddress, ReceiverInterface*>
00255         Address2ReceiverContainer;
00256 
00262         void
00263         stopTransmission(const UnicastTransmissionPtr& ut);
00264 
00270         void
00271         stopTransmission(const BroadcastTransmissionPtr& bt);
00272 
00276         bool
00277         isFree() const;
00278 
00285         void
00286         checkForCollision(const TransmissionPtr& t);
00287 
00296         void
00297         signalCopperFreeAgainToReceivers();
00298 
00303         template <typename TRANSMISSIONTYPE>
00304         void
00305         addTransmissionEndEvent(const TRANSMISSIONTYPE& t, simTimeType arrivalTime)
00306         {
00307             assure(t, "must be non-NULL");
00308             assure(
00309                 this->transmissions.find(t->pdu) == this->transmissions.end(),
00310                 "already got this event");
00311 
00312             TransmissionEndEvent<TRANSMISSIONTYPE> te (this, t);
00313 
00314             
00315             this->transmissions[t->pdu] = te.getTransmission();
00316             this->transmissionEndEvents[t->pdu] =
00317                 wns::simulator::getEventScheduler()->schedule(te, arrivalTime);
00318             
00319         }
00320 
00321         template <typename TRANSMISSIONTYPE>
00322         wns::simulator::Time
00323         sendDataGeneric(const TRANSMISSIONTYPE& transmission, simTimeType duration)
00324         {
00325             assure(transmission, "must be non-NULL");
00326             // if wire is not already blocked set time to now and
00327             // tell every Receiver the wire is busy
00328             if (this->isFree())
00329             {
00330                 this->timeWireBlocked = wns::simulator::getEventScheduler()->getTime();
00331                 std::for_each(
00332                     receivers.begin(),
00333                     receivers.end(),
00334                     std::mem_fun(&ReceiverInterface::onCopperBusy));
00335             }
00336 
00337             wns::simulator::Time arrivalTime =
00338                 wns::simulator::getEventScheduler()->getTime() + duration;
00339             this->checkForCollision(transmission);
00340 
00341             this->addTransmissionEndEvent(transmission, arrivalTime);
00342             return arrivalTime;
00343         }
00344 
00348         void
00349         removeTransmissionEndEvent(const TransmissionPtr& transmission);
00350 
00354         std::string name;
00355 
00360         wns::RoundRobin<ReceiverInterface*> roundRobin;
00361 
00366         std::list<ReceiverInterface*> receivers;
00367 
00372         TransmissionEndEventContainer transmissionEndEvents;
00373 
00377         Transmissions transmissions;
00378 
00383         Address2ReceiverContainer addressMapping;
00384 
00388         wns::logger::Logger logger;
00389 
00393         simTimeType timeWireBlocked;
00394     };
00395 
00400     typedef wns::Broker<Wire> WireBroker;
00401 }
00402 
00403 #endif // NOT defined COPPER_WIRE_HPP
00404 
00405 

Generated on Sat May 26 03:32:16 2012 for openWNS by  doxygen 1.5.5