Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPC: Fix port checking #86

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading