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()
try {
val socket = Socket() val socket = Socket()
try {
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,6 +120,7 @@ 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 {
try {
var json = downloadJson(PEER_LIST_URL) var json = downloadJson(PEER_LIST_URL)
var countries = CCPCountry.getLibraryMasterCountriesEnglish() var countries = CCPCountry.getLibraryMasterCountriesEnglish()
val mapType: Type = object : val mapType: Type = object :
@ -128,33 +130,42 @@ class PeerListActivity : AppCompatActivity() {
for ((country, peers) in peersMap.entries) { for ((country, peers) in peersMap.entries) {
println("$country:") println("$country:")
for ((peer, status) in peers) { for ((peer, status) in peers) {
if(status.up){ if (status.up) {
for (ccp in countries){ for (ccp in countries) {
if(ccp.name.toLowerCase().contains(country.replace(".md","").replace("-", " "))){ if (ccp.name.toLowerCase()
.contains(country.replace(".md", "").replace("-", " "))
) {
var url = URI(peer) var url = URI(peer)
try {
var address = InetAddress.getByName(url.host) var address = InetAddress.getByName(url.host)
var ping = ping(address, url.port) var ping = ping(address, url.port)
var peerInfo = PeerInfo(url.scheme, address, url.port, ccp.nameCode) var peerInfo =
PeerInfo(url.scheme, address, url.port, ccp.nameCode)
peerInfo.ping = ping peerInfo.ping = ping
allOnlinePeers.add(peerInfo) 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(
extras.getStringArrayList(MainActivity.PEER_LIST)!!
)
var currentPeers = ArrayList(cp) var currentPeers = ArrayList(cp)
for(peerInfo in allPeers){ for (peerInfo in allPeers) {
if(currentPeers.contains(peerInfo)){ if (currentPeers.contains(peerInfo)) {
currentPeers.remove(peerInfo) currentPeers.remove(peerInfo)
} }
} }
for(currentPeer in currentPeers){ for (currentPeer in currentPeers) {
allPeers.add(0, currentPeer) allPeers.add(0, currentPeer)
} }
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp) var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
@ -167,6 +178,9 @@ class PeerListActivity : AppCompatActivity() {
peerList.adapter = adapter peerList.adapter = adapter
} }
} }
} catch (e: Throwable){
e.printStackTrace()
}
} }
} }