Skip to content

Commit f24e4f1

Browse files
committed
accept global defaults as ctor args in Base Layer
1 parent 1a9ea48 commit f24e4f1

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

examples/basic/A00_simple_as/simple_as.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def run(dumpfile = None):
2929

3030
# Initialize the emulator and layers
3131
emu = Emulator()
32-
base = Base()
32+
# set global defaults..
33+
base = Base(bandwidth=OptionRegistry().net_bandwidth(10000000),# 10 Mbit/s on all links
34+
mtu=OptionRegistry().net_mtu(9000)) # use JumboFrames
3335
routing = Routing()
3436
ebgp = Ebgp()
3537
web = WebService()
@@ -43,7 +45,7 @@ def run(dumpfile = None):
4345

4446
# Create an autonomous system
4547
as150 = base.createAutonomousSystem(150)
46-
as150.setOption(OptionRegistry().net_mtu(9000))
48+
as150.setOption(OptionRegistry().net_mtu(1500))
4749

4850
# Create a network
4951
as150.createNetwork('net0')

seedemu/layers/Base.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,24 @@ def getNetOptions(self):
6060
return [OptionRegistry().getOption(o) for o in opt_keys]
6161

6262

63-
def __init__(self):
63+
def __init__(self, **kwargs):
6464
"""!
6565
@brief Base layer constructor.
6666
"""
6767
super().__init__()
6868
self.__ases = {}
6969
self.__ixes = {}
7070
self.__name_servers = []
71+
from seedemu.core import OptionRegistry
72+
for k,v in kwargs.items():
73+
# Replace the 'defaults' class methods dynamically
74+
75+
opt_cls = type(v)
76+
# Capture 'new_value' as default argument (forces a snapshot of the current value)
77+
opt_cls.default = classmethod(lambda cls, new_value=v.value: new_value)
78+
opt_cls.defaultMode = classmethod(lambda cls, newmode=v.mode: newmode)
79+
prefix = getattr(opt_cls, '__prefix') if hasattr(opt_cls, '__prefix') else None
80+
OptionRegistry().register(opt_cls, prefix)
7181

7282
def getName(self) -> str:
7383
return "Base"

0 commit comments

Comments
 (0)