Index: fedd/federation/topdl.py
===================================================================
--- fedd/federation/topdl.py	(revision db6b092dbd30839cdaeb24ac51df1e525a79befa)
+++ fedd/federation/topdl.py	(revision cc8d8e971b7119cdebf6c0a7ae1d1da299531ce6)
@@ -48,5 +48,5 @@
 class Capacity(base):
     def __init__(self, rate, kind):
-	self.rate = rate
+	self.rate = float(rate)
 	self.kind = kind
 
@@ -55,9 +55,9 @@
 
     def to_dict(self):
-	return { 'rate': self.rate, 'kind': self.kind }
+	return { 'rate': float(self.rate), 'kind': self.kind }
 
 class Latency(base):
     def __init__(self, time, kind):
-	self.time = time
+	self.time = float(time)
 	self.kind = kind
 
@@ -66,5 +66,5 @@
 
     def to_dict(self):
-	return { 'time': self.time, 'kind': self.kind }
+	return { 'time': float(self.time), 'kind': self.kind }
 
 class Substrate(base):
@@ -117,8 +117,5 @@
 class Storage(base):
     def __init__(self, amount, persistence, attribute=[]):
-	if isinstance(amount, basestring):
-	    self.amount = float(amount)
-	else:
-	    self.amount = amount
+	self.amount = float(amount)
 	self.presistence = persistence
 	self.attribute = [ self.init_class(Attribute, a) \
@@ -411,4 +408,5 @@
 	f = open(filename, "r")
 	xp.ParseFile(f)
+	f.close()
     elif file:
 	xp.ParseFile(file)
@@ -455,10 +453,9 @@
 
     for eidx, e in enumerate(t.elements):
-	n = { }
 	if e.name: name = e.name[0]
 	else: name = "unnamed_node%d" % eidx
 	
 	ips = [ ]
-	for i in e.interfaces:
+	for idx, i in enumerate(e.interface):
 	    ip = i.get_attribute('ip4_address')
 	    ips.append(ip)
@@ -490,4 +487,99 @@
 	    })
 
-	nodes.append(n)
-    return { 'vtopo': { 'node': node, 'lan': lan } }
+    return { 'node': nodes, 'lan': lans }
+
+def topology_to_ns2(t):
+    out = """
+set ns [new Simulator]
+source tb_compat.tcl
+
+"""
+    for e in t.elements:
+	rpms = ""
+	tarfiles = ""
+	if isinstance(e, Computer):
+	    name = e.name[0]
+	    out += "set %s [$ns node]\n" % name
+	    if e.os and len(e.os) == 1:
+		osid = e.os[0].get_attribute('osid')
+		if osid:
+		    out += "tb-set-node-os $%s %s\n" % (name, osid)
+	    for s in e.software:
+		if s.install:
+		    tarfiles += "%s %s " % (s.install, s.location)
+		else:
+		    rpms += "%s " % s.location
+	    if rpms:
+		out += "tb-set-node-rpms $%s %s\n" % (name, rpms)
+	    if tarfiles:
+		out += "tb-set-node-tarfiles $%s %s\n" % (name, tarfiles)
+	    startcmd = e.get_attribute('startup')
+	    if startcmd:
+		out+= 'tb-set-node-startcmd $%s "%s"\n' % (name, startcmd)
+	    out+= "\n"
+    
+    for idx, s in enumerate(t.substrates):
+	loss = s.get_attribute('loss')
+	if s.latency:
+	    delay = s.latency.time
+	else:
+	    delay = 0
+	name = s.name or "sub%d" % idx
+
+	if len(s.interfaces) > 2:
+	    # Lan
+	    members = [ i.element.name[0] for i in s.interfaces]
+	    out += 'set %s [$ns make-lan "%s" %f %fms ]\n' % \
+		    (name, " ".join(members), s.capacity.rate, delay)
+	    if loss:
+		"tb-set-lan-loss $%s %f\n" % (name, float(loss))
+
+	    for i in s.interfaces:
+		e = i.element
+		ip = e.get_attribute("ip4_address")
+		if ip:
+		    out += "tb-set-ip-lan $%s $%s %s\n" % (e.name, name, ip)
+		if i.capacity and i.capacity.rate != s.capacity.rate:
+		    out += "tb-set-node-lan-bandwidth $%s $%s %f\n" % \
+			    (e.name[0], name, i.capacity.rate)
+		if i.latency and i.latency.time != delay:
+		    out += "tb-set-node-lan-delay $%s $%s %fms\n" % \
+			    (e.name[0], name, i.latency.time)
+		iloss = i.get_attribute('loss')
+		if loss and iloss != loss :
+		    out += "tb-set-node-lan-loss $%s $%s %f" % \
+			    (e.name[0], name, float(loss))
+	    out+= "\n"
+	elif len(s.interfaces) == 2:
+	    f = s.interfaces[0]
+	    t = s.interfaces[1]
+
+	    out += "set %s [$ns duplex-link $%s $%s %f %fms DropTail]\n" %\
+		    (name, f.element.name[0], t.element.name[0],
+			    s.capacity.rate, delay)
+	    if loss:
+		out += "tb-set-link-loss $%s %f\n" % (name, float(loss))
+
+	    for i in s.interfaces:
+		lloss = i.get_attribute("loss")
+		cap_override = i.capacity and \
+			i.capacity.rate != s.capacity.rate
+		delay_override = i.latency and \
+			i.latency.time != delay
+		loss_override = lloss and lloss != loss
+		if cap_override or delay_override or loss_override:
+		    if i.capacity: cap = i.capacity.rate
+		    else: cap = s.capacity.rate
+
+		    if i.latency: delay = i.latency.time
+
+		    if lloss: loss = lloss
+		    else: loss = loss or 0.0
+
+		    out += "tb-set-link-simplex-params $%s $%s %fms %f %f\n" % \
+			    (name, i.element.name[0], delay, cap, loss)
+	    out+= "\n"
+    out+="""
+$ns run
+"""
+    return out
