1.5 KiB
1.5 KiB
7.7. Chat server
Explain how you would design a chat server. In particular, provide details about the various backend components, classes, and methods. What would be the hardest problems to solve?
Hints 213, 245, 271
public class chatServer {
private ArrayList<User> allUsers;
void startChat();
void createUser(String username){
allUsers.push(new User(username));
}
}
public class User {
private int id;
private String username;
private ArraList<User> allContacts;
private ArrayList<Conversation> allConversations; // should be a hashmap
public User(String username) {
this.id = rand();
this.username = username;
this.allContacts = new ArrayList();
this.allConversations = new ArrayList();
}
void createConversation(String username);
void sendMessage(String username) {
this.allConversations[username].sendMessage();
}
}
public class Conversation {
private ArrayList<User> participants;
private ArrayList<Object> sharedFiles;
}
Solution
The key objects of the system are users, conversations and status messages.
- How to know if someone is online?
- Regularly ping the client
- How do we deal with conflicting information? There's info stored in the computer's memory and some other in the database, what if they get out of sync, which one is right?
- How do we make the server scale?
- Split the data across many servers, incresing the concern about out-of-sync data
- How do we prevent DoS attacks?