00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "csma-helper.h"
00021 #include "ns3/simulator.h"
00022 #include "ns3/object-factory.h"
00023 #include "ns3/queue.h"
00024 #include "ns3/csma-net-device.h"
00025 #include "ns3/csma-channel.h"
00026 #include "ns3/pcap-writer.h"
00027 #include "ns3/config.h"
00028 #include "ns3/packet.h"
00029 #include <string>
00030
00031 namespace ns3 {
00032
00033 CsmaHelper::CsmaHelper ()
00034 {
00035 m_queueFactory.SetTypeId ("ns3::DropTailQueue");
00036 m_deviceFactory.SetTypeId ("ns3::CsmaNetDevice");
00037 m_channelFactory.SetTypeId ("ns3::CsmaChannel");
00038 }
00039
00040 void
00041 CsmaHelper::SetQueue (std::string type,
00042 std::string n1, const AttributeValue &v1,
00043 std::string n2, const AttributeValue &v2,
00044 std::string n3, const AttributeValue &v3,
00045 std::string n4, const AttributeValue &v4)
00046 {
00047 m_queueFactory.SetTypeId (type);
00048 m_queueFactory.Set (n1, v1);
00049 m_queueFactory.Set (n2, v2);
00050 m_queueFactory.Set (n3, v3);
00051 m_queueFactory.Set (n4, v4);
00052 }
00053
00054 void
00055 CsmaHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
00056 {
00057 m_deviceFactory.Set (n1, v1);
00058 }
00059
00060 void
00061 CsmaHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
00062 {
00063 m_channelFactory.Set (n1, v1);
00064 }
00065
00066 void
00067 CsmaHelper::SetDeviceParameter (std::string n1, const AttributeValue &v1)
00068 {
00069 SetDeviceAttribute (n1, v1);
00070 }
00071 void
00072 CsmaHelper::SetChannelParameter (std::string n1, const AttributeValue &v1)
00073 {
00074 SetChannelAttribute (n1, v1);
00075 }
00076
00077 void
00078 CsmaHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid)
00079 {
00080 std::ostringstream oss;
00081 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/";
00082 Config::MatchContainer matches = Config::LookupMatches (oss.str ());
00083 if (matches.GetN () == 0)
00084 {
00085 return;
00086 }
00087 oss.str ("");
00088 oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
00089 Ptr<PcapWriter> pcap = Create<PcapWriter> ();
00090 pcap->Open (oss.str ());
00091 pcap->WriteEthernetHeader ();
00092 oss.str ("");
00093 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/Rx";
00094 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&CsmaHelper::RxEvent, pcap));
00095 oss.str ("");
00096 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue";
00097 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&CsmaHelper::EnqueueEvent, pcap));
00098 }
00099 void
00100 CsmaHelper::EnablePcap (std::string filename, NetDeviceContainer d)
00101 {
00102 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
00103 {
00104 Ptr<NetDevice> dev = *i;
00105 EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
00106 }
00107 }
00108 void
00109 CsmaHelper::EnablePcap (std::string filename, NodeContainer n)
00110 {
00111 NetDeviceContainer devs;
00112 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
00113 {
00114 Ptr<Node> node = *i;
00115 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
00116 {
00117 devs.Add (node->GetDevice (j));
00118 }
00119 }
00120 EnablePcap (filename, devs);
00121 }
00122
00123 void
00124 CsmaHelper::EnablePcapAll (std::string filename)
00125 {
00126 EnablePcap (filename, NodeContainer::GetGlobal ());
00127 }
00128
00129 void
00130 CsmaHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
00131 {
00132 Packet::EnablePrinting ();
00133 std::ostringstream oss;
00134 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/Rx";
00135 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiRxEvent, &os));
00136 oss.str ("");
00137 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Enqueue";
00138 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiEnqueueEvent, &os));
00139 oss.str ("");
00140 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Dequeue";
00141 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiDequeueEvent, &os));
00142 oss.str ("");
00143 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::CsmaNetDevice/TxQueue/Drop";
00144 Config::Connect (oss.str (), MakeBoundCallback (&CsmaHelper::AsciiDropEvent, &os));
00145 }
00146 void
00147 CsmaHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
00148 {
00149 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
00150 {
00151 Ptr<NetDevice> dev = *i;
00152 EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
00153 }
00154 }
00155 void
00156 CsmaHelper::EnableAscii (std::ostream &os, NodeContainer n)
00157 {
00158 NetDeviceContainer devs;
00159 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
00160 {
00161 Ptr<Node> node = *i;
00162 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
00163 {
00164 devs.Add (node->GetDevice (j));
00165 }
00166 }
00167 EnableAscii (os, devs);
00168 }
00169
00170 void
00171 CsmaHelper::EnableAsciiAll (std::ostream &os)
00172 {
00173 EnableAscii (os, NodeContainer::GetGlobal ());
00174 }
00175
00176 NetDeviceContainer
00177 CsmaHelper::Install (Ptr<Node> node) const
00178 {
00179 Ptr<CsmaChannel> channel = m_channelFactory.Create ()->GetObject<CsmaChannel> ();
00180 return Install (node, channel);
00181 }
00182
00183 NetDeviceContainer
00184 CsmaHelper::Install (Ptr<Node> node, Ptr<CsmaChannel> channel) const
00185 {
00186 return NetDeviceContainer (InstallPriv (node, channel));
00187 }
00188
00189 NetDeviceContainer
00190 CsmaHelper::Install (const NodeContainer &c) const
00191 {
00192 Ptr<CsmaChannel> channel = m_channelFactory.Create ()->GetObject<CsmaChannel> ();
00193
00194 return Install (c, channel);
00195 }
00196
00197 NetDeviceContainer
00198 CsmaHelper::Install (const NodeContainer &c, Ptr<CsmaChannel> channel) const
00199 {
00200 NetDeviceContainer devs;
00201
00202 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); i++)
00203 {
00204 devs.Add (InstallPriv (*i, channel));
00205 }
00206
00207 return devs;
00208 }
00209
00210 Ptr<NetDevice>
00211 CsmaHelper::InstallPriv (Ptr<Node> node, Ptr<CsmaChannel> channel) const
00212 {
00213 Ptr<CsmaNetDevice> device = m_deviceFactory.Create<CsmaNetDevice> ();
00214 device->SetAddress (Mac48Address::Allocate ());
00215 node->AddDevice (device);
00216 Ptr<Queue> queue = m_queueFactory.Create<Queue> ();
00217 device->SetQueue (queue);
00218 device->Attach (channel);
00219
00220 return device;
00221 }
00222
00223 void
00224 CsmaHelper::InstallStar (Ptr<Node> hub, NodeContainer spokes,
00225 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
00226 {
00227 for (uint32_t i = 0; i < spokes.GetN (); ++i)
00228 {
00229 NodeContainer nodes (hub, spokes.Get (i));
00230 NetDeviceContainer nd = Install (nodes);
00231 hubDevices.Add (nd.Get (0));
00232 spokeDevices.Add (nd.Get (1));
00233 }
00234 }
00235
00236 void
00237 CsmaHelper::EnqueueEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
00238 {
00239 writer->WritePacket (packet);
00240 }
00241 void
00242 CsmaHelper::RxEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
00243 {
00244 writer->WritePacket (packet);
00245 }
00246 void
00247 CsmaHelper::AsciiEnqueueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00248 {
00249 *os << "+ " << Simulator::Now ().GetSeconds () << " ";
00250 *os << path << " " << *packet << std::endl;
00251 }
00252
00253 void
00254 CsmaHelper::AsciiDequeueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00255 {
00256 *os << "- " << Simulator::Now ().GetSeconds () << " ";
00257 *os << path << " " << *packet << std::endl;
00258 }
00259
00260 void
00261 CsmaHelper::AsciiDropEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00262 {
00263 *os << "d " << Simulator::Now ().GetSeconds () << " ";
00264 *os << path << " " << *packet << std::endl;
00265 }
00266
00267 void
00268 CsmaHelper::AsciiRxEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00269 {
00270 *os << "r " << Simulator::Now ().GetSeconds () << " ";
00271 *os << path << " " << *packet << std::endl;
00272 }
00273
00274 }