added UpdateIpTableListClient

This commit is contained in:
2021-04-04 21:51:11 +04:00
parent 1c733a694e
commit 2875b7228b
5 changed files with 108 additions and 6 deletions

79
client/Iptable.go Normal file
View File

@@ -0,0 +1,79 @@
package client
import (
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
"net/url"
)
// Does the following to update it's IP table
func UpdateIpTable(IpAddress string) error {
// Gets information from IP table
Addresses, err := p2p.ReadIpTable()
if err != nil {
return err
}
u, err := url.Parse("http://" + IpAddress + ":8088/IpTable")
if err != nil {
panic(err)
}
params := url.Values{}
params.Add("data", fmt.Sprint(Addresses))
u.RawQuery = params.Encode()
fmt.Print(u.RawQuery)
return nil
}
//updates IP tables (Default 3 hops) based on server information avaliable
//on the ip tables
func UpdateIpTableListClient() error {
// Ensure that the IP Table has Node pingable
err := p2p.LocalSpeedTestIpTable()
if err != nil {
return err
}
// IP addresses to not read from
var DoNotRead p2p.IpAddresses
// Run loop 3 times
for i := 0; i < 3; i++ {
// Gets information from IP table
Addresses, err := p2p.ReadIpTable()
if err != nil {
return err
}
// Updates IP table based on server IP table
for j, _ := range Addresses.IpAddress {
// Check if IP addresses is there in the struct DoNotRead
Exists := false
for k, _ := range DoNotRead.IpAddress {
if DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[i].Ipv4 {
Exists = true
break
}
}
// If the struct exists then continues
if Exists {
continue
}
err = UpdateIpTable(Addresses.IpAddress[j].Ipv4)
if err != nil {
return err
}
//Appends server1 IP address to variable DoNotRead
DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
}
}
return nil
}

11
client/iptable_test.go Normal file
View File

@@ -0,0 +1,11 @@
package client
import "testing"
func TestUpdateIpTableListClient(t *testing.T) {
err := UpdateIpTableListClient()
if err != nil {
t.Error(err)
}
}

View File

@@ -15,8 +15,14 @@ var CliAction = func(ctx *cli.Context) error {
//server.Rpc()
}
//Listing servers avaliable
if List_servers {
//Listing servers and also updates IP tables (Default 3 hops)
if ListServers {
err := client.UpdateIpTableListClient()
if err != nil {
fmt.Print(err)
}
p2p.PrintIpTable()
}

View File

@@ -5,7 +5,7 @@ import (
)
var Mode,IpAddress string
var List_servers, Ip_table, Abspath bool
var ListServers, Ip_table, Abspath bool
var AppConfigFlags = []cli.Flag{
// Deprecated to be implemented using GRPC
@@ -20,7 +20,7 @@ var AppConfigFlags = []cli.Flag{
Name: "ListServers",
Usage: "List servers which can render tasks",
EnvVars: []string{"LIST_SERVERS"},
Destination: &List_servers,
Destination: &ListServers,
},
&cli.StringFlag{
Name: "CreateVM",
@@ -31,7 +31,7 @@ var AppConfigFlags = []cli.Flag{
&cli.BoolFlag{
Name: "FilePath",
Usage: "Testing for absolute path",
EnvVars: []string{"CREATE_VM"},
Destination: &Abspath,
},
}

View File

@@ -34,8 +34,14 @@ func Server() {
//Gets Ip Table from server node
r.GET("/IpTable", func(c *gin.Context) {
//jsonData, err := ioutil.ReadAll(c.Request.Body)
//if err != nil {
// c.String(http.StatusOK, fmt.Sprint(err))
//}
// Runs speed test to return only servers in the IP table pingable
err := p2p.SpeedTest()
err := p2p.LocalSpeedTestIpTable()
if err != nil {
c.String(http.StatusOK, fmt.Sprint(err))
}