1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include <algorithm>
21 #include "simple-channel.h"
22 #include "simple-net-device.h"
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "ns3/node.h"
26 #include "ns3/log.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("SimpleChannel");
31 
32 NS_OBJECT_ENSURE_REGISTERED (SimpleChannel);
33 
34 TypeId
GetTypeId(void)35 SimpleChannel::GetTypeId (void)
36 {
37   static TypeId tid = TypeId ("ns3::SimpleChannel")
38     .SetParent<Channel> ()
39     .SetGroupName("Network")
40     .AddConstructor<SimpleChannel> ()
41     .AddAttribute ("Delay", "Transmission delay through the channel",
42                    TimeValue (Seconds (0)),
43                    MakeTimeAccessor (&SimpleChannel::m_delay),
44                    MakeTimeChecker ())
45   ;
46   return tid;
47 }
48 
SimpleChannel()49 SimpleChannel::SimpleChannel ()
50 {
51   NS_LOG_FUNCTION (this);
52 }
53 
54 void
Send(Ptr<Packet> p,uint16_t protocol,Mac48Address to,Mac48Address from,Ptr<SimpleNetDevice> sender)55 SimpleChannel::Send (Ptr<Packet> p, uint16_t protocol,
56                      Mac48Address to, Mac48Address from,
57                      Ptr<SimpleNetDevice> sender)
58 {
59   NS_LOG_FUNCTION (this << p << protocol << to << from << sender);
60   for (std::vector<Ptr<SimpleNetDevice> >::const_iterator i = m_devices.begin (); i != m_devices.end (); ++i)
61     {
62       Ptr<SimpleNetDevice> tmp = *i;
63       if (tmp == sender)
64         {
65           continue;
66         }
67       if (m_blackListedDevices.find (tmp) != m_blackListedDevices.end ())
68         {
69           if (find (m_blackListedDevices[tmp].begin (), m_blackListedDevices[tmp].end (), sender) !=
70               m_blackListedDevices[tmp].end () )
71             {
72               continue;
73             }
74         }
75       Simulator::ScheduleWithContext (tmp->GetNode ()->GetId (), m_delay,
76                                       &SimpleNetDevice::Receive, tmp, p->Copy (), protocol, to, from);
77     }
78 }
79 
80 void
Add(Ptr<SimpleNetDevice> device)81 SimpleChannel::Add (Ptr<SimpleNetDevice> device)
82 {
83   NS_LOG_FUNCTION (this << device);
84   m_devices.push_back (device);
85 }
86 
87 std::size_t
GetNDevices(void) const88 SimpleChannel::GetNDevices (void) const
89 {
90   NS_LOG_FUNCTION (this);
91   return m_devices.size ();
92 }
93 
94 Ptr<NetDevice>
GetDevice(std::size_t i) const95 SimpleChannel::GetDevice (std::size_t i) const
96 {
97   NS_LOG_FUNCTION (this << i);
98   return m_devices[i];
99 }
100 
101 void
BlackList(Ptr<SimpleNetDevice> from,Ptr<SimpleNetDevice> to)102 SimpleChannel::BlackList (Ptr<SimpleNetDevice> from, Ptr<SimpleNetDevice> to)
103 {
104   if (m_blackListedDevices.find (to) != m_blackListedDevices.end ())
105     {
106       if (find (m_blackListedDevices[to].begin (), m_blackListedDevices[to].end (), from) ==
107           m_blackListedDevices[to].end () )
108         {
109           m_blackListedDevices[to].push_back (from);
110         }
111     }
112   else
113     {
114       m_blackListedDevices[to].push_back (from);
115     }
116 }
117 
118 void
UnBlackList(Ptr<SimpleNetDevice> from,Ptr<SimpleNetDevice> to)119 SimpleChannel::UnBlackList (Ptr<SimpleNetDevice> from, Ptr<SimpleNetDevice> to)
120 {
121   if (m_blackListedDevices.find (to) != m_blackListedDevices.end ())
122     {
123       std::vector<Ptr<SimpleNetDevice> >::iterator iter;
124       iter = find (m_blackListedDevices[to].begin (), m_blackListedDevices[to].end (), from);
125       if (iter != m_blackListedDevices[to].end () )
126         {
127           m_blackListedDevices[to].erase (iter);
128         }
129     }
130 }
131 
132 
133 } // namespace ns3
134