当前位置:网站首页>Cluster chat server: Framework Design of model data layer and encapsulation of database code
Cluster chat server: Framework Design of model data layer and encapsulation of database code
2022-07-23 21:18:00 【_ Soren】
List of articles
framework design
User, Group, GroupUser It is a user related operation , That is, set or return to the user 、 Group or group member information . and Model It represents the addition, deletion, modification and query of the database .

Functions of each class
User
User That is, you can manage users' information
#ifndef USER_H
#define USER_H
#include <string>
using namespace std;
class User
{
public:
User(int id = -1, string name = "", string password = "", string state = "offline")
: m_id(id), m_name(name), m_password(password), m_state(state)
{
}
void setId(int id) {
this->m_id = id; }
void setName(string name) {
this->m_name = name; }
void setPwd(string password) {
this->m_password = password; }
void setState(string state) {
this->m_state = state; }
int getId() const {
return this->m_id; }
string getName() const {
return this->m_name; }
string getPwd() const {
return this->m_password; }
string getState() const {
return this->m_state; }
protected:
int m_id;
string m_name;
string m_password;
string m_state;
};
#endif
GroupUser
This class is for group members , Then just inherit User class , Add one more identity , You can know whether a user is an administrator or an ordinary member .
#ifndef GROUPUSER_H
#define GROUPUSER_H
#include "user.hpp"
// Group users , One more. role The role of information ( Administrator or ordinary member )
// from User Direct class inheritance , Reuse User Other information about
class GroupUser : public User
{
public:
void setRole(string role) {
this->role = role; }
string getRole() {
return this->role; }
private:
string role;
};
#endif
Group
The design of group class is also relatively simple , Groups only id, Group name and group description .
#ifndef GROUP_H
#define GROUP_H
#include "groupUser.hpp"
#include <vector>
#include <string>
using namespace std;
// User Tabular ORM class
class Group
{
public:
Group(int id = -1, string name = "", string desc = "")
{
this->id = id;
this->name = name;
this->desc = desc;
}
void setId(int id) {
this->id = id; }
void setName(string name) {
this->name = name; }
void setDesc(string desc) {
this->desc = desc; }
int getId() const {
return this->id; }
string getName() const {
return this->name; }
string getDesc() const {
return this->desc; }
vector<GroupUser>& getUsers() {
return this->users;}
private:
int id;
string name;
string desc;
vector<GroupUser> users;
};
#endif
UserModel
Model Classes are operations on databases , I.e. add, delete, change and check , Here is a header file that is the database connection pool , Later on .
#ifndef USERMODEL_H
#define USERMODEL_H
#include "user.hpp"
#include "dbConnectionPool.h"
extern ConnectionPool* sp;
// User Data operation class of table
class UserModel {
public:
// User Add table
bool insert(User& user);
// Return user information according to user number
User query(int id);
// Update the user's status information
bool updateState(User user);
// Reset the user's status information
void resetState();
};
#endif
GroupModel
#ifndef GROUPMODEL_H
#define GROUPMODEL_H
#include "group.hpp"
using namespace std;
// Operation interface method for maintaining group information
class GroupModel
{
public:
// Create groups
bool createGroup(Group& group);
// Join the group
void addGroup(int userid, int groupid, string role);
// Query the user's group information
vector<Group> queryGroups(int userid);
// According to the designation groupid Query group users id list
// except userid own , The main user group chat business sends messages to other members of the Group
vector<int> queryGroupUsers(int userid, int groupid);
};
#endif
FriendModel
#ifndef FRIENDMODEL_H
#define FIRENDMODEL_H
#include "user.hpp"
#include <string>
#include <vector>
using namespace std;
// Operation interface method for maintaining friend information
class FriendModel
{
public:
// Add friends
void insert(int userid, int friendid);
// Return to the user's friend list Joint query of two tables
vector<User> query(int userid);
};
#endif
OfflineMsgModel
#ifndef OFFLINEMESSAGEMODEL_H
#define OFFLINEMESSAGEMODEL_H
#include "dbConnectionPool.h"
#include <string>
#include <vector>
using namespace std;
extern ConnectionPool* sp;
// Provide the operation interface method of offline message table
class OfflineMsgModel
{
public:
// Store offline messages of users
void insert(int userId, string msg);
// Delete the user's offline message
void remove(int userId);
// Query the user's offline message
vector<string> query(int userId);
};
#endif
Use database connection pool
In order to improve the MySQL database ( be based on C/S Design ) Access bottleneck of , In addition to adding cache on the server side, the server caches commonly used data ( for example redis), You can also add connection pools , To improve the MySQL Server Access efficiency , In high concurrency , a large number of TCP Three handshakes 、MySQL Server Connection authentication 、MySQL Server Close the connection to recycle resources and TCP The performance time spent on four waves is also obvious , The purpose of adding connection pool is to reduce the performance loss of this part .
Implementation of connection pool :【 Database connection pool 】
The connection pool is in singleton mode
// Get connection pool object instance Static
static ConnectionPool* getConnectionPool();
// Provide an interface to the outside , Provide an idle connection
shared_ptr<Connection> getConnection();
// Initialize connection pool
ConnectionPool* sp = ConnectionPool::getConnectionPool();
Initialize the connection pool in the source file of the connection pool , Because in Model The layer operates on the database , So we need to visit MySQL File usage extern Introduce the connection pool , Example :
extern ConnectionPool* sp

边栏推荐
- WinDbg实践--入门篇
- How to get the worker's hat? Where is the worker's helmet?
- 如何在面试中介绍自己的项目经验
- 1063 Set Similarity
- Basic syntax of MySQL DDL and DML and DQL
- 【攻防世界WEB】难度四星12分进阶题:Confusion1
- [continuous update] collection of raspberry pie startup and failure series
- Chapter1 data cleaning
- TCP half connection queue and full connection queue (the most complete in History)
- 高数下|三重积分的计算1|高数叔|手写笔记
猜你喜欢
随机推荐
Microservice architecture vs single service architecture [what can Huawei cloud service do in the microservice mode]
确定括号序列中的一些位置
h264编码参数
集群聊天服务器:集群与分布式理论
集群聊天服务器:如何解决跨服务器通信问题 | redis发布-订阅
高数下|三重积分的计算1|高数叔|手写笔记
[shader realizes roundwave circular ripple effect _shader effect Chapter 6]
如何在面试中介绍自己的项目经验
High numbers | calculation of double integral 4 | high numbers | handwritten notes
Major optimization of openim - Message loading on demand, consistent cache, uniapp Publishing
分布式能源的不确定性——风速测试(Matlab代码实现)
Be a professional software craftsman
【微信小程序】你了解小程序开发吗?
[attack and defense world web] difficulty four-star 12 point advanced question: flatscience
Unity solves that animation is not available: the animationclip 'xxx' used by the animation component 'xxx' must be marked as legacy
OpenCV图像处理——拉普拉斯金字塔
Failed to introspect class feignclientfactorybean exception troubleshooting
CMake的学习
UnauthorizedAccessException:Access to the path “/xx/xx.xx“ is denied
When we talk about Chen Chunhua and Huawei, what are we talking about?








