adding primary and seconadry test files
+154 · 8 files changed
-
codefiles/src/primary/config.ini+3 −0 -
codefiles/src/primary/primary.py+33 −0 -
codefiles/src/secondary/config.ini+7 −0 -
codefiles/src/secondary/secondary.py+61 −0 -
container/Dockerfile+0 −0 -
container/README.md+3 −0 -
container/primary/Dockerfile+23 −0 -
container/secondary/Dockerfile+24 −0
@@ -0,0 +1,3 @@ +[PRIMARY] +HOST = 0.0.0.0 +PORT = 12345 @@ -0,0 +1,33 @@ +import socket +import configparser +from threading import Thread + +# Load configuration +config = configparser.ConfigParser() +config.read('config.ini') +HOST = config['PRIMARY']['HOST'] +PORT = int(config['PRIMARY']['PORT']) + +def handle_client(connection, address): + try: + print(f"Connection from {address} has been established.") + while True: + data = connection.recv(1024) + if not data: + break # Connection closed by the client + # Discard data explicitly + finally: + connection.close() + +def main(): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((HOST, PORT)) + s.listen() + print(f"Listening on {HOST}:{PORT}...") + while True: + conn, addr = s.accept() + client_thread = Thread(target=handle_client, args=(conn, addr)) + client_thread.start() + +if __name__ == "__main__": + main() @@ -0,0 +1,7 @@ +[SECONDARY] +HOST = 0.0.0.0 +PORT = 23456 +PRIMARY_HOST = primary_container +PRIMARY_PORT = 12345 +CHECK_INTERVAL = 5 + @@ -0,0 +1,61 @@ +import socket +import configparser +import sys +import time +from threading import Thread + +# Load configuration +config = configparser.ConfigParser() +config.read('config.ini') +PRIMARY_HOST = config['SECONDARY']['PRIMARY_HOST'] +PRIMARY_PORT = int(config['SECONDARY']['PRIMARY_PORT']) +SECONDARY_PORT = int(config['SECONDARY']['PORT']) +CHECK_INTERVAL = int(config['SECONDARY']['CHECK_INTERVAL']) + +primary_online = True + +def check_primary(): + global primary_online + while True: + try: + with socket.create_connection((PRIMARY_HOST, PRIMARY_PORT), timeout=2): + if not primary_online: + print("Primary is back online.") + primary_online = True + except socket.error: + if primary_online: + print("Primary is offline.") + primary_online = False + time.sleep(CHECK_INTERVAL) + +def handle_client(connection, address): + if primary_online: + print(f"Rejecting connection from {address} because primary is online.") + connection.close() + else: + print(f"Handling connection from {address} because primary is offline.") + try: + while True: + data = connection.recv(1024) + if not data: + break # Connection closed by the client + # Discard data explicitly + finally: + connection.close() + +def main(): + check_thread = Thread(target=check_primary) + check_thread.start() + + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(('0.0.0.0', SECONDARY_PORT)) + s.listen() + print(f"Secondary listening on port {SECONDARY_PORT}...") + while True: + conn, addr = s.accept() + client_thread = Thread(target=handle_client, args=(conn, addr)) + client_thread.start() + +if __name__ == "__main__": + main() + @@ -0,0 +1,3 @@ +BUILD ARGS + +RUNTIME ARGS @@ -0,0 +1,23 @@ +# Use an official Python runtime as a parent image +FROM python:3.8-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY ../../codefiles/src/primary /app + +# Install any needed packages specified in requirements.txt +# Assuming you might have a requirements.txt for any Python dependencies +# For this example, it's likely empty but included for completeness +COPY requirements.txt /app +RUN pip install --no-cache-dir -r requirements.txt + +# Make port 12345 available to the world outside this container +EXPOSE 12345 + +# Define environment variable +ENV NAME Primary + +# Run primary.py when the container launches +CMD ["python", "primary.py"] @@ -0,0 +1,24 @@ +# Use an official Python runtime as a parent image +FROM python:3.8-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY ../../codefiles/src/secondary /app + +# Install any needed packages specified in requirements.txt +# Assuming you might have a requirements.txt for any Python dependencies +# For this example, it's likely empty but included for completeness +COPY requirements.txt /app +RUN pip install --no-cache-dir -r requirements.txt + +# Make port 23456 available to the world outside this container +EXPOSE 23456 + +# Define environment variable +ENV NAME Secondary + +# Run secondary.py when the container launches +CMD ["python", "secondary.py"] +