doc/manual/source/python.rst
author Tom Henderson <tomh@tomh.org>
Wed, 27 Jan 2016 22:29:00 -0800
changeset 11680 8ec1579db3a0
parent 11679 4e11cd28d86f
permissions -rw-r--r--
fix typos in attribute section of manual
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6742
a1759a95842c convert manual to sphinx format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     1
.. include:: replace.txt
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
     2
.. highlight:: python
6742
a1759a95842c convert manual to sphinx format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
     4
Using Python to Run |ns3|
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
     5
-------------------------
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
     6
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
     7
Python bindings allow the C++ code in |ns3| to be called from Python.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
     8
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
     9
This chapter shows you how to create a Python script that can run |ns3| and also the process of creating Python bindings for a C++ |ns3| module.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    10
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    11
Introduction
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    12
************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    13
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    14
The goal of Python bindings for |ns3| are two fold:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    15
7478
768d1500f0c1 Add Gnuplot section to ns-3 Manual
Mitch Watrous <watrous@u.washington.edu>
parents: 7311
diff changeset
    16
#. Allow the programmer to write complete simulation scripts in Python (http://www.python.org);
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    17
#. Prototype new models (e.g. routing protocols).
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    18
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    19
For the time being, the primary focus of the bindings is the first goal, but the second goal will eventually be supported as well.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    20
Python bindings for |ns3| are being developed using a new tool called PyBindGen (http://code.google.com/p/pybindgen).
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    21
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    22
An Example Python Script that Runs |ns3|
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    23
****************************************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    24
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    25
Here is some example code that is written in Python and that runs |ns3|, which is written in C++.  This Python example can be found in ``examples/tutorial/first.py``:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    26
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    27
::
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    28
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    29
  import ns.applications
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    30
  import ns.core
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    31
  import ns.internet
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    32
  import ns.network
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    33
  import ns.point_to_point
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    34
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    35
  ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    36
  ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    37
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    38
  nodes = ns.network.NodeContainer()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    39
  nodes.Create(2)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    40
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    41
  pointToPoint = ns.point_to_point.PointToPointHelper()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    42
  pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    43
  pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    44
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    45
  devices = pointToPoint.Install(nodes)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    46
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    47
  stack = ns.internet.InternetStackHelper()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    48
  stack.Install(nodes)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    49
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    50
  address = ns.internet.Ipv4AddressHelper()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    51
  address.SetBase(ns.network.Ipv4Address("10.1.1.0"), ns.network.Ipv4Mask("255.255.255.0"))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    52
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    53
  interfaces = address.Assign (devices);
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    54
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    55
  echoServer = ns.applications.UdpEchoServerHelper(9)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    56
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    57
  serverApps = echoServer.Install(nodes.Get(1))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    58
  serverApps.Start(ns.core.Seconds(1.0))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    59
  serverApps.Stop(ns.core.Seconds(10.0))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    60
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    61
  echoClient = ns.applications.UdpEchoClientHelper(interfaces.GetAddress(1), 9)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    62
  echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    63
  echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds (1.0)))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    64
  echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(1024))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    65
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    66
  clientApps = echoClient.Install(nodes.Get(0))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    67
  clientApps.Start(ns.core.Seconds(2.0))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    68
  clientApps.Stop(ns.core.Seconds(10.0))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    69
  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    70
  ns.core.Simulator.Run()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    71
  ns.core.Simulator.Destroy()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    72
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    73
Running Python Scripts
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    74
**********************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    75
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    76
waf contains some options that automatically update the python path to find the ns3 module.  To run example programs, there are two ways to use waf to take care of this.  One is to run a waf shell; e.g.:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    77
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
    78
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    79
11679
4e11cd28d86f update documentation for Waf shell command syntax
Tom Henderson <tomh@tomh.org>
parents: 10401
diff changeset
    80
  $ ./waf shell
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
    81
  $ python examples/wireless/mixed-wireless.py
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    82
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    83
and the other is to use the --pyrun option to waf:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    84
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
    85
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    86
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
    87
  $ ./waf --pyrun examples/wireless/mixed-wireless.py
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    88
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    89
To run a python script under the C debugger:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    90
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
    91
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    92
11679
4e11cd28d86f update documentation for Waf shell command syntax
Tom Henderson <tomh@tomh.org>
parents: 10401
diff changeset
    93
  $ ./waf shell
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
    94
  $ gdb --args python examples/wireless/mixed-wireless.py
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    95
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    96
To run your own Python script that calls |ns3| and that has this path, ``/path/to/your/example/my-script.py``, do the following:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    97
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
    98
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
    99
11679
4e11cd28d86f update documentation for Waf shell command syntax
Tom Henderson <tomh@tomh.org>
parents: 10401
diff changeset
   100
  $ ./waf shell
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   101
  $ python /path/to/your/example/my-script.py
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   102
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   103
Caveats
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   104
*******
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   105
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   106
Python bindings for |ns3| are a work in progress, and some limitations are known by developers.  Some of these limitations (not all) are listed here.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   107
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   108
Incomplete Coverage
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   109
+++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   110
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   111
First of all, keep in mind that not 100% of the API is supported in Python.  Some of the reasons are:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   112
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   113
#. some of the APIs involve pointers, which require knowledge of what kind of memory passing semantics (who owns what memory). Such knowledge is not part of the function signatures, and is either documented or sometimes not even documented.  Annotations are needed to bind those functions;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   114
#. Sometimes a unusual fundamental data type or C++ construct is used which is not yet supported by PyBindGen;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   115
#. GCC-XML does not report template based classes unless they are instantiated.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   116
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   117
Most of the missing APIs can be wrapped, given enough time, patience, and expertise, and will likely be wrapped if bug reports are submitted.  However, don't file a bug report saying "bindings are incomplete", because we do not have manpower to complete 100% of the bindings.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   118
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   119
Conversion Constructors
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   120
+++++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   121
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   122
`Conversion constructors <http://publib.boulder.ibm.com/infocenter/compbgpl/v9v111/topic/com.ibm.xlcpp9.bg.doc/language_ref/cplr384.htm>`_ are not fully supported yet by PyBindGen, and they always act as explicit constructors when translating an API into Python.  For example, in C++ you can do this:
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   123
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   124
.. sourcecode:: cpp
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   125
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   126
 Ipv4AddressHelper ipAddrs;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   127
 ipAddrs.SetBase ("192.168.0.0", "255.255.255.0");
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   128
 ipAddrs.Assign (backboneDevices);
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   129
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   130
In Python, for the time being you have to do:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   131
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   132
::
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   133
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   134
 ipAddrs = ns3.Ipv4AddressHelper()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   135
 ipAddrs.SetBase(ns3.Ipv4Address("192.168.0.0"), ns3.Ipv4Mask("255.255.255.0"))
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   136
 ipAddrs.Assign(backboneDevices)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   137
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   138
CommandLine
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   139
+++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   140
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   141
:cpp:func:`CommandLine::AddValue` works differently in Python than it does in |ns3|.  In Python, the first parameter is a string that represents the command-line option name.  When the option is set, an attribute with the same name as the option name is set on the :cpp:func:`CommandLine` object.  Example:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   142
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   143
::
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   144
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   145
    NUM_NODES_SIDE_DEFAULT = 3
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   146
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   147
    cmd = ns3.CommandLine()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   148
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   149
    cmd.NumNodesSide = None
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   150
    cmd.AddValue("NumNodesSide", "Grid side number of nodes (total number of nodes will be this number squared)")
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   151
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   152
    cmd.Parse(argv)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   153
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   154
    [...]
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   155
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   156
    if cmd.NumNodesSide is None:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   157
        num_nodes_side = NUM_NODES_SIDE_DEFAULT
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   158
    else:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   159
        num_nodes_side = int(cmd.NumNodesSide)
6742
a1759a95842c convert manual to sphinx format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   160
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   161
Tracing
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   162
+++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   163
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   164
Callback based tracing is not yet properly supported for Python, as new |ns3| API needs to be provided for this to be supported.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   165
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   166
Pcap file writing is supported via the normal API.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   167
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   168
Ascii tracing is supported since |ns3|.4 via the normal C++ API translated to Python.  However, ascii tracing requires the creation of an ostream object to pass into the ascii tracing methods.  In Python, the C++ std::ofstream has been minimally wrapped to allow this.  For example:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   169
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   170
::
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   171
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   172
    ascii = ns3.ofstream("wifi-ap.tr") # create the file
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   173
    ns3.YansWifiPhyHelper.EnableAsciiAll(ascii)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   174
    ns3.Simulator.Run()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   175
    ns3.Simulator.Destroy()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   176
    ascii.close() # close the file
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   177
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   178
There is one caveat: you must not allow the file object to be garbage collected while |ns3| is still using it.  That means that the 'ascii' variable above must not be allowed to go out of scope or else the program will crash.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   179
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   180
Cygwin limitation
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   181
+++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   182
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   183
Python bindings do not work on Cygwin.  This is due to a gccxml bug.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   184
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   185
You might get away with it by re-scanning API definitions from within the
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   186
cygwin environment (./waf --python-scan).  However the most likely solution
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   187
will probably have to be that we disable python bindings in CygWin.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   188
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   189
If you really care about Python bindings on Windows, try building with mingw and native
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   190
python instead.  Or else, to build without python bindings, disable python bindings in the configuration stage:  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   191
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   192
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   193
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   194
  $ ./waf configure --disable-python
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   195
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   196
Working with Python Bindings
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   197
****************************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   198
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   199
There are currently two kinds of Python bindings in |ns3|:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   200
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   201
#. Monolithic bindings contain API definitions for all of the modules and can be found in a single directory, ``bindings/python``.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   202
#. Modular bindings contain API definitions for a single module and can be found in each module's  ``bindings`` directory. 
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   203
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   204
Python Bindings Workflow
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   205
++++++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   206
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   207
The process by which Python bindings are handled is the following:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   208
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   209
#. Periodically a developer uses a GCC-XML (http://www.gccxml.org) based API scanning script, which saves the scanned API definition as ``bindings/python/ns3_module_*.py`` files or as Python files in each modules' ``bindings`` directory.  These files are kept under version control in the main |ns3| repository;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   210
#. Other developers clone the repository and use the already scanned API definitions;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   211
#. When configuring |ns3|, pybindgen will be automatically downloaded if not already installed.  Released |ns3| tarballs will ship a copy of pybindgen.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   212
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   213
If something goes wrong with compiling Python bindings and you just want to ignore them and move on with C++, you can disable Python with:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   214
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   215
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   216
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   217
  $ ./waf --disable-python
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   218
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   219
Instructions for Handling New Files or Changed API's
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   220
****************************************************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   221
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   222
So you have been changing existing |ns3| APIs and Python bindings no longer compile?  Do not despair, you can rescan the bindings to create new bindings that reflect the changes to the |ns3| API.
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   223
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   224
Depending on if you are using monolithic or modular bindings, see the discussions below to learn how to rescan your Python bindings. 
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   225
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   226
Monolithic Python Bindings
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   227
**************************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   228
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   229
Scanning the Monolithic Python Bindings
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   230
+++++++++++++++++++++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   231
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   232
To scan the monolithic Python bindings do the following:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   233
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   234
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   235
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   236
  $ ./waf --python-scan  
6742
a1759a95842c convert manual to sphinx format
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   237
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   238
Organization of the Monolithic Python Bindings
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   239
++++++++++++++++++++++++++++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   240
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   241
The monolithic Python API definitions are organized as follows. For each |ns3| module <name>, the file ``bindings/python/ns3_module_<name>.py`` describes its API.  Each of those files have 3 toplevel functions:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   242
7662
c13e33e96231 clear warnings in manual build process
Tom Henderson <tomh@tomh.org>
parents: 7478
diff changeset
   243
#. :py:func:`def register_types(module)`: this function takes care of registering new types (e.g. C++ classes, enums) that are defined in tha module;
c13e33e96231 clear warnings in manual build process
Tom Henderson <tomh@tomh.org>
parents: 7478
diff changeset
   244
#. :py:func:`def register_methods(module)`: this function calls, for each class <name>, another function register_methods_Ns3<name>(module).  These latter functions add method definitions for each class;
c13e33e96231 clear warnings in manual build process
Tom Henderson <tomh@tomh.org>
parents: 7478
diff changeset
   245
#. :py:func:`def register_functions(module)`: this function registers |ns3| functions that belong to that module.
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   246
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   247
Modular Python Bindings
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   248
***********************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   249
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   250
Overview
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   251
++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   252
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   253
Since ns 3.11, the modular bindings are being added, in parallel to the old monolithic bindings.  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   254
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   255
The new python bindings are generated into an 'ns' namespace, instead of 'ns3' for the old bindings.  Example:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   256
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   257
::
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   258
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   259
  from ns.network import Node
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   260
  n1 = Node()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   261
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   262
With modular Python bindings:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   263
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   264
#. There is one separate Python extension module for each |ns3| module;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   265
#. Scanning API definitions (apidefs) is done on a per ns- module basis;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   266
#. Each module's apidefs files are stored in a 'bindings' subdirectory of the module directory;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   267
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   268
Scanning the Modular Python Bindings
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   269
+++++++++++++++++++++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   270
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   271
To scan the modular Python bindings for the core module, for example, do the following:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   272
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   273
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   274
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   275
  $ ./waf --apiscan=core
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   276
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   277
To scan the modular Python bindings for all of the modules, do the following:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   278
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   279
.. sourcecode:: bash
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   280
9957
1a4d84a85bad Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents: 9824
diff changeset
   281
  $ ./waf --apiscan=all
7311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   282
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   283
Creating a New Module
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   284
+++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   285
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   286
If you are adding a new module, Python bindings will continue to compile but will not cover the new module.  
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   287
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   288
To cover a new module, you have to create a ``bindings/python/ns3_module_<name>.py`` file, similar to the what is described in the previous sections, and register it in the variable :cpp:func:`LOCAL_MODULES` in ``bindings/python/ns3modulegen.py``
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   289
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   290
Adding Modular Bindings To A Existing Module
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   291
++++++++++++++++++++++++++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   292
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   293
To add support for modular bindings to an existing |ns3| module, simply add the following line to its wscript build() function:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   294
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   295
::
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   296
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   297
    bld.ns3_python_bindings()
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   298
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   299
Organization of the Modular Python Bindings
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   300
+++++++++++++++++++++++++++++++++++++++++++
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   301
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   302
The ``src/<module>/bindings`` directory may contain the following files, some of them optional:
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   303
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   304
* ``callbacks_list.py``: this is a scanned file, DO NOT TOUCH.  Contains a list of Callback<...> template instances found in the scanned headers;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   305
* ``modulegen__gcc_LP64.py``: this is a scanned file, DO NOT TOUCH.  Scanned API definitions for the GCC, LP64 architecture (64-bit)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   306
* ``modulegen__gcc_ILP32.py``: this is a scanned file, DO NOT TOUCH.  Scanned API definitions for the GCC, ILP32 architecture (32-bit)
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   307
* ``modulegen_customizations.py``: you may optionally add this file in order to customize the pybindgen code generation
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   308
* ``scan-header.h``: you may optionally add this file to customize what header file is scanned for the module.  Basically this file is scanned instead of ns3/<module>-module.h.  Typically, the first statement is #include "ns3/<module>-module.h", plus some other stuff to force template instantiations;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   309
* ``module_helpers.cc``: you may add additional files, such as this, to be linked to python extension module, but they have to be registered in the wscript. Look at src/core/wscript for an example of how to do so;
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   310
* ``<module>.py``: if this file exists, it becomes the "frontend" python module for the ns3 module, and the extension module (.so file) becomes _<module>.so instead of <module>.so.  The <module>.py file has to import all symbols from the module _<module> (this is more tricky than it sounds, see src/core/bindings/core.py for an example), and then can add some additional pure-python definitions.   
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   311
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   312
More Information for Developers
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   313
*******************************
b1e882592130 Fix Bug 1191 again and turn Python bindings wiki into manual chapter
Mitch Watrous <watrous@u.washington.edu>
parents: 6742
diff changeset
   314
10401
6e9d4ceb880b Update ns-3 wiki URL
Daniel Lertpratchya <nikkipui@gmail.com>
parents: 9957
diff changeset
   315
If you are a developer and need more information on |ns3|'s Python bindings, please see the `Python Bindings wiki page <http://www.nsnam.org/wiki/NS-3_Python_Bindings>`_.