add an ascii writer class
This commit is contained in:
89
src/common/ascii-writer.cc
Normal file
89
src/common/ascii-writer.cc
Normal file
@@ -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 <guillaume@segu.in>
|
||||
*/
|
||||
#include "ascii-writer.h"
|
||||
|
||||
#include "ns3/log.h"
|
||||
#include "ns3/simulator.h"
|
||||
#include "ns3/simulation-singleton.h"
|
||||
#include "ns3/packet.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
|
||||
|
||||
NS_LOG_COMPONENT_DEFINE ("AsciiWriter");
|
||||
|
||||
namespace ns3 {
|
||||
|
||||
typedef std::map<std::ostream*, Ptr<AsciiWriter> > AsciiWritersMap;
|
||||
|
||||
Ptr<AsciiWriter>
|
||||
AsciiWriter::Get (std::ostream &os)
|
||||
{
|
||||
AsciiWritersMap *map = SimulationSingleton<AsciiWritersMap>::Get ();
|
||||
Ptr<AsciiWriter> writer = (*map)[&os];
|
||||
if (writer == 0)
|
||||
{
|
||||
// don't call Create<> because constructor is private
|
||||
writer = Ptr<AsciiWriter> (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<const Packet> 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
|
||||
67
src/common/ascii-writer.h
Normal file
67
src/common/ascii-writer.h
Normal file
@@ -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 <guilla...@segu.in>
|
||||
*/
|
||||
|
||||
#ifndef ASCII_WRITER_H
|
||||
#define ASCII_WRITER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ostream>
|
||||
#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<AsciiWriter> 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<const Packet> p);
|
||||
|
||||
private:
|
||||
AsciiWriter (std::ostream *os);
|
||||
|
||||
std::ostream *m_writer;
|
||||
};
|
||||
|
||||
} // namespace ns3
|
||||
|
||||
#endif /* ASCII_WRITER_H */
|
||||
@@ -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',
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user