2. fix adding Peers when json url unavailable

This commit is contained in:
vadym 2020-07-03 01:08:39 -07:00
parent 2e25e2e08c
commit 0201705c45

View File

@ -28,6 +28,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.FileNotFoundException
import java.lang.reflect.Type import java.lang.reflect.Type
import java.net.InetAddress import java.net.InetAddress
import java.net.URI import java.net.URI
@ -77,41 +78,45 @@ class PeerListActivity : AppCompatActivity() {
var ping = ping(pi.address, pi.port) var ping = ping(pi.address, pi.port)
pi.ping = ping pi.ping = ping
} }
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
for ((country, peers) in peersMap.entries) { val peersMap: Map<String, Map<String, Status>> = Gson().fromJson(json, mapType)
for ((peer, status) in peers) { for ((country, peers) in peersMap.entries) {
if (status.up) { for ((peer, status) in peers) {
for (ccp in countries) { if (status.up) {
if (ccp.name.toLowerCase() for (ccp in countries) {
.contains(country.replace(".md", "").replace("-", " ")) if (ccp.name.toLowerCase()
) { .contains(country.replace(".md", "").replace("-", " "))
var url = URI(peer) ) {
try { var url = URI(peer)
var address = InetAddress.getByName(url.host) try {
var peerInfo = var address = InetAddress.getByName(url.host)
PeerInfo(url.scheme, address, url.port, ccp.nameCode) var peerInfo =
if(cp.contains(peerInfo)){ PeerInfo(url.scheme, address, url.port, ccp.nameCode)
continue if(cp.contains(peerInfo)){
} continue
var ping = ping(address, url.port)
peerInfo.ping = ping
withContext(Dispatchers.Main) {
adapter.addItem(peerInfo)
if(adapter.count % 5 == 0) {
adapter.sort()
} }
var ping = ping(address, url.port)
peerInfo.ping = ping
withContext(Dispatchers.Main) {
adapter.addItem(peerInfo)
if(adapter.count % 5 == 0) {
adapter.sort()
}
}
} catch (e: Throwable){
e.printStackTrace()
} }
} catch (e: Throwable){
e.printStackTrace()
} }
} }
} }
} }
} }
} catch(e: FileNotFoundException){
e.printStackTrace()
} }
var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping })) var currentPeers = ArrayList(cp.sortedWith(compareBy { it.ping }))
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {