[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中,将网站属性里的执 ...
随机推荐
- scanf 用法及陷阱(转)
函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]); scanf()函数是通用终端格式化输入函数,它从标准输入设备( ...
- Java 读取指定目录下的文件名和目录名
需求:读取指定目录下的文件名和目录名 实现如下: package com.test.common.util; import java.io.File; public class ReadFile { ...
- Effective Java 11 Override clone judiciously
Principles If you override the clone method in a nonfinal class, you should return an object obtaine ...
- MaxMin搜索
- cocos2d-x之文件读写
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } auto fu=FileUtils::getInstance(); ...
- jQuery Validate 表单验证插件----通过name属性来关联字段来验证,改变默认的提示信息,将校验规则写到 js 代码中
一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW 访问密码 f224 二. 添加一个另外一个插件jquery.validate.messages_cn.js. ...
- cordova 环境搭建
安装环境前题是nodejs已安装,android环境搭建完成,android环境没有通过http://www.androiddevtools.cn/安装,安装使用 淘宝 NPM 镜像 方式 1.运行c ...
- C++控制台应用程序之贪吃蛇(改进版)
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<time.h> #in ...
- python实现插入排序
代码如下@.·.@ # *-* coding: utf- *-* if __name__ == '__main__': def insert_sort(l): ,len(l)): tmp = l[i] ...
- 安装多个版本的unity
版本特性导致新版本Unity打开老版本的项目工程报错,所以最好在电脑上安装多个不同版本的Unity 方法一 安装目录命名:Unity_3.5 , Unity_4.3.1 确保默认例子的安装路径分开C: ...