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;
}
}
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.
I must use only this custom function Ping.getIPAddress() which gives true if given IP address exists else false.
Please provide me example or reference or approach to solve this issue .
Using NSOperationQueue with MaxConcurrentOperationCount set to 10 will be good ?
Find your IP address on an iOS deviceOn the Home screen, tap Settings. Tap Wi-Fi. Tap the information icon (blue i, in a circle) to the right of the network name (eduroam). Tap DHCP and the IP Address will be listed as the first line item below the heading.
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.
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"
}
}
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.
This is the class I prepared
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)
}
}
}
}
}
}
}
The class performs 256 asynchronous calls to Ping.getIPAddress(...)
.
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)
}
}
}
The previous block of code (from line #2 to #9) is executed in my queue serialQueue
. Here the 256 distinct closures run synchronously.
resultFound
and numProcessed
;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.
}
}
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
It's only 12 seconds!
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With