User, Group, GroupUser是和用户相关操作,即设置或返回用户、群组或群成员的信息。而Model代表着对数据库的增删改查操作。
User即就是可以管理用户的信息
#ifndef USER_H
#define USER_H
#include
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
这个类是针对群组成员,那么只需要继承User类,多添加一个身份信息,就可得知某个用户是管理员还是普通成员。
#ifndef GROUPUSER_H
#define GROUPUSER_H
#include "user.hpp"
// 群组用户,多了一个role角色信息(管理员或普通成员)
// 从User类直接继承,复用User的其他信息
class GroupUser : public User
{
public:
void setRole(string role) { this->role = role; }
string getRole() { return this->role; }
private:
string role;
};
#endif
群组类的设计也比较简单,只有群组id,群名称和群描述。
#ifndef GROUP_H
#define GROUP_H
#include "groupUser.hpp"
#include
#include
using namespace std;
// User表的ORM类
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
Model类都是对数据库的操作,即增删改查,这里有个头文件是数据库连接池,稍后介绍。
#ifndef USERMODEL_H
#define USERMODEL_H
#include "user.hpp"
#include "dbConnectionPool.h"
extern ConnectionPool* sp;
// User表的数据操作类
class UserModel {
public:
// User表的增加操作
bool insert(User& user);
// 根据用户号码返回用户信息
User query(int id);
// 更新用户的状态信息
bool updateState(User user);
// 重置用户得状态信息
void resetState();
};
#endif
#ifndef GROUPMODEL_H
#define GROUPMODEL_H
#include "group.hpp"
using namespace std;
// 维护群组信息的操作接口方法
class GroupModel
{
public:
// 创建群组
bool createGroup(Group& group);
// 加入群组
void addGroup(int userid, int groupid, string role);
// 查询用户所在群组信息
vector<Group> queryGroups(int userid);
// 根据指定的groupid查询群组用户id列表
// 除了userid自己,主要用户群聊业务给群组其他成员群发消息
vector<int> queryGroupUsers(int userid, int groupid);
};
#endif
#ifndef FRIENDMODEL_H
#define FIRENDMODEL_H
#include "user.hpp"
#include
#include
using namespace std;
// 维护好友信息的操作接口方法
class FriendModel
{
public:
// 添加好友关系
void insert(int userid, int friendid);
// 返回用户好友列表 两个表的联合查询
vector<User> query(int userid);
};
#endif
#ifndef OFFLINEMESSAGEMODEL_H
#define OFFLINEMESSAGEMODEL_H
#include "dbConnectionPool.h"
#include
#include
using namespace std;
extern ConnectionPool* sp;
// 提供离线消息表的操作接口方法
class OfflineMsgModel
{
public:
// 存储用户的离线消息
void insert(int userId, string msg);
// 删除用户的离线消息
void remove(int userId);
// 查询用户的离线消息
vector<string> query(int userId);
};
#endif
为了提高MySQL数据库(基于C/S设计)的访问瓶颈,除了在服务器端增加缓存服务器缓存常用的数据之外(例如redis),还可以增加连接池,来提高MySQL Server的访问效率,在高并发情况下,大量的TCP三次握手、MySQL Server连接认证、MySQL Server关闭连接回收资源和TCP四次挥手所耗费的性能时间也是很明显的,增加连接池就是为了减少这一部分的性能损耗。
连接池的实现:【数据库连接池】
该连接池是单例模式的
// 获取连接池对象实例 静态的
static ConnectionPool* getConnectionPool();
// 给外部提供接口,提供一个空闲的连接
shared_ptr<Connection> getConnection();
// 初始化连接池
ConnectionPool* sp = ConnectionPool::getConnectionPool();
在连接池的源文件初始化连接池,因为要在Model层对数据库操作,所以在要对访问MySQL的文件使用extern引入该连接池,示例:
extern ConnectionPool* sp