Simplified and refactored examples/dynamicnet.py

Extended LinearTopo to support mulitple hosts per switch
This commit is contained in:
Brian O'Connor
2013-07-19 15:56:06 -07:00
parent bf97d21c03
commit 921123155a
2 changed files with 28 additions and 108 deletions
+13 -6
View File
@@ -236,23 +236,30 @@ class SingleSwitchReversedTopo(Topo):
port1=0, port2=(k - h + 1))
class LinearTopo(Topo):
"Linear topology of k switches, with one host per switch."
"Linear topology of k switches, with n hosts per switch."
def __init__(self, k=2, **opts):
def __init__(self, k=2, n=1, **opts):
"""Init.
k: number of switches (and hosts)
k: number of switches
n: number of hosts per switch
hconf: host configuration options
lconf: link configuration options"""
super(LinearTopo, self).__init__(**opts)
self.k = k
self.n = n
lastSwitch = None
for i in irange(1, k):
host = self.addHost('h%s' % i)
# Add switch
switch = self.addSwitch('s%s' % i)
self.addLink( host, switch)
# Add hosts to switch
for j in irange(1, n):
hostNum = (i-1)*n + j
host = self.addHost('h%s' % hostNum)
self.addLink(host, switch)
# Connect switch to previous
if lastSwitch:
self.addLink( switch, lastSwitch)
self.addLink(switch, lastSwitch)
lastSwitch = switch