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