Fix SSHD example by generalizing input intf args

A number of functions in node.py look like this:
   return self.intf( intf ).<other stuff>

Previously, self.intf(...) in Node would expect a string name for an
interface and return None if an object was passed in instead of a
string name.

Now, be more permissive and assume that objects passed in are for Intf
objects.  This makes all such functions in node.py handle more flexible
input args, either name or actual Intf object.

An alternative and equally valid approach would be to raise an Exception
whenever a non-string, non-falsy value was passed in to Node.intf(), and
to modify the code in at least one place - examples/sshd.py - to pass
the interface name, rather than the interface object.

Also fix input args for examples/scratchnetuser.py - the interface name
was being passed in as the prefix len, which makes no sense.
This commit is contained in:
Brandon Heller
2012-11-13 22:44:47 -08:00
parent 12fea0f6d5
commit bf208cdeb6
2 changed files with 14 additions and 8 deletions
+4 -4
View File
@@ -38,12 +38,12 @@ def scratchNetUser( cname='controller', cargs='ptcp:' ):
h1intf, sintf2 = linkIntfs( h1, switch )
info( '*** Configuring control network\n' )
controller.setIP( '10.0.123.1/24', cintf )
switch.setIP( '10.0.123.2/24', sintf)
controller.setIP( '10.0.123.1/24', intf=cintf )
switch.setIP( '10.0.123.2/24', intf=sintf)
info( '*** Configuring hosts\n' )
h0.setIP( '192.168.123.1/24', h0intf )
h1.setIP( '192.168.123.2/24', h1intf )
h0.setIP( '192.168.123.1/24', intf=h0intf )
h1.setIP( '192.168.123.2/24', intf=h1intf )
info( '*** Network state:\n' )
for node in controller, switch, h0, h1:
+10 -4
View File
@@ -358,14 +358,20 @@ class Node( object ):
'has no interfaces\n' )
def intf( self, intf='' ):
"""Return our interface object with given name,
or default intf if name is empty"""
"""Return our interface object with given string name,
default intf if name is falsy (None, empty string, etc).
or the input intf arg.
Having this fcn return its arg for Intf objects makes it
easier to construct functions with flexible input args for
interfaces (those that accept both string names and Intf objects).
"""
if not intf:
return self.defaultIntf()
elif type( intf) is str:
return self.nameToIntf[ intf ]
else:
return None
return intf
def connectionsTo( self, node):
"Return [ intf1, intf2... ] for all intfs that connect self to node."
@@ -425,7 +431,7 @@ class Node( object ):
def setIP( self, ip, prefixLen=8, intf=None ):
"""Set the IP address for an interface.
intf: interface name
intf: intf or intf name
ip: IP address as a string
prefixLen: prefix length, e.g. 8 for /8 or 16M addrs"""
# This should probably be rethought