DeePeNinG: Distributed Prime Number Generator
DeePeNinG was written as a final project for SIUC’s CS 420 course by Chad Milburn, Ryan LaPorte, and myself.
Introduction
The DeePeNinG program is a Distributed Prime Number Generator. By nature, prime number generation is a grueling task. Prime numbers are central in number theory and there are many reasons to try and calculate these numbers. Unfortunately, generating prime numbers takes time and effort from even the most high tech computers. This is why we decided to distribute the process. By sending out ranges of numbers to various clients, prime numbers can be quickly found within that range. The DeePeNinG server will manage connections to the clients, distribute the ranges and save any prime numbers the client returns. Meanwhile, the client will accept the ranges, calculate the prime numbers within the range and send the results back to the server. This client/server model allows for a largely scalable operation. By allowing the clients to do all calculations, the server is free to manage the connections and use free time to allow more clients to connect.
Within this program, TCP sockets and streams are used. We decided to use sockets instead of higher level tools such as RMI due to the amount of overhead RMI would bring to our process. The DeePeNinG program only sends strings back and forth over a connection; everything else is done on one side or the other. There is no reason to access remote objects or pass sophisticated data. By using basic socket communication, we remove any excess overhead and stick with just passing a string over a port.
Server Design
The DeePeNinG server uses a number of classes to handle client connections and keep track of data processed as well as all prime numbers found. The application is written in Java and makes use of plain text files for data storage.
Upon launching the server the user is given a blank log screen with a number of choices. In the “File” menu they are presented with a “Preferences” option where they may select which network port the server will listen on as well as they may open the list of prime numbers found in their text editor. The “Server” menu allows them to start and stop the serer as well as view the full server log, and finally, the “Help” menu lists an “About” command to display current information on the application.
The server itself is composed of 5 Java classes. Main contains the user interface and processes all normal user interactions. Server is coordinator of all major server and file operations. Upon launch it opens a TCP socket on a port specified by the user and begins to listen for connections. In addition, the server also creates two other objects to handle the file operations themselves. The ClientHandler Class is where individual clients are processed. Each client has it’s own thread and can access file handling on other methods of the main server. The next class, FileHandler, is the primary class for distributing data range clients as well as saving any prime numbers found. In addition all basic reports are generated by FileHandler. Finally, the ServerLogger class logs all major server actions and pushes current actions out to the user interface so the user may have an idea what the server has been up to.
In order to keep track of users the server stores their actions in a single file called numbers.dat. Each entry corresponds to a range of numbers that a client needs to check for primes and contains the line number, the email address of the user who’s client is processing the range and a timestamp indicating when the range was sent. Upon request of a new range the server first checks to see if the user has any outstanding ranges and then for any “old” ranges (for this assignment any range sent over 24 hours earlier is considered old). If an old range is found or the user has an existing range then the range is re-sent to the user to continue processing. If there are no old ranges found and the user does not have a range checked out then they are sent a new range which is then recorded in the file.
In order to store primes, at the end of a range a client sends all the primes found in the range to the server in a single message to reduce network connections. The server then processes each prime by saving it to the file primes.dat along with email address of the user who’s client discovered the number. Finally, as all primes in a given range are sent at once, the server than returns to numbers.dat and replaces the timestamp on the corresponding entry with the word “Completed” indicating that this range has been processed.
All error checking is done internally and will display the error message to the main screen on the user interface of the server. In addition, all error messages except a conflicting network port and a failure of the logging system will be recorded in the logfile server.log. In the case of a conflicting port the error message will be displayed on the user interface, and in the case of a log file error a popup will be displayed to the user informing them of the error.
Finally, to facilitate easier operation, on supporting computers the server makes use of the system tray to prevent closing and move the application out of the user’s site when requested.
Server Compilation and Execution
The server is compiled into an executable Jar straight from the IDE. This jar also includes the image icon used throughout the application.
Execution can be handled either by clicking on the jar via the file browser in the user’s operating system or through the command line with the java -jar command.
The Client
The DeePeNinG client uses two classes, one to handle the GUI and one to handle the background processes. The GUI was written using the GUI builder in NetBeans, while the background doPrimes class was written using Eclipse. The classes are written utilizing separate threads, in order that the client GUI might be responsive while the background process is testing a range of prime numbers.
When running the primeGenerator class, the user is presented with the main application window. The user must click on the “File” menu then “Change Users” to start the background doPrimes thread. A window pops up with a textbox for username and range of integers to be checked for primality using the Miller-Rabin primality test. There are two radio buttons near the bottom that allow the user to choose whether to run the program locally or with a server. Upon clicking server, the current window is hidden and a new window is brought to the foreground, replacing the range with a textbox for entering the server host name or IP address. When the user clicks OK, the background SwingWorker thread is created and executed. If “local” is selected, the client will simply test the range of integers (stored as BigIntegers) and return a comma-separated string of the found primes to the standard output. If “server” is selected, the client will attempt to connect to the given server (which is initially set to the local IP address) with the username and obtain a range of prime numbers to test. At this point, the “local” and “server” processes function exactly the same, except that the string of primes is eventually sent back to the server in “server” mode. The client doPrimes background process is run in continuous loop in “server” mode, reporting the found primes and requesting a new range, while in “local” mode the doPrimes thread is only run once. The GUI will start a new doPrimes thread on a new range once the user clicks on “Change User” under the “File” menu.
The connection to the server is a socket that exchanges strings either containing a list of primes found or a range of primes to be tested. A socket seemed to be the simplest method of exchanging the trivial information that is needed in such a project as this, and the overhead of remote method calls seemed to be overkill. If there were to be more complicated information sharing and/or a much wider range of functions needed in either the server or client, such remote calls would be necessary.
The “File” menu also contains the “Exit” option, which simply executes “System.exit(0)” and exits both threads. Under the “Edit” menu, the user can click “Project Page,” which opens the default web browser to the project’s development page at SourceForge, or the user can click “About Us,” which presents the user with the purpose of the application as well as the names of the developers. The “Project Page” link will only work in Windows, since it uses a Windows DLL to launch the browser.
On the main GUI window, the user can see the name of the current user, the server currently connected to (“local” if in local mode), and a slider bar representing the CPU usage of the program. The slider changes the amount of sleep between each check of an integer for primality. The sleep is adjustable from 0-1000 milliseconds between checks.
In case of any error, the client will simply stop the background process. Upon choosing “Change User” from the “File” menu, the client will simply request another range from the server (in “server” mode). The server will handle whether the client receives the same range or a new range. Since the client only processes a small range, there is no need to store the found primes in case of error.
