communication_layer.hpp

00001 
00002 #ifndef COMMUNICATION_LAYER_H
00003 #define COMMUNICATION_LAYER_H
00004 
00005 #include <vector>
00006 #include <list>
00007 #include <iostream>
00008 using namespace std;
00009 #include <boost/shared_ptr.hpp>
00010 #include <boost/enable_shared_from_this.hpp>
00011 #include <boost/utility.hpp>
00012 
00013 #include "event.hpp"
00014 #include "utility.hpp"
00015 #include "sim_time.hpp"
00016 #include "simulation_end_listener.hpp"
00017 
00018 class Node;
00019 typedef boost::shared_ptr<Node> NodePtr;
00020 typedef boost::shared_ptr<Node const> ConstNodePtr;
00021 class NodeId;
00022 class Packet;
00023 typedef boost::shared_ptr<Packet> PacketPtr;
00024 
00035 class CommunicationLayer : 
00036    public SimulationEndListener,
00037    boost::noncopyable, 
00038    public boost::enable_shared_from_this<CommunicationLayer> {
00039    // Only node can call setNode()
00040    friend class Node;
00041    friend class LayerRecvEvent;
00042 public:
00044    typedef boost::shared_ptr<CommunicationLayer> CommunicationLayerPtr;
00046    typedef boost::shared_ptr<SimulationEndListener> 
00047       SimulationEndListenerPtr;
00048 
00054    enum Directions { 
00055       Directions_Lower, 
00056       Directions_Upper  
00057    };
00058 
00063    enum Types {
00064       Types_Physical, 
00065       Types_Link, 
00066       Types_Network, 
00067       Types_Transport, 
00068       Types_Application 
00069    };
00070 
00074    virtual ~CommunicationLayer();
00075 
00082    virtual SimulationEndListenerPtr thisSimulationEndListener() = 0;
00083 
00092    inline void insertLowerLayer(CommunicationLayerPtr layerToInsert);
00093 
00099    inline t_uint numberOfLayers(Directions direction) const;
00100 
00108    inline bool setDefaultLayer(Directions direction, t_uint newDefaultIdx);
00109 
00115    inline t_uint getDefaultLayer(Directions direction) const;
00116 
00123    inline void setLayerDelay(Directions direction, const SimTime& delay);
00124 
00131    inline SimTime getLayerDelay(Directions direction) const;
00132 
00139    bool sendToQueue(PacketPtr packet);
00140 
00148    bool sendToQueue(PacketPtr packet, t_uint lowerLayerIdx);
00149 
00154    inline void unblockQueue();
00155 
00160    inline void blockQueue();
00161 
00166    inline bool getQueueIsBlocked() const;
00167 
00173    inline void setMaxQueueLength(t_uint maxQueueLength);
00174 
00180    inline t_uint getMaxQueueLength() const;
00181 
00191    bool sendToLayer(Directions direction, PacketPtr packet);
00192 
00204    bool sendToLayer(Directions direction, PacketPtr packet, 
00205       t_uint recvingLayerIdx);
00206 
00214    bool sendToAllLayers(Directions direction, PacketPtr packet);
00215 
00225    bool recvFromLayer(Directions direction, PacketPtr packet, 
00226       CommunicationLayerPtr sendingLayer);
00227 
00240    virtual bool recvFromLayer(Directions direction, PacketPtr packet, 
00241       t_uint sendingLayerIdx);
00242 
00247    virtual Types getLayerType() const = 0;
00248 
00253    NodeId getNodeId() const;
00254 
00255 protected:
00256 
00262    CommunicationLayer(NodePtr node);
00263 
00266    bool m_lowerLayerRecvEventPending;
00267 
00271    inline NodePtr getNode() const;
00272 
00277    inline bool queueIsFull() const;
00278 
00282    void blockUpperQueues();
00283 
00288    void unblockUpperQueues();
00289 
00294    void sendFromQueue();
00295 
00304    inline void setLowerLayerRecvEventPending(bool isPending);
00305 
00311    void removeLayerData(PacketPtr packet) const;
00312 
00320    inline CommunicationLayerPtr getLayer(Directions direction, 
00321       t_uint sendingLayerIdx);
00322 
00330    inline void insertLayer(Directions direction, 
00331       CommunicationLayerPtr layerToInsert);
00332 
00333 private: 
00334 
00336    static const t_uint m_DEFAULT_MAX_QUEUE_LENGTH;
00337 
00341    NodePtr m_node;
00342 
00346    SimTime m_lowerLayerDelay;
00347 
00351    SimTime m_upperLayerDelay;
00352 
00356    vector<CommunicationLayerPtr> m_lowerLayers;
00357 
00361    vector<CommunicationLayerPtr> m_upperLayers;
00362 
00368    t_uint m_defaultLowerLayerIdx;
00369 
00375    t_uint m_defaultUpperLayerIdx;
00376 
00378    t_uint m_maxQueueLength;
00379 
00383    list<pair<PacketPtr,t_uint> > m_packetQueue;
00384 
00389    bool m_queueIsBlocked;
00390 
00391 };
00392 typedef boost::shared_ptr<CommunicationLayer> CommunicationLayerPtr;
00393 
00395 // Inline Functions
00397 
00398 inline NodePtr CommunicationLayer::getNode() const
00399 {
00400    return m_node;
00401 }
00402 
00403 inline void CommunicationLayer::insertLowerLayer(
00404    CommunicationLayerPtr layerToInsert)
00405 {
00406    assert(layerToInsert.get() != 0);
00407    assert(getNode() == layerToInsert->getNode());
00408    insertLayer(CommunicationLayer::Directions_Lower, layerToInsert);
00409    layerToInsert->insertLayer(CommunicationLayer::Directions_Upper, 
00410       shared_from_this());
00411 }
00412 
00413 inline void CommunicationLayer::insertLayer(
00414    CommunicationLayer::Directions direction,
00415    CommunicationLayerPtr layerToInsert)
00416 {
00417    assert(layerToInsert.get() != 0);
00418    assert(getNode() == layerToInsert->getNode());
00419    switch(direction) {
00420    case CommunicationLayer::Directions_Lower:
00421       m_lowerLayers.push_back(layerToInsert);
00422       break;
00423    case CommunicationLayer::Directions_Upper:
00424       m_upperLayers.push_back(layerToInsert);
00425       break;
00426    default:
00427       assert(false);
00428    }
00429    // If this is the first layer inserted, set it as the
00430    // default layer
00431    if(numberOfLayers(direction) == 1)
00432       setDefaultLayer(direction, 0);
00433 }
00434 
00435 inline void CommunicationLayer::blockQueue()
00436 {
00437    m_queueIsBlocked = true;
00438 }
00439 
00440 inline void CommunicationLayer::unblockQueue()
00441 {
00442    m_queueIsBlocked = false;
00443    sendFromQueue();
00444 }
00445 
00446 inline void CommunicationLayer::setLowerLayerRecvEventPending(
00447    bool isPending)
00448 {
00449    bool previousValue = m_lowerLayerRecvEventPending;
00450    m_lowerLayerRecvEventPending = isPending;
00451    // If there was an event pending, but there no longer is,
00452    // then we need to try sending from the queue again.
00453    if(previousValue && !m_lowerLayerRecvEventPending)
00454       sendFromQueue();
00455 }
00456 
00457 inline bool CommunicationLayer::getQueueIsBlocked() const
00458 {
00459    return m_queueIsBlocked;
00460 }
00461 
00462 inline bool CommunicationLayer::queueIsFull() const
00463 {
00464    assert(m_packetQueue.size() <= m_maxQueueLength);
00465    return (m_packetQueue.size() == m_maxQueueLength);
00466 }
00467 
00468 inline void CommunicationLayer::setMaxQueueLength(t_uint maxQueueLength)
00469 {
00470    assert(maxQueueLength > 0);
00471    m_maxQueueLength = maxQueueLength;
00472 }
00473 
00474 inline t_uint CommunicationLayer::getMaxQueueLength() const
00475 {
00476    return m_maxQueueLength;
00477 }
00478 
00479 inline CommunicationLayerPtr CommunicationLayer::getLayer(
00480    CommunicationLayer::Directions direction, t_uint sendingLayerIdx)
00481 {
00482    assert(sendingLayerIdx < numberOfLayers(direction));
00483 
00484    vector<CommunicationLayerPtr>* layerVector = 0;
00485    switch(direction) {
00486    case Directions_Lower:
00487       layerVector = &m_lowerLayers;
00488       break;
00489    case Directions_Upper:
00490       layerVector = &m_upperLayers;
00491       break;
00492    default:
00493       assert(false);
00494    }
00495    assert(layerVector != 0);
00496 
00497    return (*layerVector)[sendingLayerIdx];
00498 
00499 }
00500 
00501 inline t_uint CommunicationLayer::numberOfLayers(
00502    CommunicationLayer::Directions direction) const
00503 {
00504    switch(direction) {
00505    case CommunicationLayer::Directions_Lower:
00506       return m_lowerLayers.size();
00507       break;
00508    case CommunicationLayer::Directions_Upper:
00509       return m_upperLayers.size();
00510       break;
00511    default:
00512       assert(false);
00513       return 0;
00514    }
00515 }
00516 
00517 inline bool CommunicationLayer::setDefaultLayer(
00518    CommunicationLayer::Directions direction, t_uint newDefaultIdx)
00519 {
00520    bool setDefaultLayer = false;
00521    if(newDefaultIdx < numberOfLayers(direction)) {
00522       switch(direction) {
00523       case CommunicationLayer::Directions_Lower:
00524          m_defaultLowerLayerIdx = newDefaultIdx;
00525          break;
00526       case CommunicationLayer::Directions_Upper:
00527          m_defaultUpperLayerIdx = newDefaultIdx;
00528          break;
00529       default:
00530          assert(false);
00531       }
00532       setDefaultLayer = true;
00533    }
00534 
00535    return setDefaultLayer;
00536 }
00537 
00538 inline t_uint CommunicationLayer::getDefaultLayer(
00539    CommunicationLayer::Directions direction) const
00540 {
00541    switch(direction) {
00542    case CommunicationLayer::Directions_Lower:
00543       return m_defaultLowerLayerIdx;
00544       break;
00545    case CommunicationLayer::Directions_Upper:
00546       return m_defaultUpperLayerIdx;
00547       break;
00548    default:
00549       assert(false);
00550       return 0;
00551    }
00552 }
00553 
00554 inline void CommunicationLayer::setLayerDelay(
00555    CommunicationLayer::Directions direction, const SimTime& delay)
00556 {
00557    switch(direction) {
00558    case CommunicationLayer::Directions_Lower:
00559       m_lowerLayerDelay = delay;
00560       break;
00561    case CommunicationLayer::Directions_Upper:
00562       m_upperLayerDelay = delay;
00563       break;
00564    default:
00565       assert(false);
00566    }
00567 }
00568 
00569 inline SimTime CommunicationLayer::getLayerDelay(
00570    CommunicationLayer::Directions direction) const
00571 {
00572    switch(direction) {
00573    case CommunicationLayer::Directions_Lower:
00574       return m_lowerLayerDelay;
00575       break;
00576    case CommunicationLayer::Directions_Upper:
00577       return m_upperLayerDelay;
00578       break;
00579    default:
00580       assert(false);
00581       SimTime neverReturned;
00582       return neverReturned;
00583    }
00584 }
00585 
00587 // Overloaded Operators
00589 
00590 inline ostream& operator<< (ostream& s, 
00591       const CommunicationLayer& communicationLayer)
00592 {
00593    return s<< "CommunicationLayer state (pointer=" << 
00594          &communicationLayer << ", number of upper layers= " << 
00595          communicationLayer.numberOfLayers(
00596          CommunicationLayer::Directions_Upper) << 
00597          ", number of lower layers= " <<
00598          communicationLayer.numberOfLayers(
00599          CommunicationLayer::Directions_Lower) << 
00600          ", default upper layer= " <<
00601          communicationLayer.getDefaultLayer(
00602          CommunicationLayer::Directions_Upper) << 
00603          ", default lower layer= " <<
00604          communicationLayer.getDefaultLayer(
00605          CommunicationLayer::Directions_Lower) << 
00606          ", upper layer delay= " <<
00607          communicationLayer.getLayerDelay(
00608          CommunicationLayer::Directions_Upper) << 
00609          ", lower layer delay= " <<
00610          communicationLayer.getLayerDelay(
00611          CommunicationLayer::Directions_Lower) << 
00612          ")";
00613 }
00614 
00615 inline ostream& operator<< (ostream& s, 
00616       const CommunicationLayer::Types& communicationLayerType)
00617 {
00618 
00619    switch (communicationLayerType) {
00620       case CommunicationLayer::Types_Physical:
00621          s << "PHY";
00622          break;
00623       case CommunicationLayer::Types_Link:
00624          s << "LINK";
00625          break;
00626       case CommunicationLayer::Types_Network:
00627          s << "NET";
00628          break;
00629       case CommunicationLayer::Types_Transport:
00630          s << "TRAN";
00631          break;
00632       case CommunicationLayer::Types_Application:
00633          s << "APP";
00634          break;
00635    }
00636 
00637    return s;
00638 
00639 }
00640 
00642 // Event Subclasses
00644 
00649 class LayerRecvEvent : public Event {
00650 public:
00652    typedef boost::shared_ptr<LayerRecvEvent> LayerRecvEventPtr;
00653 
00658    static inline LayerRecvEventPtr create(
00659       CommunicationLayer::Directions sendDirection,
00660       PacketPtr packet, 
00661       CommunicationLayerPtr recvingLayer, 
00662       CommunicationLayerPtr sendingLayer)
00663    {
00664       LayerRecvEventPtr p(new LayerRecvEvent(sendDirection,
00665          packet, recvingLayer, sendingLayer));
00666       return p;
00667    }
00668 
00669    void execute()
00670    {
00671       // Give a default recvDirection for the compiler even
00672       // though the switch statement should be setup such
00673       // that it is guaranteed to set recvDirection.
00674       CommunicationLayer::Directions recvDirection = 
00675          CommunicationLayer::Directions_Upper;
00676 
00677       // Just toggle the direction from the sending direction.
00678       switch(m_sendDirection) {
00679       case CommunicationLayer::Directions_Lower:
00680          recvDirection = CommunicationLayer::Directions_Upper;
00681          break;
00682       case CommunicationLayer::Directions_Upper:
00683          recvDirection = CommunicationLayer::Directions_Lower;
00684          break;
00685       default:
00686          assert(false);
00687       }
00688 
00689       assert(m_recvingLayer.get() != 0);
00690       m_recvingLayer->recvFromLayer(recvDirection, m_packet, 
00691          m_sendingLayer);
00692       if(m_sendDirection == CommunicationLayer::Directions_Lower) {
00693          assert(m_sendingLayer.get() != 0);
00694          m_sendingLayer->setLowerLayerRecvEventPending(false);
00695       }
00696    }
00697 
00698 protected:
00700    LayerRecvEvent(CommunicationLayer::Directions sendDirection,
00701       PacketPtr packet, 
00702       CommunicationLayerPtr recvingLayer, 
00703       CommunicationLayerPtr sendingLayer)
00704          : Event()
00705    {
00706       m_sendDirection = sendDirection;
00707       m_packet = packet;
00708       m_recvingLayer = recvingLayer;
00709       m_sendingLayer = sendingLayer;
00710    }
00711 
00712 private:
00713    PacketPtr m_packet;
00714    CommunicationLayer::Directions m_sendDirection;
00715    CommunicationLayerPtr m_recvingLayer;
00716    CommunicationLayerPtr m_sendingLayer;
00717 };
00718 typedef boost::shared_ptr<LayerRecvEvent> LayerRecvEventPtr;
00719 
00720 #endif // COMMUNICATION_LAYER_H
00721 

Generated on Tue Dec 12 17:04:37 2006 for rfidsim by  doxygen 1.4.7