Skip to content

Commit

Permalink
Merge pull request #86 from UM-Bridge/hpc-fix-port-issue
Browse files Browse the repository at this point in the history
HPC: Fix port checking
  • Loading branch information
linusseelinger authored Oct 21, 2024
2 parents 11fb3c9 + a93e53a commit 660d146
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
5 changes: 4 additions & 1 deletion hpc/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
all: build-load-balancer build-testmodel
all: build-load-balancer build-is-port-free build-testmodel

build-load-balancer:
- g++ -O3 -Wno-unused-result -std=c++17 -I../lib/ LoadBalancer.cpp -o load-balancer -pthread

build-is-port-free:
- g++ -O3 -std=c++17 is_port_free.cpp -o is_port_free

build-testmodel:
- g++ -O3 -Wno-unused-result -std=c++17 -I../lib/ ../models/testmodel/minimal-server.cpp -o testmodel -pthread
2 changes: 1 addition & 1 deletion hpc/hq_scripts/job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function get_available_port {
port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1)

# Check if the port is in use
while lsof -Pi :$port -sTCP:LISTEN -t >/dev/null; do
until ./is_port_free $port; do
# If the port is in use, generate a new random port number
port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1)
done
Expand Down
43 changes: 43 additions & 0 deletions hpc/is_port_free.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "../lib/httplib.h"

#include <iostream>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

// Takes one integer argument - a port.
// If the port is free, returns EXIT_SUCCESS; otherwise returns EXIT_FAILURE.
// Note: Uses the Linux IPv4 protocol implementation!
int main(int argc, char* argv[])
{
// Get port to check from command line args
if (argc < 2) {
std::cerr << "Missing required positional argument: port" << std::endl;
return EXIT_FAILURE;
}
int port = std::atoi(argv[1]);

// Create socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
std::cerr << "Failed to create socket: " << std::strerror(errno) << std::endl;
return EXIT_FAILURE;
}

// Set address to 0.0.0.0:port
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = htonl(INADDR_ANY);

// Attempt to assign address to socket
int success = bind(sockfd, (struct sockaddr*) &addr, sizeof(addr));

if (success == -1) {
return EXIT_FAILURE;
} else {
return EXIT_SUCCESS;
}
}
2 changes: 1 addition & 1 deletion hpc/slurm_scripts/job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function get_available_port {
port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1)

# Check if the port is in use
while lsof -Pi :$port -sTCP:LISTEN -t >/dev/null; do
until ./is_port_free $port; do
# If the port is in use, generate a new random port number
port=$(shuf -i $MIN_PORT-$MAX_PORT -n 1)
done
Expand Down

0 comments on commit 660d146

Please sign in to comment.