try to fix p2p module
This commit is contained in:
@@ -1,34 +1,42 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
|
||||
"net/url"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Does the following to update it's IP table
|
||||
func UpdateIpTable(IpAddress string) error {
|
||||
// Gets information from IP table
|
||||
Addresses, err := p2p.ReadIpTable()
|
||||
|
||||
resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
|
||||
"/etc/p2p-rendering/ip_table.json",
|
||||
"json")
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
u, err := url.Parse("http://" + IpAddress + ":8088/IpTable")
|
||||
var ipStruct p2p.IpAddresses
|
||||
json.Unmarshal(resp, &ipStruct)
|
||||
|
||||
// Updates IP table based on information provided
|
||||
// by the server
|
||||
err = ipStruct.SpeedTestUpdatedIPTable()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return 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
|
||||
//updates IP tables (Default 3 hops) based on server information available
|
||||
//on the ip tables
|
||||
func UpdateIpTableListClient() error {
|
||||
// Ensure that the IP Table has Node pingable
|
||||
@@ -49,11 +57,11 @@ func UpdateIpTableListClient() error {
|
||||
}
|
||||
|
||||
// Updates IP table based on server IP table
|
||||
for j, _ := range Addresses.IpAddress {
|
||||
for j := range Addresses.IpAddress {
|
||||
|
||||
// Check if IP addresses is there in the struct DoNotRead
|
||||
Exists := false
|
||||
for k, _ := range DoNotRead.IpAddress {
|
||||
for k := range DoNotRead.IpAddress {
|
||||
if DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[j].Ipv4 {
|
||||
Exists = true
|
||||
break
|
||||
@@ -77,3 +85,48 @@ func UpdateIpTableListClient() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sends a file as a POST request.
|
||||
// Reference (https://stackoverflow.com/questions/51234464/upload-a-file-with-post-request-golang)
|
||||
func SendPostRequest (url string, filename string, filetype string) ([]byte,error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
part, err := writer.CreateFormFile(filetype, filepath.Base(file.Name()))
|
||||
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
|
||||
io.Copy(part, file)
|
||||
writer.Close()
|
||||
request, err := http.NewRequest("POST", url, body)
|
||||
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
|
||||
request.Header.Add("Content-Type", writer.FormDataContentType())
|
||||
client := &http.Client{}
|
||||
|
||||
response, err := client.Do(request)
|
||||
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
content, err := ioutil.ReadAll(response.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil,err
|
||||
}
|
||||
|
||||
return content, nil
|
||||
}
|
||||
Reference in New Issue
Block a user