A Discrete-Event Network Simulator
API
base.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import ns.point_to_point
3 import ns.csma
4 import ns.wifi
5 import ns.bridge
6 import ns.internet
7 import ns.mesh
8 import ns.wimax
9 import ns.lte
10 
11 from gi.repository import GObject
12 import os.path
13 import sys
14 
15 PIXELS_PER_METER = 3.0 # pixels-per-meter, at 100% zoom level
16 
17 
18 class PyVizObject(GObject.GObject):
19 
20  __gtype_name__ = "PyVizObject"
21 
22 
26  def tooltip_query(self, tooltip):
27  tooltip.set_text("TODO: tooltip for %r" % self)
28 
29 
31  pass
32 
33 
34 
35 class InformationWindow(object):
36 
39  def update(self):
40  raise NotImplementedError
41 
42 
44 
49  def __init__(self, is_wireless=None, is_virtual=False):
50  """!
51  Initialize function.
52  @param self The current class
53  @param is_wireless is wireless flag
54  @param is_virtual is virtual flag
55  """
56  assert is_virtual or is_wireless is not None
57  self.is_wirelessis_wireless = is_wireless
58  self.is_virtualis_virtual = is_virtual
59 
60 netdevice_traits = {
61  ns.point_to_point.PointToPointNetDevice: NetDeviceTraits(is_wireless=False),
62  ns.csma.CsmaNetDevice: NetDeviceTraits(is_wireless=False),
63  ns.wifi.WifiNetDevice: NetDeviceTraits(is_wireless=True),
64  ns.bridge.BridgeNetDevice: NetDeviceTraits(is_virtual=True),
65  ns.internet.LoopbackNetDevice: NetDeviceTraits(is_virtual=True, is_wireless=False),
66  ns.mesh.MeshPointDevice: NetDeviceTraits(is_virtual=True),
67  ns.wimax.SubscriberStationNetDevice: NetDeviceTraits(is_wireless=True),
68  ns.wimax.BaseStationNetDevice: NetDeviceTraits(is_wireless=True),
69  ns.lte.LteUeNetDevice: NetDeviceTraits(is_wireless=True),
70  ns.lte.LteEnbNetDevice: NetDeviceTraits(is_wireless=True),
71 }
72 
73 def lookup_netdevice_traits(class_type):
74  try:
75  return netdevice_traits[class_type]
76  except KeyError:
77  sys.stderr.write("WARNING: no NetDeviceTraits registered for device type %r; "
78  "I will assume this is a non-virtual wireless device, "
79  "but you should edit %r, variable 'netdevice_traits',"
80  " to make sure.\n" % (class_type.__name__, __file__))
81  t = NetDeviceTraits(is_virtual=False, is_wireless=True)
82  netdevice_traits[class_type] = t
83  return t
84 
86  return d*PIXELS_PER_METER
87 
89  return x*PIXELS_PER_METER, y*PIXELS_PER_METER
90 
92  return d/PIXELS_PER_METER
93 
95  return x/PIXELS_PER_METER, y/PIXELS_PER_METER
96 
97 
98 
99 
100 plugins = []
101 plugin_modules = {}
102 
103 def register_plugin(plugin_init_func, plugin_name=None, plugin_module=None):
104  """
105  Register a plugin.
106 
107  @param plugin: a callable object that will be invoked whenever a
108  Visualizer object is created, like this: plugin(visualizer)
109  """
110  assert callable(plugin_init_func)
111  plugins.append(plugin_init_func)
112  if plugin_module is not None:
113  plugin_modules[plugin_name] = plugin_module
114 
115 plugins_loaded = False
117  global plugins_loaded
118  if plugins_loaded:
119  return
120  plugins_loaded = True
121  plugins_dir = os.path.join(os.path.dirname(__file__), 'plugins')
122  old_path = list(sys.path)
123  sys.path.insert(0, plugins_dir)
124  for filename in os.listdir(plugins_dir):
125  name, ext = os.path.splitext(filename)
126  if ext != '.py':
127  continue
128  try:
129  plugin_module = __import__(name)
130  except ImportError as ex:
131  print("Could not load plugin %r: %s" % (filename, str(ex)), file=sys.stderr)
132  continue
133  try:
134  plugin_func = plugin_module.register
135  except AttributeError:
136  print("Plugin %r has no 'register' function" % name, file=sys.stderr)
137  else:
138  #print("Plugin %r registered" % name, file=sys.stderr)
139  register_plugin(plugin_func, name, plugin_module)
140  sys.path = old_path
141 
def update(self)
update function
Definition: base.py:39
NetDeviceTraits class.
Definition: base.py:43
def __init__(self, is_wireless=None, is_virtual=False)
Initialize function.
Definition: base.py:49
is_wireless
class variablesis wireless
Definition: base.py:57
PyVizObject class.
Definition: base.py:18
def tooltip_query(self, tooltip)
Returns tooltip text string.
Definition: base.py:26
def transform_distance_simulation_to_canvas(d)
Definition: base.py:85
def register_plugin(plugin_init_func, plugin_name=None, plugin_module=None)
Definition: base.py:103
def transform_distance_canvas_to_simulation(d)
Definition: base.py:91
def transform_point_canvas_to_simulation(x, y)
Definition: base.py:94
def load_plugins()
Definition: base.py:116
def lookup_netdevice_traits(class_type)
Definition: base.py:73
def transform_point_simulation_to_canvas(x, y)
Definition: base.py:88
#define list