Merge pull request #186 from mininet/devel/clifix

Replaced nodelist and nodemap in CLI with mn
This commit is contained in:
Brian O'Connor
2013-08-12 14:03:33 -07:00
2 changed files with 45 additions and 23 deletions
+22 -22
View File
@@ -43,13 +43,8 @@ class CLI( Cmd ):
def __init__( self, mininet, stdin=sys.stdin, script=None ): def __init__( self, mininet, stdin=sys.stdin, script=None ):
self.mn = mininet self.mn = mininet
self.nodelist = self.mn.controllers + self.mn.switches + self.mn.hosts
self.nodemap = {} # map names to Node objects
for node in self.nodelist:
self.nodemap[ node.name ] = node
# Local variable bindings for py command # Local variable bindings for py command
self.locals = { 'net': mininet } self.locals = { 'net': mininet }
self.locals.update( self.nodemap )
# Attempt to handle input # Attempt to handle input
self.stdin = stdin self.stdin = stdin
self.inPoller = poll() self.inPoller = poll()
@@ -63,7 +58,7 @@ class CLI( Cmd ):
while True: while True:
try: try:
# Make sure no nodes are still waiting # Make sure no nodes are still waiting
for node in self.nodelist: for node in self.mn.values():
while node.waiting: while node.waiting:
node.sendInt() node.sendInt()
node.monitor() node.monitor()
@@ -78,6 +73,11 @@ class CLI( Cmd ):
"Don't repeat last command when you hit return." "Don't repeat last command when you hit return."
pass pass
def getLocals( self ):
"Local variable bindings for py command"
self.locals.update( self.mn )
return self.locals
# Disable pylint "Unused argument: 'arg's'" messages, as well as # Disable pylint "Unused argument: 'arg's'" messages, as well as
# "method could be a function" warning, since each CLI function # "method could be a function" warning, since each CLI function
# must have the same interface # must have the same interface
@@ -110,12 +110,12 @@ class CLI( Cmd ):
def do_nodes( self, _line ): def do_nodes( self, _line ):
"List all nodes." "List all nodes."
nodes = ' '.join( [ node.name for node in sorted( self.nodelist ) ] ) nodes = ' '.join( sorted( self.mn ) )
output( 'available nodes are: \n%s\n' % nodes ) output( 'available nodes are: \n%s\n' % nodes )
def do_net( self, _line ): def do_net( self, _line ):
"List network connections." "List network connections."
dumpNodeConnections( self.nodelist ) dumpNodeConnections( self.mn.values() )
def do_sh( self, line ): def do_sh( self, line ):
"Run an external shell command" "Run an external shell command"
@@ -128,7 +128,7 @@ class CLI( Cmd ):
"""Evaluate a Python expression. """Evaluate a Python expression.
Node names may be used, e.g.: py h1.cmd('ls')""" Node names may be used, e.g.: py h1.cmd('ls')"""
try: try:
result = eval( line, globals(), self.locals ) result = eval( line, globals(), self.getLocals() )
if not result: if not result:
return return
elif isinstance( result, str ): elif isinstance( result, str ):
@@ -145,7 +145,7 @@ class CLI( Cmd ):
"""Execute a Python statement. """Execute a Python statement.
Node names may be used, e.g.: px print h1.cmd('ls')""" Node names may be used, e.g.: px print h1.cmd('ls')"""
try: try:
exec( line, globals(), self.locals ) exec( line, globals(), self.getLocals() )
except Exception, e: except Exception, e:
output( str( e ) + '\n' ) output( str( e ) + '\n' )
@@ -176,11 +176,11 @@ class CLI( Cmd ):
hosts = [] hosts = []
err = False err = False
for arg in args: for arg in args:
if arg not in self.nodemap: if arg not in self.mn:
err = True err = True
error( "node '%s' not in network\n" % arg ) error( "node '%s' not in network\n" % arg )
else: else:
hosts.append( self.nodemap[ arg ] ) hosts.append( self.mn[ arg ] )
if not err: if not err:
self.mn.iperf( hosts ) self.mn.iperf( hosts )
else: else:
@@ -196,11 +196,11 @@ class CLI( Cmd ):
hosts = [] hosts = []
err = False err = False
for arg in args[ 1:3 ]: for arg in args[ 1:3 ]:
if arg not in self.nodemap: if arg not in self.mn:
err = True err = True
error( "node '%s' not in network\n" % arg ) error( "node '%s' not in network\n" % arg )
else: else:
hosts.append( self.nodemap[ arg ] ) hosts.append( self.mn[ arg ] )
if not err: if not err:
self.mn.iperf( hosts, l4Type='UDP', udpBw=udpBw ) self.mn.iperf( hosts, l4Type='UDP', udpBw=udpBw )
else: else:
@@ -209,13 +209,13 @@ class CLI( Cmd ):
def do_intfs( self, _line ): def do_intfs( self, _line ):
"List interfaces." "List interfaces."
for node in self.nodelist: for node in self.mn.values():
output( '%s: %s\n' % output( '%s: %s\n' %
( node.name, ','.join( node.intfNames() ) ) ) ( node.name, ','.join( node.intfNames() ) ) )
def do_dump( self, _line ): def do_dump( self, _line ):
"Dump node info." "Dump node info."
for node in self.nodelist: for node in self.mn.values():
output( '%s\n' % repr( node ) ) output( '%s\n' % repr( node ) )
def do_link( self, line ): def do_link( self, line ):
@@ -235,10 +235,10 @@ class CLI( Cmd ):
error( 'usage: %s node1 node2 ...\n' % term ) error( 'usage: %s node1 node2 ...\n' % term )
else: else:
for arg in args: for arg in args:
if arg not in self.nodemap: if arg not in self.mn:
error( "node '%s' not in network\n" % arg ) error( "node '%s' not in network\n" % arg )
else: else:
node = self.nodemap[ arg ] node = self.mn[ arg ]
self.mn.terms += makeTerms( [ node ], term = term ) self.mn.terms += makeTerms( [ node ], term = term )
def do_x( self, line ): def do_x( self, line ):
@@ -329,11 +329,11 @@ class CLI( Cmd ):
args = args[ :-1 ] args = args[ :-1 ]
rest = args.split( ' ' ) rest = args.split( ' ' )
if first in self.nodemap: if first in self.mn:
node = self.nodemap[ first ] node = self.mn[ first ]
# Substitute IP addresses for node names in command # Substitute IP addresses for node names in command
rest = [ self.nodemap[ arg ].defaultIntf().updateIP() rest = [ self.mn[ arg ].defaultIntf().updateIP()
if arg in self.nodemap else arg if arg in self.mn else arg
for arg in rest ] for arg in rest ]
rest = ' '.join( rest ) rest = ' '.join( rest )
# Run cmd on node: # Run cmd on node:
+23 -1
View File
@@ -236,7 +236,29 @@ class Mininet( object ):
def __iter__( self ): def __iter__( self ):
"return iterator over nodes" "return iterator over nodes"
return chain( self.hosts, self.switches, self.controllers ) #or dow we want to iterate of the keys i.e. node.name like a dict
for node in chain( self.hosts, self.switches, self.controllers ):
yield node.name
def __len__( self ):
"returns number of nodes in net"
return len( self.hosts ) + len( self.switches ) + len( self.controllers )
def __contains__( self, item ):
"returns True if net contains named node"
return item in self.keys()
def keys( self ):
"return a list of all node names or net's keys"
return list( self.__iter__() )
def values( self ):
"return a list of all nodes or net's values"
return [ self[name] for name in self.__iter__() ]
def items( self ):
"return (key,value) tuple list for every node in net"
return zip( self.keys(), self.values() )
def addLink( self, node1, node2, port1=None, port2=None, def addLink( self, node1, node2, port1=None, port2=None,
cls=None, **params ): cls=None, **params ):