Sunday, March 22, 2015

Design a 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? 

The fun part of OOD is that you can add any feature you want to the project as long as it's logical. This problem, we need a user class, which has some basic features such as sending requests, approve/deny requests, send messages, add users, etc. We also need a server class which is pretty much the database of all users. 

There are lots of limitations to the class I design. I am not quite familiar with server, so I cannot say mush. 


enum StatusType{
 online, offline, away;
}
class Status{
 StatusType status_type;
 String status_message;
}
class User{
 String username;
 String display_name;
 List contactList;
 List requests;
 StatusType currentStatus = StatusType.offline;
 String status;
 private Server belongsTo;
 private Map> messageBox;
 public User(Server belongsTo, String userName){
  this.belongsTo = belongsTo;
  username = userName;
  contactList = new ArrayList ();
  requests = new ArrayList ();
  messageBox = new HashMap> ();
  
 }
 boolean updateStatus(String message){
  status = message;
 }
 boolean sendRequest(String userName){
  if(!belongsTo.userExists(userName)){
   System.out.println("No such user!");
   return false;
  }
  User toAdd = belongsTo.getUser(userName);
  ConnectRequest send = new ConnectRequest(this, toAdd);
  toAdd.requests.add(send);
  return true;
 }
 boolean approveRequest(Request r){
  //user deactivates the account before the request is approved
  if(!belongsTo.contains(r.receiver){
   System.out.println("No such user!");
   return false;
  }
  User toAdd = belongsTo.get(r.receiver);
  contactList.add(toAdd);
  requests.remove(r);
  return true;
 }
 boolean denyRequest(Request r){
  requests.remove(r);
  return true;
 }
 void removeContact(String userName){
  User toRemove = belongsTo.get(userName);
  contactList.remove(toRemove);
 }
 boolean sendMessage(String username, String message){
  User toBuzz = belongsTo.get(username);
  if(!contactList.contains(toBuzz)){
   System.out.println("The user is not in your contact list!");
   return false;
  }
  if(toBuzz.status == StatusType.offline){
   System.out.println("User is currently offline!");
   return false;
  }
  if(!messageBox.contains(toBuzz)){
   messageBox.put(toBuzz, new ArrayList());
   toBuzz.messageBox.put(this, new ArrayList());
  }
  messageBox.get(toBuzz).add(message);
  toBuzz.messageBox.get(this).add(message);
  return true;
 }
 void setStatus(StatusType s){
  currentStatus = s;
 }
 
}
class ConnectRequest{
 User sender;
 User receiver;
}
class Server{
 List dataBase;
 boolean userExists(String userName){
  for(User u : dataBase){
   if(u.userName.equals(userName))
    return true;
  }
  return false;
 }
 void registration(String userName){
  if(userExists(userName){
   System.out.println("user name exists, please change for another one");
  }
  dataBase.add(new User(this, userName));
 }
 void deactivate(String userName){
  dataBase.remove(getUser(userName));
 }
 User getUser(String userName){
  for(User u : dataBase){
   if(u.username = userName)
    return u;
  }
  return null;
 }
}

No comments:

Post a Comment