diff --git a/src/common/ascii-writer.cc b/src/common/ascii-writer.cc new file mode 100644 index 000000000..e00b6f485 --- /dev/null +++ b/src/common/ascii-writer.cc @@ -0,0 +1,89 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 INRIA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Guillaume Seguin + */ +#include "ascii-writer.h" + +#include "ns3/log.h" +#include "ns3/simulator.h" +#include "ns3/simulation-singleton.h" +#include "ns3/packet.h" + +#include +#include + + +NS_LOG_COMPONENT_DEFINE ("AsciiWriter"); + +namespace ns3 { + +typedef std::map > AsciiWritersMap; + +Ptr +AsciiWriter::Get (std::ostream &os) +{ + AsciiWritersMap *map = SimulationSingleton::Get (); + Ptr writer = (*map)[&os]; + if (writer == 0) + { + // don't call Create<> because constructor is private + writer = Ptr (new AsciiWriter (&os), false); + (*map)[&os] = writer; + } + return writer; +} + +AsciiWriter::AsciiWriter (std::ostream *os) + : m_writer (os) +{ + NS_LOG_FUNCTION (this); +} + +AsciiWriter::~AsciiWriter (void) +{ + NS_LOG_FUNCTION (this); +} + +void +AsciiWriter::WritePacket (enum Type type, std::string message, Ptr packet) +{ + std::string typeString; + switch (type) + { + case ENQUEUE: + typeString = "+"; + break; + case DEQUEUE: + typeString = "-"; + break; + case RX: + typeString = "r"; + break; + case TX: + typeString = "t"; + break; + case DROP: + typeString = "d"; + break; + } + NS_LOG_FUNCTION (this << typeString << message); + *m_writer << typeString << " " << Simulator::Now ().GetSeconds () << " " + << message << " " << *packet << std::endl; +} + +} // namespace ns3 diff --git a/src/common/ascii-writer.h b/src/common/ascii-writer.h new file mode 100644 index 000000000..03cdc7cf4 --- /dev/null +++ b/src/common/ascii-writer.h @@ -0,0 +1,67 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2009 INRIA + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Guillaume Seguin + */ + +#ifndef ASCII_WRITER_H +#define ASCII_WRITER_H + +#include +#include +#include "ns3/ref-count-base.h" +#include "ns3/ptr.h" + +namespace ns3 { + +class Packet; + +/** + * \ingroup common + * + * \brief Ascii output + */ +class AsciiWriter : public RefCountBase +{ +public: + static Ptr Get (std::ostream &os); + + enum Type { + ENQUEUE, + DEQUEUE, + DROP, + TX, + RX + }; + + ~AsciiWriter (void); + + /** + * Writes a message in the output file, checking if the files will + * need to be reordered. + */ + void WritePacket (enum Type type, std::string message, Ptr p); + +private: + AsciiWriter (std::ostream *os); + + std::ostream *m_writer; +}; + +} // namespace ns3 + +#endif /* ASCII_WRITER_H */ diff --git a/src/common/wscript b/src/common/wscript index e8e028511..d6f0d8d09 100644 --- a/src/common/wscript +++ b/src/common/wscript @@ -17,6 +17,7 @@ def build(bld): 'byte-tag-list.cc', 'tag-buffer.cc', 'packet-tag-list.cc', + 'ascii-writer.cc', ] headers = bld.new_task_gen('ns3header') @@ -35,4 +36,5 @@ def build(bld): 'byte-tag-list.h', 'tag-buffer.h', 'packet-tag-list.h', + 'ascii-writer.h', ]