[Ns-developers] NS-3 PyBindGen again

Gustavo Carneiro gjcarneiro at gmail.com
Sun Jan 6 15:51:01 PST 2008


  Hello again.

  Good news: I have finally wrapped NS-3 Simulator::Schedule* and most
concrete callbacks (Callback<T1,T2, ...>).  It is not uploaded yet because
of transient  mercurial server problems, but if anyone is impatient just
checkout the normal gjc/ns-3-pybindgen branch and apply this bundle:
 http://telecom.inescporto.pt/~gjc/ns-3-pybindgen.bundle

  Notably missing is the support for parameters of type CallbackBase.  This
is used mainly for tracing.  But I do not find it that much useful for
Python (tracing with Python callbacks will surely slow down simulations
considerably), so I'd rather not work on it for now.  I'm sure there are
many APIs still missing; they will require more manual work, but I belive
most methods are being supported, at least for scritping kind of work.  For
protocol implementation I am not so sure.

  The Python API is very similar to the C++ one.  For example, the script
below is a port of samples/main-simple.cc to Python.

  The generated source file is currently about 2.4MB, 68k lines.  Although I
would love to be able to split this single source file into several smaller
files, some day.

  So what are the next steps?  First I think the NS-3 team needs to decide
whether to adopt this solution.  I certainly hope so, or else I wasted a ton
of effort :P

  Next, how would it be best integrated with NS-3?  I can see several
degrees of integration:

   1. Make python bindings generation mandatory, require gccxml (currently
from CVS only, unfortunately), pygccxml, and pybindgen, from everybody;

   2. Ship with NS-3 a pregenerated ns3module.cc file;

      a) And add a --pybindgen waf option to re-generate the file (just in
case someone decides to modify the API and make the bindings stop compiling)
?

      b) Add the ns3module.cc to the repository also, for developers?  I
would adivse against it, though, as the file is rather large...

  If no major problems are found, I would envision start of python bindings
since the NS-3 release in mid February.

------------------------------------------------------------
#!/usr/bin/env python
import ns3 as ns

def GenerateTraffic(socket, size):
    print "at=", ns.Simulator.Now().GetSeconds(), "s, tx bytes=", size
    socket.Send(ns.Packet(size))
    if size > 0:
        ns.Simulator.Schedule(ns.Seconds(0.5), GenerateTraffic, socket, size
- 50)
    else:
        socket.Close()

def SocketPrinter(socket, packet, addrFrom):
    print "at=", ns.Simulator.Now().GetSeconds(), "s, rx bytes=",
packet.GetSize()

def PrintTraffic(socket):
    socket.SetRecvCallback(SocketPrinter)

def RunSimulation():
    nodeA = ns.InternetNode()

    iid = ns.InterfaceId.LookupByName("Udp")
    socketFactory = nodeA.QueryInterface(iid)

    sink = socketFactory.CreateSocket()
    local = ns.InetSocketAddress(ns.Ipv4Address.GetAny(), 80)
    sink.Bind(local)

    source = socketFactory.CreateSocket()
    remote = ns.InetSocketAddress(ns.Ipv4Address.GetLoopback(), 80)
    source.Connect(remote)

    GenerateTraffic(source, 500)
    PrintTraffic(sink)

    ns.Simulator.Run()

    ns.Simulator.Destroy()

if __name__ == '__main__':
    RunSimulation()


-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert


More information about the Ns-developers mailing list