1. implemented peers list load. added peer sort by ping. iteration 3

This commit is contained in:
vadym 2020-06-21 13:41:02 -07:00
parent 2f357be07e
commit be549f8421
5 changed files with 60 additions and 21 deletions

View File

@ -24,10 +24,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.ByteArrayOutputStream
import java.lang.reflect.Type
import java.net.Inet4Address
import java.net.InetAddress
import java.net.URI
import java.net.URL
import java.net.*
import java.nio.charset.Charset
@ -98,6 +95,17 @@ class PeerListActivity : AppCompatActivity() {
}
}
fun ping(address: InetAddress, port:Int): Int {
val start = System.currentTimeMillis()
try {
val socket = Socket()
socket.connect(InetSocketAddress(address, port), 5000)
socket.close()
} catch (e: Exception) {
return Int.MAX_VALUE
}
return (System.currentTimeMillis() - start).toInt()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -122,9 +130,12 @@ class PeerListActivity : AppCompatActivity() {
for ((peer, status) in peers) {
if(status.up){
for (ccp in countries){
if(ccp.name.toLowerCase().contains(country.replace(".md",""))){
if(ccp.name.toLowerCase().contains(country.replace(".md","").replace("-", " "))){
var url = URI(peer)
var peerInfo = PeerInfo(url.scheme, InetAddress.getByName(url.host), url.port, ccp.nameCode)
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)
}
}
@ -132,7 +143,7 @@ class PeerListActivity : AppCompatActivity() {
}
}
if(allOnlinePeers.size>0){
allPeers = allOnlinePeers
allPeers = ArrayList(allOnlinePeers.sortedWith(compareBy { it.ping }))
}
if (extras != null) {
@ -146,12 +157,12 @@ class PeerListActivity : AppCompatActivity() {
for(currentPeer in currentPeers){
allPeers.add(0, currentPeer)
}
var adapter = SelectPeerInfoListAdapter(instance, allOnlinePeers, cp)
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
withContext(Dispatchers.Main) {
peerList.adapter = adapter
}
} else {
var adapter = SelectPeerInfoListAdapter(instance, allOnlinePeers, ArrayList())
var adapter = SelectPeerInfoListAdapter(instance, allPeers, ArrayList())
withContext(Dispatchers.Main) {
peerList.adapter = adapter
}

View File

@ -17,7 +17,7 @@ class PeerInfo {
var address: InetAddress
var port = 0
var countryCode: String
var ping: Float = Float.MAX_VALUE
var ping: Int = Int.MAX_VALUE
override fun toString(): String {
var a = address.toString();

View File

@ -1,6 +1,7 @@
package io.github.chronosx88.yggdrasil.models.config
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -31,6 +32,7 @@ class SelectPeerInfoListAdapter(
peerInfoHolder.checkbox = listItem.findViewById(R.id.checkbox) as CheckBox
peerInfoHolder.countryFlag = listItem.findViewById(R.id.countryFlag) as ImageView
peerInfoHolder.peerInfoText = listItem.findViewById(R.id.peerInfoText) as TextView
peerInfoHolder.ping = listItem.findViewById(R.id.ping) as TextView
listItem.tag = peerInfoHolder
} else {
peerInfoHolder = listItem.tag as PeerInfoHolder
@ -38,7 +40,15 @@ class SelectPeerInfoListAdapter(
val currentPeer = allPeers[position]
peerInfoHolder.countryFlag.setImageResource(currentPeer.getCountry(mContext)!!.flagID)
val peerId = currentPeer.toString()
peerInfoHolder.peerInfoText.text = peerId
if(currentPeer.ping == Int.MAX_VALUE){
peerInfoHolder.peerInfoText.text = "$peerId"
peerInfoHolder.ping.text=""
peerInfoHolder.peerInfoText.setTextColor(Color.GRAY)
} else {
peerInfoHolder.peerInfoText.text = "$peerId" //peerId + " " + currentPeer.ping + " ms"
peerInfoHolder.ping.text = currentPeer.ping.toString() + " ms"
peerInfoHolder.peerInfoText.setTextColor(Color.WHITE)
}
peerInfoHolder.checkbox.setOnCheckedChangeListener { _, isChecked ->
if(isChecked){
if(!currentPeers.contains(currentPeer)){
@ -62,6 +72,7 @@ class SelectPeerInfoListAdapter(
lateinit var checkbox: CheckBox
lateinit var countryFlag: ImageView
lateinit var peerInfoText: TextView
lateinit var ping: TextView
}
}

View File

@ -8,7 +8,7 @@
android:id="@+id/countryFlag"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginStart="18dp"
android:scaleX="0.7"
android:scaleY="0.7"/>
<TextView

View File

@ -1,16 +1,17 @@
<LinearLayout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
android:orientation="horizontal">
<FrameLayout
android:id="@+id/frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.4"
android:scaleY="0.4"
android:background="@drawable/checkbox_rounded_corner"
android:layout_marginStart="5dp">
android:layout_alignParentStart="true"
android:layout_centerInParent="true">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
@ -25,16 +26,32 @@
<ImageView
android:id="@+id/countryFlag"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="5dp"/>
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_toEndOf="@+id/frame"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/peerInfoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginStart="10dp"
android:paddingEnd="20dp"
android:layout_marginEnd="60dp"
android:minHeight="45dp"
android:textSize="14sp"
android:textColor="@color/white"/>
</LinearLayout>
android:textColor="@color/white"
android:layout_toEndOf="@+id/countryFlag"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/ping"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginStart="10dp"
android:paddingEnd="10dp"
android:minHeight="45dp"
android:textSize="14sp"
android:textColor="@color/white"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"/>
</RelativeLayout>