OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2014 Tiago Cerqueira |
| 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: Tiago Cerqueira <1090678@isep.ipp.pt> <tiago.miguel43@gmail.com> |
| 19 */ |
| 20 |
| 21 #include "ns3/routes-mobility-helper.h" |
| 22 |
| 23 NS_LOG_COMPONENT_DEFINE ("RoutesMobilityModel"); |
| 24 |
| 25 namespace ns3 { |
| 26 |
| 27 RoutesMobilityHelper::RoutesMobilityHelper () |
| 28 { |
| 29 NS_LOG_FUNCTION (this); |
| 30 m_strategy = new GoogleMapsApiConnect (); //TODO:Inquire about creating the st
rategy |
| 31 m_travelMode = "driving"; |
| 32 m_departureTime = GetCurrentTime (); |
| 33 } |
| 34 |
| 35 RoutesMobilityHelper::RoutesMobilityHelper (double lat, double lng, double altit
ude) |
| 36 { |
| 37 NS_LOG_FUNCTION (this); |
| 38 m_strategy = new GoogleMapsApiConnect (lat, lng,altitude); |
| 39 m_travelMode = "driving"; |
| 40 m_departureTime = GetCurrentTime (); |
| 41 } |
| 42 |
| 43 RoutesMobilityHelper::RoutesMobilityHelper (const RoutesMobilityHelper& orig) |
| 44 { |
| 45 this->m_strategy = orig.m_strategy; |
| 46 this->m_travelMode = orig.m_travelMode; |
| 47 } |
| 48 |
| 49 RoutesMobilityHelper::~RoutesMobilityHelper () |
| 50 { |
| 51 NS_LOG_FUNCTION (this); |
| 52 free (m_strategy); |
| 53 } |
| 54 std::string |
| 55 RoutesMobilityHelper::GetCurrentTime () |
| 56 { |
| 57 NS_LOG_FUNCTION (this); |
| 58 std::ostringstream strs; |
| 59 std::time_t time = std::time (NULL); |
| 60 std::asctime (std::localtime (&time)); |
| 61 strs << time; |
| 62 return strs.str (); |
| 63 } |
| 64 int |
| 65 RoutesMobilityHelper::SchedulePoints (Ptr<Node> node, std::list<Ptr<Leg> > legLi
st) |
| 66 { |
| 67 NS_LOG_FUNCTION (this); |
| 68 int addedWaypoints = 0; |
| 69 std::list<Ptr<Step> > stepList; |
| 70 std::list<Ptr<Point> > pointList; |
| 71 Ptr<Point> p; |
| 72 bool first = false; |
| 73 Ptr<WaypointMobilityModel> waypointMobility = DynamicCast<WaypointMobilityMode
l> ( node->GetObject<MobilityModel> ()); |
| 74 for (std::list<Ptr<Leg> >::iterator i = legList.begin (); i != legList.end ();
i++) |
| 75 { |
| 76 (*i)->GetStepList (stepList); |
| 77 for (std::list<Ptr<Step> >::iterator j = stepList.begin (); j != stepList.
end (); j++) |
| 78 { |
| 79 NS_LOG_DEBUG ("Iterating step " << *(*j)); |
| 80 pointList = (*j)->GetPointList (); |
| 81 for (std::list<Ptr<Point> >::iterator k = pointList.begin (); k != poi
ntList.end (); k++) |
| 82 { |
| 83 p = ((*k)); |
| 84 if (first) |
| 85 { |
| 86 p->SetWaypointTime (((p->GetWaypointTime ()) + 0.001)); |
| 87 first = false; |
| 88 } |
| 89 waypointMobility->AddWaypoint (Waypoint (Seconds (p->GetWaypointTi
me ()),Vector (p->GetXCoordinate (),p->GetYCoordinate (),p->GetZCoordinate ())))
; |
| 90 NS_LOG_DEBUG (*p << "Waypoint number " << (addedWaypoints + 1) <<
" added to Node " << node->GetId () << std::endl << "---------------------"); |
| 91 addedWaypoints++; |
| 92 } |
| 93 first = true; |
| 94 } |
| 95 } |
| 96 NS_ASSERT_MSG (addedWaypoints != 0, "No waypoints were added"); |
| 97 NS_LOG_INFO ("Added " << addedWaypoints << " waypoints to Node " << node->GetI
d ()); |
| 98 return addedWaypoints; |
| 99 } |
| 100 |
| 101 int |
| 102 RoutesMobilityHelper::ChooseRoute (std::string startPoint, std::string endPoint,
Ptr<Node> node) |
| 103 { |
| 104 NS_LOG_FUNCTION (this); |
| 105 std::list<Ptr<Leg> > legList; |
| 106 legList = m_strategy->PerformRequest (startPoint,endPoint, m_travelMode, m_dep
artureTime); |
| 107 if (g_log.IsEnabled (LOG_DEBUG)) |
| 108 { |
| 109 uint32_t i = 0; |
| 110 for (std::list<Ptr<Leg> >::iterator list_iter = legList.begin (); |
| 111 list_iter != legList.end(); list_iter++, i++) |
| 112 { |
| 113 NS_LOG_DEBUG ("Leg " << i << ": " << *list_iter); |
| 114 } |
| 115 } |
| 116 SchedulePoints (node,legList); |
| 117 return 0; |
| 118 } |
| 119 //listTokenStartEndPoint is supposed to be filled with pairs of start and endpoi
nts |
| 120 int |
| 121 RoutesMobilityHelper::ChooseRoute (std::list<std::string> const &listTokenStartE
ndPoint, NodeContainer &nodeContainer) |
| 122 { |
| 123 NS_LOG_FUNCTION (this); |
| 124 int sizeOfContainer = nodeContainer.GetN (); |
| 125 NS_ASSERT_MSG ( (listTokenStartEndPoint.size () / 2) == nodeContainer.GetN (),
"The number of nodes and the number of routes do not match!"); |
| 126 std::string start = "",end = ""; |
| 127 Ptr<Node> node; |
| 128 std::list<std::string>::const_iterator j = listTokenStartEndPoint.begin (); |
| 129 for (int i = 0; i < sizeOfContainer; i++,j++) |
| 130 { |
| 131 node = nodeContainer.Get (i); |
| 132 start = (*j); |
| 133 j++; |
| 134 end = (*j); |
| 135 ChooseRoute (start,end,node); |
| 136 } |
| 137 return 0; |
| 138 } |
| 139 |
| 140 int |
| 141 RoutesMobilityHelper::ChooseRoute (std::string const *listTokenStartEndPoint, No
deContainer &nodeContainer) |
| 142 { |
| 143 NS_LOG_FUNCTION (this); |
| 144 int sizeOfContainer = nodeContainer.GetN (); |
| 145 std::string start = "", end = ""; |
| 146 Ptr<Node> node; |
| 147 for (int i = 0, j = 0; i < sizeOfContainer; i++, j++) |
| 148 { |
| 149 node = nodeContainer.Get (i); |
| 150 start = listTokenStartEndPoint[j]; |
| 151 j++; |
| 152 end = listTokenStartEndPoint[j]; |
| 153 ChooseRoute (start, end, node); |
| 154 } |
| 155 return 0; |
| 156 } |
| 157 int |
| 158 RoutesMobilityHelper::ChooseRoute (Ptr<Node> node, std::string path) |
| 159 { |
| 160 NS_LOG_FUNCTION (this); |
| 161 std::list<Ptr<Leg> > legList; |
| 162 legList = m_strategy->PerformOfflineRequest (path); |
| 163 SchedulePoints (node,legList); |
| 164 return 0; |
| 165 } |
| 166 |
| 167 int |
| 168 RoutesMobilityHelper::ChooseRoute (NodeContainer &nodeContainer, std::list<doubl
e> latLngTokens, double radius) |
| 169 { |
| 170 NS_LOG_FUNCTION (this); |
| 171 int i = 0, k = 1, size = nodeContainer.GetN (), numOfUniqueRoutes = 0; |
| 172 double lat = 0, lng = 0; |
| 173 numOfUniqueRoutes = (((latLngTokens.size ())) * 15); |
| 174 NS_ASSERT_MSG (size <= numOfUniqueRoutes, "The number of nodes is superior to
the number of locations that can be obtained from the Google Maps Places API. Pl
ease add more locations or remove nodes from the node container."); |
| 175 for (std::list<double>::iterator j = latLngTokens.begin (); j != latLngTokens.
end () && i < size; j++,i++,k++) |
| 176 { |
| 177 NodeContainer auxNodeContainer; |
| 178 lat = (*j); |
| 179 j++; |
| 180 lng = (*j); |
| 181 for (; i < (k * 30) && i < size; i++) |
| 182 { |
| 183 auxNodeContainer.Add (nodeContainer.Get (i)); |
| 184 } |
| 185 ChooseRoute (auxNodeContainer, lat, lng, radius); |
| 186 } |
| 187 return 0; |
| 188 } |
| 189 |
| 190 int |
| 191 RoutesMobilityHelper::ChooseRoute (NodeContainer &nodeContainer, double lat, dou
ble lng, double radius) |
| 192 { |
| 193 NS_LOG_FUNCTION (this); |
| 194 std::ostringstream strs; |
| 195 std::string start = "",endpoint = ""; |
| 196 GoogleMapsPlacesApiConnect places; |
| 197 std::list<Ptr<Place> > placeList; |
| 198 Ptr<Place> place; |
| 199 unsigned int j = 0; |
| 200 placeList = places.PerformRequest (lat,lng,radius); |
| 201 for (std::list<Ptr<Place> >::iterator i = placeList.begin (); i != placeList.e
nd () && j < nodeContainer.GetN (); i++,j++) |
| 202 { |
| 203 strs.str (""); |
| 204 strs.clear (); |
| 205 strs << (*i)->GetLatitude (); |
| 206 strs << ","; |
| 207 strs << (*i)->GetLongitude (); |
| 208 start = strs.str (); |
| 209 i++; |
| 210 strs.str (""); |
| 211 strs.clear (); |
| 212 strs << (*i)->GetLatitude (); |
| 213 strs << ","; |
| 214 strs << (*i)->GetLongitude (); |
| 215 endpoint = strs.str (); |
| 216 ChooseRoute (start, endpoint, nodeContainer.Get (j)); |
| 217 } |
| 218 return 0; |
| 219 } |
| 220 |
| 221 int |
| 222 RoutesMobilityHelper::ChooseRoute (NodeContainer &nodeContainer, std::string dir
Path) |
| 223 { |
| 224 NS_LOG_FUNCTION (this); |
| 225 std::list<std::string> files; |
| 226 files = Open (dirPath); |
| 227 int nodeContainerSize = nodeContainer.GetN (); |
| 228 NS_ASSERT_MSG (files.size () == nodeContainer.GetN (), "The number of nodes is
not equal to the number of XML files "); |
| 229 int j = 0; |
| 230 std::string absolutePath = ""; |
| 231 for (std::list<std::string>::iterator i = files.begin (); i != files.end () &&
j < nodeContainerSize; i++, j++) |
| 232 { |
| 233 absolutePath = dirPath + "/" + (*i); |
| 234 ChooseRoute (nodeContainer.Get (j), absolutePath); |
| 235 } |
| 236 return 0; |
| 237 } |
| 238 std::list<std::string> |
| 239 RoutesMobilityHelper::Open (std::string dirPath) |
| 240 { |
| 241 NS_LOG_FUNCTION (this); |
| 242 DIR* dir; |
| 243 dirent* pdir; |
| 244 std::list<std::string> files; |
| 245 |
| 246 dir = opendir (dirPath.c_str ()); |
| 247 |
| 248 while ((pdir = readdir (dir))) |
| 249 { |
| 250 if (!((strcmp (pdir->d_name, ".") == 0) || (strcmp (pdir->d_name,"..") ==
0))) |
| 251 { |
| 252 files.push_back (pdir->d_name); |
| 253 } |
| 254 } |
| 255 |
| 256 return files; |
| 257 } |
| 258 int RoutesMobilityHelper::ChooseRoute (Ptr<Node> node, std::string start, std::s
tring end, std::string redirectedDestination, double timeToTrigger) |
| 259 { |
| 260 NS_LOG_FUNCTION (this); |
| 261 std::list<Ptr<Step> > stepList; |
| 262 std::list<Ptr<Point> > pointList; |
| 263 Time lastTime; |
| 264 Ptr<Point> p, pt; |
| 265 bool isRedirected = false; |
| 266 std::ostringstream strs; |
| 267 std::list<Ptr<Leg> > legList, auxLegList; |
| 268 double lastPointTime; |
| 269 legList = m_strategy->PerformRequest (start, end, m_travelMode, m_departureTim
e); |
| 270 for (std::list<Ptr<Leg> >::iterator i = legList.begin (); i != legList.end ();
i++) |
| 271 { |
| 272 (*i)->GetStepList (stepList); |
| 273 for (std::list<Ptr<Step> >::iterator j = stepList.begin (); j != stepList.
end () && !isRedirected; j++) |
| 274 { |
| 275 pointList = (*j)->GetPointList (); |
| 276 for (std::list<Ptr<Point> >::iterator k = pointList.begin (); k != poi
ntList.end () && !isRedirected; k++) |
| 277 { |
| 278 if ((*k)->GetWaypointTime () > timeToTrigger) |
| 279 { |
| 280 p = ((*k)); |
| 281 lastTime = Time (p->GetWaypointTime ()); |
| 282 pointList.erase (k, pointList.end ()); |
| 283 strs.str (""); |
| 284 strs.clear (); |
| 285 strs << p->GetLatitude (); |
| 286 strs << ","; |
| 287 strs << p->GetLongitude (); |
| 288 auxLegList = m_strategy->PerformRequest (strs.str (), redirect
edDestination, m_travelMode, m_departureTime); |
| 289 isRedirected = true; |
| 290 } |
| 291 } |
| 292 if (isRedirected) |
| 293 { |
| 294 stepList.erase (j,stepList.end ()); |
| 295 } |
| 296 } |
| 297 (*i)->SetStepList (stepList); |
| 298 } |
| 299 for (std::list<Ptr<Leg> >::iterator i = auxLegList.begin (); i != auxLegList.e
nd (); i++) |
| 300 { |
| 301 (*i)->GetStepList (stepList); |
| 302 for (std::list<Ptr<Step> >::iterator j = stepList.begin (); j != stepList.
end (); j++) |
| 303 { |
| 304 pointList = (*j)->GetPointList (); |
| 305 for (std::list<Ptr<Point> >::iterator k = pointList.begin (); k != poi
ntList.end (); k++) |
| 306 { |
| 307 pt = ((*k)); |
| 308 lastPointTime = lastTime.GetSeconds (); |
| 309 lastPointTime += ((p->GetWaypointTime () + pt->GetWaypointTime ())
+ 0.001); |
| 310 pt->SetWaypointTime (lastPointTime); |
| 311 } |
| 312 } |
| 313 } |
| 314 for (std::list<Ptr<Leg> >::iterator i = auxLegList.begin (); i != auxLegList.e
nd (); i++) |
| 315 { |
| 316 legList.push_back ((*i)); |
| 317 } |
| 318 SchedulePoints (node, legList); |
| 319 return 0; |
| 320 } |
| 321 |
| 322 void |
| 323 RoutesMobilityHelper::SetTransportationMethod (std::string travelMode) |
| 324 { |
| 325 NS_LOG_FUNCTION (this); |
| 326 this->m_travelMode = travelMode; |
| 327 } |
| 328 void |
| 329 RoutesMobilityHelper::SetDepartureTime (std::string departureTime) |
| 330 { |
| 331 NS_LOG_FUNCTION (this); |
| 332 this->m_departureTime = departureTime; |
| 333 } |
| 334 int |
| 335 RoutesMobilityHelper::DownloadDirectionsXml (std::string startPoint, std::string
endPoint, std::string pathToFilename) |
| 336 { |
| 337 NS_LOG_FUNCTION (this); |
| 338 m_strategy->PerformRequest (startPoint, endPoint, m_travelMode, m_departureTim
e, true, pathToFilename); |
| 339 return 0; |
| 340 } |
| 341 int |
| 342 RoutesMobilityHelper::DownloadDirectionsXml (std::list<std::string> listTokenSta
rtEndPoint, std::string pathToFolder) |
| 343 { |
| 344 NS_LOG_FUNCTION (this); |
| 345 std::string pathToFile = ""; |
| 346 std::string start = "", end = ""; |
| 347 for (std::list<std::string>::iterator i = listTokenStartEndPoint.begin (); i !
= listTokenStartEndPoint.end (); i++) |
| 348 { |
| 349 start = (*i); |
| 350 i++; |
| 351 end = (*i); |
| 352 pathToFile = pathToFolder; |
| 353 pathToFile.append ("/" + start + " - " + end + ".xml"); |
| 354 DownloadDirectionsXml (start, end, pathToFile); |
| 355 pathToFile = ""; |
| 356 } |
| 357 return 0; |
| 358 } |
| 359 } //namespace ns3 |
OLD | NEW |