Skip to content

Commit 4e9f166

Browse files
Alberto Gallegos RamonetGabrielcarvfer
authored andcommitted
visualizer: Fix overlapping labels and Simulation stop time
1 parent 2eefe03 commit 4e9f166

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

src/visualizer/model/pyviz.cc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ PyViz::CallbackStopSimulation()
261261
NS_LOG_FUNCTION_NOARGS();
262262
if (m_runUntil <= Simulator::Now())
263263
{
264-
Simulator::Stop(Seconds(0)); // Stop right now
264+
Simulator::Stop(); // Stop right now
265265
m_stop = true;
266266
}
267267
}
@@ -307,6 +307,7 @@ PyViz::SimulatorRunUntil(Time time)
307307
{
308308
return;
309309
}
310+
310311
// Schedule a dummy callback function for the target time, to make
311312
// sure we stop at the right time. Otherwise, simulations with few
312313
// events just appear to "jump" big chunks of time.
@@ -330,6 +331,14 @@ PyViz::SimulatorRunUntil(Time time)
330331
}
331332
}
332333

334+
Time
335+
PyViz::GetSimulatorStopTime()
336+
{
337+
Ptr<SimulatorImpl> impl = Simulator::GetImplementation();
338+
Ptr<VisualSimulatorImpl> visualImpl = DynamicCast<VisualSimulatorImpl>(impl);
339+
return visualImpl->GetStopTime();
340+
}
341+
333342
bool
334343
PyViz::TransmissionSampleKey::operator<(const PyViz::TransmissionSampleKey& other) const
335344
{

src/visualizer/model/pyviz.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ class PyViz
7171
*/
7272
void SimulatorRunUntil(Time time);
7373

74+
/**
75+
* Get the stop time of the underlying visual simulator implementation.
76+
* @return The stop time of the visual simulator implementation.
77+
*/
78+
Time GetSimulatorStopTime();
79+
7480
/**
7581
* Pause function
7682
* @param message the pause message

src/visualizer/model/visual-simulator-impl.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,16 @@ VisualSimulatorImpl::Stop()
132132
EventId
133133
VisualSimulatorImpl::Stop(const Time& delay)
134134
{
135+
m_stopTime = delay;
135136
return m_simulator->Stop(delay);
136137
}
137138

139+
Time
140+
VisualSimulatorImpl::GetStopTime()
141+
{
142+
return m_stopTime;
143+
}
144+
138145
//
139146
// Schedule an event for a _relative_ time in the future.
140147
//

src/visualizer/model/visual-simulator-impl.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class VisualSimulatorImpl : public SimulatorImpl
6262
uint32_t GetContext() const override;
6363
uint64_t GetEventCount() const override;
6464

65+
/**
66+
* Get the simulator implementation stop time.
67+
* @returns The stop time of the simulation implementation.
68+
*/
69+
Time GetStopTime();
70+
6571
/// calls Run() in the wrapped simulator
6672
void RunRealSimulator();
6773

@@ -70,8 +76,9 @@ class VisualSimulatorImpl : public SimulatorImpl
7076
void NotifyConstructionCompleted() override;
7177

7278
private:
73-
Ptr<SimulatorImpl> m_simulator; ///< the simulator implementation
74-
ObjectFactory m_simulatorImplFactory; ///< simulator implementation factory
79+
Ptr<SimulatorImpl> m_simulator; //!< The simulator implementation
80+
ObjectFactory m_simulatorImplFactory; //!< Simulator implementation factory
81+
Time m_stopTime; //!< The stop time of the simulation
7582
};
7683

7784
} // namespace ns3

src/visualizer/visualizer/core.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@
8383
transform_point_simulation_to_canvas,
8484
)
8585

86-
PI_OVER_2 = math.pi / 2
87-
PI_TIMES_2 = math.pi * 2
88-
8986

9087
## Node class
9188
class Node(PyVizObject):
@@ -712,18 +709,19 @@ def run(self):
712709
# print "sim: Acquire lock"
713710
self.lock.acquire()
714711
try:
715-
if 0:
716-
if ns3.Simulator.IsFinished():
717-
self.viz.play_button.set_sensitive(False)
718-
break
719712
# print "sim: Current time is %f; Run until: %f" % (ns3.Simulator.Now ().GetSeconds (), self.target_time)
720-
# if ns3.Simulator.Now ().GetSeconds () > self.target_time:
721713
# print "skipping, model is ahead of view!"
722714
self.sim_helper.SimulatorRunUntil(ns.Seconds(self.target_time))
723715
# print "sim: Run until ended at current time: ", ns3.Simulator.Now ().GetSeconds ()
724716
self.pause_messages.extend(self.sim_helper.GetPauseMessages())
725717
GLib.idle_add(self.viz.update_model, priority=PRIORITY_UPDATE_MODEL)
726-
# print "sim: Run until: ", self.target_time, ": finished."
718+
719+
if (
720+
ns.Simulator.Now().GetSeconds()
721+
>= self.sim_helper.GetSimulatorStopTime().GetSeconds()
722+
):
723+
GLib.idle_add(self.viz._on_simulation_finished)
724+
break
727725
finally:
728726
self.lock.release()
729727
# print "sim: Release lock, loop."
@@ -1435,18 +1433,24 @@ def _update_transmissions_view(self):
14351433
visibility_threshold=0.5,
14361434
font=("Sans Serif %f" % int(1 + BITRATE_FONT_SIZE * k)),
14371435
)
1436+
14381437
angle = math.atan2((pos2_y - pos1_y), (pos2_x - pos1_x))
1439-
if -PI_OVER_2 <= angle <= PI_OVER_2:
1438+
if angle > math.pi / 2 or angle < -math.pi / 2:
1439+
# Normalize the angle, in essence, adjust the angle to keep
1440+
# it from rotating beyond +/- 90 degrees. In this way, the
1441+
# direction of the label remain always readable regardless of
1442+
# the angle.
1443+
angle += math.pi
14401444
label.set_properties(
1441-
text=("%.2f kbit/s" % (kbps,)),
1445+
text=("%.2f kbit/s" % (kbps,)),
14421446
alignment=Pango.Alignment.CENTER,
14431447
anchor=GooCanvas.CanvasAnchorType.S,
14441448
x=0,
14451449
y=-line_width / 2,
14461450
)
14471451
else:
14481452
label.set_properties(
1449-
text=("%.2f kbit/s" % (kbps,)),
1453+
text=("%.2f kbit/s" % (kbps,)),
14501454
alignment=Pango.Alignment.CENTER,
14511455
anchor=GooCanvas.CanvasAnchorType.N,
14521456
x=0,
@@ -1544,6 +1548,14 @@ def _update_drops_view(self):
15441548

15451549
self._drop_arrows = new_arrows + old_arrows
15461550

1551+
def _on_simulation_finished(self):
1552+
print("Simulation finished.")
1553+
if self._update_timeout_id is not None:
1554+
GLib.source_remove(self._update_timeout_id)
1555+
self._update_timeout_id = None
1556+
self.play_button.set_active(False)
1557+
self.play_button.set_sensitive(False)
1558+
15471559
def update_view_timeout(self):
15481560
# print "view: update_view_timeout called at real time ", time.time()
15491561

0 commit comments

Comments
 (0)