In this lab, you will build a VERY basic peer-to-peer chat system using existing Python libraries and the lab_chat.py file shared in this repo. The goal is to create functions that allow users to join a chat group and send and receive messages. You’ll explore core concepts of Python defining functions, parameters, simple loops, and importing and calling existing functions.
This lab is divided into four parts and should take approximately two hours to complete.
- Installation of necessary Python modules
- You can install the necessary modules either through your IDE's terminal as indicated below, or by installing each module as depicted in the linked video.
pip install zeromq-pyre
- You can install the necessary modules either through your IDE's terminal as indicated below, or by installing each module as depicted in the linked video.
In this part, you'll create several basic functions to handle user input and data that will be used in the later stages. Here you should focus on practicing creating proper function headers and bodies.
- Create a new file in your project named lab003.py
- Create a function named get_username that asks the user for their desired username. (HINT: input())
- If you are not sure about the proper parts of a function see the class notes.
- Your function should remove forward and trailing whitespace. (HINT: strip())
- Your function should return the username in upper case letters. (HINT: upper())
- return is a keyword you can put at the end of a function that will send back any data you type after it.
- return returns to the location in the code the function was called from.
- Write a function named get_group that asks the user for a group name they'd like to join. (HINT: input())
- If you are not sure about the proper parts of a function see the class notes.
- Your function should remove forward and trailing whitespace. (HINT: strip())
- Your function should return the username in upper case letters. (HINT: upper())
- Create a function named get_message that asks the user to input the message they’d like to send.
- Your function should remove forward and trailing whitespace.
- Your function should return the typed message.
Now that you have user inputs ready, you’ll move to handling peer-to-peer communication using the lab_chat.py code. In this file, there are some preexisting functions to connect/become a peer-to-peer node, join a chat group, and get a communication channel to send and receive messages. Your job is to identify these functions and their parts, then integrate these functions into your functions.
- In your project create a README.md file
- For each function in the lab_chat.py file add the following to your README.md file:
- Full Function Header, and indicate the function name with a comment.
# Example def chat_task(ctx, pipe, n, group): # function name is chat_task def get_peer_node(username): # function name is get_peer_node username # I assume this is your username that we'll get from our get_username function # This function return something that looks like a Pyre Node to me (my peer node?) def join_group(node, group): # function name join_group node # I assume this is my peer computer node group # I assume this is a group that I want to join from our get_group function # This function returns Nothing Lebowski! def get_channel(node, group): # function name get_channela node # I assume this is my peer computer node group # I assume this is a group that I want to join similar to join_group # Yes it returns a zhelper, Not sure what that is, but maybe a channel
- List all parameters and what you think they are. Put "UNSURE" if you don't have a guess.
# Example ctx: This is a ZeroMQ Connection Context pipe: This is a communications pipe polled by ZeroMQ for messages. n: This is the peer to peer node my chat app is connected as group: This is the peer chat group I wanted to join
- Note if the function returns anything. If it does, note what you believe it returns, and make a final note about what you believe the function may do.
# Example The chat_task method does not return anything, it appears to be the send/recieve manager.
In this part, you will combine all the functions from Part 1 and Part 2 to create a functioning peer-to-peer chat.
- Import the lab_chat.py functions into your lab003.py file
- Remember, there are a few ways you could do this.
- import the entire file
import lab_chat, but then every function would need to be called bylab_chat.func - import the functions by name
from lab_chat import get_peer_node, other_funcs - OR import the entire file and alias the module so it's shorter
import lab_chat as lc
- import the entire file
- Remember, there are a few ways you could do this.
- Now, create a function name initialize_chat that initializes the chat
- It should get the user's username. (HINT: you wrote this method in part 1)
- Get the desired chat group to join. (HINT: you wrote this method in part 1)
- Connect as a peer node. (HINT: you identified this method in part 2)
- Pay attention to your parameters and return notes for clues on how to use this function!
- Joins the group. (HINT: you identified this method in part 2)
- Pay attention to your parameters and return notes for clues on how to use this function!
- return a chat channel. (HINT: you identified a method for this in part 2 also!)
Write a function named start_chat that uses your get_message function, a loop, and the channel returned by initialize_chat to send a message.
- HINT: Your last function should look like the below example:
- Obviously there are a few things we've not talked about, so now is your time to ask me what they are!
def start_chat():
channel = initialize_chat()
while True:
try:
msg = get_message()
channel.send(msg.encode('utf_8'))
except (KeyboardInterrupt, SystemExit):
break
channel.send("$$STOP".encode('utf_8'))
print("FINISHED")In this final part, you will focus on testing and debugging your chat application. This will involve testing each of the functions independently and then testing the entire system.
- Test
get_username(),get_group(), andget_message()to ensure they return the expected inputs. - Test the
initialize_chat()function by running it and making sure it returns a channel without throwing any exceptions. (HINT: use the type() function)- A channel type will be something like:
<class 'zmq.sugar.socket.Socket'>
- A channel type will be something like:
- Test the
start_chat()function by running a basic chat where you send and receive messages to a group with a single other peer.
- If messages are not being received, check whether the group has been joined correctly.
- Verify that Pyre is installed correctly by running
pip list | grep pyrein your terminal or simply checking your installed modules in your IDE settings.
- Create a GitHub Repository:
- Log in to your GitHub account.
- Create a new repository with a suitable name (e.g., "python_lab3").
- Configure Git in PyCharm:
- Open the "Git" menu in PyCharm and initialize a Git repository for your project.
- Add your GitHub remote repository by going to "Git" -> "Manage Remotes".
- Commit Changes:
- Stage all changes in your PyCharm project.
- Commit the changes with a descriptive message (e.g., "Initial commit").
- Push to GitHub:
- Push the committed changes to your GitHub repository using the "Push" button in PyCharm.
Submit the link from your GitHub repository containing your lab 3 repo to Canvas.