我在ns3.py中有一个名为WIFISegment的类,如下所示:

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


此类具有一个称为def add(self,node,port = None,intfName = None,mode = None)的方法,在此方法中,我们定义了bilityhelper。

我想知道我是否可以在另一个程序中使用bilityhelper。
例如,我编写了另一个程序,然后在程序中导入WIFISegment,然后定义wifi = WIFISegment(),然后可以按照wifi.add(h1)的方式使用其“ add”方法(h1是此处的主机)。

我的问题是如何在其他程序中使用add()方法的bilityhelper。因为我每次都需要设置新的移动性。

谢谢
法尔扎内

最佳答案

最明显的方法是将其返回:

return tb, mobilityhelper


并像这样使用它:

original_ret_value, your_mobilityhelper = wifi.add(h1)


但这会破坏与旧代码的兼容性(add返回了TBIntf,但现在它返回了元组)。您可以添加可选参数以指示add方法是否应返回bilityhelper:

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


现在,如果以与以前相同的方式使用add,则其行为与在wifi.add(h1)之前相同。但是您可以使用新参数,然后获取您的移动性帮助器

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


要么

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




另一种方法是修改参数(列表)

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


它仍然与您的旧代码兼容,但是您可以将列表传递给参数,然后提取您的移动性帮助器

mobhelp_list = []
wifi.add(h1, out_mobilityhelper=mobhelp_list)
mobilityhelper = mobhelp_list[0]

09-04 07:25
查看更多