added WASM support

This commit is contained in:
2025-05-03 01:29:11 +01:00
parent ece1e63fda
commit 034f9e2896
56 changed files with 1293 additions and 10443 deletions

View File

@@ -1,310 +1,301 @@
package client
import (
"encoding/json"
"errors"
"github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/google/uuid"
"io/ioutil"
"os"
)
// Groups Data Structure type
type Groups struct {
GroupList []*Group `json:"Groups"`
}
// Group Information about a single group
type Group struct {
ID string `json:"ID"`
TrackContainerList []*TrackContainer `json:"TrackContainer"`
// Sneaky as required only when removing the element
// Set when GetGroup function is called
index int
}
// CreateGroup Creates a new group to add a set of track containers
func CreateGroup() (*Group, error) {
// Creating variable of type new group
var NewGroup Group
// Generate new UUID for group ID
id := uuid.New()
// Add new group id and prepend with the string "grp"
// The reason this is done is to differentiate between a
// group ID and docker container ID
NewGroup.ID = "grp" + id.String()
// Adding the new group to the
// GroupTrackContainer File
err := NewGroup.AddGroupToFile()
if err != nil {
return nil, err
}
return &NewGroup, nil
}
// RemoveGroup Removes group based on the group ID provided
func RemoveGroup(GroupID string) error {
// Read group information from the
//grouptrackcontainer json file
groups, err := ReadGroup()
if err != nil {
return err
}
// Gets Group struct based on group ID
// provided
group, err := GetGroup(GroupID)
if err != nil {
return err
}
// Remove Group struct from the groups variable
groups.GroupList = append(groups.GroupList[:group.index], groups.GroupList[group.index+1:]...)
// Write new groups to the grouptrackcontainer json file
err = groups.WriteGroup()
if err != nil {
return err
}
return nil
}
// AddContainerToGroup Adds container information to the Group based on the Group ID
func AddContainerToGroup(ContainerID string, GroupID string) (*Group, error) {
// Gets container information based on container ID provided
containerInfo, err := GetContainerInformation(ContainerID)
if err != nil {
return nil, err
}
// Gets group information based on the group ID provided
group, err := GetGroup(GroupID)
if err != nil {
return nil, err
}
// Adds container information the group
group.AddContainer(containerInfo)
// Get Groups information from reading the grouptrackcontainer.json file
groups, err := ReadGroup()
if err != nil {
return nil, err
}
// Updating specific element in the group list with the added container
groups.GroupList[group.index] = group
// Write groups information on the grouptrackcontainer.json file
err = groups.WriteGroup()
if err != nil {
return nil, err
}
return group, nil
}
// RemoveContainerGroup Remove Container from the group ID specified
func RemoveContainerGroup(ContainerID string, GroupID string) (*Group, error) {
// Get container information based on container ID provided
containerInfo, err := GetContainerInformation(ContainerID)
if err != nil {
return nil, err
}
// Gets group information based on the group ID provided
group, err := GetGroup(GroupID)
if err != nil {
return nil, err
}
// Remove container from the appropriate group
err = group.RemoveContainerGroup(containerInfo)
if err != nil {
return nil, err
}
// Get Groups information from reading the grouptrackcontainer.json file
groups, err := ReadGroup()
if err != nil {
return nil, err
}
// Updating specific element in the group list with the remove container
groups.GroupList[group.index] = group
// Write groups information on the grouptrackcontainer.json file
err = groups.WriteGroup()
if err != nil {
return nil, err
}
return group, nil
}
// RemoveContainerGroups Remove Container from groups (i.e which ever groups has information
// about that container). This is mostly called when a container is deleted or removed from
// the tracked container list
func RemoveContainerGroups(ContainerID string) error {
// Get container information based on container ID provided
containerInfo, err := GetContainerInformation(ContainerID)
if err != nil {
return err
}
// Get Groups information from reading the grouptrackcontainer.json file
groups, err := ReadGroup()
if err != nil {
return err
}
// Removes container information from all groups it is found in
err = groups.RemoveContainerGroups(containerInfo)
if err != nil {
return err
}
// Write groups information on the grouptrackcontainer.json file
err = groups.WriteGroup()
if err != nil {
return err
}
return nil
}
// GetGroup Gets group information based on
// group id provided
func GetGroup(GroupID string) (*Group, error) {
// Read group information from the
//grouptrackcontainer json file
groups, err := ReadGroup()
if err != nil {
return nil, err
}
// Iterate through the set of groups and
// if the group ID matches then return it
for i, group := range groups.GroupList {
if group.ID == GroupID {
group.index = i
return group, nil
}
}
return nil, errors.New("Group not found. ")
}
// AddGroupToFile Adds Group struct to the GroupTrackContainer File
func (grp *Group) AddGroupToFile() error {
// Gets all group information from the
// GroupTrackContainer JSON file
groups, err := ReadGroup()
if err != nil {
return err
}
// Appending the newly created group
groups.GroupList = append(groups.GroupList, grp)
// Writing Group information to the GroupTrackContainer
// JSON file
err = groups.WriteGroup()
if err != nil {
return err
}
return nil
}
//type Groups struct {
// GroupList []*Group `json:"Groups"`
//}
//
//// Group Information about a single group
//type Group struct {
// ID string `json:"ID"`
// TrackContainerList []*TrackContainer `json:"TrackContainer"`
// // Sneaky as required only when removing the element
// // Set when GetGroup function is called
// index int
//}
//
//// CreateGroup Creates a new group to add a set of track containers
//func CreateGroup() (*Group, error) {
// // Creating variable of type new group
// var NewGroup Group
// // Generate new UUID for group ID
// id := uuid.New()
// // Add new group id and prepend with the string "grp"
// // The reason this is done is to differentiate between a
// // group ID and docker container ID
// NewGroup.ID = "grp" + id.String()
// // Adding the new group to the
// // GroupTrackContainer File
// err := NewGroup.AddGroupToFile()
// if err != nil {
// return nil, err
// }
//
// return &NewGroup, nil
//}
//
//// RemoveGroup Removes group based on the group ID provided
//func RemoveGroup(GroupID string) error {
// // Read group information from the
// //grouptrackcontainer json file
// groups, err := ReadGroup()
// if err != nil {
// return err
// }
// // Gets Group struct based on group ID
// // provided
// group, err := GetGroup(GroupID)
// if err != nil {
// return err
// }
// // Remove Group struct from the groups variable
// groups.GroupList = append(groups.GroupList[:group.index], groups.GroupList[group.index+1:]...)
//
// // Write new groups to the grouptrackcontainer json file
// err = groups.WriteGroup()
// if err != nil {
// return err
// }
//
// return nil
//}
//
//// AddContainerToGroup Adds container information to the Group based on the Group ID
//func AddContainerToGroup(ContainerID string, GroupID string) (*Group, error) {
// // Gets container information based on container ID provided
// containerInfo, err := GetContainerInformation(ContainerID)
// if err != nil {
// return nil, err
// }
// // Gets group information based on the group ID provided
// group, err := GetGroup(GroupID)
// if err != nil {
// return nil, err
// }
// // Adds container information the group
// group.AddContainer(containerInfo)
//
// // Get Groups information from reading the grouptrackcontainer.json file
// groups, err := ReadGroup()
// if err != nil {
// return nil, err
// }
// // Updating specific element in the group list with the added container
// groups.GroupList[group.index] = group
// // Write groups information on the grouptrackcontainer.json file
// err = groups.WriteGroup()
// if err != nil {
// return nil, err
// }
//
// return group, nil
//}
//
//// RemoveContainerGroup Remove Container from the group ID specified
//func RemoveContainerGroup(ContainerID string, GroupID string) (*Group, error) {
// // Get container information based on container ID provided
// containerInfo, err := GetContainerInformation(ContainerID)
// if err != nil {
// return nil, err
// }
// // Gets group information based on the group ID provided
// group, err := GetGroup(GroupID)
// if err != nil {
// return nil, err
// }
// // Remove container from the appropriate group
// err = group.RemoveContainerGroup(containerInfo)
// if err != nil {
// return nil, err
// }
// // Get Groups information from reading the grouptrackcontainer.json file
// groups, err := ReadGroup()
// if err != nil {
// return nil, err
// }
// // Updating specific element in the group list with the remove container
// groups.GroupList[group.index] = group
// // Write groups information on the grouptrackcontainer.json file
// err = groups.WriteGroup()
// if err != nil {
// return nil, err
// }
//
// return group, nil
//}
//
//// RemoveContainerGroups Remove Container from groups (i.e which ever groups has information
//// about that container). This is mostly called when a container is deleted or removed from
//// the tracked container list
//func RemoveContainerGroups(ContainerID string) error {
// // Get container information based on container ID provided
// containerInfo, err := GetContainerInformation(ContainerID)
// if err != nil {
// return err
// }
// // Get Groups information from reading the grouptrackcontainer.json file
// groups, err := ReadGroup()
// if err != nil {
// return err
// }
// // Removes container information from all groups it is found in
// err = groups.RemoveContainerGroups(containerInfo)
// if err != nil {
// return err
// }
// // Write groups information on the grouptrackcontainer.json file
// err = groups.WriteGroup()
// if err != nil {
// return err
// }
//
// return nil
//}
//
//// GetGroup Gets group information based on
//// group id provided
//func GetGroup(GroupID string) (*Group, error) {
// // Read group information from the
// //grouptrackcontainer json file
// groups, err := ReadGroup()
// if err != nil {
// return nil, err
// }
// // Iterate through the set of groups and
// // if the group ID matches then return it
// for i, group := range groups.GroupList {
// if group.ID == GroupID {
// group.index = i
// return group, nil
// }
// }
//
// return nil, errors.New("Group not found. ")
//}
//
//// AddGroupToFile Adds Group struct to the GroupTrackContainer File
//func (grp *Group) AddGroupToFile() error {
// // Gets all group information from the
// // GroupTrackContainer JSON file
// groups, err := ReadGroup()
// if err != nil {
// return err
// }
// // Appending the newly created group
// groups.GroupList = append(groups.GroupList, grp)
// // Writing Group information to the GroupTrackContainer
// // JSON file
// err = groups.WriteGroup()
// if err != nil {
// return err
// }
//
// return nil
//}
// ReadGroup Function reads grouptrackcontainers.json and converts
// result to Groups
func ReadGroup() (*Groups, error) {
// Get Path from config
config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}
jsonFile, err := os.Open(config.GroupTrackContainersPath)
// if we os.Open returns an error then handle it
if err != nil {
return nil, err
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// we initialize our Users array
var groups Groups
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &groups)
return &groups, nil
}
// WriteGroup Function to write type Groups to the grouptrackcontainers.json file
func (grp *Groups) WriteGroup() error {
file, err := json.MarshalIndent(grp, "", " ")
if err != nil {
return err
}
// Get Path from config
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
// Writes to the appropriate file
err = ioutil.WriteFile(config.GroupTrackContainersPath, file, 0644)
if err != nil {
return err
}
return nil
}
// AddContainer Adds a container to the Tracked container list of the group
func (grp *Group) AddContainer(Container *TrackContainer) error {
grp.TrackContainerList = append(grp.TrackContainerList, Container)
return nil
}
// RemoveContainerGroup Removes container information from the group
func (grp *Group) RemoveContainerGroup(Container *TrackContainer) error {
// Iterating through all container in the Group of Tracked Container
for i, container := range grp.TrackContainerList {
// If the container ID matches then remove the container from the group
if container.Id == Container.Id {
grp.TrackContainerList = append(grp.TrackContainerList[:i], grp.TrackContainerList[i+1:]...)
}
}
return nil
}
// RemoveContainerGroups removes container found in all groups
func (grp *Groups) RemoveContainerGroups(Container *TrackContainer) error {
// Iterating through all groups
for i, group := range grp.GroupList {
// Removes the container in the following group
// if it exists
err := group.RemoveContainerGroup(Container)
if err != nil {
return err
}
// Set group information to list groups
grp.GroupList[i] = group
}
return nil
}
// ModifyContainerGroups Modifies container information is all groups
// available
func (TC *TrackContainer) ModifyContainerGroups() error {
group, err := ReadGroup()
if err != nil {
return err
}
// Iterate though all groups and modify the container
// information in groups where the modified container
// ID matches
for i, _ := range group.GroupList {
// Checking in each group if the modified container ID exists
for j, _ := range group.GroupList[i].TrackContainerList {
// If there is match then change them
if group.GroupList[i].TrackContainerList[j].Id == TC.Id {
group.GroupList[i].TrackContainerList[j] = TC
}
}
}
// Write modified result to the Groups track container JSON file
err = group.WriteGroup()
if err != nil {
return err
}
return nil
}
//func ReadGroup() (*Groups, error) {
// // Get Path from config
// config, err := config.ConfigInit(nil, nil)
// if err != nil {
// return nil, err
// }
// jsonFile, err := os.Open(config.GroupTrackContainersPath)
// // if we os.Open returns an error then handle it
// if err != nil {
// return nil, err
// }
// // defer the closing of our jsonFile so that we can parse it later on
// defer jsonFile.Close()
// // read our opened xmlFile as a byte array.
// byteValue, _ := ioutil.ReadAll(jsonFile)
// // we initialize our Users array
// var groups Groups
// // we unmarshal our byteArray which contains our
// // jsonFile's content into 'users' which we defined above
// json.Unmarshal(byteValue, &groups)
// return &groups, nil
//}
//
//// WriteGroup Function to write type Groups to the grouptrackcontainers.json file
//func (grp *Groups) WriteGroup() error {
// file, err := json.MarshalIndent(grp, "", " ")
// if err != nil {
// return err
// }
// // Get Path from config
// config, err := config.ConfigInit(nil, nil)
// if err != nil {
// return err
// }
// // Writes to the appropriate file
// err = ioutil.WriteFile(config.GroupTrackContainersPath, file, 0644)
// if err != nil {
// return err
// }
// return nil
//}
//
//// AddContainer Adds a container to the Tracked container list of the group
//func (grp *Group) AddContainer(Container *TrackContainer) error {
// grp.TrackContainerList = append(grp.TrackContainerList, Container)
// return nil
//}
//
//// RemoveContainerGroup Removes container information from the group
//func (grp *Group) RemoveContainerGroup(Container *TrackContainer) error {
// // Iterating through all container in the Group of Tracked Container
// for i, container := range grp.TrackContainerList {
// // If the container ID matches then remove the container from the group
// if container.Id == Container.Id {
// grp.TrackContainerList = append(grp.TrackContainerList[:i], grp.TrackContainerList[i+1:]...)
// }
// }
// return nil
//}
//
//// RemoveContainerGroups removes container found in all groups
//func (grp *Groups) RemoveContainerGroups(Container *TrackContainer) error {
// // Iterating through all groups
// for i, group := range grp.GroupList {
// // Removes the container in the following group
// // if it exists
// err := group.RemoveContainerGroup(Container)
// if err != nil {
// return err
// }
// // Set group information to list groups
// grp.GroupList[i] = group
// }
// return nil
//}
//
//// ModifyContainerGroups Modifies container information is all groups
//// available
//func (TC *TrackContainer) ModifyContainerGroups() error {
// group, err := ReadGroup()
// if err != nil {
// return err
// }
// // Iterate though all groups and modify the container
// // information in groups where the modified container
// // ID matches
// for i, _ := range group.GroupList {
// // Checking in each group if the modified container ID exists
// for j, _ := range group.GroupList[i].TrackContainerList {
// // If there is match then change them
// if group.GroupList[i].TrackContainerList[j].Id == TC.Id {
// group.GroupList[i].TrackContainerList[j] = TC
// }
// }
// }
//
// // Write modified result to the Groups track container JSON file
// err = group.WriteGroup()
// if err != nil {
// return err
// }
//
// return nil
//}

View File

@@ -1,220 +1,210 @@
package client
import (
"encoding/json"
"errors"
"fmt"
"github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
"io/ioutil"
"os"
)
// TrackContainers This struct stores arrays of current containers running
type TrackContainers struct {
TrackContainerList []TrackContainer `json:"TrackContainer"`
}
// TrackContainer Stores information of current containers
type TrackContainer struct {
Id string `json:"ID"`
Container *docker.DockerVM `json:"Container"`
IpAddress string `json:"IpAddress"`
}
// AddTrackContainer Adds new container which has been added to the track container
func AddTrackContainer(d *docker.DockerVM, ipAddress string) error {
// Checking if pointer d is null
if d == nil {
return errors.New("d is nil")
}
//Get config information to derive paths for track containers json file
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
// Getting information about the file trackcontainers.json file
stat, err := os.Stat(config.TrackContainersPath)
if err != nil {
return err
}
// Initialize variable for TrackContainers
var trackContainers TrackContainers
// If the trackcontainers.json file is not empty then
// Read from that file
if stat.Size() != 0 {
// Reads tracked container file
trackContainersFile, err := ReadTrackContainers(config.TrackContainersPath)
if err != nil {
return err
}
trackContainers = *trackContainersFile
}
// Initialize new variable with type struct TrackContainers and
// add container struct and ip address
var trackContainer TrackContainer
trackContainer.Id = d.ID
trackContainer.Container = d
trackContainer.IpAddress = ipAddress
// Adds new container as passed in the parameter to the struct
if &trackContainer == nil {
return errors.New("trackContainer variable is nil")
}
trackContainers.TrackContainerList = append(trackContainers.TrackContainerList, trackContainer)
// write modified information to the tracked json file
data, err := json.MarshalIndent(trackContainers, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
if err != nil {
return err
}
return nil
}
// RemoveTrackedContainer This function removos tracked container from the trackcontainer JSON file
func RemoveTrackedContainer(id string) error {
//Get config information to derive paths for track containers json file
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err
}
// Getting tracked container struct
trackedContainers, err := ReadTrackContainers(config.TrackContainersPath)
// Storing index of element to remove
var removeElement int
removeElement = -1
for i := range trackedContainers.TrackContainerList {
if trackedContainers.TrackContainerList[i].Id == id {
removeElement = i
break
}
}
// Checks if the element to be removed has been detected
if removeElement == -1 {
return errors.New("Container ID not found in the tracked list")
}
// Remove the detected element from the struct
trackedContainers.TrackContainerList = append(trackedContainers.TrackContainerList[:removeElement], trackedContainers.TrackContainerList[removeElement+1:]...)
// write modified information to the tracked json file
data, err := json.MarshalIndent(trackedContainers, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
if err != nil {
return err
}
return nil
}
// ViewTrackedContainers View Containers currently tracked
func ViewTrackedContainers() (error, *TrackContainers) {
config, err := config.ConfigInit(nil, nil)
if err != nil {
return err, nil
}
trackedContianers, err := ReadTrackContainers(config.TrackContainersPath)
if err != nil {
return err, nil
}
return nil, trackedContianers
}
// ReadTrackContainers Reads containers which are currently tracked
func ReadTrackContainers(filename string) (*TrackContainers, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c := &TrackContainers{}
err = json.Unmarshal(buf, c)
if err != nil {
return nil, fmt.Errorf("in file %q: %v", filename, err)
}
return c, nil
}
//func ModifyTrackContainers()
// GetContainerInformation gets information about container based on
// container ID provided
func GetContainerInformation(ID string) (*TrackContainer, error) {
// Getting the current containers
err, CurrentContainers := ViewTrackedContainers()
if err != nil {
return nil, err
}
// Iterating through all tracked containers to get the container information
// of the ID passed through the function parameter
for _, container := range CurrentContainers.TrackContainerList {
if container.Container.ID == ID {
return &container, nil
}
}
return nil, errors.New("Container not found. ")
}
// ModifyContainerInformation Modifies information inside the container
func (TC *TrackContainer) ModifyContainerInformation() error {
// Gets all the information of tracker containers
err, t := ViewTrackedContainers()
if err != nil {
return err
}
// Find the element where the containers match and
// change them
for i, container := range t.TrackContainerList {
if TC.Id == container.Id {
t.TrackContainerList[i] = *TC
break
}
}
// Write the modified information to the file
// write modified information to the tracked json file
err = t.WriteContainers()
if err != nil {
return err
}
return nil
}
// WriteContainers Write information back to the config file
func (TC *TrackContainers) WriteContainers() error {
// Initialize config file
config, err := config.ConfigInit(nil, nil)
// write modified information to the tracked json file
data, err := json.MarshalIndent(TC, "", "\t")
if err != nil {
return err
}
err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
if err != nil {
return err
}
return nil
}
// CheckID Checks if the ID belongs to a group or a single container
func CheckID(ID string) (string, error) {
// For group checks if the 1st characters is "grp"
if ID[0:3] == "grp" {
return "group", nil
} else {
return "container", nil
}
return "", nil
}
//// TrackContainers This struct stores arrays of current containers running
//type TrackContainers struct {
// TrackContainerList []TrackContainer `json:"TrackContainer"`
//}
//
//// TrackContainer Stores information of current containers
//type TrackContainer struct {
// Id string `json:"ID"`
// Container *docker.DockerVM `json:"Container"`
// IpAddress string `json:"IpAddress"`
//}
//
//// AddTrackContainer Adds new container which has been added to the track container
//func AddTrackContainer(d *docker.DockerVM, ipAddress string) error {
// // Checking if pointer d is null
// if d == nil {
// return errors.New("d is nil")
// }
// //Get config information to derive paths for track containers json file
// config, err := config.ConfigInit(nil, nil)
// if err != nil {
// return err
// }
//
// // Getting information about the file trackcontainers.json file
// stat, err := os.Stat(config.TrackContainersPath)
// if err != nil {
// return err
// }
// // Initialize variable for TrackContainers
// var trackContainers TrackContainers
// // If the trackcontainers.json file is not empty then
// // Read from that file
// if stat.Size() != 0 {
// // Reads tracked container file
// trackContainersFile, err := ReadTrackContainers(config.TrackContainersPath)
// if err != nil {
// return err
// }
// trackContainers = *trackContainersFile
// }
//
// // Initialize new variable with type struct TrackContainers and
// // add container struct and ip address
// var trackContainer TrackContainer
// trackContainer.Id = d.ID
// trackContainer.Container = d
// trackContainer.IpAddress = ipAddress
//
// // Adds new container as passed in the parameter to the struct
// if &trackContainer == nil {
// return errors.New("trackContainer variable is nil")
// }
// trackContainers.TrackContainerList = append(trackContainers.TrackContainerList, trackContainer)
//
// // write modified information to the tracked json file
// data, err := json.MarshalIndent(trackContainers, "", "\t")
// if err != nil {
// return err
// }
// err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
// if err != nil {
// return err
// }
//
// return nil
//}
//
//// RemoveTrackedContainer This function removos tracked container from the trackcontainer JSON file
//func RemoveTrackedContainer(id string) error {
// //Get config information to derive paths for track containers json file
// config, err := config.ConfigInit(nil, nil)
// if err != nil {
// return err
// }
// // Getting tracked container struct
// trackedContainers, err := ReadTrackContainers(config.TrackContainersPath)
// // Storing index of element to remove
// var removeElement int
// removeElement = -1
// for i := range trackedContainers.TrackContainerList {
// if trackedContainers.TrackContainerList[i].Id == id {
// removeElement = i
// break
// }
// }
// // Checks if the element to be removed has been detected
// if removeElement == -1 {
// return errors.New("Container ID not found in the tracked list")
// }
// // Remove the detected element from the struct
// trackedContainers.TrackContainerList = append(trackedContainers.TrackContainerList[:removeElement], trackedContainers.TrackContainerList[removeElement+1:]...)
//
// // write modified information to the tracked json file
// data, err := json.MarshalIndent(trackedContainers, "", "\t")
// if err != nil {
// return err
// }
// err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
// if err != nil {
// return err
// }
//
// return nil
//}
//
//// ViewTrackedContainers View Containers currently tracked
//func ViewTrackedContainers() (error, *TrackContainers) {
// config, err := config.ConfigInit(nil, nil)
// if err != nil {
// return err, nil
// }
// trackedContianers, err := ReadTrackContainers(config.TrackContainersPath)
// if err != nil {
// return err, nil
// }
//
// return nil, trackedContianers
//}
//
//// ReadTrackContainers Reads containers which are currently tracked
//func ReadTrackContainers(filename string) (*TrackContainers, error) {
// buf, err := ioutil.ReadFile(filename)
// if err != nil {
// return nil, err
// }
//
// c := &TrackContainers{}
// err = json.Unmarshal(buf, c)
// if err != nil {
// return nil, fmt.Errorf("in file %q: %v", filename, err)
// }
//
// return c, nil
//}
//
////func ModifyTrackContainers()
//
//// GetContainerInformation gets information about container based on
//// container ID provided
//func GetContainerInformation(ID string) (*TrackContainer, error) {
// // Getting the current containers
// err, CurrentContainers := ViewTrackedContainers()
// if err != nil {
// return nil, err
// }
// // Iterating through all tracked containers to get the container information
// // of the ID passed through the function parameter
// for _, container := range CurrentContainers.TrackContainerList {
// if container.Container.ID == ID {
// return &container, nil
// }
// }
// return nil, errors.New("Container not found. ")
//}
//
//// ModifyContainerInformation Modifies information inside the container
//func (TC *TrackContainer) ModifyContainerInformation() error {
// // Gets all the information of tracker containers
// err, t := ViewTrackedContainers()
// if err != nil {
// return err
// }
// // Find the element where the containers match and
// // change them
// for i, container := range t.TrackContainerList {
// if TC.Id == container.Id {
// t.TrackContainerList[i] = *TC
// break
// }
// }
// // Write the modified information to the file
// // write modified information to the tracked json file
// err = t.WriteContainers()
// if err != nil {
// return err
// }
//
// return nil
//}
//
//// WriteContainers Write information back to the config file
//func (TC *TrackContainers) WriteContainers() error {
// // Initialize config file
// config, err := config.ConfigInit(nil, nil)
// // write modified information to the tracked json file
// data, err := json.MarshalIndent(TC, "", "\t")
// if err != nil {
// return err
// }
// err = ioutil.WriteFile(config.TrackContainersPath, data, 0777)
// if err != nil {
// return err
// }
//
// return nil
//}
//
//// CheckID Checks if the ID belongs to a group or a single container
//func CheckID(ID string) (string, error) {
// // For group checks if the 1st characters is "grp"
// if ID[0:3] == "grp" {
// return "group", nil
// } else {
// return "container", nil
// }
// return "", nil
//}

View File

@@ -1,192 +1,180 @@
package client
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"github.com/Akilan1999/p2p-rendering-computation/config"
"github.com/Akilan1999/p2p-rendering-computation/p2p"
"github.com/Akilan1999/p2p-rendering-computation/server/docker"
"io/ioutil"
"net/http"
"strconv"
)
var (
serverPort = "8088"
client = http.Client{}
)
// StartContainer Start container using REST api Implementation
// From the selected server IP address
// TODO: Test cases for this function
// Calls URL ex: http://0.0.0.0:8088/startcontainer?ports=0&GPU=false&ContainerName=docker-ubuntu-sshd
func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string, baseImage string) (*docker.DockerVM, error) {
// Passes URL with number of TCP ports to allocated and to give GPU access to the docker container
var URL string
//version := p2p.Ip4or6(IP)
//
////Get port number of the server
//serverPort, err := GetServerPort(IP)
//if err != nil {
// return nil, err
//}
//if version == "version 6" {
// URL = "http://[" + IP + "]:" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName
//} else {
// Get config information
Config, err := config.ConfigInit(nil, nil)
if err != nil {
return nil, err
}
// Get public key
PublicKey, err := Config.GetPublicKey()
if err != nil {
return nil, err
}
// Used in URL to pass public key -> b64.StdEncoding.EncodeToString([]byte(PublicKey))
URL = `http://` + IP + `/startcontainer?ports=` + fmt.Sprint(NumPorts) + `&GPU=` + strconv.FormatBool(GPU) + `&ContainerName=` + ContainerName + `&BaseImage=` + baseImage + `&PublicKey=` + b64.StdEncoding.EncodeToString([]byte(PublicKey))
// Encode URL due to public key passed.
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
}
// Adds the container to the tracked list
err = AddTrackContainer(&dockerResult, IP)
if err != nil {
return nil, err
}
return &dockerResult, nil
}
// RemoveContianer Stops and removes container from the server
func RemoveContianer(IP string, ID string) error {
var URL string
//version := p2p.Ip4or6(IP)
//
////Get port number of the server
//serverPort, err := GetServerPort(IP)
//if err != nil {
// return err
//}
//if version == "version 6" {
// URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID
//} else {
URL = "http://" + IP + "/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
}
// Checks if success is returned in the body
if string(byteValue[:]) == "success" {
fmt.Println("success")
}
// Remove container from groups it exists in
err = RemoveContainerGroups(ID)
if err != nil {
return err
}
// Remove container created from the tracked list
err = RemoveTrackedContainer(ID)
if err != nil {
return err
}
return nil
}
// ViewContainers This function displays all containers available on server side
func ViewContainers(IP string) (*docker.DockerContainers, error) {
// Passes URL with route /ShowImages
var URL string
//version := p2p.Ip4or6(IP)
//
////Get port number of the server
//serverPort, err := GetServerPort(IP)
//if err != nil {
// return nil, err
//}
//if version == "version 6" {
// URL = "http://[" + IP + "]:" + serverPort + "/ShowImages"
//} else {
URL = "http://" + IP + "/ShowImages"
//}
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 Result docker.DockerContainers
// Adds byte value to docker.DockerContainers struct
json.Unmarshal(byteValue, &Result)
if err != nil {
return nil, err
}
return &Result, nil
}
// GetServerPort Helper function to do find out server port information
func GetServerPort(IpAddress string) (string, error) {
// Getting information from the clients ip table
ipTable, err := p2p.ReadIpTable()
if err != nil {
return "", err
}
// Iterate thorough ip table struct and find
// out which port is for the ip address provided
// in the parameter of the function
for _, address := range ipTable.IpAddress {
// If we found a match then return a port
if address.Ipv4 == IpAddress || address.Ipv6 == IpAddress {
return address.ServerPort, nil
}
}
// We return default port
return "8088", nil
}
//var (
// serverPort = "8088"
// client = http.Client{}
//)
//
//// StartContainer Start container using REST api Implementation
//// From the selected server IP address
//// TODO: Test cases for this function
//// Calls URL ex: http://0.0.0.0:8088/startcontainer?ports=0&GPU=false&ContainerName=docker-ubuntu-sshd
//func StartContainer(IP string, NumPorts int, GPU bool, ContainerName string, baseImage string) (*docker.DockerVM, error) {
// // Passes URL with number of TCP ports to allocated and to give GPU access to the docker container
// var URL string
// //version := p2p.Ip4or6(IP)
// //
// ////Get port number of the server
// //serverPort, err := GetServerPort(IP)
// //if err != nil {
// // return nil, err
// //}
//
// //if version == "version 6" {
// // URL = "http://[" + IP + "]:" + serverPort + "/startcontainer?ports=" + fmt.Sprint(NumPorts) + "&GPU=" + strconv.FormatBool(GPU) + "&ContainerName=" + ContainerName
// //} else {
// // Get config information
// Config, err := config.ConfigInit(nil, nil)
// if err != nil {
// return nil, err
// }
//
// // Get public key
// PublicKey, err := Config.GetPublicKey()
// if err != nil {
// return nil, err
// }
//
// // Used in URL to pass public key -> b64.StdEncoding.EncodeToString([]byte(PublicKey))
// URL = `http://` + IP + `/startcontainer?ports=` + fmt.Sprint(NumPorts) + `&GPU=` + strconv.FormatBool(GPU) + `&ContainerName=` + ContainerName + `&BaseImage=` + baseImage + `&PublicKey=` + b64.StdEncoding.EncodeToString([]byte(PublicKey))
//
// // Encode URL due to public key passed.
// 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
// }
//
// // Adds the container to the tracked list
// err = AddTrackContainer(&dockerResult, IP)
// if err != nil {
// return nil, err
// }
//
// return &dockerResult, nil
//}
//
//// RemoveContianer Stops and removes container from the server
//func RemoveContianer(IP string, ID string) error {
// var URL string
// //version := p2p.Ip4or6(IP)
// //
// ////Get port number of the server
// //serverPort, err := GetServerPort(IP)
// //if err != nil {
// // return err
// //}
//
// //if version == "version 6" {
// // URL = "http://[" + IP + "]:" + serverPort + "/RemoveContainer?id=" + ID
// //} else {
// URL = "http://" + IP + "/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
// }
//
// // Checks if success is returned in the body
// if string(byteValue[:]) == "success" {
// fmt.Println("success")
// }
//
// // Remove container from groups it exists in
// err = RemoveContainerGroups(ID)
// if err != nil {
// return err
// }
// // Remove container created from the tracked list
// err = RemoveTrackedContainer(ID)
// if err != nil {
// return err
// }
//
// return nil
//}
//
//// ViewContainers This function displays all containers available on server side
//func ViewContainers(IP string) (*docker.DockerContainers, error) {
// // Passes URL with route /ShowImages
// var URL string
// //version := p2p.Ip4or6(IP)
// //
// ////Get port number of the server
// //serverPort, err := GetServerPort(IP)
// //if err != nil {
// // return nil, err
// //}
//
// //if version == "version 6" {
// // URL = "http://[" + IP + "]:" + serverPort + "/ShowImages"
// //} else {
// URL = "http://" + IP + "/ShowImages"
// //}
// 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 Result docker.DockerContainers
//
// // Adds byte value to docker.DockerContainers struct
// json.Unmarshal(byteValue, &Result)
// if err != nil {
// return nil, err
// }
//
// return &Result, nil
//}
//
//// GetServerPort Helper function to do find out server port information
//func GetServerPort(IpAddress string) (string, error) {
// // Getting information from the clients ip table
// ipTable, err := p2p.ReadIpTable()
// if err != nil {
// return "", err
// }
//
// // Iterate thorough ip table struct and find
// // out which port is for the ip address provided
// // in the parameter of the function
// for _, address := range ipTable.IpAddress {
// // If we found a match then return a port
// if address.Ipv4 == IpAddress || address.Ipv6 == IpAddress {
// return address.ServerPort, nil
// }
// }
//
// // We return default port
// return "8088", nil
//}
// PrintStartContainer Prints results Generated container
//func PrintStartContainer(d *docker.DockerVM){