2

I have a class called WIFISegment as below in ns3.py :

class WIFISegment( object ):
    """Equivalent of radio WiFi channel.
       Only Ap and WDS devices support SendFrom()."""
    def __init__( self ):
        # Helpers instantiation.
        self.channelhelper = ns.wifi.YansWifiChannelHelper.Default()
        self.phyhelper = ns.wifi.YansWifiPhyHelper.Default()
        self.wifihelper = ns.wifi.WifiHelper.Default()
        self.machelper = ns.wifi.NqosWifiMacHelper.Default()
        # Setting channel to phyhelper.
        self.channel = self.channelhelper.Create()
        self.phyhelper.SetChannel( self.channel )

    def add( self, node, port=None, intfName=None, mode=None ):
        """Connect Mininet node to the segment.
           Will create WifiNetDevice with Mac type specified in
           the MacHelper (default: AdhocWifiMac).
           node: Mininet node
           port: node port number (optional)
           intfName: node tap interface name (optional)
           mode: TapBridge mode (UseLocal or UseBridge) (optional)"""
        # Check if this Mininet node has assigned an underlying ns-3 node.
        if hasattr( node, 'nsNode' ) and node.nsNode is not None:
            # If it is assigned, go ahead.
            pass
        else:
            # If not, create new ns-3 node and assign it to this Mininet node.
            node.nsNode = ns.network.Node()
            allNodes.append( node )
        # Install new device to the ns-3 node, using provided helpers.
        device = self.wifihelper.Install( self.phyhelper, self.machelper, node.nsNode ).Get( 0 )
        mobilityhelper = ns.mobility.MobilityHelper()
        # Install mobility object to the ns-3 node.
        mobilityhelper.Install( node.nsNode )
        # If port number is not specified...
        if port is None:
            # ...obtain it automatically.
            port = node.newPort()
        # If interface name is not specified...
        if intfName is None:
            # ...obtain it automatically.
            intfName = Link.intfName( node, port ) # classmethod
        # In the specified Mininet node, create TBIntf bridged with the 'device'.
        tb = TBIntf( intfName, node, port, node.nsNode, device, mode )
        return tb

This class has a method called def add( self, node, port=None, intfName=None, mode=None ) and in this method we define mobilityhelper.

I was wondering if I can use mobilityhelper in another program. For example, I wrote another program and in my program I import WIFISegment, then define wifi = WIFISegment() and I can use its method "add" as follow wifi.add(h1) (h1 is host here).

My question is How I can use mobilityhelper of add() method in my other program. Because I need to set a new mobility each time.

Thanks Farzaneh

user226876
  • 51
  • 8

2 Answers2

2

The obvious way is to return it:

return tb, mobilityhelper

and use it like this:

original_ret_value, your_mobilityhelper = wifi.add(h1)

But that would break compatibility with your old code (add returned TBIntf, but now it returns tuple). You could add optional parameter to indicate whether the add method should return mobilityhelper or not:

def add( self, node, port=None, intfName=None, mode=None, return_mobilityhelper=False ):
    ...
    if return_mobilityhelper:
        return tb, mobilityhelper
    else:
        return tb

Now if you use add the same way you did before, it behaves the same way it did before wifi.add(h1). But you can use the new parameter and then get your mobilityhelper

whatever, mobilityhelper = wifi.add(h1, return_mobilityhelper=True)

or

returned_tuple = wifi.add(h1, return_mobilityhelper=True)
mobilityhelper = returned_tuple[1]

The other way is to modify a parameter (a list)

def add( self, node, port=None, intfName=None, mode=None, out_mobilityhelper=None):
    ...
    if hasattr(out_mobilityhelper, "append"):  
        out_mobilityhelper.append(mobilityhelper)
    return tb

It's still compatible with your old code but you can pass a list to the parameter and then extract your mobilityhelper

mobhelp_list = []
wifi.add(h1, out_mobilityhelper=mobhelp_list)
mobilityhelper = mobhelp_list[0]
then0rTh
  • 165
  • 3
  • 3
  • Thanks a lot for your reply. It could help me a lot. Because of the limited space of the comment I ask the second part of my question here, as using mobility in ns3 is a little bit different than defining mobhelp_list = []. – user226876 Feb 18 '16 at 01:52
  • @user226876 Well, you did confused me a little bit :D Both ways I suggested are for getting the `ns.mobility.MobilityHelper` object you created in `add` method. But maybe you wanted to provide premade mobilityhelper to the `add` method in the first place (create the mobilityhelper in your other program and pass it to `add` method as argument)? – then0rTh Feb 18 '16 at 11:13
1

The mobilityhelper is defining as below in class WIFISegment add method (as mentioned in the last question) :

        mobilityhelper = ns.mobility.MobilityHelper()

        # Install mobility object to the ns-3 node.
        mobilityhelper.Install( node.nsNode )

and it installs on each host (node.nsNode) when we use wifi.add().

Now in my other program I want to use this mobilityhelper, but install a new type of mobility such as below:

      mobilityhelper.SetPositionAllocator ("ns3::GridPositionAllocator",
                             "MinX", ns.core.DoubleValue(0.0),
                             "MinY", ns.core.DoubleValue(0.0),
                             "DeltaX", ns.core.DoubleValue(130.0),
                             "DeltaY", ns.core.DoubleValue(100.0),
                             "GridWidth", ns.core.UintegerValue(2),
                             "LayoutType", ns.core.StringValue("RowFirst"))

     mobilityhelper.SetMobilityModel("ns3::ConstantPositionMobilityModel")


        # Install mobility object to the ns-3 node.
        mobilityhelper.Install( h1 )

Now was wondering how I can use that mobility in my new program and install a new mobility for my node.

I hope I don't make you confuse.

user226876
  • 51
  • 8