mininet-wifi之Association/Handover

之前搜的时候,搜到了这里的,还以为到时候程序执行的时候,会调用这里的库
这里写图片描述
使用pdb调试的时候才知道python的这些库,是在这里:
/usr/local/lib/python2.7/dist-packages/mininet_wifi-2.2.1d1-py2.7.egg/mininet/wifi/net.py
这里写图片描述
然后下面记录一下找到association的过程。

mininet/wifi/associationControl.py

from mininet.log import debug

class associationControl(object):

    changeAP = False

    def __init__(self, sta, ap, wlan, ac):
        self.customAssociationControl(sta, ap, wlan, ac)

    def customAssociationControl(self, sta, ap, wlan, ac):
        """Mechanisms that optimize the use of the APs   自定义AP的Association的方式
        llf: Least-loaded-first                          负载最小的优先()
        ssf: Strongest-signal-first                      信号最强的优先
        """
        if ac == "llf":    #负载最小优先
            apref = sta.params['associatedTo'][wlan]
            if apref != '':
                ref_llf = len(apref.params['associatedStations'])
                if len(ap.params['associatedStations']) + 2 < ref_llf:
                    debug('iw dev %s disconnect' % sta.params['wlan'][wlan])
                    # 断开当前wlan网络
                    sta.pexec('iw dev %s disconnect' % sta.params['wlan'][wlan])
                    self.changeAP = True
            else:
                self.changeAP = True
        elif ac == "ssf":    #信号最强优先
            distance = sta.get_distance_to(sta.params['associatedTo'][wlan])
            rssi = sta.set_rssi(sta.params['associatedTo'][wlan],
                                wlan, distance)
            ref_dist = sta.get_distance_to(ap)
            ref_rssi = sta.set_rssi(ap, wlan, ref_dist)
            if float(ref_rssi) > float(rssi + 0.1):
                debug('iw dev %s disconnect' % sta.params['wlan'][wlan])
                sta.pexec('iw dev %s disconnect' % sta.params['wlan'][wlan])
                self.changeAP = True
        return self.changeAP

其中sta.pexec('iw dev %s disconnect' % sta.params['wlan'][wlan])这一句,是在sta上执行字符串中的命令。在这个命令中,后面的sta.params['wlan'][wlan]中的中括号里的带引号的’wlan’是指sta中的所有wlan网卡,然后再加一个[wlan]这个wlan是一个wlan id,表示第几个wlan网卡,所以这句话的意思就是使用iw dev命令断开当前wlan网路。

mininet/wifi/mobility.py

class mobility(object):
    ...    
    @classmethod
    def handover(cls, sta, ap, wlan, ap_wlan):
        """handover

        :param sta: station
        :param ap: access point
        :param wlan: wlan ID"""
        changeAP = False
        # Assocation策略不为空,并且sta的Associated AP不是参数ap,并且这个wlan不为空
        "Association Control: mechanisms that optimize the use of the APs"
        if cls.AC != '' and sta.params['associatedTo'][wlan] != ap \
                and sta.params['associatedTo'][wlan] != '':
            ac = cls.AC
            value = associationControl(sta, ap, wlan, ac)
            changeAP = value.changeAP
        if sta.params['associatedTo'][wlan] == '' or changeAP is True:
            if ap not in sta.params['associatedTo']:
                Association.printCon = False
                Association.associate_infra(sta, ap, wlan, ap_wlan)

关键的是associationControl()这个方法。在mininet/wifi/associationControl.py中的associationControl类。

from mininet.wifi.associationControl import associationControl

mininet/wifi/link.py

class Association(object):
    ...
    @classmethod
    def associate(cls, sta, ap, enable_wmediumd, enable_interference,
                  wlan=0, ap_wlan=0):
        "Associate to Access Point"
        if wlan == 0:
            wlan = sta.ifaceToAssociate
        if 'position' in sta.params:
            cls.configureWirelessLink(sta, ap, enable_wmediumd,
                                      enable_interference, ap_wlan=0)
        else:
            cls.associate_infra(sta, ap, wlan=0, ap_wlan=0)
        sta.ifaceToAssociate += 1
    @classmethod
    def associate_infra(cls, sta, ap, wlan, ap_wlan):
        """Association when infra

        :param sta: station
        :param ap: access point
        :param wlan: wlan ID"""
        associated = 0
        if 'ieee80211r' in ap.params and ap.params['ieee80211r'] == 'yes' \
        and ('encrypt' not in sta.params or 'encrypt' in sta.params and
                        'wpa' in sta.params['encrypt'][wlan]):
            if sta.params['associatedTo'][wlan] == '':
                command = ('ps -aux | grep %s | wc -l' % sta.params['wlan'][wlan])
                np = int(subprocess.check_output(command, shell=True))
                if np == 2:
                    cls.associate_wpa(sta, ap, wlan, ap_wlan)
                else:
                    cls.handover_ieee80211r(sta, ap, wlan, ap_wlan)
            else:
                cls.handover_ieee80211r(sta, ap, wlan, ap_wlan)
            associated = 1
        elif 'encrypt' not in ap.params:
            associated = 1
            cls.associate_noEncrypt(sta, ap, wlan, ap_wlan)
        else:
            if sta.params['associatedTo'][wlan] == '':
                if 'wpa' in ap.params['encrypt'][ap_wlan] \
                and ('encrypt' not in sta.params or 'encrypt' in sta.params and
                                'wpa' in sta.params['encrypt'][wlan]):
                    cls.associate_wpa(sta, ap, wlan, ap_wlan)
                    associated = 1
                elif ap.params['encrypt'][ap_wlan] == 'wep':
                    cls.associate_wep(sta, ap, wlan, ap_wlan)
                    associated = 1
        if cls.printCon:
            iface = sta.params['wlan'][wlan]
            info("Associating %s to %s\n" % (iface, ap))
        if associated:
            cls.update_association(sta, ap, wlan)

3月29日更新
取消自动关联

allAutoAssociation = False

然后根据算法决策,主动关联到某ap(当然前提是该ap在此sta的信号范围内)

from mininet.wifi.link import Association
Association.associate(sta, ap, ...)

参考:
这里写图片描述
https://groups.google.com/forum/#!topic/mininet-wifi-discuss/YMuDNTuoS0k

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值