Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ | 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
2 /* | 2 /* |
3 * Copyright (c) 2007 INRIA | 3 * Copyright (c) 2007 INRIA |
4 * 2009,2010 Contributors | 4 * 2009,2010 Contributors |
5 * | 5 * |
6 * This program is free software; you can redistribute it and/or modify | 6 * This program is free software; you can redistribute it and/or modify |
7 * it under the terms of the GNU General Public License version 2 as | 7 * it under the terms of the GNU General Public License version 2 as |
8 * published by the Free Software Foundation; | 8 * published by the Free Software Foundation; |
9 * | 9 * |
10 * This program is distributed in the hope that it will be useful, | 10 * This program is distributed in the hope that it will be useful, |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 * GNU General Public License for more details. | 13 * GNU General Public License for more details. |
14 * | 14 * |
15 * You should have received a copy of the GNU General Public License | 15 * You should have received a copy of the GNU General Public License |
16 * along with this program; if not, write to the Free Software | 16 * along with this program; if not, write to the Free Software |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 * | 18 * |
19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> | 19 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
20 * Contributors: Thomas Waldecker <twaldecker@rocketmail.com> | 20 * Contributors: Thomas Waldecker <twaldecker@rocketmail.com> |
21 * Martín Giachino <martin.giachino@gmail.com> | 21 * Martín Giachino <martin.giachino@gmail.com> |
22 * | 22 * |
23 * File: ns2-transmobility-helper.cc | 23 * File: ns2-transmobility-helper.cc |
24 * Brief description: Implementation of a ns2 movement trace file reader. | 24 * Brief description: Implementation of a ns2 movement trace file reader. |
25 * | 25 * |
26 * This implementation is based on the ns2 movement documentation of ns2 | 26 * This implementation is based on the ns2 movement documentation of ns2 |
27 * as described in http://www.isi.edu/nsnam/ns/doc/node174.html | 27 * as described in http://www.isi.edu/nsnam/ns/doc/node174.html |
28 * | 28 * |
29 * Valid trace files use the following ns2 statements: | |
30 * | |
31 * $node set X_ x1 | |
32 * $node set Y_ y1 | |
33 * $node set Z_ z1 | |
34 * $ns at $time $node setdest x2 y2 speed | |
35 * $ns at $time $node set X_ x1 | |
36 * $ns at $time $node set Y_ Y1 | |
37 * $ns at $time $node set Z_ Z1 | |
38 * | |
29 */ | 39 */ |
30 | 40 |
31 | 41 |
32 #include <fstream> | 42 #include <fstream> |
33 #include <sstream> | 43 #include <sstream> |
34 #include <map> | 44 #include <map> |
35 #include <algorithm> | 45 #include <algorithm> |
36 #include <cstdio> // for remove() | |
37 #include "ns3/log.h" | 46 #include "ns3/log.h" |
38 #include "ns3/simulator.h" | 47 #include "ns3/simulator.h" |
39 #include "ns3/node-list.h" | 48 #include "ns3/node-list.h" |
40 #include "ns3/node.h" | 49 #include "ns3/node.h" |
41 #include "ns3/constant-velocity-mobility-model.h" | 50 #include "ns3/constant-velocity-mobility-model.h" |
42 #include "ns3/test.h" | 51 #include "ns3/test.h" |
43 #include "ns3/node-container.h" | 52 #include "ns3/node-container.h" |
44 #include "ns3/names.h" | 53 #include "ns3/names.h" |
45 #include "ns3/config.h" | 54 #include "ns3/config.h" |
46 #include "ns2-transmobility-helper.h" | 55 #include "ns2-transmobility-helper.h" |
47 | 56 |
48 NS_LOG_COMPONENT_DEFINE ("Ns2TransmobilityHelper"); | 57 NS_LOG_COMPONENT_DEFINE ("Ns2TransmobilityHelper"); |
49 | 58 |
50 using namespace std; | 59 using namespace std; |
51 | 60 |
52 namespace ns3 { | 61 namespace ns3 { |
53 | 62 |
54 // Type to mantain line parsed and its values | 63 // Constants definitions |
55 struct ParseResult { | 64 #define NS2_AT "at" |
65 #define NS2_X_COORD "X_" | |
66 #define NS2_Y_COORD "Y_" | |
67 #define NS2_Z_COORD "Z_" | |
68 #define NS2_SETDEST "setdest" | |
69 #define NS2_SET "set" | |
70 #define NS2_NODEID "$node_(" | |
71 #define NS2_NS_SCH "$ns_" | |
72 | |
73 | |
74 // Type to maintain line parsed and its values | |
75 struct ParseResult | |
76 { | |
56 vector<string> tokens; // tokens from a line | 77 vector<string> tokens; // tokens from a line |
57 vector<int> ivals; // int values for each tokens | 78 vector<int> ivals; // int values for each tokens |
58 vector<bool> has_ival; // points if a tokens has an int value | 79 vector<bool> has_ival; // points if a tokens has an int value |
59 vector<double> dvals; // double values for each tokens | 80 vector<double> dvals; // double values for each tokens |
60 vector<bool> has_dval; // points if a tokens has a double value | 81 vector<bool> has_dval; // points if a tokens has a double value |
61 vector<string> svals; // string value for each token | 82 vector<string> svals; // string value for each token |
62 }; | 83 }; |
63 | 84 |
64 | 85 |
65 // Parses a line of ns2 mobility | 86 // Parses a line of ns2 mobility |
66 static ParseResult ParseNs2Line(const string& str); | 87 static ParseResult ParseNs2Line (const string& str); |
67 | 88 |
68 // Put out blank spaces at the begining and end of a line | 89 // Put out blank spaces at the start and end of a line |
69 static string TrimNs2Line(const string& s); | 90 static string TrimNs2Line (const string& str); |
70 | |
71 // converts a string into a number | |
72 static string Num(const string& str); | |
73 | 91 |
74 // Checks if a string represents a number or it has others characters than digit s an point. | 92 // Checks if a string represents a number or it has others characters than digit s an point. |
75 bool IsNumber(const string& str); | 93 static bool IsNumber (const string& s); |
76 | 94 |
77 // Check if s string represents a numeric value | 95 // Check if s string represents a numeric value |
78 template<class T> | 96 template<class T> |
79 static bool IsVal(const string& str, T& ret); | 97 static bool IsVal (const string& str, T& ret); |
80 | 98 |
81 // Checks if the value between brackets is a correct nodeId number | 99 // Checks if the value between brackets is a correct nodeId number |
82 static bool hasNodeIdNumber(string str); | 100 static bool HasNodeIdNumber (string str); |
101 | |
102 // Gets nodeId number in string format from the string like $node_(4) | |
103 static string GetNodeIdFromToken (string str); | |
83 | 104 |
84 // Get node id number in int format | 105 // Get node id number in int format |
85 static int GetNodeIdInt(ParseResult pr); | 106 static int GetNodeIdInt (ParseResult pr); |
86 | 107 |
87 // Get node id number in string format | 108 // Get node id number in string format |
88 static string GetNodeIdString(ParseResult pr); | 109 static string GetNodeIdString (ParseResult pr); |
89 | 110 |
90 // Add one coord to a vector position | 111 // Add one coord to a vector position |
91 static Vector SetOneInitialCoord(Vector actPos, string& coord, double value); | 112 static Vector SetOneInitialCoord (Vector actPos, string& coord, double value); |
92 | 113 |
93 // Check if this corresponds to a line like this: $node_(0) set X_ 123 | 114 // Check if this corresponds to a line like this: $node_(0) set X_ 123 |
94 static bool IsSetInitialPos(ParseResult pr); | 115 static bool IsSetInitialPos (ParseResult pr); |
95 | 116 |
96 // Check if this corresponds to a line like this: $ns_ at 1 "$node_(0) setdest 2 3 4" | 117 // Check if this corresponds to a line like this: $ns_ at 1 "$node_(0) setdest 2 3 4" |
97 static bool IsSchedSetPos(ParseResult pr); | 118 static bool IsSchedSetPos (ParseResult pr); |
98 | 119 |
99 // Check if this corresponds to a line like this: $ns_ at 1 "$node_(0) set X_ 2" | 120 // Check if this corresponds to a line like this: $ns_ at 1 "$node_(0) set X_ 2" |
100 static bool IsSchedMobilityPos(ParseResult pr); | 121 static bool IsSchedMobilityPos (ParseResult pr); |
101 | 122 |
102 // Set waypoints and speed for movement. | 123 // Set waypoints and speed for movement. |
103 static Vector SetMovement(Ptr<ConstantVelocityMobilityModel> model, Vector lastP os, double at, | 124 static Vector SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector last Pos, double at, |
104 double xFinalPosition, double yFinalPosition, double speed); | 125 double xFinalPosition, double yFinalPosition, double speed); |
105 | 126 |
106 // Set initial position for a node | 127 // Set initial position for a node |
107 static Vector SetInitialPosition(Ptr<ConstantVelocityMobilityModel> model, strin g coord, double coordVal); | 128 static Vector SetInitialPosition (Ptr<ConstantVelocityMobilityModel> model, stri ng coord, double coordVal); |
108 | 129 |
109 // Schedule a set of position for a node | 130 // Schedule a set of position for a node |
110 static Vector SetSchedPosition(Ptr<ConstantVelocityMobilityModel> model, double at, string coord, double coordVal); | 131 static Vector SetSchedPosition (Ptr<ConstantVelocityMobilityModel> model, double at, string coord, double coordVal); |
111 | 132 |
112 | 133 |
113 Ns2TransmobilityHelper::Ns2TransmobilityHelper (std::string filename) | 134 Ns2TransmobilityHelper::Ns2TransmobilityHelper (std::string filename) |
114 : m_filename (filename) | 135 : m_filename (filename) |
115 {} | 136 { |
137 } | |
116 | 138 |
117 Ptr<ConstantVelocityMobilityModel> | 139 Ptr<ConstantVelocityMobilityModel> |
118 Ns2TransmobilityHelper::GetMobilityModel (std::string idString, const ObjectStor e &store) const | 140 Ns2TransmobilityHelper::GetMobilityModel (std::string idString, const ObjectStor e &store) const |
119 { | 141 { |
120 std::istringstream iss; | 142 std::istringstream iss; |
121 iss.str (idString); | 143 iss.str (idString); |
122 uint32_t id; | 144 uint32_t id; |
123 iss >> id; | 145 iss >> id; |
124 Ptr<Object> object = store.Get (id); | 146 Ptr<Object> object = store.Get (id); |
125 if (object == 0) | 147 if (object == 0) |
126 { | 148 { |
127 return 0; | 149 return 0; |
128 } | 150 } |
129 Ptr<ConstantVelocityMobilityModel> model = object->GetObject<ConstantVelocityM obilityModel> (); | 151 Ptr<ConstantVelocityMobilityModel> model = object->GetObject<ConstantVelocityM obilityModel> (); |
130 if (model == 0) | 152 if (model == 0) |
131 { | 153 { |
132 model = CreateObject<ConstantVelocityMobilityModel> (); | 154 model = CreateObject<ConstantVelocityMobilityModel> (); |
133 object->AggregateObject (model); | 155 object->AggregateObject (model); |
134 } | 156 } |
135 return model; | 157 return model; |
136 } | 158 } |
137 | 159 |
138 | 160 |
139 void | 161 void |
140 Ns2TransmobilityHelper::ConfigNodesMovements (const ObjectStore &store) const | 162 Ns2TransmobilityHelper::ConfigNodesMovements (const ObjectStore &store) const |
141 { | 163 { |
142 map<int, Vector> last_pos;» // Vector containing lasts positions for each no de | 164 map<int, Vector> last_pos; // Vector containing lasts positions for each no de |
143 | 165 |
144 std::ifstream file (m_filename.c_str (), std::ios::in); | 166 std::ifstream file (m_filename.c_str (), std::ios::in); |
145 if (file.is_open()) | 167 if (file.is_open ()) |
146 { | 168 { |
147 while (!file.eof() ) | 169 while (!file.eof () ) |
148 { | 170 { |
149 » int iNodeId = 0; | 171 int iNodeId = 0; |
150 » std::string nodeId; | 172 std::string nodeId; |
151 std::string line; | 173 std::string line; |
152 | 174 |
153 getline (file, line); | 175 getline (file, line); |
154 | 176 |
155 ParseResult pr = ParseNs2Line(line); // Parse line and obtain tokens | 177 // ignore empty lines |
178 if (line.empty ()) | |
179 { | |
180 continue; | |
181 } | |
182 | |
183 ParseResult pr = ParseNs2Line (line); // Parse line and obtain tokens | |
156 | 184 |
157 // Check if the line corresponds with one of the three types of line | 185 // Check if the line corresponds with one of the three types of line |
158 » if (pr.tokens.size() != 4 && pr.tokens.size() != 7 && pr.tokens.size() != 8) | 186 if (pr.tokens.size () != 4 && pr.tokens.size () != 7 && pr.tokens.size () != 8) |
159 { | 187 { |
160 »» NS_LOG_WARN ("Parsed line has not a correct number of paramete rs: " << line << "\n"); | 188 NS_LOG_ERROR ("Line has not correct number of parameters (corrupte d file?): " << line << "\n"); |
161 »» continue; | 189 continue; |
162 } | 190 } |
163 | 191 |
164 » // Get the node Id | 192 // Get the node Id |
165 » nodeId= GetNodeIdString(pr); | 193 nodeId = GetNodeIdString (pr); |
166 iNodeId = GetNodeIdInt(pr); | 194 iNodeId = GetNodeIdInt (pr); |
167 | 195 if (iNodeId == -1) |
168 //get mobility model of node | 196 { |
197 » NS_LOG_ERROR ("Node number couldn't be obtained (corrupted fil e?): " << line << "\n"); | |
198 continue; | |
199 } | |
200 | |
201 // get mobility model of node | |
169 Ptr<ConstantVelocityMobilityModel> model = GetMobilityModel (nodeId,st ore); | 202 Ptr<ConstantVelocityMobilityModel> model = GetMobilityModel (nodeId,st ore); |
170 | 203 |
171 //if model not exists, continue | 204 // if model not exists, continue |
172 if (model == 0) | 205 if (model == 0) |
173 { | 206 { |
174 » NS_LOG_WARN("Unknown node ID: " << nodeId << ""); | 207 NS_LOG_ERROR ("Unknown node ID (corrupted file?): " << nodeId << " \n"); |
175 continue; | 208 continue; |
176 } | 209 } |
177 | 210 |
178 | 211 |
179 /* | 212 /* |
180 » * In this case a initial position is being seted | 213 * In this case a initial position is being seted |
181 » * line like $node_(0) set X_ 151.05190721688197 | 214 * line like $node_(0) set X_ 151.05190721688197 |
182 » */ | 215 */ |
183 if (IsSetInitialPos(pr)) | 216 if (IsSetInitialPos (pr)) |
184 { | 217 { |
185 » last_pos[iNodeId] = SetInitialPosition(model, pr.tokens[2], pr .dvals[3]); | 218 // coord coord value |
219 last_pos[iNodeId] = SetInitialPosition (model, pr.tokens[2], pr.dv als[3]); | |
220 | |
221 // Log new position | |
222 NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << | |
223 " x=" << last_pos[iNodeId].x << " y=" << last_pos[iN odeId].y << " z=" << last_pos[iNodeId].z); | |
186 } | 224 } |
187 | 225 |
188 else // NOW EVENTS TO BE SCHEDULED | 226 else // NOW EVENTS TO BE SCHEDULED |
189 { | 227 { |
190 | 228 |
191 // This is a scheduled event, so time at should be present | 229 // This is a scheduled event, so time at should be present |
192 double at; | 230 double at; |
193 if (!IsNumber(pr.tokens[2])) | 231 |
194 { | 232 if (!IsNumber (pr.tokens[2])) |
195 » NS_LOG_WARN("Time is not a number: " << pr.tokens[2]); | 233 { |
234 NS_LOG_WARN ("Time is not a number: " << pr.tokens[2]); | |
196 continue; | 235 continue; |
197 } | 236 } |
198 | 237 |
199 at = pr.dvals[2]; // set time at | 238 at = pr.dvals[2]; // set time at |
200 | 239 |
201 » /* | 240 if ( at < 0 ) |
202 » * In this case a new waypoint is added | |
203 » * line like $ns_ at 1 "$node_(0) setdest 2 3 4" | |
204 » */ | |
205 if (IsSchedMobilityPos(pr)) | |
206 { | 241 { |
207 » last_pos[iNodeId] = SetMovement(model, last_pos[iNodeId], at, pr.dvals[5], pr.dvals[6], pr.dvals[7]); | 242 NS_LOG_WARN ("Time is less than cero: " << at); |
243 continue; | |
244 } | |
245 | |
246 | |
247 | |
248 /* | |
249 * In this case a new waypoint is added | |
250 * line like $ns_ at 1 "$node_(0) setdest 2 3 4" | |
251 */ | |
252 if (IsSchedMobilityPos (pr)) | |
253 { | |
254 // last position time X coord Y coord velocity | |
255 last_pos[iNodeId] = SetMovement (model, last_pos[iNodeId], at, pr.dvals[5], pr.dvals[6], pr.dvals[7]); | |
256 | |
257 // Log new position | |
258 NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << | |
259 " x=" << last_pos[iNodeId].x << " y=" << last_po s[iNodeId].y << " z=" << last_pos[iNodeId].z); | |
208 } | 260 } |
209 | 261 |
210 | 262 |
211 /* | 263 /* |
212 * Scheduled set position | 264 * Scheduled set position |
213 * line like $ns_ at 4.634906291962 "$node_(0) set X_ 28.675920486 450" | 265 * line like $ns_ at 4.634906291962 "$node_(0) set X_ 28.675920486 450" |
214 */ | 266 */ |
215 else if (IsSchedSetPos(pr)) | 267 else if (IsSchedSetPos (pr)) |
216 { | 268 { |
217 » last_pos[iNodeId] = SetSchedPosition(model, at, pr.tokens[5], pr.dvals[6]); | 269 // time coordinate coord value |
270 last_pos[iNodeId] = SetSchedPosition (model, at, pr.tokens[5], pr.dvals[6]); | |
271 | |
272 // Log new position | |
273 NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << | |
274 " x=" << last_pos[iNodeId].x << " y=" << last_po s[iNodeId].y << " z=" << last_pos[iNodeId].z); | |
218 } | 275 } |
219 else | 276 else |
220 { | 277 { |
221 NS_LOG_WARN ("Format Line is not correct: " << line << "\n"); | 278 NS_LOG_WARN ("Format Line is not correct: " << line << "\n"); |
222 } | 279 } |
223 } | 280 } |
224 // Log new position | |
225 NS_LOG_DEBUG("Positions after parse for node " << iNodeId << " " << no deId << | |
226 " x=" << last_pos[iNodeId].x << " y=" << last_pos[iNode Id].y << " z=" << last_pos[iNodeId].z); | |
227 } | 281 } |
228 file.close(); | 282 file.close (); |
229 } | 283 } |
230 } | 284 } |
231 | 285 |
232 | 286 |
233 ParseResult | 287 ParseResult |
234 ParseNs2Line(const string& str) | 288 ParseNs2Line (const string& str) |
235 { | 289 { |
236 ParseResult ret; | 290 ParseResult ret; |
237 istringstream s; | 291 istringstream s; |
238 string line; | 292 string line; |
239 | 293 |
240 //ignore comments (#) | 294 // ignore comments (#) |
241 size_t pos_sharp = str.find_first_of('#'); | 295 size_t pos_sharp = str.find_first_of ('#'); |
242 if (pos_sharp != string::npos) | 296 if (pos_sharp != string::npos) |
243 { | 297 { |
244 » line = str.substr(0, pos_sharp); | 298 line = str.substr (0, pos_sharp); |
245 » } | 299 } |
Mathieu Lacage
2010/04/12 12:34:11
coding style
| |
246 else | 300 else |
247 { | 301 { |
248 » line = str; | 302 line = str; |
249 } | 303 } |
250 | 304 |
251 line = TrimNs2Line(line); | 305 line = TrimNs2Line (line); |
252 | 306 |
253 // If line hasn't a correct node Id | 307 // If line hasn't a correct node Id |
254 if (!hasNodeIdNumber(line)) | 308 if (!HasNodeIdNumber (line)) |
255 { | 309 { |
256 NS_LOG_WARN("Line has no node Id: " << line); | 310 NS_LOG_WARN ("Line has no node Id: " << line); |
257 return ret; | 311 return ret; |
258 » } | 312 } |
Mathieu Lacage
2010/04/12 12:34:11
coding style
| |
259 | 313 |
260 s.str(line); | 314 s.str (line); |
261 | 315 |
262 while (!s.eof()) | 316 while (!s.eof ()) |
263 { | 317 { |
264 string x; | 318 string x; |
265 s >> x; | 319 s >> x; |
266 ret.tokens.push_back(x); | 320 ret.tokens.push_back (x); |
267 int ii; | 321 int ii; |
268 double d; | 322 double d; |
269 ret.has_ival.push_back(IsVal<int>(x, ii)); | 323 if (HasNodeIdNumber (x)) |
270 ret.ivals.push_back(ii); | 324 { |
271 ret.has_dval.push_back(IsVal<double>(x, d)); | 325 x = GetNodeIdFromToken (x); |
272 ret.dvals.push_back(d); | 326 } |
273 ret.svals.push_back(Num(x)); | 327 ret.has_ival.push_back (IsVal<int> (x, ii)); |
274 } | 328 ret.ivals.push_back (ii); |
275 | 329 ret.has_dval.push_back (IsVal<double> (x, d)); |
276 size_t tokensLength = ret.tokens.size(); // number of tokens in line | 330 ret.dvals.push_back (d); |
277 size_t lasTokenLength = ret.tokens[tokensLength-1].size(); // length of the la st token | 331 ret.svals.push_back (x); |
332 } | |
333 | |
334 size_t tokensLength = ret.tokens.size (); // number of token s in line | |
335 size_t lasTokenLength = ret.tokens[tokensLength - 1].size (); // length of the last token | |
278 | 336 |
279 // if it is a scheduled set _[XYZ] or a setdest I need to remove the last " | 337 // if it is a scheduled set _[XYZ] or a setdest I need to remove the last " |
280 // and re-calculate values | 338 // and re-calculate values |
281 if( (tokensLength == 7 || tokensLength == 8) | 339 if ( (tokensLength == 7 || tokensLength == 8) |
282 && (ret.tokens[tokensLength-1][lasTokenLength-1] == '"') ) | 340 && (ret.tokens[tokensLength - 1][lasTokenLength - 1] == '"') ) |
283 { | 341 { |
284 | 342 |
285 » // removes " from the last position | 343 // removes " from the last position |
286 ret.tokens[tokensLength-1] = ret.tokens[tokensLength-1].substr(0,lasTokenL ength-1); | 344 ret.tokens[tokensLength - 1] = ret.tokens[tokensLength - 1].substr (0,lasT okenLength - 1); |
287 | 345 |
288 string x; | 346 string x; |
289 x = ret.tokens[tokensLength-1]; | 347 x = ret.tokens[tokensLength - 1]; |
348 | |
349 if (HasNodeIdNumber (x)) | |
350 { | |
351 x = GetNodeIdFromToken (x); | |
352 } | |
290 | 353 |
291 // Re calculate values | 354 // Re calculate values |
292 int ii; | 355 int ii; |
293 double d; | 356 double d; |
294 ret.has_ival[tokensLength-1] = IsVal<int>(x, ii); | 357 ret.has_ival[tokensLength - 1] = IsVal<int> (x, ii); |
295 ret.ivals[tokensLength-1] = ii; | 358 ret.ivals[tokensLength - 1] = ii; |
296 ret.has_dval[tokensLength-1] = IsVal<double>(x, d); | 359 ret.has_dval[tokensLength - 1] = IsVal<double> (x, d); |
297 ret.dvals[tokensLength-1] = d; | 360 ret.dvals[tokensLength - 1] = d; |
298 ret.svals[tokensLength-1] = Num(x); | 361 ret.svals[tokensLength - 1] = x; |
299 | 362 |
300 } | 363 } |
301 else if ( (tokensLength == 9 && ret.tokens[tokensLength-1] == "\"") | 364 else if ( (tokensLength == 9 && ret.tokens[tokensLength - 1] == "\"") |
302 || (tokensLength == 8 && ret.tokens[tokensLength-1] == "\"")) | 365 || (tokensLength == 8 && ret.tokens[tokensLength - 1] == "\"")) |
303 { | 366 { |
304 // if the line has the " character in this way: $ns_ at 1 "$node_(0) setde st 2 2 1 " | 367 // if the line has the " character in this way: $ns_ at 1 "$node_(0) setde st 2 2 1 " |
305 » // or in this: $ns_ at 4 "$node_(0) set X_ 2 " we need to ignore this last token | 368 // or in this: $ns_ at 4 "$node_(0) set X_ 2 " we need to ignore this las t token |
306 | 369 |
307 ret.tokens.erase(ret.tokens.begin()+tokensLength-1); | 370 ret.tokens.erase (ret.tokens.begin () + tokensLength - 1); |
308 ret.has_ival.erase(ret.has_ival.begin()+tokensLength-1); | 371 ret.has_ival.erase (ret.has_ival.begin () + tokensLength - 1); |
309 ret.ivals.erase(ret.ivals.begin()+tokensLength-1); | 372 ret.ivals.erase (ret.ivals.begin () + tokensLength - 1); |
310 ret.has_dval.erase(ret.has_dval.begin()+tokensLength-1); | 373 ret.has_dval.erase (ret.has_dval.begin () + tokensLength - 1); |
311 ret.dvals.erase(ret.dvals.begin()+tokensLength-1); | 374 ret.dvals.erase (ret.dvals.begin () + tokensLength - 1); |
312 ret.svals.erase(ret.svals.begin()+tokensLength-1); | 375 ret.svals.erase (ret.svals.begin () + tokensLength - 1); |
313 | 376 |
314 } | 377 } |
315 | 378 |
316 | 379 |
317 | 380 |
318 return ret; | 381 return ret; |
319 } | 382 } |
320 | 383 |
321 | 384 |
322 string | 385 string |
323 TrimNs2Line(const string& s) | 386 TrimNs2Line (const string& s) |
324 { | 387 { |
325 string ret = s; | 388 string ret = s; |
326 | 389 |
327 while(ret.size() > 0 && isblank(ret[0])) | 390 while (ret.size () > 0 && isblank (ret[0])) |
328 { | 391 { |
329 » ret.erase(0, 1); // Removes blank spaces at the begining of the line | 392 ret.erase (0, 1); // Removes blank spaces at the begining of the line |
330 } | 393 } |
331 | 394 |
332 while(ret.size() > 0 && isblank(ret[ret.size()-1])) | 395 while (ret.size () > 0 && isblank (ret[ret.size () - 1])) |
333 { | 396 { |
334 » ret.erase(ret.size()-1, 1); // Removes blank spaces from at end of lin e | 397 ret.erase (ret.size () - 1, 1); // Removes blank spaces from at end of lin e |
335 } | 398 } |
336 | 399 |
337 return ret; | 400 return ret; |
338 } | 401 } |
339 | 402 |
340 | 403 |
341 bool | 404 bool |
342 IsNumber(const string& str) | 405 IsNumber (const string& s) |
343 { | 406 { |
344 int numPoints = 0; | 407 char *endp; |
345 | 408 double unused; |
346 if (str.length() == 0) return false; // Empty strings are not numbers | 409 unused = strtod (s.c_str (), &endp); // declared with warn_unused_result |
347 | 410 return endp == s.c_str () + s.size (); |
348 for (unsigned int i = 0; i < str.size(); i++) | 411 } |
349 { | 412 |
350 | 413 |
351 if ( !(isdigit(str[i])) && !(str[i] == '.') ) | 414 template<class T> |
352 { | 415 bool IsVal (const string& str, T& ret) |
353 » return false; // If is not digit or dot, return false | 416 { |
354 } | 417 if (str.size () == 0) |
355 else | |
356 { | |
357 if (str[i] == '.') | |
358 { | |
359 » numPoints++; // if it is a dot character, increment number of points | |
360 } | |
361 } | |
362 } | |
363 | |
364 // If the string has more than one dot character, return false | |
365 if (numPoints > 1) | |
366 { | 418 { |
367 return false; | 419 return false; |
368 } | 420 } |
421 else if (IsNumber (str)) | |
422 { | |
423 string s2 = str; | |
424 istringstream s (s2); | |
425 s >> ret; | |
426 return true; | |
427 } | |
369 else | 428 else |
370 { | 429 { |
371 return true; | 430 return false; |
372 } | 431 } |
373 } | |
374 | |
375 | |
376 string | |
377 Num(const string& str) | |
378 { | |
379 string ret; | |
380 | |
381 // There is a '-' not at firs position | |
382 if ((str[0] != '-') && (str.find('-', 0) != string::npos)) | |
383 return ret; | |
Mathieu Lacage
2010/04/12 12:34:11
if you run this through check-style.py --level=3,
| |
384 | |
385 // if starts with "-", but it has more than one "-" returns | |
386 if ((str[0] == '-') && (str.find('-', 1) != string::npos)) | |
387 return ret; | |
388 | |
389 // generate string | |
390 for (unsigned int i = 0; i < str.size(); i++) | |
391 { | |
392 if (isdigit(str[i]) || str[i] == '.' || str[i] == '-') ret += str[i]; | |
393 } | |
394 | |
395 return ret; | |
396 | |
397 } | |
398 | |
399 | |
400 template<class T> | |
401 bool IsVal(const string& str, T& ret) | |
402 { | |
403 string s2 = Num(str); | |
404 if (s2.size() == 0) return false; | |
405 istringstream s(s2); | |
406 s >> ret; | |
407 return true; | |
408 } | 432 } |
409 | 433 |
410 | 434 |
411 bool | 435 bool |
412 hasNodeIdNumber(string str) | 436 HasNodeIdNumber (string str) |
413 { | 437 { |
414 | 438 |
415 //find brackets | 439 // find brackets |
416 std::string::size_type startNodeId = str.find_first_of ("("); // index of left bracket | 440 std::string::size_type startNodeId = str.find_first_of ("("); // index of left bracket |
417 std::string::size_type endNodeId = str.find_first_of (")"); // index of righ t bracket | 441 std::string::size_type endNodeId = str.find_first_of (")"); // index of righ t bracket |
418 | 442 |
419 // Get de nodeId in a string and in a int value | 443 // Get de nodeId in a string and in a int value |
420 std::string nodeId; // node id | 444 std::string nodeId; // node id |
421 | 445 |
422 //if no brackets, continue! | 446 // if no brackets, continue! |
423 if (startNodeId == std::string::npos || endNodeId == std::string::npos) | 447 if (startNodeId == std::string::npos || endNodeId == std::string::npos) |
424 { | 448 { |
Mathieu Lacage
2010/04/12 12:34:11
coding style: missing indent
| |
425 return false; | 449 return false; |
426 } | 450 } |
427 | 451 |
428 nodeId = str.substr(startNodeId + 1, endNodeId - (startNodeId + 1)); // set no de id | 452 nodeId = str.substr (startNodeId + 1, endNodeId - (startNodeId + 1)); // set n ode id |
429 | 453 |
430 NS_LOG_DEBUG("NODEID SACADO DE LA LINEA: " << nodeId); | 454 // is number is integer i s not negative |
431 | 455 if (IsNumber (nodeId) && (nodeId.find_first_of (".") == std::string::npos) && (nodeId[0] != '-')) |
432 if (IsNumber(nodeId) && (nodeId.find_first_of(".") == std::string::npos)) | |
433 { | 456 { |
434 return true; | 457 return true; |
435 } | 458 } |
436 else | 459 else |
437 { | 460 { |
438 return false; | 461 return false; |
439 } | 462 } |
440 } | 463 } |
441 | 464 |
442 | 465 |
466 string | |
467 GetNodeIdFromToken (string str) | |
468 { | |
469 if (HasNodeIdNumber (str)) | |
470 { | |
471 // find brackets | |
472 std::string::size_type startNodeId = str.find_first_of ("("); // index of left bracket | |
473 std::string::size_type endNodeId = str.find_first_of (")"); // index of right bracket | |
474 | |
475 return str.substr (startNodeId + 1, endNodeId - (startNodeId + 1)); // set node id | |
476 } | |
477 else | |
478 { | |
479 return ""; | |
480 } | |
481 } | |
482 | |
483 | |
443 int | 484 int |
444 GetNodeIdInt(ParseResult pr) | 485 GetNodeIdInt (ParseResult pr) |
445 { | 486 { |
446 switch (pr.tokens.size()) | 487 switch (pr.tokens.size ()) |
447 { | 488 { |
448 case 4: // line like $node_(0) set X_ 11 | 489 case 4: // line like $node_(0) set X_ 11 |
449 return pr.ivals[0]; | 490 return pr.ivals[0]; |
450 » » break; | 491 break; |
451 case 7: // line like $ns_ at 4 "$node_(0) set X_ 28" | 492 case 7: // line like $ns_ at 4 "$node_(0) set X_ 28" |
452 return pr.ivals[3]; | 493 return pr.ivals[3]; |
453 case 8: // line like $ns_ at 1 "$node_(0) setdest 2 3 4" | 494 break; |
454 return pr.ivals[3]; | 495 case 8: // line like $ns_ at 1 "$node_(0) setdest 2 3 4" |
455 » default: | 496 return pr.ivals[3]; |
456 return -1; | 497 break; |
498 default: | |
499 return -1; | |
457 } | 500 } |
458 } | 501 } |
459 | 502 |
460 // Get node id number in string format | 503 // Get node id number in string format |
461 string | 504 string |
462 GetNodeIdString(ParseResult pr) | 505 GetNodeIdString (ParseResult pr) |
463 { | 506 { |
464 switch (pr.tokens.size()) | 507 switch (pr.tokens.size ()) |
465 { | 508 { |
466 case 4: // line like $node_(0) set X_ 11 | 509 case 4: // line like $node_(0) set X_ 11 |
467 return pr.svals[0]; | 510 return pr.svals[0]; |
468 » » break; | 511 break; |
469 case 7: // line like $ns_ at 4 "$node_(0) set X_ 28" | 512 case 7: // line like $ns_ at 4 "$node_(0) set X_ 28" |
470 return pr.svals[3]; | 513 return pr.svals[3]; |
471 case 8: // line like $ns_ at 1 "$node_(0) setdest 2 3 4" | 514 break; |
472 return pr.svals[3]; | 515 case 8: // line like $ns_ at 1 "$node_(0) setdest 2 3 4" |
473 » default: | 516 return pr.svals[3]; |
474 return ""; | 517 break; |
518 default: | |
519 return ""; | |
475 } | 520 } |
476 } | 521 } |
477 | 522 |
478 | 523 |
479 Vector | 524 Vector |
480 SetOneInitialCoord(Vector position, string& coord, double value) | 525 SetOneInitialCoord (Vector position, string& coord, double value) |
481 { | 526 { |
482 | 527 |
483 //set the position for the coord. | 528 // set the position for the coord. |
484 if (coord == "X_") | 529 if (coord == NS2_X_COORD) |
485 { | 530 { |
486 » position.x = value; | 531 position.x = value; |
487 NS_LOG_DEBUG ("X=" << value); | 532 NS_LOG_DEBUG ("X=" << value); |
488 } | 533 } |
489 else if (coord == "Y_") | 534 else if (coord == NS2_Y_COORD) |
490 { | 535 { |
491 position.y = value; | 536 position.y = value; |
492 NS_LOG_DEBUG ("Y=" << value); | 537 NS_LOG_DEBUG ("Y=" << value); |
493 } | 538 } |
494 else if (coord == "Z_") | 539 else if (coord == NS2_Z_COORD) |
495 { | 540 { |
496 position.z = value; | 541 position.z = value; |
497 NS_LOG_DEBUG ("Z=" << value); | 542 NS_LOG_DEBUG ("Z=" << value); |
498 } | 543 } |
499 return position; | 544 return position; |
500 } | 545 } |
501 | 546 |
502 | 547 |
503 bool | 548 bool |
504 IsSetInitialPos(ParseResult pr) | 549 IsSetInitialPos (ParseResult pr) |
505 { | 550 { |
506 | 551 // number of tokens has $node_( ? has "s et" has doble for position? |
507 if (pr.tokens.size() != 4 && pr.tokens[0].find("$node_(") != 0 && pr.tokens[1] != "set" && !pr.has_dval[3]) | 552 return pr.tokens.size () == 4 && HasNodeIdNumber (pr.tokens[0]) && pr.tokens[1 ] == NS2_SET && pr.has_dval[3] |
508 { | 553 // coord name is X_, Y_ or Z_ ? |
509 » return false; | 554 && (pr.tokens[2] == NS2_X_COORD || pr.tokens[2] == NS2_Y_COORD || pr.to kens[2] == NS2_Z_COORD); |
510 } | |
511 | |
512 if (pr.tokens[2] != "X_" && pr.tokens[2] != "Y_" && pr.tokens[2] != "Z_") | |
513 { | |
514 » NS_LOG_WARN ("Not a correct coord: " << pr.tokens[2]); | |
515 » return false; | |
516 } | |
517 return true; | |
518 | 555 |
519 } | 556 } |
520 | 557 |
521 | 558 |
522 bool | 559 bool |
523 IsSchedSetPos(ParseResult pr) | 560 IsSchedSetPos (ParseResult pr) |
524 { | 561 { |
525 if (pr.tokens.size() == 7 && pr.tokens[0] == "$ns_" && pr.tokens[1] == "at" | 562 // correct number of tokens, has $ns_ and at |
526 && pr.tokens[4] == "set" && pr.has_dval[2] | 563 return pr.tokens.size () == 7 && pr.tokens[0] == NS2_NS_SCH && pr.tokens[1] == NS2_AT |
527 » && ( pr.tokens[5] == "X_" || pr.tokens[5] == "Y_" || pr.tokens[5] == "Z_") | 564 && pr.tokens[4] == NS2_SET && pr.has_dval[2] && pr.has_dval[3] // has set and double value for time and nodeid |
528 » && IsNumber(pr.tokens[2]) && pr.has_dval[3]) | 565 && ( pr.tokens[5] == NS2_X_COORD || pr.tokens[5] == NS2_Y_COORD || pr.t okens[5] == NS2_Z_COORD) // has X_, Y_ or Z_? |
529 { | 566 && pr.has_dval[2]; // time is a number |
530 return true; | |
531 } | |
532 | |
533 » return false; | |
534 } | 567 } |
535 | 568 |
536 bool | 569 bool |
537 IsSchedMobilityPos(ParseResult pr) | 570 IsSchedMobilityPos (ParseResult pr) |
538 { | 571 { |
539 if (!IsNumber(pr.tokens[2]) || !IsNumber(pr.tokens[5]) | 572 // number of tokens and has $ns_ and has at |
540 || !IsNumber(pr.tokens[6]) || !IsNumber(pr.tokens[7])) | 573 return pr.tokens.size () == 8 && pr.tokens[0] == NS2_NS_SCH && pr.tokens[1] == NS2_AT |
541 { | 574 // time x coord y coord velocity are n umbers? |
542 NS_LOG_WARN("Time, x coor, y coord or speed are not a number"); | 575 && pr.has_dval[2] && pr.has_dval[5] && pr.has_dval[6] && pr.has_dval[7] |
543 return false; | 576 && pr.tokens[4] == NS2_SETDEST; // and has setdest |
544 } | |
545 | |
546 if (pr.tokens.size() == 8 && pr.tokens[0] == "$ns_" && pr.tokens[1] == "at" | |
547 » && pr.tokens[4] == "setdest" && pr.has_dval[2] && pr.has_dval[5] | |
548 && pr.has_dval[6] && pr.has_dval[7]) | |
549 { | |
550 return true; | |
551 } | |
552 | |
553 return false; | |
554 | |
555 | 577 |
556 } | 578 } |
557 | 579 |
558 Vector | 580 Vector |
559 SetMovement(Ptr<ConstantVelocityMobilityModel> model, Vector last_pos, double at , | 581 SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector last_pos, double a t, |
560 double xFinalPosition, double yFinalPosition, double speed) | 582 double xFinalPosition, double yFinalPosition, double speed) |
561 { | 583 { |
562 Vector position; | 584 Vector position; |
563 position.x = last_pos.x; | 585 position.x = last_pos.x; |
564 position.y = last_pos.y; | 586 position.y = last_pos.y; |
565 position.z = last_pos.z; | 587 position.z = last_pos.z; |
566 | 588 |
567 if (speed == 0) | 589 if (speed == 0) |
568 { | 590 { |
569 // We have to maintain last position, and stop the movement | 591 // We have to maintain last position, and stop the movement |
570 Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelo city, model, | 592 Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelo city, model, |
571 Vector (0, 0, 0)); | 593 Vector (0, 0, 0)); |
572 } | 594 } |
573 else if (speed > 0) | 595 else if (speed > 0) |
574 { | 596 { |
575 //first calculate the time; time = distance / speed | 597 // first calculate the time; time = distance / speed |
576 double time = sqrt (pow (xFinalPosition-position.x, 2) + pow (yFinalPositi on-position.y, 2))/speed; | 598 double time = sqrt (pow (xFinalPosition - position.x, 2) + pow (yFinalPosi tion - position.y, 2)) / speed; |
577 NS_LOG_DEBUG ("at=" << at << " time=" << time); | 599 NS_LOG_DEBUG ("at=" << at << " time=" << time); |
578 //now calculate the xSpeed = distance / time | 600 // now calculate the xSpeed = distance / time |
579 double xSpeed = (xFinalPosition-position.x) / time; | 601 double xSpeed = (xFinalPosition - position.x) / time; |
580 double ySpeed = (yFinalPosition-position.y) / time; //& same with ySpeed | 602 double ySpeed = (yFinalPosition - position.y) / time; // & same with ySpee d |
581 | 603 |
582 //quick and dirty set zSpeed = 0 | 604 // quick and dirty set zSpeed = 0 |
583 double zSpeed = 0; | 605 double zSpeed = 0; |
584 | 606 |
585 NS_LOG_DEBUG ("Calculated Speed: X=" << xSpeed << " Y=" << ySpeed << " Z=" << zSpeed); | 607 NS_LOG_DEBUG ("Calculated Speed: X=" << xSpeed << " Y=" << ySpeed << " Z=" << zSpeed); |
586 | 608 |
587 //Set the Values | 609 // Set the Values |
588 Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelo city, model, | 610 Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelo city, model, |
589 Vector (xSpeed, ySpeed, zSpeed)); | 611 Vector (xSpeed, ySpeed, zSpeed)); |
590 | 612 |
591 if (time >= 0) | 613 if (time >= 0) |
592 { | 614 { |
593 Simulator::Schedule(Seconds(at + time), &ConstantVelocityMobilityModel ::SetVelocity, | 615 Simulator::Schedule (Seconds (at + time), &ConstantVelocityMobilityMod el::SetVelocity, |
594 model, Vector(0, 0, 0)); | 616 model, Vector (0, 0, 0)); |
595 } | 617 } |
596 | 618 |
597 position.x = xFinalPosition; | 619 position.x = xFinalPosition; |
598 position.y = yFinalPosition; | 620 position.y = yFinalPosition; |
599 position.z = 0; | 621 position.z = 0; |
600 } | 622 } |
601 | 623 |
602 return position; | 624 return position; |
603 } | 625 } |
604 | 626 |
605 | 627 |
606 Vector | 628 Vector |
607 SetInitialPosition(Ptr<ConstantVelocityMobilityModel> model, string coord, doubl e coordVal) | 629 SetInitialPosition (Ptr<ConstantVelocityMobilityModel> model, string coord, doub le coordVal) |
608 { | 630 { |
609 model->SetPosition (SetOneInitialCoord(model->GetPosition () , coord, coordVal )); | 631 model->SetPosition (SetOneInitialCoord (model->GetPosition (), coord, coordVal )); |
610 | 632 |
611 Vector position; | 633 Vector position; |
612 position.x = model->GetPosition ().x; | 634 position.x = model->GetPosition ().x; |
613 position.y = model->GetPosition ().y; | 635 position.y = model->GetPosition ().y; |
614 position.z = model->GetPosition ().z; | 636 position.z = model->GetPosition ().z; |
615 | 637 |
616 return position; | 638 return position; |
617 } | 639 } |
618 | 640 |
619 // Schedule a set of position for a node | 641 // Schedule a set of position for a node |
620 Vector | 642 Vector |
621 SetSchedPosition(Ptr<ConstantVelocityMobilityModel> model, double at, string coo rd, double coordVal) | 643 SetSchedPosition (Ptr<ConstantVelocityMobilityModel> model, double at, string co ord, double coordVal) |
622 { | 644 { |
623 // update position | 645 // update position |
624 model->SetPosition (SetOneInitialCoord(model->GetPosition (), coord, coordVal) ); | 646 model->SetPosition (SetOneInitialCoord (model->GetPosition (), coord, coordVal )); |
625 | 647 |
626 Vector position; | 648 Vector position; |
627 position.x = model->GetPosition ().x; | 649 position.x = model->GetPosition ().x; |
628 position.y = model->GetPosition ().y; | 650 position.y = model->GetPosition ().y; |
629 position.z = model->GetPosition ().z; | 651 position.z = model->GetPosition ().z; |
630 | 652 |
631 // Chedule next positions | 653 // Chedule next positions |
632 Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetPosition , model,position); | 654 Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetPosition , model,position); |
633 | 655 |
634 return position; | 656 return position; |
635 } | 657 } |
636 | 658 |
637 void | 659 void |
638 Ns2TransmobilityHelper::Install (void) const | 660 Ns2TransmobilityHelper::Install (void) const |
639 { | 661 { |
640 Install (NodeList::Begin (), NodeList::End ()); | 662 Install (NodeList::Begin (), NodeList::End ()); |
641 } | 663 } |
642 | 664 |
643 //----------------------------------------------------------------------------- | 665 // ----------------------------------------------------------------------------- |
644 // Testing | 666 // Testing |
645 //----------------------------------------------------------------------------- | 667 // ----------------------------------------------------------------------------- |
646 bool operator==(Vector const & a, Vector const & b) | 668 bool operator== (Vector const & a, Vector const & b) |
647 { | 669 { |
648 return (a.x == b.x && a.y == b.y && a.z == b.z); | 670 return (a.x == b.x && a.y == b.y && a.z == b.z); |
649 } | 671 } |
650 /** | 672 /** |
651 * Every test case is supposed to: | 673 * Every test case is supposed to: |
652 * 1. Generate short mobility trace file | 674 * 1. Generate short mobility trace file |
653 * 2. Read it back using Ns2TransmobilityHelper | 675 * 2. Read it back using Ns2TransmobilityHelper |
654 * 3. Check initial node positions and speeds. | 676 * 3. Check initial node positions and speeds. |
655 * 4. Run simulation listening for all CourseChange events and compare actual m obility with the reference | 677 * 4. Run simulation listening for all CourseChange events and compare actual m obility with the reference |
656 */ | 678 */ |
657 class Ns2TransmobilityHelperTest : public TestCase | 679 class Ns2TransmobilityHelperTest : public TestCase |
658 { | 680 { |
659 public: | 681 public: |
660 /// Single record in mobility reference | 682 /// Single record in mobility reference |
661 struct ReferencePoint | 683 struct ReferencePoint |
662 { | 684 { |
663 std::string node; ///< node ID as string, e.g. "1" | 685 std::string node; ///< node ID as string, e.g. "1" |
664 Time time; ///< timestamp | 686 Time time; ///< timestamp |
665 Vector pos; ///< reference position | 687 Vector pos; ///< reference position |
666 Vector vel; ///< reference velocity | 688 Vector vel; ///< reference velocity |
667 | 689 |
668 ReferencePoint (std::string const & id, Time t, Vector const & p, Vector con st & v) : | 690 ReferencePoint (std::string const & id, Time t, Vector const & p, Vector con st & v) |
669 node (id), time (t), pos (p), vel (v) | 691 : node (id), |
692 time (t), | |
693 pos (p), | |
694 vel (v) | |
670 { | 695 { |
671 } | 696 } |
672 /// Sort by timestamp | 697 /// Sort by timestamp |
673 bool operator<(ReferencePoint const & o) const | 698 bool operator< (ReferencePoint const & o) const |
674 { | 699 { |
675 return (time < o.time); | 700 return (time < o.time); |
676 } | 701 } |
677 }; | 702 }; |
678 /** | 703 /** |
679 * Create new test case. To make it useful SetTrace () and AddReferencePoint ( ) must be called | 704 * Create new test case. To make it useful SetTrace () and AddReferencePoint ( ) must be called |
680 * | 705 * |
681 * \param name Short description | 706 * \param name Short description |
682 * \param nodes Number of nodes used in the test trace, 1 by default | 707 * \param nodes Number of nodes used in the test trace, 1 by default |
683 */ | 708 */ |
684 Ns2TransmobilityHelperTest (std::string const & name, Time timeLimit, uint32_t nodes = 1) : | 709 Ns2TransmobilityHelperTest (std::string const & name, Time timeLimit, uint32_t nodes = 1) |
685 TestCase (name), m_timeLimit (timeLimit), m_nodeCount (nodes), m_nextRefPoin t (0) | 710 : TestCase (name), |
711 m_timeLimit (timeLimit), | |
712 m_nodeCount (nodes), | |
713 m_nextRefPoint (0) | |
686 { | 714 { |
687 } | 715 } |
688 /// Empty | 716 /// Empty |
689 virtual ~Ns2TransmobilityHelperTest () | 717 virtual ~Ns2TransmobilityHelperTest () |
690 { | 718 { |
691 } | 719 } |
692 /// Set NS-2 trace to read as single large string (don't forget to add \n and quote ") | 720 /// Set NS-2 trace to read as single large string (don't forget to add \n and quote ") |
693 void SetTrace (std::string const & trace) | 721 void SetTrace (std::string const & trace) |
694 { | 722 { |
695 m_trace = trace; | 723 m_trace = trace; |
696 } | 724 } |
697 /// Add next reference point | 725 /// Add next reference point |
698 void AddReferencePoint (ReferencePoint const & r) | 726 void AddReferencePoint (ReferencePoint const & r) |
699 { | 727 { |
700 m_reference.push_back (r); | 728 m_reference.push_back (r); |
701 } | 729 } |
702 /// Sugar | 730 /// Sugar |
703 void AddReferencePoint (const char * id, double sec, Vector const & p, Vector const & v) | 731 void AddReferencePoint (const char * id, double sec, Vector const & p, Vector const & v) |
704 { | 732 { |
705 AddReferencePoint (ReferencePoint (id, Seconds (sec), p, v)); | 733 AddReferencePoint (ReferencePoint (id, Seconds (sec), p, v)); |
706 } | 734 } |
707 | 735 |
708 private: | 736 private: |
709 /// Test time limit | 737 /// Test time limit |
710 Time m_timeLimit; | 738 Time m_timeLimit; |
711 /// Number of nodes used in the test | 739 /// Number of nodes used in the test |
712 uint32_t m_nodeCount; | 740 uint32_t m_nodeCount; |
713 /// Trace as string | 741 /// Trace as string |
714 std::string m_trace; | 742 std::string m_trace; |
715 /// Reference mobility | 743 /// Reference mobility |
716 std::vector<ReferencePoint> m_reference; | 744 std::vector<ReferencePoint> m_reference; |
717 /// Next reference point to be checked | 745 /// Next reference point to be checked |
718 size_t m_nextRefPoint; | 746 size_t m_nextRefPoint; |
719 /// TMP trace file name | 747 /// TMP trace file name |
720 std::string m_traceFile; | 748 std::string m_traceFile; |
721 | 749 |
722 private: | 750 private: |
723 /// Dump NS-2 trace to tmp file | 751 /// Dump NS-2 trace to tmp file |
724 bool WriteTrace () | 752 bool WriteTrace () |
725 { | 753 { |
726 m_traceFile = GetTempDir () + "Ns2TransmobilityHelperTest.tcl"; | 754 m_traceFile = GetTempDir () + "Ns2TransmobilityHelperTest.tcl"; |
727 std::ofstream of (m_traceFile.c_str ()); | 755 std::ofstream of (m_traceFile.c_str ()); |
728 NS_TEST_ASSERT_MSG_EQ (of.is_open (), true, "Need to write tmp. file"); | 756 NS_TEST_ASSERT_MSG_EQ (of.is_open (), true, "Need to write tmp. file"); |
729 of << m_trace; | 757 of << m_trace; |
730 of.close (); | 758 of.close (); |
731 return false; // no errors | 759 return false; // no errors |
732 } | 760 } |
733 /// Create and name nodes | 761 /// Create and name nodes |
734 void CreateNodes () | 762 void CreateNodes () |
735 { | 763 { |
736 NodeContainer nodes; | 764 NodeContainer nodes; |
737 nodes.Create (m_nodeCount); | 765 nodes.Create (m_nodeCount); |
738 for (uint32_t i = 0; i < m_nodeCount; ++i) | 766 for (uint32_t i = 0; i < m_nodeCount; ++i) |
739 { | 767 { |
740 std::ostringstream os; | 768 std::ostringstream os; |
741 os << i; | 769 os << i; |
742 Names::Add (os.str(), nodes.Get (i)); | 770 Names::Add (os.str (), nodes.Get (i)); |
743 } | 771 } |
744 } | 772 } |
745 /// Check that all initial positions are correct | 773 /// Check that all initial positions are correct |
746 bool CheckInitialPositions () | 774 bool CheckInitialPositions () |
747 { | 775 { |
748 std::stable_sort (m_reference.begin (), m_reference.end ()); | 776 std::stable_sort (m_reference.begin (), m_reference.end ()); |
749 while (m_nextRefPoint < m_reference.size () && m_reference[m_nextRefPoint].t ime == Seconds (0)) | 777 while (m_nextRefPoint < m_reference.size () && m_reference[m_nextRefPoint].t ime == Seconds (0)) |
750 { | 778 { |
751 ReferencePoint const & rp = m_reference[m_nextRefPoint]; | 779 ReferencePoint const & rp = m_reference[m_nextRefPoint]; |
752 Ptr<Node> node = Names::Find<Node> (rp.node); | 780 Ptr<Node> node = Names::Find<Node> (rp.node); |
753 NS_TEST_ASSERT_MSG_NE (node, 0, "Can't find node with id " << rp.node); | 781 NS_TEST_ASSERT_MSG_NE (node, 0, "Can't find node with id " << rp.node); |
754 Ptr<MobilityModel> mob = node->GetObject<MobilityModel> (); | 782 Ptr<MobilityModel> mob = node->GetObject<MobilityModel> (); |
755 NS_TEST_ASSERT_MSG_NE (mob, 0, "Can't find mobility for node " << rp.nod e); | 783 NS_TEST_ASSERT_MSG_NE (mob, 0, "Can't find mobility for node " << rp.nod e); |
756 | 784 |
757 NS_TEST_EXPECT_MSG_EQ (rp.pos, mob->GetPosition (), "Initial position mi smatch for node " << rp.node); | 785 NS_TEST_EXPECT_MSG_EQ (rp.pos, mob->GetPosition (), "Initial position mi smatch for node " << rp.node); |
758 NS_TEST_EXPECT_MSG_EQ (rp.vel, mob->GetVelocity (), "Initial velocity mi smatch for node " << rp.node); | 786 NS_TEST_EXPECT_MSG_EQ (rp.vel, mob->GetVelocity (), "Initial velocity mi smatch for node " << rp.node); |
759 | 787 |
760 m_nextRefPoint ++; | 788 m_nextRefPoint++; |
761 } | 789 } |
762 return GetErrorStatus (); | 790 return GetErrorStatus (); |
763 } | 791 } |
764 /// Listen for course change events | 792 /// Listen for course change events |
765 void CourseChange (std::string context, Ptr<const MobilityModel> mobility) | 793 void CourseChange (std::string context, Ptr<const MobilityModel> mobility) |
766 { | 794 { |
767 Time time = Simulator::Now (); | 795 Time time = Simulator::Now (); |
768 Ptr<Node> node = mobility->GetObject<Node> (); | 796 Ptr<Node> node = mobility->GetObject<Node> (); |
769 NS_ASSERT (node); | 797 NS_ASSERT (node); |
770 std::string id = Names::FindName (node); | 798 std::string id = Names::FindName (node); |
771 NS_ASSERT (! id.empty ()); | 799 NS_ASSERT (!id.empty ()); |
772 Vector pos = mobility->GetPosition (); | 800 Vector pos = mobility->GetPosition (); |
773 Vector vel = mobility->GetVelocity (); | 801 Vector vel = mobility->GetVelocity (); |
774 | 802 |
775 NS_TEST_EXPECT_MSG_LT (m_nextRefPoint, m_reference.size (), "Not enough refe rence points"); | 803 NS_TEST_EXPECT_MSG_LT (m_nextRefPoint, m_reference.size (), "Not enough refe rence points"); |
776 if (m_nextRefPoint >= m_reference.size ()) return; | 804 if (m_nextRefPoint >= m_reference.size ()) |
777 ···· | 805 { |
778 ReferencePoint const & ref = m_reference [m_nextRefPoint ++]; | 806 return; |
807 } | |
808 | |
809 ReferencePoint const & ref = m_reference [m_nextRefPoint++]; | |
779 NS_TEST_EXPECT_MSG_EQ (time, ref.time, "Time mismatch"); | 810 NS_TEST_EXPECT_MSG_EQ (time, ref.time, "Time mismatch"); |
780 NS_TEST_EXPECT_MSG_EQ (id, ref.node, "Node ID mismatch at time " << time.Get Seconds () << " s"); | 811 NS_TEST_EXPECT_MSG_EQ (id, ref.node, "Node ID mismatch at time " << time.Get Seconds () << " s"); |
781 NS_TEST_EXPECT_MSG_EQ (pos, ref.pos, "Position mismatch at time " << time.Ge tSeconds () << " s for node " << id); | 812 NS_TEST_EXPECT_MSG_EQ (pos, ref.pos, "Position mismatch at time " << time.Ge tSeconds () << " s for node " << id); |
782 NS_TEST_EXPECT_MSG_EQ (vel, ref.vel, "Velocity mismatch at time " << time.Ge tSeconds () << " s for node " << id); | 813 NS_TEST_EXPECT_MSG_EQ (vel, ref.vel, "Velocity mismatch at time " << time.Ge tSeconds () << " s for node " << id); |
783 } | 814 } |
784 /// Go | 815 /// Go |
785 bool DoRun () | 816 bool DoRun () |
786 { | 817 { |
787 NS_TEST_ASSERT_MSG_EQ (m_trace.empty (), false, "Need trace"); | 818 NS_TEST_ASSERT_MSG_EQ (m_trace.empty (), false, "Need trace"); |
788 NS_TEST_ASSERT_MSG_EQ (m_reference.empty (), false, "Need reference"); | 819 NS_TEST_ASSERT_MSG_EQ (m_reference.empty (), false, "Need reference"); |
789 | 820 |
790 if (WriteTrace ()) return GetErrorStatus (); | 821 if (WriteTrace ()) |
822 { | |
823 return GetErrorStatus (); | |
824 } | |
791 CreateNodes (); | 825 CreateNodes (); |
792 Ns2TransmobilityHelper mobility (m_traceFile); | 826 Ns2TransmobilityHelper mobility (m_traceFile); |
793 mobility.Install (); | 827 mobility.Install (); |
794 if (CheckInitialPositions ()) return GetErrorStatus (); | 828 if (CheckInitialPositions ()) |
795 Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange", | 829 { |
796 MakeCallback (& Ns2TransmobilityHelperTest::CourseChange, t his)); | 830 return GetErrorStatus (); |
831 } | |
832 Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange", | |
833 MakeCallback (&Ns2TransmobilityHelperTest::CourseChange, th is)); | |
797 Simulator::Stop (m_timeLimit); | 834 Simulator::Stop (m_timeLimit); |
798 Simulator::Run (); | 835 Simulator::Run (); |
799 Names::Clear (); | 836 Names::Clear (); |
800 std::remove (m_traceFile.c_str ()); | 837 std::remove (m_traceFile.c_str ()); |
801 Simulator::Destroy (); | 838 Simulator::Destroy (); |
802 return GetErrorStatus (); | 839 return GetErrorStatus (); |
803 } | 840 } |
804 }; | 841 }; |
805 | 842 |
806 /// The test suite | 843 /// The test suite |
807 class Ns2TransmobilityHelperTestSuite : public TestSuite | 844 class Ns2TransmobilityHelperTestSuite : public TestSuite |
808 { | 845 { |
809 public: | 846 public: |
810 Ns2TransmobilityHelperTestSuite () : TestSuite ("mobility-ns2-trace-helper", U NIT) | 847 Ns2TransmobilityHelperTestSuite () : TestSuite ("mobility-ns2-trace-helper", U NIT) |
811 { | 848 { |
812 // to be used as temporary variable for test cases. | 849 // to be used as temporary variable for test cases. |
813 » //Note that test suite takes care of deleting all test cases. | 850 // Note that test suite takes care of deleting all test cases. |
814 Ns2TransmobilityHelperTest * t (0); | 851 Ns2TransmobilityHelperTest * t (0); |
815 | 852 |
816 // Initial position | 853 // Initial position |
817 t = new Ns2TransmobilityHelperTest ("initial position", Seconds (1)); | 854 t = new Ns2TransmobilityHelperTest ("initial position", Seconds (1)); |
818 t->SetTrace ("$node_(0) set X_ 1.0\n" | 855 t->SetTrace ("$node_(0) set X_ 1.0\n" |
819 "$node_(0) set Y_ 2.0\n" | 856 "$node_(0) set Y_ 2.0\n" |
820 "$node_(0) set Z_ 3.0\n" | 857 "$node_(0) set Z_ 3.0\n" |
821 ); | 858 ); |
822 t->AddReferencePoint ("0", 0, Vector(1, 2, 3), Vector(0, 0, 0)); | 859 t->AddReferencePoint ("0", 0, Vector (1, 2, 3), Vector (0, 0, 0)); |
823 AddTestCase (t); | 860 AddTestCase (t); |
824 | 861 |
825 // Check parsing comments, empty lines and no EOF at the end of file | 862 // Check parsing comments, empty lines and no EOF at the end of file |
826 t = new Ns2TransmobilityHelperTest ("comments", Seconds (1)); | 863 t = new Ns2TransmobilityHelperTest ("comments", Seconds (1)); |
827 t->SetTrace ("# comment\n" | 864 t->SetTrace ("# comment\n" |
828 "\n\n" // empty lines | 865 "\n\n" // empty lines |
829 "$node_(0) set X_ 1.0 # comment \n" | 866 "$node_(0) set X_ 1.0 # comment \n" |
830 "$node_(0) set Y_ 2.0 ### \n" | 867 "$node_(0) set Y_ 2.0 ### \n" |
831 "$node_(0) set Z_ 3.0 # $node_(0) set Z_ 3.0\n" | 868 "$node_(0) set Z_ 3.0 # $node_(0) set Z_ 3.0\n" |
832 "#$node_(0) set Z_ 100 #" | 869 "#$node_(0) set Z_ 100 #" |
833 ); | 870 ); |
834 t->AddReferencePoint ("0", 0, Vector(1, 2, 3), Vector(0, 0, 0)); | 871 t->AddReferencePoint ("0", 0, Vector (1, 2, 3), Vector (0, 0, 0)); |
835 AddTestCase (t); | 872 AddTestCase (t); |
836 | 873 |
837 // Simple setdest. Arguments are interpreted as x, y, speed by default | 874 // Simple setdest. Arguments are interpreted as x, y, speed by default |
838 t = new Ns2TransmobilityHelperTest ("simple setdest", Seconds (10)); | 875 t = new Ns2TransmobilityHelperTest ("simple setdest", Seconds (10)); |
839 t->SetTrace ("$ns_ at 1.0 \"$node_(0) setdest 25 0 5\""); | 876 t->SetTrace ("$ns_ at 1.0 \"$node_(0) setdest 25 0 5\""); |
840 // id t position velocity | 877 // id t position velocity |
841 t->AddReferencePoint ("0", 0, Vector(0, 0, 0), Vector(0, 0, 0)); | 878 t->AddReferencePoint ("0", 0, Vector (0, 0, 0), Vector (0, 0, 0)); |
842 t->AddReferencePoint ("0", 1, Vector(0, 0, 0), Vector(5, 0, 0)); | 879 t->AddReferencePoint ("0", 1, Vector (0, 0, 0), Vector (5, 0, 0)); |
843 t->AddReferencePoint ("0", 6, Vector(25, 0, 0), Vector(0, 0, 0)); | 880 t->AddReferencePoint ("0", 6, Vector (25, 0, 0), Vector (0, 0, 0)); |
844 AddTestCase (t); | 881 AddTestCase (t); |
845 | 882 |
846 // Several set and setdest. Arguments are interpreted as x, y, speed by defa ult | 883 // Several set and setdest. Arguments are interpreted as x, y, speed by defa ult |
847 t = new Ns2TransmobilityHelperTest ("square setdest", Seconds (6)); | 884 t = new Ns2TransmobilityHelperTest ("square setdest", Seconds (6)); |
848 t->SetTrace ("$node_(0) set X_ 0.0\n" | 885 t->SetTrace ("$node_(0) set X_ 0.0\n" |
849 "$node_(0) set Y_ 0.0\n" | 886 "$node_(0) set Y_ 0.0\n" |
850 "$ns_ at 1.0 \"$node_(0) setdest 5 0 5\"\n" | 887 "$ns_ at 1.0 \"$node_(0) setdest 5 0 5\"\n" |
851 "$ns_ at 2.0 \"$node_(0) setdest 5 5 5\"\n" | 888 "$ns_ at 2.0 \"$node_(0) setdest 5 5 5\"\n" |
852 "$ns_ at 3.0 \"$node_(0) setdest 0 5 5\"\n" | 889 "$ns_ at 3.0 \"$node_(0) setdest 0 5 5\"\n" |
853 "$ns_ at 4.0 \"$node_(0) setdest 0 0 5\"\n" | 890 "$ns_ at 4.0 \"$node_(0) setdest 0 0 5\"\n" |
854 ); | 891 ); |
855 // id t position velocity | 892 // id t position velocity |
856 t->AddReferencePoint ("0", 0, Vector(0, 0, 0), Vector(0, 0, 0));· | 893 t->AddReferencePoint ("0", 0, Vector (0, 0, 0), Vector (0, 0, 0)); |
857 t->AddReferencePoint ("0", 1, Vector(0, 0, 0), Vector(5, 0, 0));·· | 894 t->AddReferencePoint ("0", 1, Vector (0, 0, 0), Vector (5, 0, 0)); |
858 t->AddReferencePoint ("0", 2, Vector(5, 0, 0), Vector(0, 0, 0)); | 895 t->AddReferencePoint ("0", 2, Vector (5, 0, 0), Vector (0, 0, 0)); |
859 t->AddReferencePoint ("0", 2, Vector(5, 0, 0), Vector(0, 5, 0)); | 896 t->AddReferencePoint ("0", 2, Vector (5, 0, 0), Vector (0, 5, 0)); |
860 t->AddReferencePoint ("0", 3, Vector(5, 5, 0), Vector(0, 0, 0)); | 897 t->AddReferencePoint ("0", 3, Vector (5, 5, 0), Vector (0, 0, 0)); |
861 t->AddReferencePoint ("0", 3, Vector(5, 5, 0), Vector(-5, 0, 0));·· | 898 t->AddReferencePoint ("0", 3, Vector (5, 5, 0), Vector (-5, 0, 0)); |
862 t->AddReferencePoint ("0", 4, Vector(0, 5, 0), Vector(0, 0, 0)); | 899 t->AddReferencePoint ("0", 4, Vector (0, 5, 0), Vector (0, 0, 0)); |
863 t->AddReferencePoint ("0", 4, Vector(0, 5, 0), Vector(0, -5, 0));·· | 900 t->AddReferencePoint ("0", 4, Vector (0, 5, 0), Vector (0, -5, 0)); |
864 t->AddReferencePoint ("0", 5, Vector(0, 0, 0), Vector(0, 0, 0)); | 901 t->AddReferencePoint ("0", 5, Vector (0, 0, 0), Vector (0, 0, 0)); |
865 AddTestCase (t); | 902 AddTestCase (t); |
866 | 903 |
867 // Scheduled set position | 904 // Scheduled set position |
868 t = new Ns2TransmobilityHelperTest ("scheduled set position", Seconds (2)); | 905 t = new Ns2TransmobilityHelperTest ("scheduled set position", Seconds (2)); |
869 t->SetTrace ("$ns_ at 1.0 \"$node_(0) set X_ 10\""); | 906 t->SetTrace ("$ns_ at 1.0 \"$node_(0) set X_ 10\"\n" |
907 "$ns_ at 1.0 \"$node_(0) set Z_ 10\"\n" | |
908 "$ns_ at 1.0 \"$node_(0) set Y_ 10\""); | |
870 // id t position velocity | 909 // id t position velocity |
871 t->AddReferencePoint ("0", 1, Vector(10, 0, 0), Vector(0, 0, 0));·· | 910 t->AddReferencePoint ("0", 1, Vector (10, 0, 0), Vector (0, 0, 0)); |
872 AddTestCase (t);········ | 911 t->AddReferencePoint ("0", 1, Vector (10, 0, 10), Vector (0, 0, 0)); |
873 ···· | 912 t->AddReferencePoint ("0", 1, Vector (10, 10, 10), Vector (0, 0, 0)); |
913 AddTestCase (t); | |
914 | |
874 // Malformed lines | 915 // Malformed lines |
875 t = new Ns2TransmobilityHelperTest ("malformed lines", Seconds (2)); | 916 t = new Ns2TransmobilityHelperTest ("malformed lines", Seconds (2)); |
876 t->SetTrace ("$node() set X_ 1 # node id is not present\n" | 917 t->SetTrace ("$node() set X_ 1 # node id is not present\n" |
877 "$node # incoplete line\"\n" | 918 "$node # incoplete line\"\n" |
878 "$node this line is not correct\n" | 919 "$node this line is not correct\n" |
879 "$node_(0) set X_ 1 # line OK \n" | 920 "$node_(0) set X_ 1 # line OK \n" |
880 "$node_(0) set Y_ 2 # line OK \n" | 921 "$node_(0) set Y_ 2 # line OK \n" |
881 "$node_(0) set Z_ 3 # line OK \n" | 922 "$node_(0) set Z_ 3 # line OK \n" |
882 "$ns_ at \"$node_(0) setdest 4 4 4\" # time not present\n" | 923 "$ns_ at \"$node_(0) setdest 4 4 4\" # time not present\n" |
883 "$ns_ at 1 \"$node_(0) setdest 2 2 1\" # line OK \n"); | 924 "$ns_ at 1 \"$node_(0) setdest 2 2 1 \" # line OK \n"); |
884 // id t position velocity | 925 // id t position velocity |
885 t->AddReferencePoint ("0", 0, Vector(1, 2, 3), Vector(0, 0, 0)); | 926 t->AddReferencePoint ("0", 0, Vector (1, 2, 3), Vector (0, 0, 0)); |
886 t->AddReferencePoint ("0", 1, Vector(1, 2, 3), Vector(1, 0, 0)); | 927 t->AddReferencePoint ("0", 1, Vector (1, 2, 3), Vector (1, 0, 0)); |
887 t->AddReferencePoint ("0", 2, Vector(2, 2, 3), Vector(0, 0, 0)); | 928 t->AddReferencePoint ("0", 2, Vector (2, 2, 3), Vector (0, 0, 0)); |
888 AddTestCase (t); | 929 AddTestCase (t); |
889 | 930 |
890 // Non possible values | 931 // Non possible values |
891 t = new Ns2TransmobilityHelperTest ("non possible values", Seconds (2)); | 932 t = new Ns2TransmobilityHelperTest ("non possible values", Seconds (2)); |
892 t->SetTrace ("$node_(0) set X_ 1 # line OK \n" | 933 t->SetTrace ("$node_(0) set X_ 1 # line OK \n" |
893 "$node_(0) set Y_ 2 # line OK \n" | 934 "$node_(0) set Y_ 2 # line OK \n" |
894 "$node_(0) set Z_ 3 # line OK \n" | 935 "$node_(0) set Z_ 3 # line OK \n" |
895 "$node_(-22) set Y_ 3 # node id not correct\n" | 936 "$node_(-22) set Y_ 3 # node id not correct\n" |
896 "$node_(3.3) set Y_ 1111 # node id not correct\n" | 937 "$node_(3.3) set Y_ 1111 # node id not correct\n" |
897 "$ns_ at sss \"$node_(0) setdest 5 5 5\" # time is not a number \n" | 938 "$ns_ at sss \"$node_(0) setdest 5 5 5\" # time is not a number \n" |
898 "$ns_ at 1 \"$node_(0) setdest 2 2 1\" # line OK \n" | 939 "$ns_ at 1 \"$node_(0) setdest 2 2 1\" # line OK \n" |
899 "$ns_ at 1 \"$node_(0) setdest 2 2 -1\" # negative speed is not correct\n" | 940 "$ns_ at 1 \"$node_(0) setdest 2 2 -1\" # negative speed is not correct\n" |
900 "$ns_ at 1 \"$node_(0) setdest 2 2 sdfs\" # speed is not a n umber\n" | 941 "$ns_ at 1 \"$node_(0) setdest 2 2 sdfs\" # speed is not a n umber\n" |
901 "$ns_ at 1 \"$node_(0) setdest 2 2 s232dfs\" # speed is not a n umber\n" | 942 "$ns_ at 1 \"$node_(0) setdest 2 2 s232dfs\" # speed is not a n umber\n" |
902 "$ns_ at 1 \"$node_(0) setdest 233 2.. s232dfs\" # more than one non numbers\n" | 943 "$ns_ at 1 \"$node_(0) setdest 233 2.. s232dfs\" # more than one non numbers\n" |
903 "$ns_ at -12 \"$node_(0) setdest 11 22 33\" # time should not b e negative\n"); | 944 "$ns_ at -12 \"$node_(0) setdest 11 22 33\" # time should not b e negative\n"); |
904 // id t position velocity | 945 // id t position velocity |
905 t->AddReferencePoint ("0", 0, Vector(1, 2, 3), Vector(0, 0, 0)); | 946 t->AddReferencePoint ("0", 0, Vector (1, 2, 3), Vector (0, 0, 0)); |
906 t->AddReferencePoint ("0", 1, Vector(1, 2, 3), Vector(1, 0, 0)); | 947 t->AddReferencePoint ("0", 1, Vector (1, 2, 3), Vector (1, 0, 0)); |
907 t->AddReferencePoint ("0", 2, Vector(2, 2, 3), Vector(0, 0, 0)); | 948 t->AddReferencePoint ("0", 2, Vector (2, 2, 3), Vector (0, 0, 0)); |
908 AddTestCase (t); | 949 AddTestCase (t); |
909 | 950 |
910 // More than one node | 951 // More than one node |
911 t = new Ns2TransmobilityHelperTest ("few nodes, combinations of set and setd est", Seconds (10), 3); | 952 t = new Ns2TransmobilityHelperTest ("few nodes, combinations of set and setd est", Seconds (10), 3); |
912 t->SetTrace ("$node_(0) set X_ 1.0\n" | 953 t->SetTrace ("$node_(0) set X_ 1.0\n" |
913 "$node_(0) set Y_ 2.0\n" | 954 "$node_(0) set Y_ 2.0\n" |
914 "$node_(0) set Z_ 3.0\n" | 955 "$node_(0) set Z_ 3.0\n" |
915 "$ns_ at 1.0 \"$node_(1) setdest 25 0 5\"\n" | 956 "$ns_ at 1.0 \"$node_(1) setdest 25 0 5\"\n" |
916 "$node_(2) set X_ 0.0\n" | 957 "$node_(2) set X_ 0.0\n" |
917 "$node_(2) set Y_ 0.0\n" | 958 "$node_(2) set Y_ 0.0\n" |
918 "$ns_ at 1.0 \"$node_(2) setdest 5 0 5\"\n" | 959 "$ns_ at 1.0 \"$node_(2) setdest 5 0 5\"\n" |
919 "$ns_ at 2.0 \"$node_(2) setdest 5 5 5\"\n" | 960 "$ns_ at 2.0 \"$node_(2) setdest 5 5 5\"\n" |
920 "$ns_ at 3.0 \"$node_(2) setdest 0 5 5\"\n" | 961 "$ns_ at 3.0 \"$node_(2) setdest 0 5 5\"\n" |
921 "$ns_ at 4.0 \"$node_(2) setdest 0 0 5\"\n"); | 962 "$ns_ at 4.0 \"$node_(2) setdest 0 0 5\"\n"); |
922 // id t position velocity | 963 // id t position velocity |
923 t->AddReferencePoint ("0", 0, Vector(1, 2, 3), Vector(0, 0, 0)); | 964 t->AddReferencePoint ("0", 0, Vector (1, 2, 3), Vector (0, 0, 0)); |
924 t->AddReferencePoint ("1", 0, Vector(0, 0, 0), Vector(0, 0, 0)); | 965 t->AddReferencePoint ("1", 0, Vector (0, 0, 0), Vector (0, 0, 0)); |
925 t->AddReferencePoint ("1", 1, Vector(0, 0, 0), Vector(5, 0, 0)); | 966 t->AddReferencePoint ("1", 1, Vector (0, 0, 0), Vector (5, 0, 0)); |
926 t->AddReferencePoint ("1", 6, Vector(25, 0, 0), Vector(0, 0, 0)); | 967 t->AddReferencePoint ("1", 6, Vector (25, 0, 0), Vector (0, 0, 0)); |
927 t->AddReferencePoint ("2", 0, Vector(0, 0, 0), Vector(0, 0, 0)); | 968 t->AddReferencePoint ("2", 0, Vector (0, 0, 0), Vector (0, 0, 0)); |
928 t->AddReferencePoint ("2", 1, Vector(0, 0, 0), Vector(5, 0, 0)); | 969 t->AddReferencePoint ("2", 1, Vector (0, 0, 0), Vector (5, 0, 0)); |
929 t->AddReferencePoint ("2", 2, Vector(5, 0, 0), Vector(0, 0, 0)); | 970 t->AddReferencePoint ("2", 2, Vector (5, 0, 0), Vector (0, 0, 0)); |
930 t->AddReferencePoint ("2", 2, Vector(5, 0, 0), Vector(0, 5, 0)); | 971 t->AddReferencePoint ("2", 2, Vector (5, 0, 0), Vector (0, 5, 0)); |
931 t->AddReferencePoint ("2", 3, Vector(5, 5, 0), Vector(0, 0, 0)); | 972 t->AddReferencePoint ("2", 3, Vector (5, 5, 0), Vector (0, 0, 0)); |
932 t->AddReferencePoint ("2", 3, Vector(5, 5, 0), Vector(-5, 0, 0)); | 973 t->AddReferencePoint ("2", 3, Vector (5, 5, 0), Vector (-5, 0, 0)); |
933 t->AddReferencePoint ("2", 4, Vector(0, 5, 0), Vector(0, 0, 0)); | 974 t->AddReferencePoint ("2", 4, Vector (0, 5, 0), Vector (0, 0, 0)); |
934 t->AddReferencePoint ("2", 4, Vector(0, 5, 0), Vector(0, -5, 0)); | 975 t->AddReferencePoint ("2", 4, Vector (0, 5, 0), Vector (0, -5, 0)); |
935 t->AddReferencePoint ("2", 5, Vector(0, 0, 0), Vector(0, 0, 0)); | 976 t->AddReferencePoint ("2", 5, Vector (0, 0, 0), Vector (0, 0, 0)); |
936 AddTestCase (t); | 977 AddTestCase (t); |
937 | 978 |
938 // Test for Speed == 0, that acts as stop the node. | 979 // Test for Speed == 0, that acts as stop the node. |
939 t = new Ns2TransmobilityHelperTest ("setdest with speed cero", Seconds (10)) ; | 980 t = new Ns2TransmobilityHelperTest ("setdest with speed cero", Seconds (10)) ; |
940 t->SetTrace ("$ns_ at 1.0 \"$node_(0) setdest 25 0 5\"\n" | 981 t->SetTrace ("$ns_ at 1.0 \"$node_(0) setdest 25 0 5\"\n" |
941 » » "$ns_ at 7.0 \"$node_(0) setdest 11 22 0\"\n"); | 982 "$ns_ at 7.0 \"$node_(0) setdest 11 22 0\"\n"); |
942 // id t position velocity | 983 // id t position velocity |
943 t->AddReferencePoint ("0", 0, Vector(0, 0, 0), Vector(0, 0, 0)); | 984 t->AddReferencePoint ("0", 0, Vector (0, 0, 0), Vector (0, 0, 0)); |
944 t->AddReferencePoint ("0", 1, Vector(0, 0, 0), Vector(5, 0, 0)); | 985 t->AddReferencePoint ("0", 1, Vector (0, 0, 0), Vector (5, 0, 0)); |
945 t->AddReferencePoint ("0", 6, Vector(25, 0, 0), Vector(0, 0, 0)); | 986 t->AddReferencePoint ("0", 6, Vector (25, 0, 0), Vector (0, 0, 0)); |
946 t->AddReferencePoint ("0", 7, Vector(25, 0, 0), Vector(0, 0, 0)); | 987 t->AddReferencePoint ("0", 7, Vector (25, 0, 0), Vector (0, 0, 0)); |
947 AddTestCase (t); | 988 AddTestCase (t); |
948 | 989 |
949 | 990 |
950 // Test negative positions | 991 // Test negative positions |
951 t = new Ns2TransmobilityHelperTest ("test negative positions", Seconds (10)) ; | 992 t = new Ns2TransmobilityHelperTest ("test negative positions", Seconds (10)) ; |
952 t->SetTrace ("$node_(0) set X_ -1.0\n" | 993 t->SetTrace ("$node_(0) set X_ -1.0\n" |
953 "$node_(0) set Y_ 0\n" | 994 "$node_(0) set Y_ 0\n" |
954 "$ns_ at 1.0 \"$node_(0) setdest 0 0 1\"\n" | 995 "$ns_ at 1.0 \"$node_(0) setdest 0 0 1\"\n" |
955 » » "$ns_ at 2.0 \"$node_(0) setdest 0 -1 1\"\n"); | 996 "$ns_ at 2.0 \"$node_(0) setdest 0 -1 1\"\n"); |
956 // id t position velocity | 997 // id t position velocity |
957 t->AddReferencePoint ("0", 0, Vector(-1, 0, 0), Vector(0, 0, 0)); | 998 t->AddReferencePoint ("0", 0, Vector (-1, 0, 0), Vector (0, 0, 0)); |
958 t->AddReferencePoint ("0", 1, Vector(-1, 0, 0), Vector(1, 0, 0)); | 999 t->AddReferencePoint ("0", 1, Vector (-1, 0, 0), Vector (1, 0, 0)); |
959 t->AddReferencePoint ("0", 2, Vector(0, 0, 0), Vector(0, 0, 0)); | 1000 t->AddReferencePoint ("0", 2, Vector (0, 0, 0), Vector (0, 0, 0)); |
960 t->AddReferencePoint ("0", 2, Vector(0, 0, 0), Vector(0, -1, 0)); | 1001 t->AddReferencePoint ("0", 2, Vector (0, 0, 0), Vector (0, -1, 0)); |
961 t->AddReferencePoint ("0", 3, Vector(0, -1, 0), Vector(0, 0, 0)); | 1002 t->AddReferencePoint ("0", 3, Vector (0, -1, 0), Vector (0, 0, 0)); |
962 AddTestCase (t); | 1003 AddTestCase (t); |
963 | 1004 |
964 // TODO: add many more tests, e.g. test for negative and zero velocity, test with several nodes, etc. | 1005 // Sqare setdest with values in the form 1.0e+2 |
1006 t = new Ns2TransmobilityHelperTest ("Foalt numbers in 1.0e+2 format", Second s (6)); | |
1007 t->SetTrace ("$node_(0) set X_ 0.0\n" | |
1008 "$node_(0) set Y_ 0.0\n" | |
1009 "$ns_ at 1.0 \"$node_(0) setdest 1.0e+2 0 1.0e+2\"\n" | |
1010 "$ns_ at 2.0 \"$node_(0) setdest 1.0e+2 1.0e+2 1.0e+2\"\n" | |
1011 "$ns_ at 3.0 \"$node_(0) setdest 0 1.0e+2 1.0e+2\"\n" | |
1012 "$ns_ at 4.0 \"$node_(0) setdest 0 0 1.0e+2\"\n"); | |
1013 // id t position velocity | |
1014 t->AddReferencePoint ("0", 0, Vector (0, 0, 0), Vector (0, 0, 0)); | |
1015 t->AddReferencePoint ("0", 1, Vector (0, 0, 0), Vector (100, 0, 0)); | |
1016 t->AddReferencePoint ("0", 2, Vector (100, 0, 0), Vector (0, 0, 0)); | |
1017 t->AddReferencePoint ("0", 2, Vector (100, 0, 0), Vector (0, 100, 0)); | |
1018 t->AddReferencePoint ("0", 3, Vector (100, 100, 0), Vector (0, 0, 0)); | |
1019 t->AddReferencePoint ("0", 3, Vector (100, 100, 0), Vector (-100, 0, 0)); | |
1020 t->AddReferencePoint ("0", 4, Vector (0, 100, 0), Vector (0, 0, 0)); | |
1021 t->AddReferencePoint ("0", 4, Vector (0, 100, 0), Vector (0, -100, 0)); | |
1022 t->AddReferencePoint ("0", 5, Vector (0, 0, 0), Vector (0, 0, 0)); | |
1023 AddTestCase (t); | |
1024 | |
965 } | 1025 } |
966 } g_ns2TransmobilityHelperTestSuite; | 1026 } g_ns2TransmobilityHelperTestSuite; |
967 | 1027 |
968 | 1028 |
969 } // namespace ns3 | 1029 } // namespace ns3 |
LEFT | RIGHT |