mirror of
https://github.com/yggdrasil-network/crispa-android.git
synced 2024-11-12 21:41:05 +00:00
1. implemented peers list load. added peer sort by ping. iteration 3
This commit is contained in:
parent
2f357be07e
commit
be549f8421
@ -24,10 +24,7 @@ import kotlinx.coroutines.launch
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
import java.lang.reflect.Type
|
import java.lang.reflect.Type
|
||||||
import java.net.Inet4Address
|
import java.net.*
|
||||||
import java.net.InetAddress
|
|
||||||
import java.net.URI
|
|
||||||
import java.net.URL
|
|
||||||
import java.nio.charset.Charset
|
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?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@ -122,9 +130,12 @@ class PeerListActivity : AppCompatActivity() {
|
|||||||
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",""))){
|
if(ccp.name.toLowerCase().contains(country.replace(".md","").replace("-", " "))){
|
||||||
var url = URI(peer)
|
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)
|
allOnlinePeers.add(peerInfo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -132,7 +143,7 @@ class PeerListActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(allOnlinePeers.size>0){
|
if(allOnlinePeers.size>0){
|
||||||
allPeers = allOnlinePeers
|
allPeers = ArrayList(allOnlinePeers.sortedWith(compareBy { it.ping }))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (extras != null) {
|
if (extras != null) {
|
||||||
@ -146,12 +157,12 @@ class PeerListActivity : AppCompatActivity() {
|
|||||||
for(currentPeer in currentPeers){
|
for(currentPeer in currentPeers){
|
||||||
allPeers.add(0, currentPeer)
|
allPeers.add(0, currentPeer)
|
||||||
}
|
}
|
||||||
var adapter = SelectPeerInfoListAdapter(instance, allOnlinePeers, cp)
|
var adapter = SelectPeerInfoListAdapter(instance, allPeers, cp)
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
peerList.adapter = adapter
|
peerList.adapter = adapter
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var adapter = SelectPeerInfoListAdapter(instance, allOnlinePeers, ArrayList())
|
var adapter = SelectPeerInfoListAdapter(instance, allPeers, ArrayList())
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
peerList.adapter = adapter
|
peerList.adapter = adapter
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ class PeerInfo {
|
|||||||
var address: InetAddress
|
var address: InetAddress
|
||||||
var port = 0
|
var port = 0
|
||||||
var countryCode: String
|
var countryCode: String
|
||||||
var ping: Float = Float.MAX_VALUE
|
var ping: Int = Int.MAX_VALUE
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
var a = address.toString();
|
var a = address.toString();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package io.github.chronosx88.yggdrasil.models.config
|
package io.github.chronosx88.yggdrasil.models.config
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.graphics.Color
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
@ -31,6 +32,7 @@ class SelectPeerInfoListAdapter(
|
|||||||
peerInfoHolder.checkbox = listItem.findViewById(R.id.checkbox) as CheckBox
|
peerInfoHolder.checkbox = listItem.findViewById(R.id.checkbox) as CheckBox
|
||||||
peerInfoHolder.countryFlag = listItem.findViewById(R.id.countryFlag) as ImageView
|
peerInfoHolder.countryFlag = listItem.findViewById(R.id.countryFlag) as ImageView
|
||||||
peerInfoHolder.peerInfoText = listItem.findViewById(R.id.peerInfoText) as TextView
|
peerInfoHolder.peerInfoText = listItem.findViewById(R.id.peerInfoText) as TextView
|
||||||
|
peerInfoHolder.ping = listItem.findViewById(R.id.ping) as TextView
|
||||||
listItem.tag = peerInfoHolder
|
listItem.tag = peerInfoHolder
|
||||||
} else {
|
} else {
|
||||||
peerInfoHolder = listItem.tag as PeerInfoHolder
|
peerInfoHolder = listItem.tag as PeerInfoHolder
|
||||||
@ -38,7 +40,15 @@ class SelectPeerInfoListAdapter(
|
|||||||
val currentPeer = allPeers[position]
|
val currentPeer = allPeers[position]
|
||||||
peerInfoHolder.countryFlag.setImageResource(currentPeer.getCountry(mContext)!!.flagID)
|
peerInfoHolder.countryFlag.setImageResource(currentPeer.getCountry(mContext)!!.flagID)
|
||||||
val peerId = currentPeer.toString()
|
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 ->
|
peerInfoHolder.checkbox.setOnCheckedChangeListener { _, isChecked ->
|
||||||
if(isChecked){
|
if(isChecked){
|
||||||
if(!currentPeers.contains(currentPeer)){
|
if(!currentPeers.contains(currentPeer)){
|
||||||
@ -62,6 +72,7 @@ class SelectPeerInfoListAdapter(
|
|||||||
lateinit var checkbox: CheckBox
|
lateinit var checkbox: CheckBox
|
||||||
lateinit var countryFlag: ImageView
|
lateinit var countryFlag: ImageView
|
||||||
lateinit var peerInfoText: TextView
|
lateinit var peerInfoText: TextView
|
||||||
|
lateinit var ping: TextView
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -8,7 +8,7 @@
|
|||||||
android:id="@+id/countryFlag"
|
android:id="@+id/countryFlag"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_marginStart="20dp"
|
android:layout_marginStart="18dp"
|
||||||
android:scaleX="0.7"
|
android:scaleX="0.7"
|
||||||
android:scaleY="0.7"/>
|
android:scaleY="0.7"/>
|
||||||
<TextView
|
<TextView
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
<LinearLayout
|
<RelativeLayout
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal">
|
||||||
android:gravity="center_vertical">
|
|
||||||
<FrameLayout
|
<FrameLayout
|
||||||
|
android:id="@+id/frame"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:scaleX="0.4"
|
android:scaleX="0.4"
|
||||||
android:scaleY="0.4"
|
android:scaleY="0.4"
|
||||||
android:background="@drawable/checkbox_rounded_corner"
|
android:background="@drawable/checkbox_rounded_corner"
|
||||||
android:layout_marginStart="5dp">
|
android:layout_alignParentStart="true"
|
||||||
|
android:layout_centerInParent="true">
|
||||||
<CheckBox
|
<CheckBox
|
||||||
android:id="@+id/checkbox"
|
android:id="@+id/checkbox"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
@ -25,16 +26,32 @@
|
|||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/countryFlag"
|
android:id="@+id/countryFlag"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginStart="5dp"/>
|
android:layout_marginStart="5dp"
|
||||||
|
android:layout_toEndOf="@+id/frame"
|
||||||
|
android:layout_centerInParent="true"/>
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/peerInfoText"
|
android:id="@+id/peerInfoText"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:gravity="center_vertical"
|
android:gravity="center_vertical"
|
||||||
android:layout_marginStart="10dp"
|
android:layout_marginStart="10dp"
|
||||||
android:paddingEnd="20dp"
|
android:layout_marginEnd="60dp"
|
||||||
android:minHeight="45dp"
|
android:minHeight="45dp"
|
||||||
android:textSize="14sp"
|
android:textSize="14sp"
|
||||||
android:textColor="@color/white"/>
|
android:textColor="@color/white"
|
||||||
</LinearLayout>
|
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>
|
Loading…
Reference in New Issue
Block a user