示例#1
0
 def run_test(self, method=None):
     while not rospy.is_shutdown():
         random.shuffle(self.bsses)
         for bss in self.bsses:
             yield async_helpers.async_sleep(0.1)
             yield self.try_assoc(bss, method)
             #out = ["\r\033[2K"]
             out = []
             for counter in self.counters:
                 out.append(str(counter))
             print >> sys.stderr, " ".join(out)
             sys.stdout.flush()
示例#2
0
 def run_test(self, method = None):
     while not rospy.is_shutdown():
         random.shuffle(self.bsses)
         for bss in self.bsses:
             yield async_helpers.async_sleep(0.1)
             yield self.try_assoc(bss, method)
             #out = ["\r\033[2K"]
             out = []
             for counter in self.counters:
                 out.append(str(counter))
             print >> sys.stderr, " ".join(out)
             sys.stdout.flush()
示例#3
0
    def try_assoc(self, bss, method=None):
        # Already on this AP
        cur_assoc = self.radio.associated.get()
        if cur_assoc:
            if mac_addr.same(cur_assoc.bssid, bss['bssid']):
                print >> sys.stderr, "Already associated to", bss['name']
                return
        del cur_assoc

        # Scan for this AP
        self.radio.scan([bss['freq']])
        pre_scan_assoc = self.radio.associated.get()
        print >> sys.stderr, "Scanning"
        scan_results = yield async_helpers.wait_for_event(
            self.radio.scan_results_event)
        post_scan_assoc = self.radio.associated.get()
        if pre_scan_assoc and post_scan_assoc:
            self.bgscan_counter.count(scan_results)
        elif not pre_scan_assoc and not post_scan_assoc:
            self.scan_counter.count(scan_results)
        else:
            self.mixed_scan_counter.count(scan_results)
        if not scan_results:
            print >> sys.stderr, "\nScan failed"
            return
        target_bss = filter(
            lambda cbss: mac_addr.same(cbss.bssid, bss['bssid']) and cbss.ssid
            == bss['ssid'], scan_results)
        self.ap_not_found_counter.count(target_bss)
        if not target_bss:
            print >> sys.stderr, "\nAP not found", bss['name']
            self.bss_counters[bss['name']].count(False)
            return

        # Decide which associate method to use
        if not self.radio.associated.get():
            method = SlowAssoc

        if method is None:
            # Randomly sample with priority to least sampled.
            r = random.random()
            universe = dict(
                (m, c.tries) for m, c in self.assoc_counters.iteritems())
            most_visited = max(universe.itervalues())
            for m in universe:
                universe[m] = 2 + max(universe[m] - most_visited, 0)
            total = float(sum(universe.values()))
            for method in universe:
                r -= universe[method] / total
                if r < 0:
                    break

        print >> sys.stderr, "Associate method", method

        if method != Reassociate:
            self.radio.unassociate()
            print >> sys.stderr, "Unassociating"
            yield async_helpers.wait_for_state(
                self.radio.associated, lambda x: x == radio.Unassociated)

        if method == SlowAssoc:
            print >> sys.stderr, "Waiting after unassociate"
            yield async_helpers.async_sleep(3)

        # Associate
        self.radio.associate((bss['ssid'], bss['bssid']))
        print >> sys.stderr, "Associating"
        yield async_helpers.wait_for_state(self.radio.associated,
                                           lambda x: x != radio.Associating)

        # Log success
        assoc_state = self.radio.associated.get()
        self.assoc_counters[method].count(assoc_state)
        self.bss_counters[bss['name']].count(assoc_state)

        if self.radio.associated.get():
            # If association succeeded, make sure it is stable.
            print >> sys.stderr, "Waiting after associating", bss[
                'name'], method
            yield async_helpers.async_sleep(1)
            self.early_disassoc_counter.count(self.radio.associated.get())
        else:
            print >> sys.stderr, "\nAssoc failed", bss['name'], method
            system.system('iwconfig')
示例#4
0
    def try_assoc(self, bss, method = None):
        # Already on this AP
        cur_assoc = self.radio.associated.get() 
        if cur_assoc:
            if mac_addr.same(cur_assoc.bssid, bss['bssid']):
                print >> sys.stderr, "Already associated to", bss['name']
                return
        del cur_assoc

        # Scan for this AP
        self.radio.scan([bss['freq']])
        pre_scan_assoc = self.radio.associated.get()
        print >> sys.stderr, "Scanning"
        scan_results = yield async_helpers.wait_for_event(self.radio.scan_results_event)
        post_scan_assoc = self.radio.associated.get()
        if pre_scan_assoc and post_scan_assoc:
            self.bgscan_counter.count(scan_results)
        elif not pre_scan_assoc and not post_scan_assoc:
            self.scan_counter.count(scan_results)
        else:
            self.mixed_scan_counter.count(scan_results)
        if not scan_results:
            print >> sys.stderr, "\nScan failed"
            return
        target_bss = filter(lambda cbss: mac_addr.same(cbss.bssid, bss['bssid']) and cbss.ssid == bss['ssid'], scan_results)
        self.ap_not_found_counter.count(target_bss)
        if not target_bss:
            print >> sys.stderr, "\nAP not found", bss['name']
            self.bss_counters[bss['name']].count(False)
            return

        # Decide which associate method to use
        if not self.radio.associated.get(): 
            method = SlowAssoc

        if method is None:
            # Randomly sample with priority to least sampled.
            r = random.random()
            universe = dict((m,c.tries) for m, c in self.assoc_counters.iteritems())
            most_visited = max(universe.itervalues())
            for m in universe:
                universe[m] = 2 + max(universe[m] - most_visited, 0)
            total = float(sum(universe.values()))
            for method in universe:
                r -= universe[method] / total
                if r < 0:
                    break

        print >> sys.stderr, "Associate method", method
        
        if method != Reassociate:
            self.radio.unassociate()
            print >> sys.stderr, "Unassociating"
            yield async_helpers.wait_for_state(self.radio.associated, lambda x: x == radio.Unassociated)

        if method == SlowAssoc:
            print >> sys.stderr, "Waiting after unassociate"
            yield async_helpers.async_sleep(3)

        # Associate
        self.radio.associate((bss['ssid'], bss['bssid']))
        print >> sys.stderr, "Associating"
        yield async_helpers.wait_for_state(self.radio.associated, lambda x: x != radio.Associating)

        # Log success
        assoc_state = self.radio.associated.get()
        self.assoc_counters[method].count(assoc_state)
        self.bss_counters[bss['name']].count(assoc_state)

        if self.radio.associated.get():
            # If association succeeded, make sure it is stable.
            print >> sys.stderr, "Waiting after associating", bss['name'], method
            yield async_helpers.async_sleep(1)
            self.early_disassoc_counter.count(self.radio.associated.get())
        else:
            print >> sys.stderr, "\nAssoc failed", bss['name'], method
            system.system('iwconfig')