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 * This program is free software; you can redistribute it and/or modify | 3 * This program is free software; you can redistribute it and/or modify |
4 * it under the terms of the GNU General Public License version 2 as | 4 * it under the terms of the GNU General Public License version 2 as |
5 * published by the Free Software Foundation; | 5 * published by the Free Software Foundation; |
6 * | 6 * |
7 * This program is distributed in the hope that it will be useful, | 7 * This program is distributed in the hope that it will be useful, |
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 8 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
10 * GNU General Public License for more details. | 10 * GNU General Public License for more details. |
11 * | 11 * |
12 * You should have received a copy of the GNU General Public License | 12 * You should have received a copy of the GNU General Public License |
13 * along with this program; if not, write to the Free Software | 13 * along with this program; if not, write to the Free Software |
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
15 * | 15 * |
16 * Author: George F. Riley<riley@ece.gatech.edu> | 16 * Author: George F. Riley<riley@ece.gatech.edu> |
17 */ | 17 */ |
18 | 18 |
19 // Interface between ns3 and the network animator | 19 // Interface between ns3 and the network animator |
20 | 20 |
21 #include <stdio.h> | 21 #include <stdio.h> |
22 #include <sstream> | 22 #include <sstream> |
23 | 23 |
| 24 #include "ns3/net-anim-config.h" |
| 25 |
24 // Socket related includes | 26 // Socket related includes |
| 27 #if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
25 #include <sys/socket.h> | 28 #include <sys/socket.h> |
26 #include <netinet/in.h> | 29 #include <netinet/in.h> |
| 30 #else |
| 31 #include <fcntl.h> |
| 32 #endif |
27 | 33 |
28 // ns3 includes | 34 // ns3 includes |
29 #include "ns3/animation-interface.h" | 35 #include "ns3/animation-interface.h" |
30 #include "ns3/channel.h" | 36 #include "ns3/channel.h" |
31 #include "ns3/config.h" | 37 #include "ns3/config.h" |
32 #include "ns3/node.h" | 38 #include "ns3/node.h" |
33 #include "ns3/node-location.h" | 39 #include "ns3/canvas-location.h" |
34 #include "ns3/packet.h" | 40 #include "ns3/packet.h" |
35 #include "ns3/simulator.h" | 41 #include "ns3/simulator.h" |
36 | 42 |
37 using namespace std; | 43 using namespace std; |
38 | 44 |
39 NS_LOG_COMPONENT_DEFINE ("AnimationInterface"); | 45 NS_LOG_COMPONENT_DEFINE ("AnimationInterface"); |
40 | 46 |
41 namespace ns3 { | 47 namespace ns3 { |
42 | 48 |
43 AnimationInterface::AnimationInterface () | 49 AnimationInterface::AnimationInterface () |
44 : m_fHandle (STDOUT_FILENO), m_model (0) | 50 : m_fHandle (STDOUT_FILENO), m_model (0) |
45 { | 51 { |
46 } | 52 } |
47 | 53 |
| 54 AnimationInterface::~AnimationInterface () |
| 55 { |
| 56 } |
| 57 |
48 bool AnimationInterface::SetOutputFile (const std::string& fn) | 58 bool AnimationInterface::SetOutputFile (const std::string& fn) |
49 { | 59 { |
50 FILE* f = fopen (fn.c_str (), "w"); | 60 FILE* f = fopen (fn.c_str (), "w"); |
51 if (!f) | 61 if (!f) |
52 { | 62 { |
53 return false; // Can't open | 63 return false; // Can't open |
54 } | 64 } |
55 m_fHandle = fileno (f); // Set the file handle | 65 m_fHandle = fileno (f); // Set the file handle |
56 return true; | 66 return true; |
57 } | 67 } |
58 | 68 |
59 bool AnimationInterface::SetServerPort (uint16_t port) | 69 bool AnimationInterface::SetServerPort (uint16_t port) |
60 { | 70 { |
| 71 #if defined(HAVE_SYS_SOCKET_H) && defined(HAVE_NETINET_IN_H) |
61 int s = socket (AF_INET, SOCK_STREAM, 0); | 72 int s = socket (AF_INET, SOCK_STREAM, 0); |
62 struct sockaddr_in addr; | 73 struct sockaddr_in addr; |
63 addr.sin_family = AF_INET; | 74 addr.sin_family = AF_INET; |
64 addr.sin_port = htons (port); | 75 addr.sin_port = htons (port); |
65 addr.sin_addr.s_addr = htonl (INADDR_ANY); | 76 addr.sin_addr.s_addr = htonl (INADDR_ANY); |
66 if (bind (s, (struct sockaddr*)&addr, sizeof (addr)) < 0) | 77 if (bind (s, (struct sockaddr*)&addr, sizeof (addr)) < 0) |
67 { | 78 { |
68 NS_LOG_WARN ("Can't bind to port " << port << ", exiting."); | 79 NS_LOG_WARN ("Can't bind to port " << port << ", exiting."); |
69 return false; | 80 return false; |
70 } | 81 } |
71 listen (s, 1); | 82 listen (s, 1); |
72 NS_LOG_INFO ("Waiting for animator connection"); | 83 NS_LOG_INFO ("Waiting for animator connection"); |
73 // Now wait for the animator to connect in | 84 // Now wait for the animator to connect in |
74 m_fHandle = accept (s, 0, 0); | 85 m_fHandle = accept (s, 0, 0); |
75 NS_LOG_INFO ("Got animator connection from remote"); | 86 NS_LOG_INFO ("Got animator connection from remote"); |
76 // set the linger socket option | 87 // set the linger socket option |
77 int t = 1; | 88 int t = 1; |
78 setsockopt (s, SOL_SOCKET, SO_LINGER, &t, sizeof(t)); | 89 setsockopt (s, SOL_SOCKET, SO_LINGER, &t, sizeof(t)); |
79 return true; | 90 return true; |
80 } | 91 #endif |
81 | 92 return false; // never reached unless the above is disabled |
82 bool AnimationInterface::SetInternalAnimation () | 93 // which is done to support a platform like MinGW |
83 { | |
84 return false; // Not implemented yet | |
85 } | 94 } |
86 | 95 |
87 void AnimationInterface::StartAnimation () | 96 void AnimationInterface::StartAnimation () |
88 { | 97 { |
89 // Dump the topology | 98 // Dump the topology |
90 for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) | 99 for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
91 { | 100 { |
92 Ptr<Node> n = *i; | 101 Ptr<Node> n = *i; |
93 Ptr<NodeLocation> loc = n->GetObject<NodeLocation> (); | 102 Ptr<CanvasLocation> loc = n->GetObject<CanvasLocation> (); |
94 if (loc) | 103 if (loc) |
95 { | 104 { |
96 // Location exists, dump it | 105 // Location exists, dump it |
97 Vector v = loc->GetLocation (); | 106 Vector v = loc->GetLocation (); |
98 ostringstream oss; | 107 ostringstream oss; |
99 oss << "0.0 N " << n->GetId ()· | 108 oss << "0.0 N " << n->GetId ()· |
100 << " " << v.x << " " << v.y << endl; | 109 << " " << v.x << " " << v.y << endl; |
101 WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); | 110 WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
102 } | 111 } |
103 } | 112 } |
104 // Now dump the p2p links | 113 // Now dump the p2p links |
105 for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End(); ++i) | 114 for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i) |
106 { | 115 { |
107 Ptr<Node> n = *i; | 116 Ptr<Node> n = *i; |
108 uint32_t n1Id = n->GetId (); | 117 uint32_t n1Id = n->GetId (); |
109 uint32_t nDev = n->GetNDevices (); // Number of devices | 118 uint32_t nDev = n->GetNDevices (); // Number of devices |
110 for (uint32_t i = 0; i < nDev; ++i) | 119 for (uint32_t i = 0; i < nDev; ++i) |
111 { | 120 { |
112 Ptr<NetDevice> dev = n->GetDevice (i); | 121 Ptr<NetDevice> dev = n->GetDevice (i); |
113 Ptr<Channel> ch = dev->GetChannel (); | 122 Ptr<Channel> ch = dev->GetChannel (); |
114 if (!ch)· | 123 if (!ch)· |
115 { | 124 { |
(...skipping 18 matching lines...) Expand all Loading... |
134 } | 143 } |
135 } | 144 } |
136 else | 145 else |
137 { | 146 { |
138 NS_FATAL_ERROR ("Net animation currently only supports point-to-po
int links."); | 147 NS_FATAL_ERROR ("Net animation currently only supports point-to-po
int links."); |
139 } | 148 } |
140 } | 149 } |
141 } | 150 } |
142 ·· | 151 ·· |
143 // Connect the callback for packet tx events | 152 // Connect the callback for packet tx events |
144 Config::Connect ("/NodeList/*/DeviceList/*/TxPointToPointAnim", | 153 Config::Connect ("/ChannelList/*/TxRxPointToPoint", |
145 MakeCallback (&AnimationInterface::DevTxTrace, this)); | 154 MakeCallback (&AnimationInterface::DevTxTrace, this)); |
146 } | 155 } |
147 | 156 |
148 void AnimationInterface::StopAnimation () | 157 void AnimationInterface::StopAnimation () |
149 { | 158 { |
150 if (m_fHandle > 0)· | 159 if (m_fHandle > 0)· |
151 { | 160 { |
152 close (m_fHandle); | 161 close (m_fHandle); |
153 } | 162 } |
154 } | 163 } |
(...skipping 29 matching lines...) Expand all Loading... |
184 oss << now.GetSeconds() << " P " | 193 oss << now.GetSeconds() << " P " |
185 << tx->GetNode ()->GetId () << " " | 194 << tx->GetNode ()->GetId () << " " |
186 << rx->GetNode ()->GetId () << " " | 195 << rx->GetNode ()->GetId () << " " |
187 << (now + txTime).GetSeconds () << " " // last bit tx time | 196 << (now + txTime).GetSeconds () << " " // last bit tx time |
188 << (now + rxTime - txTime).GetSeconds() << " " // first bit rx time | 197 << (now + rxTime - txTime).GetSeconds() << " " // first bit rx time |
189 << (now + rxTime).GetSeconds () << endl; // last bit rx time | 198 << (now + rxTime).GetSeconds () << endl; // last bit rx time |
190 WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); | 199 WriteN (m_fHandle, oss.str ().c_str (), oss.str ().length ()); |
191 } | 200 } |
192 | 201 |
193 } // namespace ns3 | 202 } // namespace ns3 |
LEFT | RIGHT |