Changed custom feature so that params aren't added to globals.

There is probably a better way of doing this, but currently
parseCustomFile can modify globals (e.g. TOPOS) as well as
instance variables (self.validate) and classes (e.g. MyTopo),
which are also in the global name space.

Inconveniently enough, lambdas don't seem to be full closures
in Python; if they were, this trickiness would be unnecessary.

Even so, using execfile() seems like it might be a bit dubious...
This commit is contained in:
Bob Lantz
2010-02-27 23:19:57 -08:00
parent 5468377680
commit c3a4440025
4 changed files with 32 additions and 20 deletions
+22 -11
View File
@@ -82,17 +82,28 @@ class MininetRunner( object ):
self.setup() self.setup()
self.begin() self.begin()
def parseCustomFile( self, custom ): def setCustom( self, name, value ):
"Parse custom file and add params before parsing cmd-line options." print "got", self, name, value
if os.path.isfile( custom ): if name in ( 'topos', 'switches', 'hosts', 'controllers' ):
execfile( custom, globals(), globals() ) # Update dictionaries
if 'topos' in globals(): TOPOS.update( topos ) param = name.upper()
if 'switches' in globals(): SWITCHES.update( switches ) globals()[ param ].update( value )
if 'hosts' in globals(): HOSTS.update( hosts ) elif name == 'validate':
if 'controllers' in globals(): CONTROLLERS.update( controllers ) # Add custom validate function
if 'validate' in globals(): self.validate = validate self.validate = value
else: else:
raise Exception( 'could not find custom file: %s' % custom ) # Add or modify global variable or class
globals()[ name ] = value
def parseCustomFile( self, fileName ):
"Parse custom file and add params before parsing cmd-line options."
custom = {}
if os.path.isfile( fileName ):
execfile( fileName, custom, custom )
for name in custom:
self.setCustom( name, custom[ name ] )
else:
raise Exception( 'could not find custom file: %s' % fileName )
def parseArgs( self ): def parseArgs( self ):
"""Parse command-line args and return options object. """Parse command-line args and return options object.
@@ -161,7 +172,7 @@ class MininetRunner( object ):
port=self.options.port ) port=self.options.port )
if self.validate: if self.validate:
self.validate(self.options) self.validate( self.options )
controllerParams = ControllerParams( 0x0a000000, 8 ) # 10.0.0.0/8 controllerParams = ControllerParams( 0x0a000000, 8 ) # 10.0.0.0/8
inNamespace = self.options.inNamespace inNamespace = self.options.inNamespace
+5 -3
View File
@@ -13,10 +13,10 @@ topology enables one to pass in '--topo=mytopo' from the command line.
from mininet.topo import Topo, Node from mininet.topo import Topo, Node
class MyTopo( Topo ): class MyTopo( Topo ):
"""Simple topology example.""" "Simple topology example."
def __init__( self, enable_all = True ): def __init__( self, enable_all = True ):
"""Create custom topo.""" "Create custom topo."
# Add default members to class. # Add default members to class.
super( MyTopo, self ).__init__() super( MyTopo, self ).__init__()
@@ -42,4 +42,6 @@ class MyTopo( Topo ):
self.enable_all() self.enable_all()
topos = { 'mytopo': ( lambda: MyTopo() ) } topos = { 'mytopo': MyTopo }
# topos = { 'mytopo': ( lambda: MyTopo() ) }
+1 -2
View File
@@ -88,8 +88,7 @@ class CLI( Cmd ):
def do_net( self, args ): def do_net( self, args ):
"List network connections." "List network connections."
for switchDpid in self.mn.topo.switches(): for switch in self.mn.switches:
switch = self.mn.nodes[ switchDpid ]
info( '%s <->', switch.name ) info( '%s <->', switch.name )
for intf in switch.intfs: for intf in switch.intfs:
node = switch.connection[ intf ] node = switch.connection[ intf ]
+2 -2
View File
@@ -303,14 +303,14 @@ class Mininet( object ):
ip = topo.ip( hostId ) ip = topo.ip( hostId )
host = self.addHost( name, defaultIp=ip, defaultMac=mac ) host = self.addHost( name, defaultIp=ip, defaultMac=mac )
self.idToNode[ hostId ] = host self.idToNode[ hostId ] = host
info( name ) info( name + ' ' )
info( '\n*** Adding switches:\n' ) info( '\n*** Adding switches:\n' )
for switchId in sorted( topo.switches() ): for switchId in sorted( topo.switches() ):
name = 's' + topo.name( switchId ) name = 's' + topo.name( switchId )
mac = macColonHex( switchId) if self.setMacs else None mac = macColonHex( switchId) if self.setMacs else None
switch = self.addSwitch( name, defaultMac=mac ) switch = self.addSwitch( name, defaultMac=mac )
self.idToNode[ switchId ] = switch self.idToNode[ switchId ] = switch
info( name ) info( name + ' ' )
info( '\n*** Adding edges:\n' ) info( '\n*** Adding edges:\n' )
for srcId, dstId in sorted( topo.edges() ): for srcId, dstId in sorted( topo.edges() ):
src, dst = self.idToNode[ srcId ], self.idToNode[ dstId ] src, dst = self.idToNode[ srcId ], self.idToNode[ dstId ]