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 {
val start = System.currentTimeMillis()
try {
val socket = Socket()
try {
socket.connect(InetSocketAddress(address, port), 5000)
socket.close()
} catch (e: Exception) {
//silently pass
return Int.MAX_VALUE
}
return (System.currentTimeMillis() - start).toInt()
@ -119,6 +120,7 @@ class PeerListActivity : AppCompatActivity() {
var peerList = findViewById<ListView>(R.id.peerList)
var instance = this
GlobalScope.launch {
try {
var json = downloadJson(PEER_LIST_URL)
var countries = CCPCountry.getLibraryMasterCountriesEnglish()
val mapType: Type = object :
@ -130,13 +132,20 @@ class PeerListActivity : AppCompatActivity() {
for ((peer, status) in peers) {
if (status.up) {
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)
try {
var address = InetAddress.getByName(url.host)
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
allOnlinePeers.add(peerInfo)
} catch (e: Throwable){
e.printStackTrace()
}
}
}
}
@ -147,7 +156,9 @@ class PeerListActivity : AppCompatActivity() {
}
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)
for (peerInfo in allPeers) {
if (currentPeers.contains(peerInfo)) {
@ -167,6 +178,9 @@ class PeerListActivity : AppCompatActivity() {
peerList.adapter = adapter
}
}
} catch (e: Throwable){
e.printStackTrace()
}
}
}