This project implements a multi-threaded server that receives book content from multiple clients, analyzes the frequency of a specified pattern in the books, and provides real-time analysis results.
- Multi-threaded server capable of handling multiple client connections simultaneously
- Real-time pattern analysis across all received book content
- Periodic output of top books based on pattern frequency
- Non-blocking I/O for efficient client handling
- Thread-safe data structures for concurrent operations
- Automatic saving of received book content to files
The project consists of the following main components:
server.py
: Main server script that handles incoming connections and initializes the systemclient_handler.py
: Manages individual client connections and processes incoming datapattern_analysis.py
: Performs periodic analysis of the received datalinked_list.py
: Implements a custom linked list data structure for efficient data managementutils.py
: Contains utility functions, such as writing received books to filesassignment3.py
: Main server script that handles incoming connections and initializes the systemtest.sh
: A bash script for testing the server with multiple simulated clientsbooks/
: Directory containing text files for test.sh (book1.txt, book2.txt, etc.)
-
Clone the repository:
git clone https://github.com/Richkwokkk/book-analysis-server
-
Ensure you have Python 3 installed on your system.
-
No additional dependencies are required as the project uses only Python standard libraries.
To start the server, run the following command:
python assignment3.py -l <port> -p <pattern>
Where:
<port>
is the port number the server should listen on (must be greater than 1024)<pattern>
is the search pattern to analyze in the books
Example:
python assignment3.py -l 12345 -p "happy"
This starts the server on port 12345 and analyzes the frequency of the word "happy" in the received books.
To send text files to your program, consider utilising the netcat tool (nc). For instance, to transmit a text file to your server, you may use the following command:
nc localhost <port> -i <delay> < file.txt
Where:
<port>
is the port number your server is listening on<delay>
is the delay between sending lines (in seconds)file.txt
is the text file you want to send, which should be put in root directory
Example:
nc localhost 12345 -i 0 < book1.txt
This sends the content of book1.txt to the server running on localhost:12345, with a 0.1-second delay between each line.
To stop the server, use Ctrl+C in the terminal where the server is running. This will gracefully shut down the server and save any unsaved data.
- The server starts and listens for incoming connections on the specified port.
- For each client connection, a new thread is spawned to handle the client.
- Clients send book content line by line. Each line is added to a shared linked list data structure.
- Two analysis threads periodically process the shared data, counting pattern occurrences and sorting books by frequency.
- Analysis results are printed to the console at regular intervals.
- When a client disconnects, the received book content is saved to a text file.
A bash script test.sh
is provided to test the server with multiple simulated clients. To use the test script:
-
Ensure the script is executable:
chmod +x test.sh
-
Run the script with the following parameters:
./test.sh <server_host> <server_port> <pattern> <num_clients> <delay>
Example:
./test.sh localhost 12345 "happy" 10 0
This will:
- Start the server on localhost:12345 with the pattern "happy"
- Simulate 10 clients sending book content to the server
- Use no delay between sending lines (0)
The test script will:
- Check if the specified port is in use and kill any existing process using it
- Start the server
- Send content from multiple text files (located in the
books/
directory) to simulate clients - Continue running the server after all clients have finished sending data
- To stop the server, use Ctrl+C in the terminal where the test script is running.
Note: Ensure you have the necessary text files (book1.txt, book2.txt, etc.) in the books/
directory before running the test script.