Simulation base code
This commit is contained in:
@@ -1,39 +1,119 @@
|
||||
# Spin up seperate P2PRC instances
|
||||
|
||||
# Unset P2PRC
|
||||
unset P2PRC
|
||||
|
||||
|
||||
# Create P2PRC instances
|
||||
# Parameters:
|
||||
# 1. Number of nodes
|
||||
P2PRC_instances() {
|
||||
local rootPort
|
||||
local rootPort=""
|
||||
for (( counter=0; counter<$1; counter++ )); do
|
||||
mkdir test-$counter/
|
||||
mkdir test-$counter/
|
||||
cd test-$counter/
|
||||
p2prc --dc
|
||||
|
||||
# change port no
|
||||
# Generate a new random port
|
||||
NEW_PORT=$(shuf -i 2000-65000 -n 1)
|
||||
|
||||
if [ counter == 0 ]; then
|
||||
rootPort = $NEW_PORT
|
||||
# Initialize node
|
||||
p2prc --dc
|
||||
|
||||
# Save rootPort from the first node
|
||||
if [[ $counter -eq 0 ]]; then
|
||||
rootPort=$NEW_PORT
|
||||
fi
|
||||
|
||||
echo $rootPort
|
||||
echo "Root port: $rootPort"
|
||||
echo "This node's port: $NEW_PORT"
|
||||
|
||||
p2prc --arn --ip 0.0.0.0 --port $rootPort
|
||||
# Set root port
|
||||
p2prc --arn --ip 0.0.0.0 -p "$rootPort"
|
||||
|
||||
# add current node
|
||||
p2prc --as 0.0.0.0 -p "$NEW_PORT"
|
||||
|
||||
# Replace the ServerPort in config
|
||||
sed -i.bak "s/\"ServerPort\": \"[0-9]*\"/\"ServerPort\": \"$NEW_PORT\"/" "./config.json"
|
||||
sed -i.bak "s/\"Test\": false/\"Test\": true/" "./config.json"
|
||||
sed -i.bak "s/\"BehindNAT\": true/\"BehindNAT\": false/" "./config.json"
|
||||
|
||||
cat config.json
|
||||
|
||||
# List services
|
||||
p2prc --ls
|
||||
|
||||
cd ..
|
||||
done
|
||||
done
|
||||
|
||||
# remove all instances
|
||||
rm -rf test-*
|
||||
# remove all instances after testing (optional — might want to comment this during testing)
|
||||
# rm -rf test-*
|
||||
}
|
||||
|
||||
P2PRC_instances 2
|
||||
# Arrays of process IDs
|
||||
pids=()
|
||||
|
||||
# Calls all the P2PRC instances created for testing
|
||||
Start_all_instances() {
|
||||
for (( counter=0; counter<$1; counter++ )); do
|
||||
cd test-$counter/
|
||||
# Start P2PRC as background process
|
||||
p2prc -s &
|
||||
pids+=($!)
|
||||
cd ..
|
||||
done
|
||||
}
|
||||
|
||||
# Kills all P2PRC instances
|
||||
Kill_all_instances() {
|
||||
for pid in "${pids[@]}"; do
|
||||
kill "$pid"
|
||||
echo "Killed process $pid"
|
||||
done
|
||||
}
|
||||
|
||||
# Removes all test files created
|
||||
Remove_all_test_files() {
|
||||
rm -rf test-*
|
||||
}
|
||||
|
||||
# List ip tables of all nodes started
|
||||
IP_Tables_after_Started() {
|
||||
for (( counter=0; counter<$1; counter++ )); do
|
||||
cd test-$counter/
|
||||
p2prc --ls
|
||||
cd ..
|
||||
done
|
||||
}
|
||||
|
||||
Update_All_IP_Tables() {
|
||||
for (( counter=0; counter<$1; counter++ )); do
|
||||
cd test-$counter/
|
||||
p2prc --us
|
||||
cd ..
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
# ---------------- Work flow test ---------------
|
||||
|
||||
# Unset default P2PRC env path
|
||||
unset P2PRC
|
||||
|
||||
# Run with 2 nodes
|
||||
P2PRC_instances 3
|
||||
|
||||
# Start instances
|
||||
Start_all_instances 3
|
||||
|
||||
# Delay for instances to run
|
||||
sleep 6
|
||||
|
||||
# update all instances ip table
|
||||
#Update_All_IP_Tables 3
|
||||
|
||||
# List ip tables of nodes started
|
||||
IP_Tables_after_Started 3
|
||||
|
||||
# Remove test files created
|
||||
Remove_all_test_files
|
||||
|
||||
# Kill all instances
|
||||
Kill_all_instances
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package clientIPTable
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||
"github.com/Akilan1999/p2p-rendering-computation/p2p"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Akilan1999/p2p-rendering-computation/config"
|
||||
"github.com/Akilan1999/p2p-rendering-computation/p2p"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var mu sync.Mutex
|
||||
@@ -18,146 +18,146 @@ var mu sync.Mutex
|
||||
// UpdateIpTable Does the following to update it's IP table
|
||||
func UpdateIpTable(IpAddress string, serverPort string, wg *sync.WaitGroup) error {
|
||||
|
||||
config, err := config.ConfigInit(nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config, err := config.ConfigInit(nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := http.Client{}
|
||||
client := http.Client{}
|
||||
|
||||
var resp []byte
|
||||
var resp []byte
|
||||
|
||||
version := p2p.Ip4or6(IpAddress)
|
||||
if version == "version 6" {
|
||||
resp, err = UploadMultipartFile(client, "http://["+IpAddress+"]:"+serverPort+"/IpTable", "json", config.IPTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
resp, err = UploadMultipartFile(client, "http://"+IpAddress+":"+serverPort+"/IpTable", "json", config.IPTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
version := p2p.Ip4or6(IpAddress)
|
||||
if version == "version 6" {
|
||||
resp, err = UploadMultipartFile(client, "http://["+IpAddress+"]:"+serverPort+"/IpTable", "json", config.IPTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
resp, err = UploadMultipartFile(client, "http://"+IpAddress+":"+serverPort+"/IpTable", "json", config.IPTable)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if resp == nil {
|
||||
return nil
|
||||
}
|
||||
if resp == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
//resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
|
||||
// config.IPTable,
|
||||
// "json")
|
||||
//
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
//resp, err := SendPostRequest("http://"+IpAddress+":8088/IpTable",
|
||||
// config.IPTable,
|
||||
// "json")
|
||||
//
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
|
||||
var ipStruct p2p.IpAddresses
|
||||
json.Unmarshal(resp, &ipStruct)
|
||||
var ipStruct p2p.IpAddresses
|
||||
json.Unmarshal(resp, &ipStruct)
|
||||
|
||||
// Updates IP table based on information provided
|
||||
// by the server
|
||||
if len(ipStruct.IpAddress) > 0 {
|
||||
err = ipStruct.SpeedTestUpdatedIPTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Updates IP table based on information provided
|
||||
// by the server
|
||||
if len(ipStruct.IpAddress) > 0 {
|
||||
err = ipStruct.SpeedTestUpdatedIPTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Not required to update IP table as speed test updates the IP Table
|
||||
//err = ipStruct.WriteIpTable()
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
// Not required to update IP table as speed test updates the IP Table
|
||||
//err = ipStruct.WriteIpTable()
|
||||
//if err != nil {
|
||||
// return err
|
||||
//}
|
||||
|
||||
wg.Done()
|
||||
wg.Done()
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateIpTableListClient updates IP tables (Default 3 hops) based on server information available
|
||||
// on the ip tables
|
||||
func UpdateIpTableListClient() error {
|
||||
// Get config information
|
||||
Config, err := config.ConfigInit(nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Get config information
|
||||
Config, err := config.ConfigInit(nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Ensure that the IP Table has Node pingable
|
||||
err = p2p.LocalSpeedTestIpTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 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
|
||||
// IP addresses to not append to struct due to
|
||||
// duplication
|
||||
|
||||
Addresses, err := p2p.ReadIpTable()
|
||||
var DoNotRead p2p.IpAddresses
|
||||
Addresses, err := p2p.ReadIpTable()
|
||||
var DoNotRead p2p.IpAddresses
|
||||
|
||||
currentIPV4, err := p2p.CurrentPublicIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
currentIPV4, err := p2p.CurrentPublicIP()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var w sync.WaitGroup
|
||||
var w sync.WaitGroup
|
||||
|
||||
// Run loop 2 times
|
||||
for i := 0; i < 2; i++ {
|
||||
// Gets information from IP table
|
||||
Addresses, err = p2p.ReadIpTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Run loop 2 times
|
||||
for i := 0; i < 2; i++ {
|
||||
// Gets information from IP table
|
||||
Addresses, err = p2p.ReadIpTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP)
|
||||
//DoNotRead.IpAddress = append(DoNotRead.IpAddress, PublicIP)
|
||||
|
||||
// Updates IP table based on server IP table
|
||||
for j := range Addresses.IpAddress {
|
||||
// Updates IP table based on server IP table
|
||||
for j := range Addresses.IpAddress {
|
||||
|
||||
Exists := false
|
||||
// If the address is local then add to the local list
|
||||
if currentIPV4 == Addresses.IpAddress[j].Ipv4 && Addresses.IpAddress[j].ServerPort == Config.ServerPort {
|
||||
Exists = true
|
||||
}
|
||||
Exists := false
|
||||
// If the address is local then add to the local list
|
||||
if currentIPV4 == Addresses.IpAddress[j].Ipv4 && Addresses.IpAddress[j].ServerPort == Config.ServerPort {
|
||||
Exists = true
|
||||
}
|
||||
|
||||
// Check if IP addresses is there in the struct DoNotRead
|
||||
for k := range DoNotRead.IpAddress {
|
||||
if (DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[j].Ipv4 && DoNotRead.IpAddress[k].ServerPort == Addresses.IpAddress[j].ServerPort) || (DoNotRead.IpAddress[k].Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == DoNotRead.IpAddress[k].Ipv6) {
|
||||
Exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
// Check if IP addresses is there in the struct DoNotRead
|
||||
for k := range DoNotRead.IpAddress {
|
||||
if (DoNotRead.IpAddress[k].Ipv4 == Addresses.IpAddress[j].Ipv4 && DoNotRead.IpAddress[k].ServerPort == Addresses.IpAddress[j].ServerPort) || (DoNotRead.IpAddress[k].Ipv6 != "" && Addresses.IpAddress[j].Ipv6 == DoNotRead.IpAddress[k].Ipv6) {
|
||||
Exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If the struct exists then continues
|
||||
if Exists {
|
||||
continue
|
||||
}
|
||||
// If the struct exists then continues
|
||||
if Exists {
|
||||
continue
|
||||
}
|
||||
|
||||
w.Add(1)
|
||||
if Addresses.IpAddress[j].Ipv6 != "" {
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort, &w)
|
||||
} else if Addresses.IpAddress[j].Ipv4 != currentIPV4 {
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort, &w)
|
||||
}
|
||||
w.Wait()
|
||||
w.Add(1)
|
||||
if Addresses.IpAddress[j].Ipv6 != "" {
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv6, Addresses.IpAddress[j].ServerPort, &w)
|
||||
} else if Addresses.IpAddress[j].Ipv4 != currentIPV4 {
|
||||
go UpdateIpTable(Addresses.IpAddress[j].Ipv4, Addresses.IpAddress[j].ServerPort, &w)
|
||||
}
|
||||
w.Wait()
|
||||
|
||||
//Appends server1 IP address to variable DoNotRead
|
||||
DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
|
||||
}
|
||||
}
|
||||
//Appends server1 IP address to variable DoNotRead
|
||||
DoNotRead.IpAddress = append(DoNotRead.IpAddress, Addresses.IpAddress[j])
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveOfflineNodes() error {
|
||||
// Ensure that the IP Table has Node pingable
|
||||
err := p2p.LocalSpeedTestIpTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
// Ensure that the IP Table has Node pingable
|
||||
err := p2p.LocalSpeedTestIpTable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendPostRequest Sends a file as a
|
||||
@@ -207,58 +207,58 @@ func RemoveOfflineNodes() error {
|
||||
//}
|
||||
|
||||
func UploadMultipartFile(client http.Client, uri, key, path string) ([]byte, error) {
|
||||
body, writer := io.Pipe()
|
||||
body, writer := io.Pipe()
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, uri, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, uri, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
mwriter := multipart.NewWriter(writer)
|
||||
req.Header.Add("Content-Type", mwriter.FormDataContentType())
|
||||
mwriter := multipart.NewWriter(writer)
|
||||
req.Header.Add("Content-Type", mwriter.FormDataContentType())
|
||||
|
||||
errchan := make(chan error)
|
||||
errchan := make(chan error)
|
||||
|
||||
go func() {
|
||||
defer close(errchan)
|
||||
defer writer.Close()
|
||||
defer mwriter.Close()
|
||||
go func() {
|
||||
defer close(errchan)
|
||||
defer writer.Close()
|
||||
defer mwriter.Close()
|
||||
|
||||
w, err := mwriter.CreateFormFile(key, path)
|
||||
if err != nil {
|
||||
errchan <- err
|
||||
return
|
||||
}
|
||||
w, err := mwriter.CreateFormFile(key, path)
|
||||
if err != nil {
|
||||
errchan <- err
|
||||
return
|
||||
}
|
||||
|
||||
in, err := os.Open(path)
|
||||
if err != nil {
|
||||
errchan <- err
|
||||
return
|
||||
}
|
||||
defer in.Close()
|
||||
in, err := os.Open(path)
|
||||
if err != nil {
|
||||
errchan <- err
|
||||
return
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
if written, err := io.Copy(w, in); err != nil {
|
||||
errchan <- fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err)
|
||||
return
|
||||
}
|
||||
if written, err := io.Copy(w, in); err != nil {
|
||||
errchan <- fmt.Errorf("error copying %s (%d bytes written): %v", path, written, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := mwriter.Close(); err != nil {
|
||||
errchan <- err
|
||||
return
|
||||
}
|
||||
}()
|
||||
if err := mwriter.Close(); err != nil {
|
||||
errchan <- err
|
||||
return
|
||||
}
|
||||
}()
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
content, err := ioutil.ReadAll(resp.Body)
|
||||
content, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return content, nil
|
||||
return content, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ type Config struct {
|
||||
KeyFile string
|
||||
BareMetal bool
|
||||
UnsafeMode bool
|
||||
Test bool
|
||||
CustomConfig interface{}
|
||||
//NetworkInterface string
|
||||
//NetworkInterfaceIPV6Index int
|
||||
|
||||
@@ -95,6 +95,7 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No
|
||||
Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey"
|
||||
Defaults.BareMetal = false
|
||||
Defaults.UnsafeMode = false
|
||||
Defaults.Test = false
|
||||
|
||||
// Generate certificate files for SSL
|
||||
err = GenerateCertificate()
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"github.com/Akilan1999/p2p-rendering-computation/p2p"
|
||||
"log"
|
||||
"math/big"
|
||||
"net"
|
||||
@@ -54,7 +53,8 @@ func GenerateCertificate() error {
|
||||
|
||||
//flag.Parse()
|
||||
|
||||
host, err = p2p.CurrentPublicIP()
|
||||
//host, err = p2p.CurrentPublicIP()
|
||||
host = "0.0.0.0"
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -42,6 +42,8 @@ type IP struct {
|
||||
Query string
|
||||
}
|
||||
|
||||
var Test = false
|
||||
|
||||
// ReadIpTable Read data from Ip tables from json file
|
||||
func ReadIpTable() (*IpAddresses, error) {
|
||||
// Get Path from config
|
||||
@@ -178,6 +180,16 @@ func (table *IpAddresses) RemoveDuplicates() error {
|
||||
|
||||
// CurrentPublicIP Get Current Public IP address
|
||||
func CurrentPublicIP() (string, error) {
|
||||
// Get configs
|
||||
Config, err := config.ConfigInit(nil, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// If test mode is on then return local address
|
||||
if Config.Test {
|
||||
return "0.0.0.0", nil
|
||||
}
|
||||
|
||||
req, err := http.Get("http://ip-api.com/json/")
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -312,6 +312,7 @@ func Server() (*gin.Engine, error) {
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
ProxyIpAddr.ServerPort = config.ServerPort
|
||||
ProxyIpAddr.Name = config.MachineName
|
||||
ProxyIpAddr.NAT = false
|
||||
|
||||
Reference in New Issue
Block a user