优化的方法来搜索iphone范围内的设备IP地址 [英] optimized way to search a device ip address within a range in iphone

查看:149
本文介绍了优化的方法来搜索iphone范围内的设备IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有情况在哪里我必须搜索**路由器**的IP地址,我知道它的范围是从范围163.289.2.0到163.289.2.255。
我知道这不是一个很好的搜索方式。

I have situation where-in i have to search IP address of **router ** and I know only it's range is from range 163.289.2.0 to 163.289.2.255. I know this is not good way to search.

for i in 1... 255 {

var str = "163.289.2." + "i"
var tempIP = Ping.getIPAddress(str)

if(tempIP == true)
{
   break;
}

}

现在我的问题是我的自定义类Ping.getIPAddress()需要3秒才能获得给定IP值的结果。因此,对于255次搜索,大约需要765秒(12.75分钟)。我有限制搜索应该在最多2分钟内完成。那么无论如何我可以使用swift在iPhone中实现这一点。

Now my problem is my custom class Ping.getIPAddress() takes 3 seconds to get the result for a given IP value. So for 255 searches it takes approx 765 seconds (12.75 minutes). I have restriction that search should complete in max 2 minutes. So is there anyway i can achieve this in iPhone using swift.

我必须只使用这个自定义函数Ping.getIPAddress(),如果给定的IP地址则给出true存在其他错误。

请提供解决此问题的示例或参考或方法。

Please provide me example or reference or approach to solve this issue .

使用NSOperationQueue和MaxConcurrentOperationCount设置为10会不错?

Using NSOperationQueue with MaxConcurrentOperationCount set to 10 will be good ?

推荐答案

同步方法



如果我们执行每次调用 Ping.getIPAddress(str),只有当前一个完成后我们需要等待(3秒* 256)= 768秒。

Synchronous approach

If we perform each call to Ping.getIPAddress(str) only after the previous one has completed of course we need to wait for (3 seconds * 256) = 768 seconds.

另一方面,我们可以执行多次并发调用 Ping.getIPAddress(str)

On the other hand we can perform several concurrent calls to Ping.getIPAddress(str).

这是我为测试你的功能而创建的一个类。

This is a class I created to test your function.

class Ping {
    class func getIPAddress(str:String) -> Bool {
        sleep(3)
        return str == "163.289.2.255"
    }
}

如您所见,该类确实等待3秒(模拟您的场景),然后仅在传递<时返回 true code> ip 是 163.289.2.255 。这允许我复制最坏的情况。

As you see the class does wait for 3 seconds (to simulate your scenario) and then returns true only if the passed ip is 163.289.2.255. This allows me to replicated the worst case scenario.

这是我准备的课程

class QuantumComputer {

    func search(completion:(existingIP:String?) -> ()) {
        var resultFound = false
        var numProcessed = 0
        let serialQueue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_SERIAL)
        for i in 0...255 {

            dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.value), 0)) {
                var ip = "163.289.2." + "\(i)"
                let foundThisOne = Ping.getIPAddress(ip)

                dispatch_async(serialQueue) {
                    if !resultFound {
                        resultFound = foundThisOne
                        numProcessed++
                        if resultFound {
                            completion(existingIP:ip)
                        } else if numProcessed == 256 {
                            completion(existingIP: nil)
                        }
                    }
                }
            }
        }
    }
}

该类执行 256异步调用 Ping.getIPAddress(...)

256个异步闭包的结果由以下代码处理:

The results from the 256 async closures is processed by this code:

dispatch_async(serialQueue) {
    if !resultFound {
        resultFound = foundThisOne
        numProcessed++
        if resultFound {
             completion(existingIP:ip)
        } else if numProcessed == 256 {
             completion(existingIP: nil)
        }
    }
}

执行上一段代码(从第2行到第9行)在我的队列中 serialQueue 。这里256个不同的闭包运行同步

The previous block of code (from line #2 to #9) is executed in my queue serialQueue. Here the 256 distinct closures run synchronously.


  1. 这对于确保对变量的一致访问至关重要 resultFound numProcessed ;

  2. 另一方面这不是问题所在性能观点,因为此代码非常快(只是一堆算术运算)

  1. this is crucial to ensure a consistent access to the variables resultFound and numProcessed;
  2. on the other hand this is not a problem by a performance point of view since this code is pretty fast (just a bunch of arithmetic operations)



测试



这就是我从标准ViewController调用它的方式。

Test

And this is how I call it from a standard ViewController.

class ViewController: UIViewController {
    var computer = QuantumComputer()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        debugPrintln(NSDate())
        computer.search { (existingIP) -> () in
            debugPrintln("existingIP: \(existingIP)")
            debugPrintln(NSDate())
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}



结论



最后,这是我在iOS模拟器上测试时的输出。请注意,这是最糟糕的情况(因为上次检查的号码是有效的IP)。

Conclusions

Finally this is the output when I test it on my iOS simulator. Please remind that this is the worst case scenario (since the last checked number is a valid IP).

2015-09-04 20:56:17 +0000
"existingIP: Optional(\"163.289.2.255\")"
2015-09-04 20:56:29 +0000

只有12秒!

希望这会有所帮助。

这篇关于优化的方法来搜索iphone范围内的设备IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆