00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <fstream>
00022
00023 #include "ns3/log.h"
00024 #include "ns3/nstime.h"
00025
00026 #include "data-collector.h"
00027 #include "data-calculator.h"
00028 #include "omnet-data-output.h"
00029
00030 using namespace ns3;
00031
00032 NS_LOG_COMPONENT_DEFINE("OmnetDataOutput");
00033
00034
00035
00036
00037 OmnetDataOutput::OmnetDataOutput() :
00038 m_filePrefix("data")
00039 {
00040 NS_LOG_FUNCTION_NOARGS();
00041 }
00042 OmnetDataOutput::~OmnetDataOutput()
00043 {
00044 NS_LOG_FUNCTION_NOARGS();
00045 }
00046 void
00047 OmnetDataOutput::DoDispose()
00048 {
00049 NS_LOG_FUNCTION_NOARGS();
00050
00051 DataOutputInterface::DoDispose();
00052
00053 }
00054
00055 void
00056 OmnetDataOutput::SetFilePrefix(const std::string prefix)
00057 {
00058 m_filePrefix = prefix;
00059 }
00060 std::string
00061 OmnetDataOutput::GetFilePrefix() const
00062 {
00063 return m_filePrefix;
00064 }
00065
00066
00067 void
00068 OmnetDataOutput::Output(DataCollector &dc)
00069 {
00070
00071 std::ofstream scalarFile;
00072 std::string fn = m_filePrefix + ".sca";
00073 scalarFile.open(fn.c_str(), std::ios_base::app);
00074
00075 scalarFile << std::endl;
00076 scalarFile << "run " << dc.GetRunLabel() << std::endl;
00077 scalarFile << std::endl;
00078 scalarFile << "attr experiment \"" << dc.GetExperimentLabel()
00079 << "\"" << std::endl;
00080 scalarFile << "attr strategy \"" << dc.GetStrategyLabel()
00081 << "\"" << std::endl;
00082 scalarFile << "attr input \"" << dc.GetInputLabel()
00083 << "\"" << std::endl;
00084 scalarFile << "attr description \"" << dc.GetDescription()
00085 << "\"" << std::endl;
00086 scalarFile << std::endl;
00087
00088 for (MetadataList::iterator i = dc.MetadataBegin();
00089 i != dc.MetadataEnd(); i++) {
00090 std::pair<std::string, std::string> blob = (*i);
00091 scalarFile << "attr \"" << blob.first << "\" \"" << blob.second << "\""
00092 << std::endl;
00093 }
00094
00095 scalarFile << std::endl;
00096
00097 OmnetOutputCallback callback(&scalarFile);
00098
00099 for (DataCalculatorList::iterator i = dc.DataCalculatorBegin();
00100 i != dc.DataCalculatorEnd(); i++) {
00101 (*i)->Output(callback);
00102 }
00103
00104 scalarFile << std::endl << std::endl;
00105 scalarFile.close();
00106
00107
00108 }
00109
00110 OmnetDataOutput::OmnetOutputCallback::OmnetOutputCallback
00111 (std::ostream *scalar) :
00112 m_scalar(scalar)
00113 {
00114 }
00115
00116 void
00117 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key,
00118 std::string variable,
00119 int val)
00120 {
00121 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl;
00122
00123 }
00124 void
00125 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key,
00126 std::string variable,
00127 uint32_t val)
00128 {
00129 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl;
00130
00131 }
00132 void
00133 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key,
00134 std::string variable,
00135 double val)
00136 {
00137 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl;
00138
00139 }
00140 void
00141 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key,
00142 std::string variable,
00143 std::string val)
00144 {
00145 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl;
00146
00147 }
00148 void
00149 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key,
00150 std::string variable,
00151 Time val)
00152 {
00153 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl;
00154
00155 }