Add a small mobility model visualization tool

This commit is contained in:
Gustavo J. A. M. Carneiro
2007-10-03 16:38:17 +01:00
parent e7fb2a2c7c
commit 7fae03e300
5 changed files with 368 additions and 0 deletions

View File

@@ -0,0 +1,137 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include <vector>
#include "ns3/ptr.h"
#include "ns3/mobility-model.h"
#include "ns3/mobility-model-notifier.h"
#include "ns3/random-topology.h"
#include "ns3/default-value.h"
#include "ns3/command-line.h"
#include "ns3/command-line.h"
#include "ns3/simulator.h"
#include "ns3/nstime.h"
#include "ns3/node.h"
#include "ns3/node-list.h"
#include "ns3/rectangle-default-value.h"
#include "mobility-visualizer.h"
using namespace ns3;
static Time g_sampleInterval = Seconds (SAMPLE_INTERVAL);
static uint32_t g_numNodes = 10;
template <typename T>
static const T* DefaultValueListGet (const std::string &name)
{
for (DefaultValueList::Iterator iter = DefaultValueList::Begin ();
iter != DefaultValueList::End (); iter++)
{
const DefaultValueBase *value = *iter;
if (value->GetName () == name)
{
return dynamic_cast<const T*> (value);
}
}
return NULL;
}
static void
Sample ()
{
ViewUpdateData *data = new ViewUpdateData;
for (NodeList::Iterator nodeIter = NodeList::Begin (); nodeIter != NodeList::End (); nodeIter++)
{
Ptr<Node> node = *nodeIter;
Ptr<MobilityModel> mobility = node->QueryInterface<MobilityModel> (MobilityModel::iid);
Position pos = mobility->Get ();
Speed vel = mobility->GetSpeed ();
NodeUpdate update;
update.node = PeekPointer<Node> (node);
update.x = pos.x;
update.y = pos.y;
update.vx = vel.dx;
update.vy = vel.dy;
data->updateList.push_back (update);
}
data->time = Simulator::Now ().GetSeconds ();
view_update (data);
Simulator::Schedule (g_sampleInterval, Sample);
}
int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, double *y2)
{
DefaultValue::Bind ("RandomWalk2dMode", "Time");
DefaultValue::Bind ("RandomWalk2dTime", "5s");
DefaultValue::Bind ("RandomWalk2dSpeed", "Constant:20.0");
DefaultValue::Bind ("RandomDirection2dSpeed", "Constant:20.0");
DefaultValue::Bind ("RandomWalk2dBounds", "0:400:0:300");
DefaultValue::Bind ("RandomDirection2dArea", "0:400:0:300");
DefaultValue::Bind ("RandomWaypointSpeed", "Uniform:10:30");
// DefaultValue::Bind ("RandomDiscPositionX", "100");
// DefaultValue::Bind ("RandomDiscPositionY", "50");
// DefaultValue::Bind ("RandomDiscPositionRho", "Uniform:0:30");
DefaultValue::Bind ("RandomTopologyPositionType", "RandomRectanglePosition");
DefaultValue::Bind ("RandomTopologyMobilityType", "RandomWalk2dMobilityModel");
// CommandLine::AddArgValue ("sample-interval","sample interval", g_sampleInterval);
// CommandLine::AddArgValue ("num-nodes","number of nodes", g_numNodes);
CommandLine::Parse (argc, argv);
RandomTopology topology;
for (uint32_t i = 0; i < g_numNodes; i++)
{
Ptr<Node> node = Create<Node> ();
node->AddInterface (Create<MobilityModelNotifier> ());
}
topology.Layout (NodeList::Begin (), NodeList::End ());
Simulator::Schedule (g_sampleInterval, Sample);
ClassId mobType = DefaultValueListGet<ClassIdDefaultValue> ("RandomTopologyMobilityType")->GetValue ();
if (mobType.GetName () == "RandomWalk2dMobilityModel")
{
Rectangle bounds = DefaultValueListGet<RectangleDefaultValue> ("RandomWalk2dBounds")->GetValue ();
*x1 = bounds.xMin;
*y1 = bounds.yMin;
*x2 = bounds.xMax;
*y2 = bounds.yMax;
std::cout << "RECT " << bounds.xMin << " " << bounds.xMax << " "
<< bounds.yMin << " " << bounds.yMax << std::endl;
}
else if (mobType.GetName () == "RandomDirection2dMobilityModel")
{
Rectangle bounds = DefaultValueListGet<RectangleDefaultValue> ("RandomDirection2dArea")->GetValue ();
*x1 = bounds.xMin;
*y1 = bounds.yMin;
*x2 = bounds.xMax;
*y2 = bounds.yMax;
std::cout << "RECT " << bounds.xMin << " " << bounds.xMax << " "
<< bounds.yMin << " " << bounds.yMax << std::endl;
}
else if (mobType.GetName () == "RandomWaypointMobilityModel")
{
std::cerr << "bounds for RandomWaypointMobilityModel not implemented" << std::endl;
//ClassId posType = DefaultValueList::Get<ClassIdDefaultValue> ("RandomWaypointPosition")->GetValue ();
std::cout << "?" << std::endl; // too hard to represent an abstract/probabilistic model graphically
}
else
{
NS_FATAL_ERROR ("mobility type " << mobType.GetName () << " not supported");
}
std::cerr << g_sampleInterval << std::endl;
return 0;
}

View File

@@ -0,0 +1,193 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include <map>
#include <goocanvas.h>
#include <glib/gthread.h>
#include "mobility-visualizer.h"
#include <map>
#include "ns3/simulator.h"
#define MAX_QUEUE_LENGTH 100
#define MAX_EVENTS 20
#define LOOKAHEAD_SECONDS 10
GtkWidget *g_canvas;
int model_init (int argc, char *argv[]);
struct Node
{
GooCanvasItem *m_item;
void create ()
{
GooCanvasItem *root = goo_canvas_get_root_item (GOO_CANVAS (g_canvas));
m_item = goo_canvas_ellipse_new (root, 0, 0, 2.0, 2.0,
"line_width", 0.5,
"stroke_color", "black",
"fill_color", "red",
NULL);
}
void update (double x, double y, double vx, double vy)
{
g_object_set (m_item, "center_x", x, "center_y", y, NULL);
}
};
std::map<void*, Node> g_nodes;
GTimeVal initialTime = {-1, -1};
gboolean firstTime = TRUE;
double g_lookaheadTime = 0;
G_LOCK_DEFINE (g_lookaheadTimeMux);
ViewUpdateData *g_nextData = NULL;
GAsyncQueue *queue;
double get_current_time ()
{
GTimeVal currTime;
g_get_current_time (&currTime);
GTimeVal relativeTime;
relativeTime.tv_sec = currTime.tv_sec - initialTime.tv_sec + LOOKAHEAD_SECONDS;
relativeTime.tv_usec = currTime.tv_usec;
g_time_val_add (&relativeTime, -initialTime.tv_usec);
return relativeTime.tv_sec + 1.0e-6*relativeTime.tv_usec;
}
// called from the simulation thread
void view_update (ViewUpdateData *updateData)
{
while ((G_LOCK (g_lookaheadTimeMux), g_lookaheadTime) != 0 and updateData->time >= g_lookaheadTime)
{
G_UNLOCK (g_lookaheadTimeMux);
g_usleep ((gulong) 10e3);
}
G_UNLOCK (g_lookaheadTimeMux);
g_async_queue_push (queue, updateData);
}
void view_update_process (ViewUpdateData *updateData)
{
if (firstTime)
{
firstTime = FALSE;
g_get_current_time (&initialTime);
for (std::vector<NodeUpdate>::const_iterator update
= updateData->updateList.begin ();
update != updateData->updateList.end ();
update++)
{
g_nodes[update->node].create ();
g_nodes[update->node].update (update->x, update->y, update->vx, update->vy);
}
delete updateData;
}
else
{
for (std::vector<NodeUpdate>::const_iterator update
= updateData->updateList.begin ();
update != updateData->updateList.end ();
update++)
{
g_nodes[update->node].update (update->x, update->y, update->vx, update->vy);
}
delete updateData;
G_LOCK (g_lookaheadTimeMux);
g_lookaheadTime = get_current_time () + LOOKAHEAD_SECONDS;
G_UNLOCK (g_lookaheadTimeMux);
}
}
gboolean view_update_consumer ()
{
if (!g_nextData)
g_nextData = (ViewUpdateData *) g_async_queue_try_pop (queue);
if (!g_nextData)
return TRUE;
double time = get_current_time ();
if (g_nextData->time > time)
return TRUE;
do
{
view_update_process (g_nextData);
g_nextData = (ViewUpdateData *) g_async_queue_try_pop (queue);
}
while (g_nextData && g_nextData->time <= time);
return TRUE;
}
void zoom_changed (GtkAdjustment *adj)
{
goo_canvas_set_scale (GOO_CANVAS (g_canvas), gtk_adjustment_get_value (adj));
}
int main (int argc, char *argv[])
{
g_thread_init (NULL);
gtk_init (&argc, &argv);
double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
model_init (argc, argv, &x1, &y1, &x2, &y2);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), 640, 600);
gtk_widget_show (window);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
GtkWidget *scrolled_win = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), GTK_SHADOW_IN);
gtk_widget_show (scrolled_win);
GtkWidget *vbox = gtk_vbox_new (FALSE, 4);
gtk_widget_show (vbox);
gtk_box_pack_start (GTK_BOX (vbox), scrolled_win, 1, 1, 4);
gtk_container_add (GTK_CONTAINER (window), vbox);
GtkWidget *hbox = gtk_hbox_new (FALSE, 4);
gtk_widget_show (hbox);
gtk_box_pack_start (GTK_BOX (vbox), hbox, false, false, 4);
GtkObject *zoom = gtk_adjustment_new (3.0, 0.1, 10.0, 0.2, 1.0, 1.0);
gtk_box_pack_start(GTK_BOX (hbox),
GTK_WIDGET (g_object_new (GTK_TYPE_SPIN_BUTTON, "adjustment", zoom,
"visible", true, "digits", 2, NULL)),
false, false, 4);
g_canvas = goo_canvas_new ();
gtk_widget_set_size_request (GTK_WIDGET (g_canvas), 600, 450);
goo_canvas_set_bounds (GOO_CANVAS (g_canvas), -500, -500, 500, 500);
g_signal_connect (zoom, "value-changed", G_CALLBACK (zoom_changed), NULL);
gtk_adjustment_value_changed (GTK_ADJUSTMENT (zoom));
gtk_widget_show (g_canvas);
gtk_container_add (GTK_CONTAINER (scrolled_win), g_canvas);
goo_canvas_scroll_to (GOO_CANVAS (g_canvas), 0, 0);
// create the bounds rectangle
if (x1 != x2)
{
GooCanvasItem *item =
goo_canvas_rect_new (goo_canvas_get_root_item (GOO_CANVAS (g_canvas)),
x1, y1, x2-x1, y2-y1, NULL);
g_object_set (item, "line-width", 1.0, "stroke-color", "grey", NULL);
}
queue = g_async_queue_new ();
g_timeout_add ((guint) (SAMPLE_INTERVAL*1000), (GSourceFunc) view_update_consumer, NULL);
g_thread_create (GThreadFunc (ns3::Simulator::Run), NULL, FALSE, NULL);
gtk_main ();
return 0;
}

View File

@@ -0,0 +1,23 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
#include <vector>
int model_init (int argc, char *argv[], double *x1, double *y1, double *x2, double *y2);
struct NodeUpdate
{
void *node;
double x;
double y;
double vx;
double vy;
};
struct ViewUpdateData
{
std::vector<NodeUpdate> updateList;
double time;
};
void view_update (ViewUpdateData *updateData);
#define SAMPLE_INTERVAL (1.0/100) // due to some race condition, this is the only value that works

View File

@@ -1,5 +1,12 @@
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
def configure(conf):
check = conf.create_pkgconfig_configurator()
check.name = 'goocanvas gthread-2.0'
check.uselib = 'MOBILITY_VISUALIZER'
check.mandatory = False
conf.env['ENABLE_MOBILITY_VISUALIZER'] = check.run()
def build(bld):
env = bld.env_of_name('default')
@@ -23,3 +30,10 @@ def build(bld):
obj = bld.create_ns3_program('print-trace-sources',
['internet-node', 'csma-cd', 'point-to-point'])
obj.source = 'print-trace-sources.cc'
if env['ENABLE_MOBILITY_VISUALIZER']:
obj = bld.create_ns3_program('mobility-visualizer',
['internet-node', 'mobility'])
obj.source = ['mobility-visualizer-model.cc', 'mobility-visualizer-view.cc']
obj.uselib = 'MOBILITY_VISUALIZER'

View File

@@ -143,6 +143,7 @@ def configure(conf):
variant_env.append_value("LINKFLAGS", "-Wl,--enable-runtime-pseudo-reloc")
conf.sub_config('src')
conf.sub_config('utils')
def create_ns3_program(bld, name, dependencies=('simulator',)):