UP2 Spec: Managing a Group
Introduction
In UP2, we wil be implementing another building block of the chat system: group management.
We are not yet at the stage for implementing chating, but we are going to create the system
that wil help us manage it. It wil alow users to join a group, as well as connect and disconnect
to one another.
When a per s signs in to the group, she can issue comands to find out who are in the
system. She may see something like this:
{a}, {b}, {c, d}
This indicates that there are two loners a, and b, whereas c and d are talking (presumably). If
she conects to a, the management system should update acordingly:
{a, s}, {b}, {c, d}
Or she can conect to c (or d), and become part of their group:
{a}, {b}, {c, d, s}
Likewise, if s decides to disconnect from her group, and then d did the same, we wil end up
with no group -- everyone is a loner:
{a}, {b}, {c}, {d}, {s}
The constraints for group management are:
- A group has more than 1 member
- A member can be in one and only one group at a given time
This is already a simple state machine: a peer moves from/to state S_TALKING to/from
S_ALONE, when conecting/disconecting to/from a member.
Spec: Group class
The chat_group.py implements the Group class:
In the Group class, there are two dictionaries:
- members records al the users and their states. Each item is a key-value pair which
records the user’s current state, i.e. S_ALONE or state talking (S_TALKING). Use a
user’s name as the key to map to the state in this dictionary.
- chat_grps stores groups that talking to each other as key-value pairs, maping group
numbers (0..n) to the lists of users inside that group. Only when a client is in state
S_TALKING is she in one of the chat groups. The key of the dictionary is internal to the
Group class. To make sure keys are unique, we use a variable grp_ever that is
incremented each time a new group is formed. Each group is a list of members.
In the case of {a}, {b}, {c, d, s}, there is only one group, with a list [c, d, s] (not
necessarily in this order, [d, c, s] is fine to).
The folowing page shows a table of the member functions, as wel as the functionality we
expect to have implemented. self.join(name) is already implemented, as shown earlier.
Member functions Description
join(name) A user with "name" joins. Add her to the member dictionary, initial state is S_ALONE
leave(name) A user with "name" quits. Delete her from the member and chat_grps dictionary
is_member(name) Return True if the user "name" is in the system (i.e. in the member dictionary)
conect(me, per) Connect "me" to "peer". If "peer" is already in a group, join "peer"'s group;
otherwise create a new group, record the new group in chat_grps dictionary. In the
second case, grp_ever should increment by one.
disconnect(me) Remove "me" from my current chat group. If the group has only one peer left,
remove that per as wel, and delete the chat group.
list_my_pers(name) Return the chat group "name" is in, as a list. IMPORTANT: "name" is the first
element in that returning list.
list_all(name) Return al the information in the system; the code is given. You can make it fancy,
but it NEDS TO BE A STRING.
find_group(name) Auxiliary function internal to the clas; return two variables: whether "name" is in a
group, and if true the key to its group
Note that list_al() has a simple-minded implementation given.
The functions you ned to implement for this project are given in BOLD in the table.
HINTS:
- It helps to review the syntax of dictionaries and lists first; you wil ned them
- Go step by step, for example implement join and is_member first to warm up
- The tricky part of conect and disconnect is to handle new group creation and delete
singleton groups (group with only one member). Make sure the member dictionary is
manipulated apropriately
- Use the console to debug interactively. The below picture shows an example, when
runing chat_group.py in ipython (some debug info is mine):
Alternatives and Improvements
Once you have worked out the core logic, you may atempt to make some implementation
improvements acording to this spec.
Some chalenges to think about and implement functions for:
- How many loners are in the system?
- What is the bigest group?
- Can you list al the groups with 2 members?