add files

This commit is contained in:
2021-04-11 01:26:55 +04:00
parent d2b050b14b
commit fdbb7bc587
77 changed files with 10832 additions and 0 deletions

135
client/Iptable.go Normal file
View File

@@ -0,0 +1,135 @@
package client
import (
"bytes"
"encoding/json"
"git.sr.ht/~akilan1999/p2p-rendering-computation/p2p"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
// Does the following to update it's IP table
func UpdateIpTable(IpAddress string) error {
resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
"/etc/p2p-rendering/ip_table.json",
"json")
if err != nil {
return err
}
var ipStruct p2p.IpAddresses
json.Unmarshal(resp, &ipStruct)
// Updates IP table based on information provided
// by the server
err = ipStruct.SpeedTestUpdatedIPTable()
if err != nil {
return err
}
return nil
}
//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
err := p2p.LocalSpeedTestIpTable()
if err != nil {
return err
}
// IP addresses to not append to struct due to
// duplication
Addresses, err := p2p.ReadIpTable()
DoNotRead := Addresses
// 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[j].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
}
// 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
}

50
client/ServerSpecs.go Normal file
View File

@@ -0,0 +1,50 @@
package client
import (
"encoding/json"
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/server"
"io/ioutil"
"net/http"
)
// GetSpecs Gets Specs from the server such CPU, GPU usage
// and other basic information which helps set a
// cluster of computer
func GetSpecs(IP string)(*server.SysInfo,error) {
URL := "http://" + IP + ":" + serverPort + "/server_info"
resp, err := http.Get(URL)
if err != nil {
return nil,err
}
// Convert response to byte value
byteValue, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil,err
}
// Create variable for result response type
var serverSpecsResult server.SysInfo
// Adds byte value to docker.DockerVM struct
json.Unmarshal(byteValue, &serverSpecsResult)
if err != nil {
return nil,err
}
return &serverSpecsResult, nil
}
// PrettyPrint print the contents of the obj (
// Reference: https://stackoverflow.com/questions/24512112/how-to-print-struct-variables-in-console
func PrettyPrint(data interface{}) {
var p []byte
// var err := error
p, err := json.MarshalIndent(data, "", "\t")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s \n", p)
}

109
client/container.go Normal file
View File

@@ -0,0 +1,109 @@
package client
import (
"encoding/json"
"fmt"
"git.sr.ht/~akilan1999/p2p-rendering-computation/server/docker"
"io/ioutil"
"net/http"
"strconv"
)
const (
serverPort = "8088"
)
var client = http.Client{}
// StartContainer Start container using REST api Implementation
// From the selected server IP address
// TODO: Test cases for this function
func StartContainer(IP string, NumPorts int, GPU bool) (*docker.DockerVM ,error) {
// Passes URL with number of TCP ports to allocated and to give GPU access to the docker container
URL := "http://" + IP + ":" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU)
resp, err := http.Get(URL)
if err != nil {
return nil,err
}
// Convert response to byte value
byteValue, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil,err
}
// Create variable for result response type
var dockerResult docker.DockerVM
// Adds byte value to docker.DockerVM struct
json.Unmarshal(byteValue, &dockerResult)
if err != nil {
return nil,err
}
return &dockerResult, nil
}
// RemoveContianer Stops and removes container from the server
func RemoveContianer(IP string,ID string) error {
URL := "http://" + IP + ":" + serverPort + "/RemoveContainer?id=" + ID
resp, err := http.Get(URL)
if err != nil {
return err
}
// Convert response to byte value
byteValue, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if string(byteValue[:]) == "success" {
fmt.Println("success")
}
return nil
}
// PrintStartContainer Prints results Generated container
func PrintStartContainer(d *docker.DockerVM){
fmt.Println("ID : " + fmt.Sprint(d.ID))
fmt.Println("SSH port: " + fmt.Sprint(d.SSHPort))
fmt.Println("SSH username: " + fmt.Sprint(d.SSHUsername))
fmt.Println("SSH password: " + fmt.Sprint(d.SSHPassword))
fmt.Println("VNC port: " + fmt.Sprint(d.VNCPort))
fmt.Println("VNC password: " + fmt.Sprint(d.VNCPassword))
fmt.Println("Ports Open (All TCP ports):")
for i := range d.Ports {
fmt.Println(d.Ports[i])
}
}
// TODO implementation using RPC calls
//func StartContainer(Ip string) (*docker.DockerVM,error){
// client, err := rpc.DialHTTP("tcp",Ip + ":" + serverPort)
//
// if err != nil {
// return nil,err
// }
//
// in := bufio.NewReader(os.Stdin)
//
// for {
// line, _, err := in.ReadLine()
// if err != nil {
// return nil,err
// }
//
// var reply Docker
//
// err = client.Call("Listener.StartContainer", line, &reply)
// if err != nil {
// return nil,err
// }
// log.Printf("Reply: %v, Data: %v", reply, reply.docker)
// return reply.docker, 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)
}
}