Simulation base code

This commit is contained in:
2025-05-30 16:23:39 +01:00
parent 2d55643dd9
commit a46ec3575c
7 changed files with 271 additions and 176 deletions

View File

@@ -1,39 +1,119 @@
# Spin up seperate P2PRC instances
# Unset P2PRC # Unset P2PRC
unset P2PRC unset P2PRC
# Create P2PRC instances # Create P2PRC instances
# Parameters: # Parameters:
# 1. Number of nodes # 1. Number of nodes
P2PRC_instances() { P2PRC_instances() {
local rootPort local rootPort=""
for (( counter=0; counter<$1; counter++ )); do for (( counter=0; counter<$1; counter++ )); do
mkdir test-$counter/ mkdir test-$counter/
cd test-$counter/ cd test-$counter/
p2prc --dc
# change port no # Generate a new random port
NEW_PORT=$(shuf -i 2000-65000 -n 1) NEW_PORT=$(shuf -i 2000-65000 -n 1)
if [ counter == 0 ]; then # Initialize node
rootPort = $NEW_PORT p2prc --dc
# Save rootPort from the first node
if [[ $counter -eq 0 ]]; then
rootPort=$NEW_PORT
fi 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/\"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 p2prc --ls
cd .. cd ..
done done
# remove all instances # remove all instances after testing (optional — might want to comment this during testing)
# rm -rf test-*
}
# 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-* rm -rf test-*
} }
P2PRC_instances 2 # 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

View File

@@ -38,6 +38,7 @@ type Config struct {
KeyFile string KeyFile string
BareMetal bool BareMetal bool
UnsafeMode bool UnsafeMode bool
Test bool
CustomConfig interface{} CustomConfig interface{}
//NetworkInterface string //NetworkInterface string
//NetworkInterfaceIPV6Index int //NetworkInterfaceIPV6Index int

View File

@@ -95,6 +95,7 @@ func SetDefaults(envName string, forceDefault bool, CustomConfig interface{}, No
Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey" Defaults.PrivateKeyFile = defaultPath + "p2prc.privateKey"
Defaults.BareMetal = false Defaults.BareMetal = false
Defaults.UnsafeMode = false Defaults.UnsafeMode = false
Defaults.Test = false
// Generate certificate files for SSL // Generate certificate files for SSL
err = GenerateCertificate() err = GenerateCertificate()

View File

@@ -16,7 +16,6 @@ import (
"crypto/x509" "crypto/x509"
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/pem" "encoding/pem"
"github.com/Akilan1999/p2p-rendering-computation/p2p"
"log" "log"
"math/big" "math/big"
"net" "net"
@@ -54,7 +53,8 @@ func GenerateCertificate() error {
//flag.Parse() //flag.Parse()
host, err = p2p.CurrentPublicIP() //host, err = p2p.CurrentPublicIP()
host = "0.0.0.0"
if err != nil { if err != nil {
return err return err

View File

@@ -42,6 +42,8 @@ type IP struct {
Query string Query string
} }
var Test = false
// ReadIpTable Read data from Ip tables from json file // ReadIpTable Read data from Ip tables from json file
func ReadIpTable() (*IpAddresses, error) { func ReadIpTable() (*IpAddresses, error) {
// Get Path from config // Get Path from config
@@ -178,6 +180,16 @@ func (table *IpAddresses) RemoveDuplicates() error {
// CurrentPublicIP Get Current Public IP address // CurrentPublicIP Get Current Public IP address
func CurrentPublicIP() (string, error) { 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/") req, err := http.Get("http://ip-api.com/json/")
if err != nil { if err != nil {
return "", err return "", err

View File

@@ -312,6 +312,7 @@ func Server() (*gin.Engine, error) {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
ProxyIpAddr.ServerPort = config.ServerPort ProxyIpAddr.ServerPort = config.ServerPort
ProxyIpAddr.Name = config.MachineName ProxyIpAddr.Name = config.MachineName
ProxyIpAddr.NAT = false ProxyIpAddr.NAT = false