Computer Science 111 – Fundamentals of Programming I
Programming Project 11
Due 11:59 PM Friday 11 December
In this project, you will implement a multi-client therapy application.
Download the Project11 folder from the course webpage. This folder contains a GUI- based client/server application for non directive psychotherapy discussed in lecture. The doctor server relies upon a client handler class to support multiple clients and replies upon a Doctorclass to provide a distinct doctor object for each client.
To run the program, open three terminal windows from within the project11 folder’s window. Run the server first in one terminal window, using the command python3 doctorservergui.py. Then, move to the other two windows and run clients using the command python3 doctorclientgui.py. Note that both client and server are running on the host named “localhost,” that is, on your own computer. Once all three programs are running, you can click Start Sever in the Doctor Server to listen for clients.
To disconnect a client, clear the Patient field and click Send reply. You close any client window in the usual manner to quit a client application. To quit the server application, you close the server window and then type control-c to return to the terminal prompt.
Finally, carefully review the code for the server, client, and doctor modules. Note that the code for the server side of the application is distributed across three files,doctor.py, doctorserver.py, and doctorservergui.py. The code for the client side resides in doctorclientgui.py. Like the client handler, the server is now defined as a subclass of Thread.
Part I – Clean up the Doctor class
The code is doctor.py represents a quick conversion of the doctor module discussed earlier this semester to a class-based version. You can run it from IDLE by pressing function-5 to test it out. In this part of the project, you will make sure that the code changes persons correctly when the patient addresses the doctor in the second person. Obviously you’ll want to reuse your code from the earlier doctor lab!
Part II – Saving and Loading Patient Histories
The __init__ method in the Doctor class expects the patient’s name as an argument. That name can be used to save and load that patient’s history to and from a file.
To load a file in the __init__ method, create a file name from the patient’s name with a .txt extension and see if that file exists in the current working directory (consult the
textbook, the slides, or google on how to determine whether a file exists). If the file exists, read the text in, split it around the newline character, and assign the resulting list to
the self.history variable. To save a file, the method saveHistory should write each
item in the history list as a separate line of text to an output file. Test the module in IDLE before trying it out in the GUI application.
Part III – Go Live!
Now, as an experiment, edit your GUI-based server code so that it uses the host address of your computer instead of “localhost.” You obtain this address by calling the socket functiongethostname().
A good place to do that in your code is when you set the text of the host field at startup. Launch your server and ask a fellow student to input your computer’s host name when running a client program. When this student launches the client program, the program should connect to your server and be able to receive a therapy session from it. Then ask yet another student to do the same thing on a different computer. Finally, try to connect from your laptop.
Extra Credit (10 points) – Stopping the Client
The client currently disconnects by clicking the Send reply button when the patient input field is empty. Modify the GUI so that it changes the Connectbutton to Disconnect to perform this function. An attempt to send an empty string to the server should now trigger a popup error message.