00001 00002 #ifndef LINK_LAYER_H 00003 #define LINK_LAYER_H 00004 00005 #include <boost/shared_ptr.hpp> 00006 #include <boost/weak_ptr.hpp> 00007 00008 #include "communication_layer.hpp" 00009 #include "mac_protocol.hpp" 00010 #include "simulator.hpp" 00011 00012 class Node; 00013 typedef boost::shared_ptr<Node> NodePtr; 00014 00016 // LinkLayer Class 00018 00024 class LinkLayer : public CommunicationLayer { 00025 friend class MacProtocol; 00026 public: 00028 typedef boost::shared_ptr<LinkLayer> LinkLayerPtr; 00029 00037 static inline LinkLayerPtr create(NodePtr node, 00038 MacProtocolPtr macProtocol); 00039 00046 inline LinkLayerPtr thisLinkLayer(); 00047 00054 inline SimulationEndListenerPtr thisSimulationEndListener(); 00055 00059 virtual void simulationEndHandler(); 00060 00070 bool recvFromLayer(CommunicationLayer::Directions direction, 00071 PacketPtr packet, t_uint sendingLayerIdx); 00072 00081 bool recvFromMacProtocol(CommunicationLayer::Directions direction, 00082 PacketPtr packet); 00083 00089 bool channelIsBusy(); 00090 00095 inline CommunicationLayer::Types getLayerType() const; 00096 00097 protected: 00098 00100 LinkLayer(NodePtr node, MacProtocolPtr macProtocol); 00101 00102 private: 00103 00106 boost::weak_ptr<LinkLayer> m_weakThis; 00107 00109 static const t_uint m_LINK_LAYER_QUEUE_LENGTH; 00110 00111 00114 MacProtocolPtr m_macProtocol; 00115 00116 }; 00117 typedef boost::shared_ptr<LinkLayer> LinkLayerPtr; 00118 00120 // Inline Functions 00122 00123 inline LinkLayerPtr LinkLayer::create(NodePtr node, 00124 MacProtocolPtr macProtocol) 00125 { 00126 LinkLayerPtr p(new LinkLayer(node, macProtocol)); 00127 p->m_weakThis = p; 00128 // thisLinkLayer() must be called after m_weakThis has 00129 // been set. 00130 assert(p->m_macProtocol.get() != 0); 00131 p->m_macProtocol->setLinkLayer(p->thisLinkLayer()); 00132 Simulator::instance()->addSimulationEndListener( 00133 p->thisSimulationEndListener()); 00134 return p; 00135 } 00136 00137 inline LinkLayerPtr LinkLayer::thisLinkLayer() 00138 { 00139 LinkLayerPtr p(m_weakThis); 00140 return p; 00141 } 00142 00143 inline SimulationEndListenerPtr LinkLayer::thisSimulationEndListener() 00144 { 00145 SimulationEndListenerPtr p(m_weakThis); 00146 return p; 00147 } 00148 00149 inline CommunicationLayer::Types LinkLayer::getLayerType() const 00150 { 00151 return CommunicationLayer::Types_Link; 00152 } 00153 00155 // Overloaded Operators 00157 00158 #endif // LINK_LAYER_H 00159