00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <fstream>
00021 #include <sstream>
00022 #include "ns3/log.h"
00023 #include "ns3/simulator.h"
00024 #include "ns3/node-list.h"
00025 #include "ns3/node.h"
00026 #include "ns3/static-speed-mobility-model.h"
00027 #include "ns2-mobility-helper.h"
00028
00029 NS_LOG_COMPONENT_DEFINE ("Ns2MobilityHelper");
00030
00031 namespace ns3 {
00032
00033
00034 Ns2MobilityHelper::Ns2MobilityHelper (std::string filename)
00035 : m_filename (filename)
00036 {}
00037
00038
00039
00040 Ptr<StaticSpeedMobilityModel>
00041 Ns2MobilityHelper::GetMobilityModel (std::string idString, const ObjectStore &store) const
00042 {
00043 std::istringstream iss;
00044 iss.str (idString);
00045 uint32_t id;
00046 iss >> id;
00047 Ptr<Object> object = store.Get (id);
00048 if (object == 0)
00049 {
00050 return 0;
00051 }
00052 Ptr<StaticSpeedMobilityModel> model = object->GetObject<StaticSpeedMobilityModel> ();
00053 if (model == 0)
00054 {
00055 model = CreateObject<StaticSpeedMobilityModel> ();
00056 object->AggregateObject (model);
00057 }
00058 return model;
00059 }
00060
00061 double
00062 Ns2MobilityHelper::ReadDouble (std::string valueString) const
00063 {
00064 std::istringstream iss;
00065 iss.str (valueString);
00066 double value;
00067 iss >> value;
00068 return value;
00069 }
00070
00071 void
00072 Ns2MobilityHelper::LayoutObjectStore (const ObjectStore &store) const
00073 {
00074 std::ifstream file (m_filename.c_str (), std::ios::in);
00075 if (file.is_open())
00076 {
00077 while (!file.eof() )
00078 {
00079 std::string line;
00080 getline (file, line);
00081 std::string::size_type startNodeId = line.find_first_of ("(");
00082 std::string::size_type endNodeId = line.find_first_of (")");
00083 if (startNodeId == std::string::npos ||
00084 endNodeId == std::string::npos)
00085 {
00086 continue;
00087 }
00088 Ptr<StaticSpeedMobilityModel> model = GetMobilityModel (line.substr (startNodeId + 1,
00089 endNodeId - startNodeId),
00090 store);
00091 if (model == 0)
00092 {
00093 continue;
00094 }
00095 if (startNodeId == 6)
00096 {
00097 double value = ReadDouble (line.substr (endNodeId + 9, std::string::npos));
00098 std::string coordinate = line.substr (endNodeId + 6, 1);
00099 Vector position = model->GetPosition ();
00100 if (coordinate == "X")
00101 {
00102 position.x = value;
00103 NS_LOG_DEBUG ("X=" << value);
00104 }
00105 else if (coordinate == "Y")
00106 {
00107 position.y = value;
00108 NS_LOG_DEBUG ("Y=" << value);
00109 }
00110 else if (coordinate == "Z")
00111 {
00112 position.z = value;
00113 NS_LOG_DEBUG ("Z=" << value);
00114 }
00115 else
00116 {
00117 continue;
00118 }
00119 model->SetPosition (position);
00120 }
00121 else
00122 {
00123 std::string::size_type atEnd = line.find_first_of (" ", 8);
00124 std::string atStr = line.substr (8, atEnd-8);
00125 NS_LOG_DEBUG (atStr);
00126 double at = ReadDouble (atStr);
00127 std::string::size_type xSpeedEnd = line.find_first_of (" ", endNodeId + 10);
00128 std::string::size_type ySpeedEnd = line.find_first_of (" ", xSpeedEnd + 1);
00129 double xSpeed = ReadDouble (line.substr (endNodeId + 10, xSpeedEnd - endNodeId - 10));
00130 double ySpeed = ReadDouble (line.substr (xSpeedEnd + 1, ySpeedEnd - xSpeedEnd - 1));
00131 double zSpeed = ReadDouble (line.substr (ySpeedEnd + 1, std::string::npos));
00132 NS_LOG_DEBUG ("at=" << at << "xSpeed=" << xSpeed << ", ySpeed=" << ySpeed << ", zSpeed=" << zSpeed);
00133 Simulator::Schedule (Seconds (at), &StaticSpeedMobilityModel::SetVelocity, model,
00134 Vector (xSpeed, ySpeed, zSpeed));
00135 }
00136 }
00137 file.close();
00138 }
00139 }
00140
00141 void
00142 Ns2MobilityHelper::Install (void) const
00143 {
00144 Install (NodeList::Begin (), NodeList::End ());
00145 }
00146
00147 }