1. added correct exception handling

This commit is contained in:
vadym 2020-06-21 14:51:29 -07:00
parent 2a5c6b66e2
commit 68b294df14

View File

@ -97,11 +97,12 @@ class PeerListActivity : AppCompatActivity() {
fun ping(address: InetAddress, port:Int): Int { fun ping(address: InetAddress, port:Int): Int {
val start = System.currentTimeMillis() val start = System.currentTimeMillis()
val socket = Socket()
try { try {
val socket = Socket()
socket.connect(InetSocketAddress(address, port), 5000) socket.connect(InetSocketAddress(address, port), 5000)
socket.close() socket.close()
} catch (e: Exception) { } catch (e: Exception) {
//silently pass
return Int.MAX_VALUE return Int.MAX_VALUE
} }
return (System.currentTimeMillis() - start).toInt() return (System.currentTimeMillis() - start).toInt()
@ -119,53 +120,66 @@ class PeerListActivity : AppCompatActivity() {
var peerList = findViewById<ListView>(R.id.peerList) var peerList = findViewById<ListView>(R.id.peerList)
var instance = this var instance = this
GlobalScope.launch { GlobalScope.launch {
var json = downloadJson(PEER_LIST_URL) try {
var countries = CCPCountry.getLibraryMasterCountriesEnglish() var json = downloadJson(PEER_LIST_URL)
val mapType: Type = object : var countries = CCPCountry.getLibraryMasterCountriesEnglish()
TypeToken<Map<String?, Map<String, Status>>>() {}.type val mapType: Type = object :
val peersMap: Map<String, Map<String, Status>> = Gson().fromJson(json, mapType) TypeToken<Map<String?, Map<String, Status>>>() {}.type
val allOnlinePeers = arrayListOf<PeerInfo>() val peersMap: Map<String, Map<String, Status>> = Gson().fromJson(json, mapType)
for ((country, peers) in peersMap.entries) { val allOnlinePeers = arrayListOf<PeerInfo>()
println("$country:") for ((country, peers) in peersMap.entries) {
for ((peer, status) in peers) { println("$country:")
if(status.up){ for ((peer, status) in peers) {
for (ccp in countries){ if (status.up) {
if(ccp.name.toLowerCase().contains(country.replace(".md","").replace("-", " "))){ for (ccp in countries) {
var url = URI(peer) if (ccp.name.toLowerCase()
var address = InetAddress.getByName(url.host) .contains(country.replace(".md", "").replace("-", " "))
var ping = ping(address, url.port) ) {
var peerInfo = PeerInfo(url.scheme, address, url.port, ccp.nameCode) var url = URI(peer)
peerInfo.ping = ping try {
allOnlinePeers.add(peerInfo) var address = InetAddress.getByName(url.host)
var ping = ping(address, url.port)
var peerInfo =
PeerInfo(url.scheme, address, url.port, ccp.nameCode)
peerInfo.ping = ping
allOnlinePeers.add(peerInfo)
} catch (e: Throwable){
e.printStackTrace()
}
}
} }
} }
} }
} }
} if (allOnlinePeers.size > 0) {
if(allOnlinePeers.size>0){ allPeers = ArrayList(allOnlinePeers.sortedWith(compareBy { it.ping }))
allPeers = ArrayList(allOnlinePeers.sortedWith(compareBy { it.ping })) }
}
if (extras != null) { if (extras != null) {
var cp = MainActivity.deserializeStringList2PeerInfoList(extras.getStringArrayList(MainActivity.PEER_LIST)!!) var cp = MainActivity.deserializeStringList2PeerInfoList(
var currentPeers = ArrayList(cp) extras.getStringArrayList(MainActivity.PEER_LIST)!!
for(peerInfo in allPeers){ )
if(currentPeers.contains(peerInfo)){ var currentPeers = ArrayList(cp)
currentPeers.remove(peerInfo) for (peerInfo in allPeers) {
if (currentPeers.contains(peerInfo)) {
currentPeers.remove(peerInfo)
}
}
for (currentPeer in currentPeers) {
allPeers.add(0, currentPeer)
}
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
withContext(Dispatchers.Main) {
peerList.adapter = adapter
}
} else {
var adapter = SelectPeerInfoListAdapter(instance, allPeers, ArrayList())
withContext(Dispatchers.Main) {
peerList.adapter = adapter
} }
} }
for(currentPeer in currentPeers){ } catch (e: Throwable){
allPeers.add(0, currentPeer) e.printStackTrace()
}
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
withContext(Dispatchers.Main) {
peerList.adapter = adapter
}
} else {
var adapter = SelectPeerInfoListAdapter(instance, allPeers, ArrayList())
withContext(Dispatchers.Main) {
peerList.adapter = adapter
}
} }
} }
} }