|
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
|
2 /* |
|
3 * Copyright (c) 2008 Drexel University |
|
4 * |
|
5 * This program is free software; you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License version 2 as |
|
7 * published by the Free Software Foundation; |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU General Public License |
|
15 * along with this program; if not, write to the Free Software |
|
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 * |
|
18 * Author: Joe Kopena (tjkopena@cs.drexel.edu) |
|
19 */ |
|
20 |
|
21 #include <fstream> |
|
22 |
|
23 #include "ns3/log.h" |
|
24 #include "ns3/nstime.h" |
|
25 |
|
26 #include "data-collector.h" |
|
27 #include "data-calculator.h" |
|
28 #include "omnet-data-output.h" |
|
29 |
|
30 using namespace ns3; |
|
31 |
|
32 NS_LOG_COMPONENT_DEFINE("OmnetDataOutput"); |
|
33 |
|
34 |
|
35 //-------------------------------------------------------------- |
|
36 //---------------------------------------------- |
|
37 OmnetDataOutput::OmnetDataOutput() : |
|
38 m_filePrefix("data") |
|
39 { |
|
40 NS_LOG_FUNCTION_NOARGS(); |
|
41 } |
|
42 OmnetDataOutput::~OmnetDataOutput() |
|
43 { |
|
44 NS_LOG_FUNCTION_NOARGS(); |
|
45 } |
|
46 void |
|
47 OmnetDataOutput::DoDispose() |
|
48 { |
|
49 NS_LOG_FUNCTION_NOARGS(); |
|
50 |
|
51 DataOutputInterface::DoDispose(); |
|
52 // end OmnetDataOutput::DoDispose |
|
53 } |
|
54 |
|
55 void |
|
56 OmnetDataOutput::SetFilePrefix(const std::string prefix) |
|
57 { |
|
58 m_filePrefix = prefix; |
|
59 } |
|
60 std::string |
|
61 OmnetDataOutput::GetFilePrefix() const |
|
62 { |
|
63 return m_filePrefix; |
|
64 } |
|
65 |
|
66 //---------------------------------------------- |
|
67 void |
|
68 OmnetDataOutput::Output(DataCollector &dc) |
|
69 { |
|
70 |
|
71 std::ofstream scalarFile; |
|
72 std::string fn = m_filePrefix + ".sca"; |
|
73 scalarFile.open(fn.c_str(), std::ios_base::app); |
|
74 |
|
75 scalarFile << std::endl; |
|
76 scalarFile << "run " << dc.GetRunLabel() << std::endl; |
|
77 scalarFile << std::endl; |
|
78 scalarFile << "attr experiment \"" << dc.GetExperimentLabel() |
|
79 << "\"" << std::endl; |
|
80 scalarFile << "attr strategy \"" << dc.GetStrategyLabel() |
|
81 << "\"" << std::endl; |
|
82 scalarFile << "attr input \"" << dc.GetInputLabel() |
|
83 << "\"" << std::endl; |
|
84 scalarFile << "attr description \"" << dc.GetDescription() |
|
85 << "\"" << std::endl; |
|
86 scalarFile << std::endl; |
|
87 |
|
88 for (MetadataList::iterator i = dc.MetadataBegin(); |
|
89 i != dc.MetadataEnd(); i++) { |
|
90 std::pair<std::string, std::string> blob = (*i); |
|
91 scalarFile << "attr \"" << blob.first << "\" \"" << blob.second << "\"" |
|
92 << std::endl; |
|
93 } |
|
94 |
|
95 scalarFile << std::endl; |
|
96 |
|
97 OmnetOutputCallback callback(&scalarFile); |
|
98 |
|
99 for (DataCalculatorList::iterator i = dc.DataCalculatorBegin(); |
|
100 i != dc.DataCalculatorEnd(); i++) { |
|
101 (*i)->Output(callback); |
|
102 } |
|
103 |
|
104 scalarFile << std::endl << std::endl; |
|
105 scalarFile.close(); |
|
106 |
|
107 // end OmnetDataOutput::Output |
|
108 } |
|
109 |
|
110 OmnetDataOutput::OmnetOutputCallback::OmnetOutputCallback |
|
111 (std::ostream *scalar) : |
|
112 m_scalar(scalar) |
|
113 { |
|
114 } |
|
115 |
|
116 void |
|
117 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key, |
|
118 std::string variable, |
|
119 int val) |
|
120 { |
|
121 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl; |
|
122 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton |
|
123 } |
|
124 void |
|
125 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key, |
|
126 std::string variable, |
|
127 uint32_t val) |
|
128 { |
|
129 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl; |
|
130 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton |
|
131 } |
|
132 void |
|
133 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key, |
|
134 std::string variable, |
|
135 double val) |
|
136 { |
|
137 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl; |
|
138 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton |
|
139 } |
|
140 void |
|
141 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key, |
|
142 std::string variable, |
|
143 std::string val) |
|
144 { |
|
145 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl; |
|
146 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton |
|
147 } |
|
148 void |
|
149 OmnetDataOutput::OmnetOutputCallback::OutputSingleton(std::string key, |
|
150 std::string variable, |
|
151 Time val) |
|
152 { |
|
153 (*m_scalar) << "scalar " << key << " " << variable << " " << val << std::endl; |
|
154 // end OmnetDataOutput::OmnetOutputCallback::OutputSingleton |
|
155 } |