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