From 0bdcaca087dc03df5996ffb7eccfee7ff489aa4b Mon Sep 17 00:00:00 2001 From: Akilan Selvacoumar Date: Thu, 24 Jun 2021 18:44:09 +0400 Subject: [PATCH] fixed list slice error --- p2p/ip_table.json | 22 ++-------------------- p2p/speedtest.go | 29 +++++++++++------------------ 2 files changed, 13 insertions(+), 38 deletions(-) diff --git a/p2p/ip_table.json b/p2p/ip_table.json index 63573a4..4bb8d5d 100644 --- a/p2p/ip_table.json +++ b/p2p/ip_table.json @@ -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 } diff --git a/p2p/speedtest.go b/p2p/speedtest.go index 6a49074..76f4e31 100644 --- a/p2p/speedtest.go +++ b/p2p/speedtest.go @@ -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]