In my LAN, I run the following Terminal command on 6 MAC computers to host a folder in which is discover.xml
:
python -m SimpleHTTPServer 17171
On my app having Alamofire
installed, I run this code in order to get discover.xml
content:
extension String {
func url(file: String) -> URL {
return URL(string: "http://\(self)/\(file)?rand=\(arc4random())")! // `rand` to make sure no cached content
}
}
func discover(from ip: String, completion: @escaping () -> Void) {
let url = ip.url(file: "discover.xml") // ip passed in: "192.168.1.15:17171" for ex
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.timeoutInterval = 6.0 // !
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 6.0
manager.session.configuration.timeoutIntervalForResource = 6.0
manager.request(request).responseData { (response) in
switch response.result {
case .success(let d):
// do my stuff on `multiX`
case .failure(let error):
print("failed with IP: \(ip) => \(error)")
}
}
}
Below is how I discover:
private func discover(on subnet: String) { // subnet: 192.168.1
let group = DispatchGroup()
for i in 1..<256 {
let ip = subnet + ".\(i):17171"
group.enter()
discover(from: ip) {
group.leave()
}
}
group.notify(queue: DispatchQueue.main) { [weak self] in
guard let strongSelf = self else { return }
print("all done: \(strongSelf.multiX.count)") // just can discover 2, or 3, or 4 !!
strongSelf.tableView.reloadData()
}
}
Why most of the time, I cannot get 6 discover.xml
from the 6 MAC computers? (I can only get 2 or 3, or 4).
The result that I get is not persistent. Say I have 6 MAC computers running python -m ...
named: A, B, C, D, E, F. Sometimes I can only get: A, B, D, sometimes C, F, ...
Requesting the remaining computers which host discover.xml
always have error failed with IP: x.x.x.x:17171 => Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
(I'm sure those missing computers have acquired the IP successfully)
Is it ok to discover the whole subnet using my method above? (i.e. using for loop to iterate each ip). Is 6 seconds a reasonable timeout?
EDIT:
I have tried to allow all in ATS, but things are not better
What actually blocks me from requesting discover.xml
on the MAC computers?
Thanks,