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 :
@ -128,33 +130,42 @@ class PeerListActivity : AppCompatActivity() {
for ((country, peers) in peersMap.entries) {
println("$country:")
for ((peer, status) in peers) {
if(status.up){
for (ccp in countries){
if(ccp.name.toLowerCase().contains(country.replace(".md","").replace("-", " "))){
if (status.up) {
for (ccp in countries) {
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()
}
}
}
}
}
if(allOnlinePeers.size>0){
}
if (allOnlinePeers.size > 0) {
allPeers = ArrayList(allOnlinePeers.sortedWith(compareBy { it.ping }))
}
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)){
for (peerInfo in allPeers) {
if (currentPeers.contains(peerInfo)) {
currentPeers.remove(peerInfo)
}
}
for(currentPeer in currentPeers){
for (currentPeer in currentPeers) {
allPeers.add(0, currentPeer)
}
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
@ -167,6 +178,9 @@ class PeerListActivity : AppCompatActivity() {
peerList.adapter = adapter
}
}
} catch (e: Throwable){
e.printStackTrace()
}
}
}