Changes between Version 17 and Version 18 of TopdlLibrary


Ignore:
Timestamp:
Oct 8, 2012 11:47:28 AM (7 years ago)
Author:
faber
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TopdlLibrary

    v17 v18  
    547547The next few examples will build on one another to show a more complex change to a topology.  We will take a topology, find the degrees of nodes, assign operating systems to them based on that degree, and output DETER-ready ns2.
    548548
    549 To try this we need a more complex topology.  The program below creates a topology with {{{nrouters}}} completely connected routers, each of which has {{{nleaves}}} leaves, except for one central router that has none.
     549To try this we need a more complex topology.  The program below creates a topology with {{{nrouters}}} completely connected routers, each of which has {{{nleaves}}} leaves, except for one central router that has none.  Here is an image of the topology when {{{nrouters}}}=4 and {{{nleaves}}}=4:
     550
     551[[Image(topo.png)]]
     552
     553We leave one router without leaves for aecthetics.
     554
     555Here is the program that generates that topology:
     556
     557{{{
     558#!/usr/bin/env python
     559
     560import sys
     561from deter import topdl
     562
     563# Parse the user-supplied routers and leaves arguments
     564try:
     565    if len(sys.argv) > 1: arg = sys.argv[1]
     566    else: arg = 4
     567    nrouters = int(arg)
     568   
     569    if len(sys.argv) > 2: arg = sys.argv[2]
     570    else: arg = 3
     571    nleaves = int(arg)
     572   
     573except ValueError, e:
     574    print >>sys.stderr, 'Usage %s routers leaves' % sys.argv[0]
     575    sys.exit('Cannot convert %s to an int' % arg)
     576
     577elems = []      # Elements in the topology
     578subs = []       # Substrates in the topology
     579
     580# Make nrouters routers
     581r = []
     582for i in range(0,nrouters):
     583    r.append(topdl.Computer(name='r-%d' % i))
     584
     585elems.extend(r)
     586# Interconnect them
     587n = 0
     588for i in range(0,nrouters):
     589    for j in range(i+1, nrouters):
     590        # Connect r[i] to r[j]
     591        # Pick a substrate name
     592        link_name = 'rlink%i' % n
     593        # Add interfaces to the two routers
     594        r[i].interface.append(topdl.Interface(substrate=[link_name]))
     595        r[j].interface.append(topdl.Interface(substrate=[link_name]))
     596        # Put a substrate into the substrate list with the name we picked
     597        subs.append(topdl.Substrate(name=link_name))
     598        n += 1
     599
     600# Put nleaves leaves on each router, except the center one
     601for i in range(0, (nrouters-1) * nleaves):
     602    # Pick a link name
     603    link_name = 'llink%i' % i
     604
     605    # add a computer with an interface on the new substrate to the element list
     606    elems.append(topdl.Computer(name='leaf%d' % i, interface=[
     607        topdl.Interface(substrate=[link_name])]))
     608   
     609    # Add an interface to a router on the new substrate
     610    r[i/nleaves].interface.append(topdl.Interface(substrate=[link_name]))
     611   
     612    # Put a substrate into the substrate list with the name we picked
     613    subs.append(topdl.Substrate(name=link_name))
     614
     615# Make the topology and print it
     616top = topdl.Topology(elements=elems, substrates=subs)
     617print topdl.topology_to_xml(top, top='experiment')
     618
     619}}}
     620
     621The code takes a few more shortcuts in construction,  e.g.,  most elements and substrates are appended directly to the lists rather than being assigned to variables, but is not fundamentally different from the start topology generator.