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