[CareerCup] 8.7 Chat Server 聊天服务器
8.7 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?
这个简易的聊天服务器功能十分的有限,毕竟只是针对面试题的,谁也无法在面试的有限时间内设计出像QQ那样强大的聊天工具,所以只是实现一些基本的功能即可,根据书上所述,包括如下功能:
1. 登陆和登出
2. 添加请求(发送,接受,和拒绝)
3. 更显一个状态消息
4. 建立私聊和群聊
5. 添加信息到私聊和群聊
enum UserStatusType { Offline, Away, Idle, Available, Busy };
enum RequestStatus { Unread, Read, Accepted, Rejected }; class Date {
public:
Date() {}
Date(int m, int d, int y): _month(m), _day(d), _year(y) {} private:
int _month, _day, _year;
}; class Message {
public:
Message(string content, Date *date): _content(content), _date(date) {}
string getContent() { return _content; }
Date* getDate() { return _date; } private:
string _content;
Date *_date;
}; class Conversation {
public:
vector<Message*> getMessages() { return _messages; }
bool addMessage(Message *m) {
_messages.push_back(m);
return true;
}
int getId() { return _id; } protected:
vector<User*> _participants;
int _id;
vector<Message*> _messages;
}; class GroupChat: public Conversation {
public:
void removeParticipant(User *user) {
for (vector<User*>::iterator it = _participants.begin(); it != _participants.end(); ++it) {
if (*it == user) _participants.erase(it);
}
}
void addParticipant(User *user) {
_participants.push_back(user);
}
}; class PrivateChat: public Conversation {
public:
PrivateChat(User *user1, User *user2) {
_participants.push_back(user1);
_participants.push_back(user2);
}
User* getOtherParticipant(User *primary) {
if (_participants[] == primary) {
return _participants[];
} else if (_participants[] == primary) {
return _participants[];
}
return nullptr;
}
}; class AddRequest {
public:
AddRequest(User *from, User *to, Date *date): _fromUser(from), _toUser(to), _date(date) {
_status = RequestStatus::Unread;
}
void setStatus(RequestStatus status) { _status = status; }
RequestStatus getStatus() { return _status; }
User* getFromUser() { return _fromUser; }
User* getToUser() { return _toUser; }
Date* getDate() { return _date; } private:
User *_fromUser;
User *_toUser;
Date *_date;
RequestStatus _status;
}; class UserStatus{
public:
UserStatus(UserStatusType type, string message): _type(type), _message(message) {}
UserStatusType getStatusType() { return _type; }
string getMessage() { return _message; } private:
string _message;
UserStatusType _type;
}; class UserManager; class User {
public:
User(int id, string accountName, string fullName): _id(id), _accountName(accountName), _fullName(fullName) {}
bool sendMessageToUser(User *to, string content) {
PrivateChat *chat = _privateChats[to->getId()];
if (chat == nullptr) {
chat = new PrivateChat(this, to);
_privateChats[to->getId()] = chat;
}
Message *message = new Message(content, new Date());
return chat->addMessage(message);
}
bool sendMessageToGroupChat(int id, string content) {
GroupChat *chat = _groupChats[id];
if (chat != nullptr) {
Message *message = new Message(content, new Date());
return chat->addMessage(message);
}
return false;
}
void setStatus(UserStatus *status) { _status = status; }
UserStatus* getStatus() { return _status; };
bool addContact(User *user) {
if (_contacts.find(user->getId()) != _contacts.end()) {
return false;
} else {
_contacts[user->getId()] = user;
return true;
}
}
void receivedAddRequest(AddRequest *req) {
int senderId = req->getFromUser()->getId();
if (_receivedAddRequests.find(senderId) == _receivedAddRequests.end()) {
_receivedAddRequests[senderId] = req;
}
}
void sentAddRequest(AddRequest *req) {
int receiverId = req->getFromUser()->getId();
if (_sentAddRequests.find(receiverId) == _sentAddRequests.end()) {
_sentAddRequests[receiverId] = req;
}
}
void removeAddRequest(AddRequest *req) {
if (req->getToUser() == this) {
for (unordered_map<int, AddRequest*>::iterator it = _receivedAddRequests.begin(); it != _receivedAddRequests.end(); ++it) {
if (it->second == req) _receivedAddRequests.erase(it);
}
} else if (req->getFromUser() == this) {
for (unordered_map<int, AddRequest*>::iterator it = _sentAddRequests.begin(); it != _sentAddRequests.end(); ++it) {
if (it->second == req) _sentAddRequests.erase(it);
}
}
}
void requestAddUser(string accountName) {
UserManager::getInstance()->addUser(this, accountName);
}
void addConversation(PrivateChat *conversation) {
User *otherUser = conversation->getOtherParticipant(this);
_privateChats[otherUser->getId()] = conversation;
}
void addConversation(GroupChat *conversation) {
_groupChats.push_back(conversation);
}
int getId() { return _id; }
string getAccountName() { return _accountName; };
string getFullName() { return _fullName; } private:
int _id;
UserStatus *_status = nullptr;
unordered_map<int, PrivateChat*> _privateChats;
vector<GroupChat*> _groupChats;
unordered_map<int, AddRequest*> _receivedAddRequests;
unordered_map<int, AddRequest*> _sentAddRequests;
unordered_map<int, User*> _contacts;
string _accountName;
string _fullName;
}; class UserManager {
public:
static UserManager* getInstance() {
if (_instance == nullptr) _instance = new UserManager();
return _instance;
}
void addUser(User *fromUser, string toAcountName) {
User *toUser = _usersByAccountName[toAcountName];
AddRequest *req = new AddRequest(fromUser, toUser, new Date());
toUser->receivedAddRequest(req);
fromUser->sentAddRequest(req);
}
void approveAddRequest(AddRequest *req) {
req->setStatus(RequestStatus::Accepted);
User *from = req->getFromUser();
User *to = req->getToUser();
from->addContact(to);
to->addContact(from);
}
void rejectAddRequest(AddRequest *req) {
req->setStatus(RequestStatus::Rejected);
User *from = req->getFromUser();
User *to = req->getToUser();
from->removeAddRequest(req);
to->removeAddRequest(req);
}
void userSignedOn(string accountName) {
User *user = _usersByAccountName[accountName];
if (user != nullptr) {
user->setStatus(new UserStatus(UserStatusType::Available, ""));
_onlineUsers[user->getId()] = user;
}
}
void userSignedOff(string accountName) {
User *user = _usersByAccountName[accountName];
if (user != nullptr) {
user->setStatus(new UserStatus(UserStatusType::Offline, ""));
for (unordered_map<int, User*>::iterator it = _onlineUsers.begin(); it != _onlineUsers.end(); ++it) {
if (it->first == user->getId()) _onlineUsers.erase(it);
}
}
} private:
static UserManager *_instance;
unordered_map<int, User*> _usersById;
unordered_map<string, User*> _usersByAccountName;
unordered_map<int, User*> _onlineUsers;
};
[CareerCup] 8.7 Chat Server 聊天服务器的更多相关文章
- 定制的Server-Sent Events 聊天服务器
//匿名聊天服务器 //将新的消息POST到/chat地址,或者以GET形式从通一个URL获取文本或事件流 //创建一个GET请求到"/"来返回一个简单的HTML文件,这个文件包括 ...
- openfire:基于开源 Openfire 聊天服务器 - 开发Openfire聊天记录插件
基于开源 Openfire 聊天服务器 - 开发Openfire聊天记录插件 上一篇文章介绍到怎么在自己的Java环境中搭建openfire插件开发的环境,同时介绍到怎样一步步简单的开发openfir ...
- 基于开源 Openfire 聊天服务器 - 开发Openfire聊天记录插件
原文:http://www.cnblogs.com/hoojo/archive/2013/03/29/openfire_plugin_chatlogs_plugin_.html 随笔-150 评论- ...
- HTTP 笔记与总结(9)分块传输、持久链接 与 反向 ajax(comet / server push / 服务器推技术)
反向 ajax 又叫 comet / server push / 服务器推技术 应用范围:网页聊天服务器,例如新浪微博在线聊天.google mail 网页聊天 原理:一般而言,HTTP 协议的特点是 ...
- Codeforces Beta Round #5 A. Chat Server's Outgoing Traffic 水题
A. Chat Server's Outgoing Traffic 题目连接: http://www.codeforces.com/contest/5/problem/A Description Po ...
- CodeForces.5A Chat Server's Outgoing Traffic
Chat Server's Outgoing Traffic 点我挑战提目 考察点 模拟 字符串 Time Mem Len Lang 30 0 543 c++ 题意分析 给出类似一个群的即时通讯系统, ...
- 通过python 构建一个简单的聊天服务器
构建一个 Python 聊天服务器 一个简单的聊天服务器 现在您已经了解了 Python 中基本的网络 API:接下来可以在一个简单的应用程序中应用这些知识了.在本节中,将构建一个简单的聊天服务器.使 ...
- 看完这篇包你进大厂,实战即时聊天,一文说明白:聊天服务器+聊天客户端+Web管理控制台。
一.前言 说实话,写这个玩意儿是我上周刚刚产生的想法,本想写完后把代码挂上来赚点积分也不错.写完后发现这东西值得写一篇文章,授人予鱼不如授人以渔嘛(这句话是这么说的吧),顺便赚点应届学生MM的膜拜那就 ...
- Windows Server 2003服务器无法下载.exe文件的解决方法
今天架设了一台Windows Server 2003的网站服务器,发现打开网页后无法下载网站中的.exe文件,经过研究问题得以解决,拿来做个备忘. 解决方法非常简单,只需要在IIS中,将网站属性里的执 ...
随机推荐
- Effective Java 03 Enforce the singleton property with a private constructor or an enum type
Principle When implement the singleton pattern please decorate the INSTANCE field with "static ...
- jquery实践案例--验证电子邮箱
<input type="email" name="email" id="email" value="" onpa ...
- CSS纯样式实现箭头、对话框等形状
在使用第三方框架bootstrap的时候,本以为其是图片实现的小箭头,后来使用开发工具查看是用CSS来实现的,现记录如下: 之前都没仔细去观注过其原理,都是拿来使用,在实现小箭头之前需要了解下CSS的 ...
- centos6.7 安装Docker
一.查看系统版本 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.7 (Final) 二.安装EPEL 1.进入cento ...
- c#实现验证码功能
一.验证码简介 验证码功能一般是用于防止批量注册的,不少网站为了防止用户利用机器人自动注册.登录.灌水,都采用了验证码技术.所谓验证码,就是将一串随机产生的数字或字母或符号或文字,生成一幅图片, 图片 ...
- 在AngularJS中实现自定义表单验证
除了一些已经定义好了的验证(例如 必填项.最小长度.最大长度)之外,更常用的,还是需要我们自己定义表单验证,这样才能对于项目中遇到的很多非常规问题给出自己的合适的解决方案. 在表单中控制变量 表单的属 ...
- Oracle 分组聚合二种写法,listagg和wmsys.wm_concat
with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Sh ...
- volatile与synchronized关键字
volatile关键字相信了解Java多线程的读者都很清楚它的作用.volatile关键字用于声明简单类型变量,如int.float.boolean等数据类型.如果这些简单数据类型声明为volatil ...
- uva 1606 amphiphilic carbon molecules【把缩写写出来,有惊喜】(滑动窗口)——yhx
Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new classof ...
- Redis安全访问
限制IP(只能一个) # If you want you can bind a single interface, if the bind option is not # specified all ...