diff --git a/cmd/openim-test/main.go b/cmd/openim-test/main.go index 464b45851..72e3ff11e 100644 --- a/cmd/openim-test/main.go +++ b/cmd/openim-test/main.go @@ -3,6 +3,8 @@ package main import ( "flag" "fmt" + "math/rand" + "net" "time" ) @@ -17,5 +19,39 @@ func main() { // Print the values of the flags fmt.Printf("args: -i %d -c %s\n", *index, *config) - time.Sleep(600 * time.Second) + // Initialize the random number generator + rand.Seed(time.Now().UnixNano()) + + // Randomly select two ports between 10000 and 20000 + port1 := rand.Intn(10001) + 10000 // This generates a number between 0-10000, then adds 10000 + port2 := rand.Intn(10001) + 10000 + + // Ensure port2 is different from port1 + for port2 == port1 { + port2 = rand.Intn(10001) + 10000 + } + + // Print the selected ports + fmt.Printf("Randomly selected TCP ports to listen on: %d, %d\n", port1, port2) + + // Function to start a TCP listener on a specified port + startListener := func(port int) { + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + if err != nil { + fmt.Printf("Error starting TCP listener on port %d: %v\n", port, err) + return + } + defer listener.Close() + fmt.Printf("Listening on port %d...\n", port) + + // Here we simply keep the listener running. In a real application, you would accept connections. + select {} // Block forever + } + + // Start TCP listeners on the selected ports + go startListener(port1) + go startListener(port2) + + // Block forever + select {} } diff --git a/scripts-new/check-all.sh b/scripts-new/check-all.sh index 5e5e80cef..96e315698 100644 --- a/scripts-new/check-all.sh +++ b/scripts-new/check-all.sh @@ -44,3 +44,11 @@ for binary in "${!binaries[@]}"; do done +for binary in "${!binaries[@]}"; do + expected_count=${binaries[$binary]} + full_path=$(get_bin_full_path "$binary") + check_binary "$full_path" +done + + + diff --git a/scripts-new/lib/util.sh b/scripts-new/lib/util.sh index da6dafc6f..286ab83eb 100644 --- a/scripts-new/lib/util.sh +++ b/scripts-new/lib/util.sh @@ -2831,9 +2831,53 @@ function openim::util::find_ports_for_all_services() { } +check_binary_ports() { + binary_path="$1" + binary_name=$(basename "$binary_path") + + # Check if the binary is running + if pgrep -f "$binary_path" > /dev/null; then + echo "$binary_name is running." + + # Find the PID(s) of the running binary + pids=$(pgrep -f "$binary_path") + + # Initialize an empty string to store ports + ports="" + + # Loop through each PID + for pid in $pids; do + # Check for listening ports using lsof + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + # Linux + ports+=$(lsof -i -n | grep LISTEN | grep "$pid" | awk '{print $9}' | cut -d':' -f2 | uniq) + elif [[ "$OSTYPE" == "darwin"* ]]; then + # macOS + ports+=$(lsof -i -n | grep LISTEN | grep "$pid" | awk '{print $9}' | awk -F'[:\.]+' '{print $(NF-1)}' | uniq) + fi + done + + # Remove duplicate ports if any + ports=$(echo "$ports" | uniq) + + if [ -z "$ports" ]; then + echo "$binary_name is not listening on any ports." + else + echo "$binary_name is listening on the following ports: $ports" + fi + else + echo "$binary_name is not running." + fi +} + + + if [[ "$*" =~ openim::util:: ]];then eval $* fi + + + diff --git a/scripts-new/start.sh b/scripts-new/start.sh index 4a1979696..3648355f1 100644 --- a/scripts-new/start.sh +++ b/scripts-new/start.sh @@ -14,7 +14,6 @@ source "$OPENIM_SCRIPTS/define/binaries.sh" # Main function to start binaries start_binaries() { local project_dir="$OPENIM_ROOT" # You should adjust this path as necessary - echo $OPENIM_ROOT 12322222222222222222222222222222 # Iterate over binaries defined in binary_path.sh for binary in "${!binaries[@]}"; do local count=${binaries[$binary]} @@ -27,6 +26,8 @@ start_binaries() { done } + + # Call the main function start_binaries