A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
uan-mac-aloha.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 #include "uan-mac-aloha.h"
22 #include "uan-tx-mode.h"
23 #include "uan-address.h"
24 #include "ns3/log.h"
25 #include "uan-phy.h"
26 #include "uan-header-common.h"
27 
28 #include <iostream>
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE ("UanMacAloha");
34 
35 NS_OBJECT_ENSURE_REGISTERED (UanMacAloha);
36 
38  : UanMac (),
39  m_cleared (false)
40 {
41 }
42 
44 {
45 }
46 
47 void
49 {
50  if (m_cleared)
51  {
52  return;
53  }
54  m_cleared = true;
55  if (m_phy)
56  {
57  m_phy->Clear ();
58  m_phy = 0;
59  }
60 }
61 
62 void
64 {
65  Clear ();
67 }
68 
69 TypeId
71 {
72  static TypeId tid = TypeId ("ns3::UanMacAloha")
73  .SetParent<UanMac> ()
74  .SetGroupName ("Uan")
75  .AddConstructor<UanMacAloha> ()
76  ;
77  return tid;
78 }
79 
80 Address
82 {
83  return m_address;
84 }
85 
86 void
88 {
89  m_address=addr;
90 }
91 bool
92 UanMacAloha::Enqueue (Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber)
93 {
94  NS_LOG_DEBUG ("" << Simulator::Now ().GetSeconds () << " MAC " << UanAddress::ConvertFrom (GetAddress ()) << " Queueing packet for " << UanAddress::ConvertFrom (dest));
95 
96  if (!m_phy->IsStateTx ())
97  {
99  UanAddress udest = UanAddress::ConvertFrom (dest);
100 
101  UanHeaderCommon header;
102  header.SetSrc (src);
103  header.SetDest (udest);
104  header.SetType (0);
105 
106  packet->AddHeader (header);
107  m_phy->SendPacket (packet, protocolNumber);
108  return true;
109  }
110  else
111  return false;
112 }
113 
114 void
116 {
117  m_forUpCb = cb;
118 }
119 void
121 {
122  m_phy = phy;
123  m_phy->SetReceiveOkCallback (MakeCallback (&UanMacAloha::RxPacketGood, this));
124  m_phy->SetReceiveErrorCallback (MakeCallback (&UanMacAloha::RxPacketError, this));
125 
126 }
127 void
129 {
130  UanHeaderCommon header;
131  pkt->RemoveHeader (header);
132  NS_LOG_DEBUG ("Receiving packet from " << header.GetSrc () << " For " << header.GetDest ());
133 
134  if (header.GetDest () == GetAddress () || header.GetDest () == UanAddress::GetBroadcast ())
135  {
136  m_forUpCb (pkt, header.GetSrc ());
137  }
138 
139 }
140 
141 void
143 {
144  NS_LOG_DEBUG ("" << Simulator::Now () << " MAC " << UanAddress::ConvertFrom (GetAddress ()) << " Received packet in error with sinr " << sinr);
145 }
146 
147 Address
149 {
150  UanAddress broadcast (255);
151  return broadcast;
152 }
153 
154 int64_t
156 {
157  NS_LOG_FUNCTION (this << stream);
158  return 0;
159 }
160 
161 }
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:268
Ptr< UanPhy > m_phy
PHY layer attached to this MAC.
Definition: uan-mac-aloha.h:71
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
Callback template class.
Definition: callback.h:1176
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual void SetAddress(UanAddress addr)
Set the address.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Callback< void, Ptr< Packet >, const UanAddress & > m_forUpCb
Forwarding up callback.
Definition: uan-mac-aloha.h:73
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:340
Address GetAddress(void)
Get the MAC Address.
virtual Address GetBroadcast(void) const
Get the broadcast address.
a polymophic address class
Definition: address.h:90
Virtual base class for all UAN MAC protocols.
Definition: uan-mac.h:48
tuple phy
Definition: third.py:86
A class used for addressing UAN MAC's.
Definition: uan-address.h:40
static UanAddress ConvertFrom(const Address &address)
Convert a generic address to a UanAddress.
Definition: uan-address.cc:54
void SetSrc(UanAddress src)
Set the source address.
void RxPacketGood(Ptr< Packet > pkt, double sinr, UanTxMode txMode)
Receive packet from lower layer (passed to PHY as callback).
virtual void SetForwardUpCb(Callback< void, Ptr< Packet >, const UanAddress & > cb)
Set the callback to forward packets up to higher layers.
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:41
static TypeId GetTypeId(void)
Register this type.
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetDest(UanAddress dest)
Set the destination address.
void RxPacketError(Ptr< Packet > pkt, double sinr)
Packet received at lower layer in error.
virtual ~UanMacAloha()
Dummy destructor, see DoDispose.
static UanAddress GetBroadcast(void)
Get the broadcast address (255).
Definition: uan-address.cc:92
ALOHA MAC Protocol, the simplest MAC protocol for wireless networks.
Definition: uan-mac-aloha.h:43
Common packet header fields.
UanAddress GetDest(void) const
Get the destination address.
virtual void Clear(void)
Clears all pointer references.
virtual bool Enqueue(Ptr< Packet > pkt, const Address &dest, uint16_t protocolNumber)
Enqueue packet to be transmitted.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
bool m_cleared
Flag when we've been cleared.
Definition: uan-mac-aloha.h:75
virtual void AttachPhy(Ptr< UanPhy > phy)
Attach PHY layer to this MAC.
UanMacAloha()
Default constructor.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
void SetType(uint8_t type)
Set the header type.
UanAddress GetSrc(void) const
Get the source address.
virtual void DoDispose()
Destructor implementation.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
UanAddress m_address
The MAC address.
Definition: uan-mac-aloha.h:69
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:827
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:257