Merge pull request #33 from Akilan1999/SpeedTestModuleBUG

fixed list slice error
This commit is contained in:
Akilan Selvacoumar
2021-06-24 18:45:26 +04:00
committed by GitHub
2 changed files with 13 additions and 38 deletions

View File

@@ -2,31 +2,13 @@
"ip_address": [
{
"ipv4": "0.0.0.0",
"latency": 637095,
"latency": 453359,
"download": 0,
"upload": 0
},
{
"ipv4": "127.0.0.1",
"latency": 420466,
"download": 0,
"upload": 0
},
{
"ipv4": "86.99.25.74",
"latency": 1217902,
"download": 0,
"upload": 0
},
{
"ipv4": "172.105.58.101",
"latency": 40664198,
"download": 0,
"upload": 0
},
{
"ipv4": "172.105.58.111",
"latency": 42065364,
"latency": 410267,
"download": 0,
"upload": 0
}

View File

@@ -2,10 +2,13 @@ package p2p
// SpeedTest Runs a speed test and does updates IP tables accordingly
func (ip *IpAddresses)SpeedTest() error{
//temp variable to store elements IP addresses and other information
// of IP addresses that are pingable
var ActiveIP IpAddresses
// Index to remove from struct
var RemoveIndex []int
for i, value := range ip.IpAddress {
// Index to remove from struct
for _, value := range ip.IpAddress {
var err error
//if len(ip.IpAddress) == 1 {
@@ -16,10 +19,6 @@ func (ip *IpAddresses)SpeedTest() error{
err = value.PingTest()
if err != nil {
RemoveIndex = append(RemoveIndex, i)
// Record index to remove
//ip.IpAddress = append(ip.IpAddress[:i], ip.IpAddress[i+1:]...)
// Proceed to next element in the array
continue
}
@@ -35,19 +34,12 @@ func (ip *IpAddresses)SpeedTest() error{
//}
//Set value to the list
ip.IpAddress[i] = value
ActiveIP.IpAddress = append(ActiveIP.IpAddress, value)
}
// Remove element from struct
for _, index := range RemoveIndex {
// If there is only 1 element and that has to be
// removed
if len(ip.IpAddress) == 1 {
ip.IpAddress = nil
break
}
ip.IpAddress = append(ip.IpAddress[:index], ip.IpAddress[index+1:]...)
}
ip.IpAddress = ActiveIP.IpAddress
err := ip.WriteIpTable()
if err != nil {
@@ -111,6 +103,7 @@ func LocalSpeedTestIpTable() error {
return nil
}
// Helper function to remove element from an array of a struct
//func remove(s []IpAddress, i int) []IpAddress {
// s[len(s)-1], s[i] = s[i], s[len(s)-1]