mirror of
https://gitee.com/Zhaoxin59/my-chat_-server.git
synced 2026-02-14 01:21:50 +08:00
update
This commit is contained in:
88
friend/CMakeLists.txt
Normal file
88
friend/CMakeLists.txt
Normal file
@ -0,0 +1,88 @@
|
||||
# 1. 添加cmake版本说明
|
||||
cmake_minimum_required(VERSION 3.1.3)
|
||||
# 2. 声明工程名称
|
||||
project(friend_server)
|
||||
|
||||
set(target "friend_server")
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
|
||||
|
||||
# 3. 检测并生成ODB框架代码
|
||||
# 1. 添加所需的proto映射代码文件名称
|
||||
set(proto_path ${CMAKE_CURRENT_SOURCE_DIR}/../proto)
|
||||
set(proto_files base.proto user.proto message.proto friend.proto)
|
||||
# 2. 检测框架代码文件是否已经生成
|
||||
set(proto_hxx "")
|
||||
set(proto_cxx "")
|
||||
set(proto_srcs "")
|
||||
foreach(proto_file ${proto_files})
|
||||
# 3. 如果没有生成,则预定义生成指令 -- 用于在构建项目之间先生成框架代码
|
||||
string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file})
|
||||
string(REPLACE ".proto" ".pb.h" proto_hh ${proto_file})
|
||||
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${proto_cc})
|
||||
add_custom_command(
|
||||
PRE_BUILD
|
||||
COMMAND protoc
|
||||
ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} -I ${proto_path} --experimental_allow_proto3_optional ${proto_path}/${proto_file}
|
||||
DEPENDS ${proto_path}/${proto_file}
|
||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
|
||||
COMMENT "生成Protobuf框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}
|
||||
)
|
||||
endif()
|
||||
list(APPEND proto_srcs ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc})
|
||||
endforeach()
|
||||
|
||||
# 3. 检测并生成ODB框架代码
|
||||
# 1. 添加所需的odb映射代码文件名称
|
||||
set(odb_path ${CMAKE_CURRENT_SOURCE_DIR}/../odb)
|
||||
set(odb_files chat_session_member.hxx chat_session.hxx friend_apply.hxx relation.hxx)
|
||||
# 2. 检测框架代码文件是否已经生成
|
||||
set(odb_hxx "")
|
||||
set(odb_cxx "")
|
||||
set(odb_srcs "")
|
||||
foreach(odb_file ${odb_files})
|
||||
# 3. 如果没有生成,则预定义生成指令 -- 用于在构建项目之间先生成框架代码
|
||||
string(REPLACE ".hxx" "-odb.hxx" odb_hxx ${odb_file})
|
||||
string(REPLACE ".hxx" "-odb.cxx" odb_cxx ${odb_file})
|
||||
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}${odb_cxx})
|
||||
add_custom_command(
|
||||
PRE_BUILD
|
||||
COMMAND odb
|
||||
ARGS -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time ${odb_path}/${odb_file}
|
||||
DEPENDS ${odb_path}/${odb_file}
|
||||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}
|
||||
COMMENT "生成ODB框架代码文件:" ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx}
|
||||
)
|
||||
endif()
|
||||
# 4. 将所有生成的框架源码文件名称保存起来 student-odb.cxx classes-odb.cxx
|
||||
list(APPEND odb_srcs ${CMAKE_CURRENT_BINARY_DIR}/${odb_cxx})
|
||||
endforeach()
|
||||
|
||||
# 4. 获取源码目录下的所有源码文件
|
||||
set(src_files "")
|
||||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/source src_files)
|
||||
# 5. 声明目标及依赖
|
||||
add_executable(${target} ${src_files} ${proto_srcs} ${odb_srcs})
|
||||
# 7. 设置需要连接的库
|
||||
target_link_libraries(${target} -lgflags
|
||||
-lspdlog -lfmt -lbrpc -lssl -lcrypto
|
||||
-lprotobuf -lleveldb -letcd-cpp-api
|
||||
-lcpprest -lcurl -lodb-mysql -lodb -lodb-boost
|
||||
/usr/lib/x86_64-linux-gnu/libjsoncpp.so.19
|
||||
-lcpr -lelasticlient)
|
||||
|
||||
|
||||
set(test_client "friend_client")
|
||||
set(test_files "")
|
||||
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/test test_files)
|
||||
add_executable(${test_client} ${test_files} ${proto_srcs})
|
||||
target_link_libraries(${test_client} -pthread -lgtest -lgflags -lspdlog -lfmt -lbrpc -lssl -lcrypto -lprotobuf -lleveldb -letcd-cpp-api -lcpprest -lcurl /usr/lib/x86_64-linux-gnu/libjsoncpp.so.19)
|
||||
|
||||
# 6. 设置头文件默认搜索路径
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../odb)
|
||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../third/include)
|
||||
|
||||
#8. 设置安装路径
|
||||
INSTALL(TARGETS ${target} ${test_client} RUNTIME DESTINATION bin)
|
||||
16
friend/dockerfile
Normal file
16
friend/dockerfile
Normal file
@ -0,0 +1,16 @@
|
||||
# 声明基础经镜像来源
|
||||
FROM debian:12
|
||||
|
||||
# 声明工作目录
|
||||
WORKDIR /im
|
||||
RUN mkdir -p /im/logs &&\
|
||||
mkdir -p /im/data &&\
|
||||
mkdir -p /im/conf &&\
|
||||
mkdir -p /im/bin
|
||||
|
||||
# 将可执行程序依赖,拷贝进镜像
|
||||
COPY ./build/friend_server /im/bin/
|
||||
# 将可执行程序文件,拷贝进镜像
|
||||
COPY ./depends /lib/x86_64-linux-gnu/
|
||||
# 设置容器启动的默认操作 ---运行程序
|
||||
CMD /im/bin/friend_server -flagfile=/im/conf/friend_server.conf
|
||||
20
friend/friend_server.conf
Normal file
20
friend/friend_server.conf
Normal file
@ -0,0 +1,20 @@
|
||||
-run_mode=true
|
||||
-log_file=/im/logs/friend.log
|
||||
-log_level=0
|
||||
-registry_host=http://10.0.0.235:2379
|
||||
-instance_name=/friend_service/instance
|
||||
-access_host=10.0.0.235:10006
|
||||
-listen_port=10006
|
||||
-rpc_timeout=-1
|
||||
-rpc_threads=1
|
||||
-base_service=/service
|
||||
-user_service=/service/user_service
|
||||
-message_service=/service/message_service
|
||||
-es_host=http://10.0.0.235:9200/
|
||||
-mysql_host=10.0.0.235
|
||||
-mysql_user=root
|
||||
-mysql_pswd=123456
|
||||
-mysql_db=bite_im
|
||||
-mysql_cset=utf8
|
||||
-mysql_port=0
|
||||
-mysql_pool_count=4
|
||||
49
friend/source/friend_server.cc
Normal file
49
friend/source/friend_server.cc
Normal file
@ -0,0 +1,49 @@
|
||||
//主要实现语音识别子服务的服务器的搭建
|
||||
#include "friend_server.hpp"
|
||||
|
||||
DEFINE_bool(run_mode, false, "程序的运行模式,false-调试; true-发布;");
|
||||
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
|
||||
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");
|
||||
|
||||
DEFINE_string(registry_host, "http://127.0.0.1:2379", "服务注册中心地址");
|
||||
DEFINE_string(instance_name, "/friend_service/instance", "当前实例名称");
|
||||
DEFINE_string(access_host, "127.0.0.1:10006", "当前实例的外部访问地址");
|
||||
|
||||
DEFINE_int32(listen_port, 10006, "Rpc服务器监听端口");
|
||||
DEFINE_int32(rpc_timeout, -1, "Rpc调用超时时间");
|
||||
DEFINE_int32(rpc_threads, 1, "Rpc的IO线程数量");
|
||||
|
||||
|
||||
DEFINE_string(base_service, "/service", "服务监控根目录");
|
||||
DEFINE_string(user_service, "/service/user_service", "用户管理子服务名称");
|
||||
DEFINE_string(message_service, "/service/message_service", "消息存储子服务名称");
|
||||
|
||||
DEFINE_string(es_host, "http://127.0.0.1:9200/", "ES搜索引擎服务器URL");
|
||||
|
||||
DEFINE_string(mysql_host, "127.0.0.1", "Mysql服务器访问地址");
|
||||
DEFINE_string(mysql_user, "root", "Mysql服务器访问用户名");
|
||||
DEFINE_string(mysql_pswd, "123456", "Mysql服务器访问密码");
|
||||
DEFINE_string(mysql_db, "bite_im", "Mysql默认库名称");
|
||||
DEFINE_string(mysql_cset, "utf8", "Mysql客户端字符集");
|
||||
DEFINE_int32(mysql_port, 0, "Mysql服务器访问端口");
|
||||
DEFINE_int32(mysql_pool_count, 4, "Mysql连接池最大连接数量");
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
bite_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
|
||||
|
||||
bite_im::FriendServerBuilder fsb;
|
||||
fsb.make_es_object({FLAGS_es_host});
|
||||
fsb.make_mysql_object(FLAGS_mysql_user, FLAGS_mysql_pswd, FLAGS_mysql_host,
|
||||
FLAGS_mysql_db, FLAGS_mysql_cset, FLAGS_mysql_port, FLAGS_mysql_pool_count);
|
||||
fsb.make_discovery_object(FLAGS_registry_host, FLAGS_base_service, FLAGS_user_service, FLAGS_message_service);
|
||||
fsb.make_rpc_server(FLAGS_listen_port, FLAGS_rpc_timeout, FLAGS_rpc_threads);
|
||||
fsb.make_registry_object(FLAGS_registry_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);
|
||||
auto server = fsb.build();
|
||||
server->start();
|
||||
return 0;
|
||||
}
|
||||
637
friend/source/friend_server.hpp
Normal file
637
friend/source/friend_server.hpp
Normal file
@ -0,0 +1,637 @@
|
||||
//实现语音识别子服务
|
||||
#include <brpc/server.h>
|
||||
#include <butil/logging.h>
|
||||
|
||||
#include "data_es.hpp" // es数据管理客户端封装
|
||||
#include "mysql_chat_session_member.hpp" // mysql数据管理客户端封装
|
||||
#include "mysql_chat_session.hpp" // mysql数据管理客户端封装
|
||||
#include "mysql_relation.hpp" // mysql数据管理客户端封装
|
||||
#include "mysql_apply.hpp" // mysql数据管理客户端封装
|
||||
#include "etcd.hpp" // 服务注册模块封装
|
||||
#include "logger.hpp" // 日志模块封装
|
||||
#include "utils.hpp" // 基础工具接口
|
||||
#include "channel.hpp" // 信道管理模块封装
|
||||
|
||||
|
||||
#include "friend.pb.h" // protobuf框架代码
|
||||
#include "base.pb.h" // protobuf框架代码
|
||||
#include "user.pb.h" // protobuf框架代码
|
||||
#include "message.pb.h" // protobuf框架代码
|
||||
|
||||
namespace bite_im{
|
||||
class FriendServiceImpl : public bite_im::FriendService {
|
||||
public:
|
||||
FriendServiceImpl(
|
||||
const std::shared_ptr<elasticlient::Client> &es_client,
|
||||
const std::shared_ptr<odb::core::database> &mysql_client,
|
||||
const ServiceManager::ptr &channel_manager,
|
||||
const std::string &user_service_name,
|
||||
const std::string &message_service_name) :
|
||||
_es_user(std::make_shared<ESUser>(es_client)),
|
||||
_mysql_apply(std::make_shared<FriendApplyTable>(mysql_client)),
|
||||
_mysql_chat_session(std::make_shared<ChatSessionTable>(mysql_client)),
|
||||
_mysql_chat_session_member(std::make_shared<ChatSessionMemeberTable>(mysql_client)),
|
||||
_mysql_relation(std::make_shared<RelationTable>(mysql_client)),
|
||||
_user_service_name(user_service_name),
|
||||
_message_service_name(message_service_name),
|
||||
_mm_channels(channel_manager){}
|
||||
~FriendServiceImpl(){}
|
||||
virtual void GetFriendList(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::GetFriendListReq* request,
|
||||
::bite_im::GetFriendListRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
|
||||
//1. 提取请求中的关键要素:用户ID
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
//2. 从数据库中查询获取用户的好友ID
|
||||
auto friend_id_lists = _mysql_relation->friends(uid);
|
||||
std::unordered_set<std::string> user_id_lists;
|
||||
for (auto &id : friend_id_lists) {
|
||||
user_id_lists.insert(id);
|
||||
}
|
||||
//3. 从用户子服务批量获取用户信息
|
||||
std::unordered_map<std::string, UserInfo> user_list;
|
||||
bool ret = GetUserInfo(rid, user_id_lists, user_list);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 批量获取用户信息失败!", rid);
|
||||
return err_response(rid, "批量获取用户信息失败!");
|
||||
}
|
||||
//4. 组织响应
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
for (const auto & user_it : user_list) {
|
||||
auto user_info = response->add_friend_list();
|
||||
user_info->CopyFrom(user_it.second);
|
||||
}
|
||||
}
|
||||
virtual void FriendRemove(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::FriendRemoveReq* request,
|
||||
::bite_im::FriendRemoveRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//1. 提取关键要素:当前用户ID,要删除的好友ID
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
std::string pid = request->peer_id();
|
||||
//2. 从好友关系表中删除好友关系信息
|
||||
bool ret = _mysql_relation->remove(uid, pid);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 从数据库删除好友信息失败!", rid);
|
||||
return err_response(rid, "从数据库删除好友信息失败!");
|
||||
}
|
||||
//3. 从会话信息表中,删除对应的聊天会话 -- 同时删除会话成员表中的成员信息
|
||||
ret = _mysql_chat_session->remove(uid, pid);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{}- 从数据库删除好友会话信息失败!", rid);
|
||||
return err_response(rid, "从数据库删除好友会话信息失败!");
|
||||
}
|
||||
//4. 组织响应
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
}
|
||||
virtual void FriendAdd(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::FriendAddReq* request,
|
||||
::bite_im::FriendAddRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//1. 提取请求中的关键要素:申请人用户ID; 被申请人用户ID
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
std::string pid = request->respondent_id();
|
||||
//2. 判断两人是否已经是好友
|
||||
bool ret = _mysql_relation->exists(uid, pid);
|
||||
if (ret == true) {
|
||||
LOG_ERROR("{}- 申请好友失败-两者{}-{}已经是好友关系", rid, uid, pid);
|
||||
return err_response(rid, "两者已经是好友关系!");
|
||||
}
|
||||
//3. 当前是否已经申请过好友
|
||||
ret = _mysql_apply->exists(uid, pid);
|
||||
if (ret == true) {
|
||||
LOG_ERROR("{}- 申请好友失败-已经申请过对方好友!", rid, uid, pid);
|
||||
return err_response(rid, "已经申请过对方好友!");
|
||||
}
|
||||
//4. 向好友申请表中,新增申请信息
|
||||
std::string eid = uuid();
|
||||
FriendApply ev(eid, uid, pid);
|
||||
ret = _mysql_apply->insert(ev);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 向数据库新增好友申请事件失败!", rid);
|
||||
return err_response(rid, "向数据库新增好友申请事件失败!");
|
||||
}
|
||||
//3. 组织响应
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
response->set_notify_event_id(eid);
|
||||
}
|
||||
virtual void FriendAddProcess(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::FriendAddProcessReq* request,
|
||||
::bite_im::FriendAddProcessRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//1. 提取请求中的关键要素:申请人用户ID;被申请人用户ID;处理结果;事件ID
|
||||
std::string rid = request->request_id();
|
||||
std::string eid = request->notify_event_id();
|
||||
std::string uid = request->user_id(); //被申请人
|
||||
std::string pid = request->apply_user_id();//申请人
|
||||
bool agree = request->agree();
|
||||
//2. 判断有没有该申请事件
|
||||
bool ret = _mysql_apply->exists(pid, uid);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{}- 没有找到{}-{}对应的好友申请事件!", rid, pid, uid);
|
||||
return err_response(rid, "没有找到对应的好友申请事件!");
|
||||
}
|
||||
//3. 如果有: 可以处理; --- 删除申请事件--事件已经处理完毕
|
||||
ret = _mysql_apply->remove(pid, uid);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{}- 从数据库删除申请事件 {}-{} 失败!", rid, pid, uid);
|
||||
return err_response(rid, "从数据库删除申请事件失败!");
|
||||
}
|
||||
//4. 如果处理结果是同意:向数据库新增好友关系信息;新增单聊会话信息及会话成员
|
||||
std::string cssid;
|
||||
if (agree == true) {
|
||||
ret = _mysql_relation->insert(uid, pid);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{}- 新增好友关系信息-{}-{}!", rid, uid, pid);
|
||||
return err_response(rid, "新增好友关系信息!");
|
||||
}
|
||||
cssid = uuid();
|
||||
ChatSession cs(cssid, "", ChatSessionType::SINGLE);
|
||||
ret = _mysql_chat_session->insert(cs);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{}- 新增单聊会话信息-{}!", rid, cssid);
|
||||
return err_response(rid, "新增单聊会话信息失败!");
|
||||
}
|
||||
ChatSessionMember csm1(cssid, uid);
|
||||
ChatSessionMember csm2(cssid, pid);
|
||||
std::vector<ChatSessionMember> mlist = {csm1, csm2};
|
||||
ret = _mysql_chat_session_member->append(mlist);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{}- 没有找到{}-{}对应的好友申请事件!", rid, pid, uid);
|
||||
return err_response(rid, "没有找到对应的好友申请事件!");
|
||||
}
|
||||
}
|
||||
//5. 组织响应
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
response->set_new_session_id(cssid);
|
||||
}
|
||||
virtual void FriendSearch(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::FriendSearchReq* request,
|
||||
::bite_im::FriendSearchRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//1. 提取请求中的关键要素:搜索关键字(可能是用户ID,可能是手机号,可能是昵称的一部分)
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
std::string skey = request->search_key();
|
||||
LOG_DEBUG("{} 好友搜索 : {}", uid, skey);
|
||||
//2. 根据用户ID,获取用户的好友ID列表
|
||||
auto friend_id_lists = _mysql_relation->friends(uid);
|
||||
//3. 从ES搜索引擎进行用户信息搜索 --- 过滤掉当前的好友
|
||||
std::unordered_set<std::string> user_id_lists;
|
||||
friend_id_lists.push_back(uid);// 把自己也过滤掉
|
||||
auto search_res = _es_user->search(skey, friend_id_lists);
|
||||
for (auto &it : search_res) {
|
||||
user_id_lists.insert(it.user_id());
|
||||
}
|
||||
//4. 根据获取到的用户ID, 从用户子服务器进行批量用户信息获取
|
||||
std::unordered_map<std::string, UserInfo> user_list;
|
||||
bool ret = GetUserInfo(rid, user_id_lists, user_list);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 批量获取用户信息失败!", rid);
|
||||
return err_response(rid, "批量获取用户信息失败!");
|
||||
}
|
||||
//5. 组织响应
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
for (const auto & user_it : user_list) {
|
||||
auto user_info = response->add_user_info();
|
||||
user_info->CopyFrom(user_it.second);
|
||||
}
|
||||
}
|
||||
virtual void GetPendingFriendEventList(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::GetPendingFriendEventListReq* request,
|
||||
::bite_im::GetPendingFriendEventListRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//1. 提取关键要素:当前用户ID
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
//2. 从数据库获取待处理的申请事件信息 --- 申请人用户ID列表
|
||||
auto res = _mysql_apply->applyUsers(uid);
|
||||
std::unordered_set<std::string> user_id_lists;
|
||||
for (auto &id : res) {
|
||||
user_id_lists.insert(id);
|
||||
}
|
||||
//3. 批量获取申请人用户信息、
|
||||
std::unordered_map<std::string, UserInfo> user_list;
|
||||
bool ret = GetUserInfo(rid, user_id_lists, user_list);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 批量获取用户信息失败!", rid);
|
||||
return err_response(rid, "批量获取用户信息失败!");
|
||||
}
|
||||
//4. 组织响应
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
for (const auto & user_it : user_list) {
|
||||
auto ev = response->add_event();
|
||||
ev->mutable_sender()->CopyFrom(user_it.second);
|
||||
}
|
||||
}
|
||||
virtual void GetChatSessionList(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::GetChatSessionListReq* request,
|
||||
::bite_im::GetChatSessionListRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//获取聊天会话的作用:一个用户登录成功后,能够展示自己的历史聊天信息
|
||||
//1. 提取请求中的关键要素:当前请求用户ID
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
//2. 从数据库中查询出用户的单聊会话列表
|
||||
auto sf_list = _mysql_chat_session->singleChatSession(uid);
|
||||
// 1. 从单聊会话列表中,取出所有的好友ID,从用户子服务获取用户信息
|
||||
std::unordered_set<std::string> users_id_list;
|
||||
for (const auto &f : sf_list) {
|
||||
users_id_list.insert(f.friend_id);
|
||||
}
|
||||
std::unordered_map<std::string, UserInfo> user_list;
|
||||
bool ret = GetUserInfo(rid, users_id_list, user_list);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 批量获取用户信息失败!", rid);
|
||||
return err_response(rid, "批量获取用户信息失败!");
|
||||
}
|
||||
// 2. 设置响应会话信息:会话名称就是好友名称;会话头像就是好友头像
|
||||
//3. 从数据库中查询出用户的群聊会话列表
|
||||
auto gc_list = _mysql_chat_session->groupChatSession(uid);
|
||||
|
||||
//4. 根据所有的会话ID,从消息存储子服务获取会话最后一条消息
|
||||
//5. 组织响应
|
||||
for (const auto &f : sf_list) {
|
||||
auto chat_session_info = response->add_chat_session_info_list();
|
||||
chat_session_info->set_single_chat_friend_id(f.friend_id);
|
||||
chat_session_info->set_chat_session_id(f.chat_session_id);
|
||||
chat_session_info->set_chat_session_name(user_list[f.friend_id].nickname());
|
||||
chat_session_info->set_avatar(user_list[f.friend_id].avatar());
|
||||
MessageInfo msg;
|
||||
ret = GetRecentMsg(rid, f.chat_session_id, msg);
|
||||
if (ret == false) {continue;}
|
||||
chat_session_info->mutable_prev_message()->CopyFrom(msg);
|
||||
}
|
||||
for (const auto &f : gc_list) {
|
||||
auto chat_session_info = response->add_chat_session_info_list();
|
||||
chat_session_info->set_chat_session_id(f.chat_session_id);
|
||||
chat_session_info->set_chat_session_name(f.chat_session_name);
|
||||
MessageInfo msg;
|
||||
ret = GetRecentMsg(rid, f.chat_session_id, msg);
|
||||
if (ret == false) { continue; }
|
||||
chat_session_info->mutable_prev_message()->CopyFrom(msg);
|
||||
}
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
}
|
||||
virtual void ChatSessionCreate(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::ChatSessionCreateReq* request,
|
||||
::bite_im::ChatSessionCreateRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//创建会话,其实针对的是用户要创建一个群聊会话
|
||||
//1. 提取请求关键要素:会话名称,会话成员
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
std::string cssname = request->chat_session_name();
|
||||
|
||||
//2. 生成会话ID,向数据库添加会话信息,添加会话成员信息
|
||||
std::string cssid = uuid();
|
||||
ChatSession cs(cssid, cssname, ChatSessionType::GROUP);
|
||||
bool ret = _mysql_chat_session->insert(cs);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 向数据库添加会话信息失败: {}", rid, cssname);
|
||||
return err_response(rid, "向数据库添加会话信息失败!");
|
||||
}
|
||||
std::vector<ChatSessionMember> member_list;
|
||||
for (int i = 0; i < request->member_id_list_size(); i++) {
|
||||
ChatSessionMember csm(cssid, request->member_id_list(i));
|
||||
member_list.push_back(csm);
|
||||
}
|
||||
ret = _mysql_chat_session_member->append(member_list);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 向数据库添加会话成员信息失败: {}", rid, cssname);
|
||||
return err_response(rid, "向数据库添加会话成员信息失败!");
|
||||
}
|
||||
//3. 组织响应---组织会话信息
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
response->mutable_chat_session_info()->set_chat_session_id(cssid);
|
||||
response->mutable_chat_session_info()->set_chat_session_name(cssname);
|
||||
}
|
||||
virtual void GetChatSessionMember(::google::protobuf::RpcController* controller,
|
||||
const ::bite_im::GetChatSessionMemberReq* request,
|
||||
::bite_im::GetChatSessionMemberRsp* response,
|
||||
::google::protobuf::Closure* done) {
|
||||
brpc::ClosureGuard rpc_guard(done);
|
||||
//1. 定义错误回调
|
||||
auto err_response = [this, response](const std::string &rid,
|
||||
const std::string &errmsg) -> void {
|
||||
response->set_request_id(rid);
|
||||
response->set_success(false);
|
||||
response->set_errmsg(errmsg);
|
||||
return;
|
||||
};
|
||||
//用于用户查看群聊成员信息的时候:进行成员信息展示
|
||||
//1. 提取关键要素:聊天会话ID
|
||||
std::string rid = request->request_id();
|
||||
std::string uid = request->user_id();
|
||||
std::string cssid = request->chat_session_id();
|
||||
//2. 从数据库获取会话成员ID列表
|
||||
auto member_id_lists = _mysql_chat_session_member->members(cssid);
|
||||
std::unordered_set<std::string> uid_list;
|
||||
for (const auto &id : member_id_lists) {
|
||||
uid_list.insert(id);
|
||||
}
|
||||
//3. 从用户子服务批量获取用户信息
|
||||
std::unordered_map<std::string, UserInfo> user_list;
|
||||
bool ret = GetUserInfo(rid, uid_list, user_list);
|
||||
if (ret == false) {
|
||||
LOG_ERROR("{} - 从用户子服务获取用户信息失败!", rid);
|
||||
return err_response(rid, "从用户子服务获取用户信息失败!");
|
||||
}
|
||||
//4. 组织响应
|
||||
response->set_request_id(rid);
|
||||
response->set_success(true);
|
||||
for (const auto &uit : user_list) {
|
||||
auto user_info = response->add_member_info_list();
|
||||
user_info->CopyFrom(uit.second);
|
||||
}
|
||||
}
|
||||
private:
|
||||
bool GetRecentMsg(const std::string &rid,
|
||||
const std::string &cssid, MessageInfo &msg) {
|
||||
auto channel = _mm_channels->choose(_message_service_name);
|
||||
if (!channel) {
|
||||
LOG_ERROR("{} - 获取消息子服务信道失败!!", rid);
|
||||
return false;
|
||||
}
|
||||
GetRecentMsgReq req;
|
||||
GetRecentMsgRsp rsp;
|
||||
req.set_request_id(rid);
|
||||
req.set_chat_session_id(cssid);
|
||||
req.set_msg_count(1);
|
||||
brpc::Controller cntl;
|
||||
bite_im::MsgStorageService_Stub stub(channel.get());
|
||||
stub.GetRecentMsg(&cntl, &req, &rsp, nullptr);
|
||||
if (cntl.Failed() == true) {
|
||||
LOG_ERROR("{} - 消息存储子服务调用失败: {}", rid, cntl.ErrorText());
|
||||
return false;
|
||||
}
|
||||
if ( rsp.success() == false) {
|
||||
LOG_ERROR("{} - 获取会话 {} 最近消息失败: {}", rid, cssid, rsp.errmsg());
|
||||
return false;
|
||||
}
|
||||
if (rsp.msg_list_size() > 0) {
|
||||
msg.CopyFrom(rsp.msg_list(0));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool GetUserInfo(const std::string &rid,
|
||||
const std::unordered_set<std::string> &uid_list,
|
||||
std::unordered_map<std::string, UserInfo> &user_list) {
|
||||
auto channel = _mm_channels->choose(_user_service_name);
|
||||
if (!channel) {
|
||||
LOG_ERROR("{} - 获取用户子服务信道失败!!", rid);
|
||||
return false;
|
||||
}
|
||||
GetMultiUserInfoReq req;
|
||||
GetMultiUserInfoRsp rsp;
|
||||
req.set_request_id(rid);
|
||||
for (auto &id : uid_list) {
|
||||
req.add_users_id(id);
|
||||
}
|
||||
brpc::Controller cntl;
|
||||
bite_im::UserService_Stub stub(channel.get());
|
||||
stub.GetMultiUserInfo(&cntl, &req, &rsp, nullptr);
|
||||
if (cntl.Failed() == true) {
|
||||
LOG_ERROR("{} - 用户子服务调用失败: {}", rid, cntl.ErrorText());
|
||||
return false;
|
||||
}
|
||||
if ( rsp.success() == false) {
|
||||
LOG_ERROR("{} - 批量获取用户信息失败: {}", rid, rsp.errmsg());
|
||||
return false;
|
||||
}
|
||||
for (const auto & user_it : rsp.users_info()) {
|
||||
user_list.insert(std::make_pair(user_it.first, user_it.second));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
ESUser::ptr _es_user;
|
||||
|
||||
FriendApplyTable::ptr _mysql_apply;
|
||||
ChatSessionTable::ptr _mysql_chat_session;
|
||||
ChatSessionMemeberTable::ptr _mysql_chat_session_member;
|
||||
RelationTable::ptr _mysql_relation;
|
||||
|
||||
//这边是rpc调用客户端相关对象
|
||||
std::string _user_service_name;
|
||||
std::string _message_service_name;
|
||||
ServiceManager::ptr _mm_channels;
|
||||
};
|
||||
|
||||
class FriendServer {
|
||||
public:
|
||||
using ptr = std::shared_ptr<FriendServer>;
|
||||
FriendServer(const Discovery::ptr service_discoverer,
|
||||
const Registry::ptr ®_client,
|
||||
const std::shared_ptr<elasticlient::Client> &es_client,
|
||||
const std::shared_ptr<odb::core::database> &mysql_client,
|
||||
const std::shared_ptr<brpc::Server> &server):
|
||||
_service_discoverer(service_discoverer),
|
||||
_registry_client(reg_client),
|
||||
_es_client(es_client),
|
||||
_mysql_client(mysql_client),
|
||||
_rpc_server(server){}
|
||||
~FriendServer(){}
|
||||
//搭建RPC服务器,并启动服务器
|
||||
void start() {
|
||||
_rpc_server->RunUntilAskedToQuit();
|
||||
}
|
||||
private:
|
||||
Discovery::ptr _service_discoverer;
|
||||
Registry::ptr _registry_client;
|
||||
std::shared_ptr<elasticlient::Client> _es_client;
|
||||
std::shared_ptr<odb::core::database> _mysql_client;
|
||||
std::shared_ptr<brpc::Server> _rpc_server;
|
||||
};
|
||||
|
||||
class FriendServerBuilder {
|
||||
public:
|
||||
//构造es客户端对象
|
||||
void make_es_object(const std::vector<std::string> host_list) {
|
||||
_es_client = ESClientFactory::create(host_list);
|
||||
}
|
||||
//构造mysql客户端对象
|
||||
void make_mysql_object(
|
||||
const std::string &user,
|
||||
const std::string &pswd,
|
||||
const std::string &host,
|
||||
const std::string &db,
|
||||
const std::string &cset,
|
||||
int port,
|
||||
int conn_pool_count) {
|
||||
_mysql_client = ODBFactory::create(user, pswd, host, db, cset, port, conn_pool_count);
|
||||
}
|
||||
//用于构造服务发现客户端&信道管理对象
|
||||
void make_discovery_object(const std::string ®_host,
|
||||
const std::string &base_service_name,
|
||||
const std::string &user_service_name,
|
||||
const std::string &message_service_name) {
|
||||
_user_service_name = user_service_name;
|
||||
_message_service_name = message_service_name;
|
||||
_mm_channels = std::make_shared<ServiceManager>();
|
||||
_mm_channels->declared(user_service_name);
|
||||
_mm_channels->declared(message_service_name);
|
||||
LOG_DEBUG("设置用户子服务为需添加管理的子服务:{}", user_service_name);
|
||||
LOG_DEBUG("设置消息子服务为需添加管理的子服务:{}", message_service_name);
|
||||
auto put_cb = std::bind(&ServiceManager::onServiceOnline, _mm_channels.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
auto del_cb = std::bind(&ServiceManager::onServiceOffline, _mm_channels.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
_service_discoverer = std::make_shared<Discovery>(reg_host, base_service_name, put_cb, del_cb);
|
||||
}
|
||||
//用于构造服务注册客户端对象
|
||||
void make_registry_object(const std::string ®_host,
|
||||
const std::string &service_name,
|
||||
const std::string &access_host) {
|
||||
_registry_client = std::make_shared<Registry>(reg_host);
|
||||
_registry_client->registry(service_name, access_host);
|
||||
}
|
||||
void make_rpc_server(uint16_t port, int32_t timeout, uint8_t num_threads) {
|
||||
if (!_es_client) {
|
||||
LOG_ERROR("还未初始化ES搜索引擎模块!");
|
||||
abort();
|
||||
}
|
||||
if (!_mysql_client) {
|
||||
LOG_ERROR("还未初始化Mysql数据库模块!");
|
||||
abort();
|
||||
}
|
||||
if (!_mm_channels) {
|
||||
LOG_ERROR("还未初始化信道管理模块!");
|
||||
abort();
|
||||
}
|
||||
_rpc_server = std::make_shared<brpc::Server>();
|
||||
|
||||
FriendServiceImpl *friend_service = new FriendServiceImpl(_es_client,
|
||||
_mysql_client, _mm_channels, _user_service_name, _message_service_name);
|
||||
int ret = _rpc_server->AddService(friend_service,
|
||||
brpc::ServiceOwnership::SERVER_OWNS_SERVICE);
|
||||
if (ret == -1) {
|
||||
LOG_ERROR("添加Rpc服务失败!");
|
||||
abort();
|
||||
}
|
||||
brpc::ServerOptions options;
|
||||
options.idle_timeout_sec = timeout;
|
||||
options.num_threads = num_threads;
|
||||
ret = _rpc_server->Start(port, &options);
|
||||
if (ret == -1) {
|
||||
LOG_ERROR("服务启动失败!");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
//构造RPC服务器对象
|
||||
FriendServer::ptr build() {
|
||||
if (!_service_discoverer) {
|
||||
LOG_ERROR("还未初始化服务发现模块!");
|
||||
abort();
|
||||
}
|
||||
if (!_registry_client) {
|
||||
LOG_ERROR("还未初始化服务注册模块!");
|
||||
abort();
|
||||
}
|
||||
if (!_rpc_server) {
|
||||
LOG_ERROR("还未初始化RPC服务器模块!");
|
||||
abort();
|
||||
}
|
||||
FriendServer::ptr server = std::make_shared<FriendServer>(
|
||||
_service_discoverer, _registry_client,
|
||||
_es_client, _mysql_client, _rpc_server);
|
||||
return server;
|
||||
}
|
||||
private:
|
||||
Registry::ptr _registry_client;
|
||||
|
||||
std::shared_ptr<elasticlient::Client> _es_client;
|
||||
std::shared_ptr<odb::core::database> _mysql_client;
|
||||
|
||||
std::string _user_service_name;
|
||||
std::string _message_service_name;
|
||||
ServiceManager::ptr _mm_channels;
|
||||
Discovery::ptr _service_discoverer;
|
||||
|
||||
std::shared_ptr<brpc::Server> _rpc_server;
|
||||
};
|
||||
}
|
||||
286
friend/test/friend_client.cc
Normal file
286
friend/test/friend_client.cc
Normal file
@ -0,0 +1,286 @@
|
||||
#include "etcd.hpp"
|
||||
#include "channel.hpp"
|
||||
#include "utils.hpp"
|
||||
#include <gflags/gflags.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <thread>
|
||||
#include "friend.pb.h"
|
||||
|
||||
|
||||
DEFINE_bool(run_mode, false, "程序的运行模式,false-调试; true-发布;");
|
||||
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
|
||||
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");
|
||||
|
||||
DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
|
||||
DEFINE_string(base_service, "/service", "服务监控根目录");
|
||||
DEFINE_string(friend_service, "/service/friend_service", "服务监控根目录");
|
||||
|
||||
bite_im::ServiceManager::ptr sm;
|
||||
|
||||
void apply_test(const std::string &uid1, const std::string &uid2) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendAddReq req;
|
||||
bite_im::FriendAddRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_respondent_id(uid2);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendAdd(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
}
|
||||
|
||||
void get_apply_list(const std::string &uid1) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetPendingFriendEventListReq req;
|
||||
bite_im::GetPendingFriendEventListRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
brpc::Controller cntl;
|
||||
stub.GetPendingFriendEventList(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.event_size(); i++) {
|
||||
std::cout << "---------------\n";
|
||||
std::cout << rsp.event(i).sender().user_id() << std::endl;
|
||||
std::cout << rsp.event(i).sender().nickname() << std::endl;
|
||||
std::cout << rsp.event(i).sender().avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void process_apply_test(const std::string &uid1, bool agree, const std::string &apply_user_id) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendAddProcessReq req;
|
||||
bite_im::FriendAddProcessRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_agree(agree);
|
||||
req.set_apply_user_id(apply_user_id);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendAddProcess(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
if (agree) {
|
||||
std::cout << rsp.new_session_id() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void search_test(const std::string &uid1, const std::string &key) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendSearchReq req;
|
||||
bite_im::FriendSearchRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_search_key(key);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendSearch(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.user_info_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.user_info(i).user_id() << std::endl;
|
||||
std::cout << rsp.user_info(i).nickname() << std::endl;
|
||||
std::cout << rsp.user_info(i).avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void friend_list_test(const std::string &uid1) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetFriendListReq req;
|
||||
bite_im::GetFriendListRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
brpc::Controller cntl;
|
||||
stub.GetFriendList(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.friend_list_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.friend_list(i).user_id() << std::endl;
|
||||
std::cout << rsp.friend_list(i).nickname() << std::endl;
|
||||
std::cout << rsp.friend_list(i).avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void remove_test(const std::string &uid1, const std::string &uid2) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::FriendRemoveReq req;
|
||||
bite_im::FriendRemoveRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_peer_id(uid2);
|
||||
brpc::Controller cntl;
|
||||
stub.FriendRemove(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
}
|
||||
void create_css_test(const std::string &uid1, const std::vector<std::string> &uidlist) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::ChatSessionCreateReq req;
|
||||
bite_im::ChatSessionCreateRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_chat_session_name("快乐一家人");
|
||||
for (auto &id : uidlist) {
|
||||
req.add_member_id_list(id);
|
||||
}
|
||||
brpc::Controller cntl;
|
||||
stub.ChatSessionCreate(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
std::cout << rsp.chat_session_info().chat_session_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info().chat_session_name() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void cssmember_test(const std::string &uid1, const std::string &cssid) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetChatSessionMemberReq req;
|
||||
bite_im::GetChatSessionMemberRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
req.set_chat_session_id(cssid);
|
||||
brpc::Controller cntl;
|
||||
stub.GetChatSessionMember(&cntl, &req, &rsp, nullptr);
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
ASSERT_TRUE(rsp.success());
|
||||
for (int i = 0; i < rsp.member_info_list_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.member_info_list(i).user_id() << std::endl;
|
||||
std::cout << rsp.member_info_list(i).nickname() << std::endl;
|
||||
std::cout << rsp.member_info_list(i).avatar() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void csslist_test(const std::string &uid1) {
|
||||
auto channel = sm->choose(FLAGS_friend_service);
|
||||
if (!channel) {
|
||||
std::cout << "获取通信信道失败!" << std::endl;
|
||||
return;
|
||||
}
|
||||
bite_im::FriendService_Stub stub(channel.get());
|
||||
bite_im::GetChatSessionListReq req;
|
||||
bite_im::GetChatSessionListRsp rsp;
|
||||
req.set_request_id(bite_im::uuid());
|
||||
req.set_user_id(uid1);
|
||||
brpc::Controller cntl;
|
||||
std::cout << "发送获取聊天会话列表请求!!\n";
|
||||
stub.GetChatSessionList(&cntl, &req, &rsp, nullptr);
|
||||
std::cout << "请求发送完毕1!!\n";
|
||||
ASSERT_FALSE(cntl.Failed());
|
||||
std::cout << "请求发送完毕2!!\n";
|
||||
ASSERT_TRUE(rsp.success());
|
||||
std::cout << "请求发送完毕,且成功!!\n";
|
||||
for (int i = 0; i < rsp.chat_session_info_list_size(); i++) {
|
||||
std::cout << "-------------------\n";
|
||||
std::cout << rsp.chat_session_info_list(i).single_chat_friend_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).chat_session_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).chat_session_name() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).avatar() << std::endl;
|
||||
std::cout << "消息内容:\n";
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().message_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().chat_session_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().timestamp() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().sender().user_id() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().sender().nickname() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().sender().avatar() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().message().file_message().file_name() << std::endl;
|
||||
std::cout << rsp.chat_session_info_list(i).prev_message().message().file_message().file_contents() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
bite_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
|
||||
|
||||
|
||||
//1. 先构造Rpc信道管理对象
|
||||
sm = std::make_shared<bite_im::ServiceManager>();
|
||||
sm->declared(FLAGS_friend_service);
|
||||
auto put_cb = std::bind(&bite_im::ServiceManager::onServiceOnline, sm.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
auto del_cb = std::bind(&bite_im::ServiceManager::onServiceOffline, sm.get(), std::placeholders::_1, std::placeholders::_2);
|
||||
//2. 构造服务发现对象
|
||||
bite_im::Discovery::ptr dclient = std::make_shared<bite_im::Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
|
||||
|
||||
|
||||
// apply_test("ee55-9043bfd7-0001", "672f-c755e83e-0000");
|
||||
// apply_test("67b1-35ca1b76-0000", "672f-c755e83e-0000");
|
||||
// apply_test("d9ff-65692d4a-0001", "672f-c755e83e-0000");
|
||||
|
||||
// get_apply_list("672f-c755e83e-0000");
|
||||
|
||||
// process_apply_test("672f-c755e83e-0000", true, "ee55-9043bfd7-0001");
|
||||
// process_apply_test("672f-c755e83e-0000", false, "67b1-35ca1b76-0000");
|
||||
// process_apply_test("672f-c755e83e-0000", true, "d9ff-65692d4a-0001");
|
||||
|
||||
// std::cout << "**********************\n";
|
||||
// search_test("672f-c755e83e-0000", "猪");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// search_test("ee55-9043bfd7-0001", "猪");
|
||||
// std::cout << "======================\n";
|
||||
// search_test("67b1-35ca1b76-0000", "乔治");
|
||||
|
||||
// friend_list_test("c4dc-68239a9a-0001");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// friend_list_test("731f-50086884-0000");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// friend_list_test("31ab-86a1209d-0000");
|
||||
|
||||
// remove_test("c4dc-68239a9a-0001", "053f-04e5e4c5-0001");
|
||||
|
||||
// std::vector<std::string> uidlist = {
|
||||
// "731f-50086884-0000",
|
||||
// "c4dc-68239a9a-0001",
|
||||
// "31ab-86a1209d-0000",
|
||||
// "053f-04e5e4c5-0001"};
|
||||
// create_css_test("731f-50086884-0000", uidlist);
|
||||
// cssmember_test("731f-50086884-0000", "36b5-edaf4987-0000");
|
||||
// std::cout << "++++++++++++++++++++++\n";
|
||||
// cssmember_test("c4dc-68239a9a-0001", "36b5-edaf4987-0000");
|
||||
|
||||
// csslist_test("c4dc-68239a9a-0001");
|
||||
return 0;
|
||||
}
|
||||
121
friend/test/mysql_test/main.cc
Normal file
121
friend/test/mysql_test/main.cc
Normal file
@ -0,0 +1,121 @@
|
||||
#include "mysql_chat_session.hpp"
|
||||
#include "mysql_apply.hpp"
|
||||
#include "mysql_relation.hpp"
|
||||
#include <gflags/gflags.h>
|
||||
|
||||
DEFINE_bool(run_mode, false, "程序的运行模式,false-调试; true-发布;");
|
||||
DEFINE_string(log_file, "", "发布模式下,用于指定日志的输出文件");
|
||||
DEFINE_int32(log_level, 0, "发布模式下,用于指定日志输出等级");
|
||||
|
||||
void r_insert_test(bite_im::RelationTable &tb) {
|
||||
tb.insert("用户ID1", "用户ID2");
|
||||
tb.insert("用户ID1", "用户ID3");
|
||||
}
|
||||
void r_select_test(bite_im::RelationTable &tb) {
|
||||
auto res = tb.friends("用户ID1");
|
||||
for (auto &uid:res) {
|
||||
std::cout << uid << std::endl;
|
||||
}
|
||||
}
|
||||
void r_remove_test(bite_im::RelationTable &tb) {
|
||||
tb.remove("用户ID2", "用户ID1");
|
||||
}
|
||||
|
||||
void r_exists_test(bite_im::RelationTable &tb) {
|
||||
std::cout << tb.exists("用户ID2", "用户ID1") << std::endl;
|
||||
std::cout << tb.exists("用户ID3", "用户ID1") << std::endl;
|
||||
}
|
||||
|
||||
void a_insert_test(bite_im::FriendApplyTable &tb) {
|
||||
bite_im::FriendApply fa1("uuid1", "用户ID1", "用户ID2");
|
||||
tb.insert(fa1);
|
||||
|
||||
bite_im::FriendApply fa2("uuid2", "用户ID1", "用户ID3");
|
||||
tb.insert(fa2);
|
||||
|
||||
bite_im::FriendApply fa3("uuid3", "用户ID2", "用户ID3");
|
||||
tb.insert(fa3);
|
||||
}
|
||||
void a_remove_test(bite_im::FriendApplyTable &tb) {
|
||||
tb.remove("用户ID2", "用户ID3");
|
||||
}
|
||||
|
||||
void a_select_test(bite_im::FriendApplyTable &tb) {
|
||||
// bite_im::FriendApply fa3("uuid3", "用户ID2", "用户ID3");
|
||||
// tb.insert(fa3);
|
||||
|
||||
auto res = tb.applyUsers("用户ID2");
|
||||
for (auto &uid:res) {
|
||||
std::cout << uid << std::endl;
|
||||
}
|
||||
}
|
||||
void a_exists_test(bite_im::FriendApplyTable &tb) {
|
||||
std::cout << tb.exists("用户ID1", "用户ID2") << std::endl;
|
||||
std::cout << tb.exists("31ab-86a1209d-0000", "c4dc-68239a9a-0001") << std::endl;
|
||||
std::cout << tb.exists("053f-04e5e4c5-0001", "c4dc-68239a9a-0001") << std::endl;
|
||||
}
|
||||
|
||||
void c_insert_test(bite_im::ChatSessionTable &tb) {
|
||||
bite_im::ChatSession cs1("会话ID1", "会话名称1", bite_im::ChatSessionType::SINGLE);
|
||||
tb.insert(cs1);
|
||||
bite_im::ChatSession cs2("会话ID2", "会话名称2", bite_im::ChatSessionType::GROUP);
|
||||
tb.insert(cs2);
|
||||
}
|
||||
|
||||
|
||||
void c_select_test(bite_im::ChatSessionTable &tb) {
|
||||
auto res = tb.select("会话ID1");
|
||||
std::cout << res->chat_session_id() << std::endl;
|
||||
std::cout << res->chat_session_name() << std::endl;
|
||||
std::cout << (int)res->chat_session_type() << std::endl;
|
||||
}
|
||||
|
||||
void c_single_test(bite_im::ChatSessionTable &tb) {
|
||||
auto res = tb.singleChatSession("731f-50086884-0000");
|
||||
for (auto &info : res) {
|
||||
std::cout << info.chat_session_id << std::endl;
|
||||
std::cout << info.friend_id << std::endl;
|
||||
}
|
||||
}
|
||||
void c_group_test(bite_im::ChatSessionTable &tb) {
|
||||
auto res = tb.groupChatSession("用户ID1");
|
||||
for (auto &info : res) {
|
||||
std::cout << info.chat_session_id << std::endl;
|
||||
std::cout << info.chat_session_name << std::endl;
|
||||
}
|
||||
}
|
||||
void c_remove_test(bite_im::ChatSessionTable &tb) {
|
||||
tb.remove("会话ID3");
|
||||
}
|
||||
void c_remove_test2(bite_im::ChatSessionTable &tb) {
|
||||
tb.remove("731f-50086884-0000", "c4dc-68239a9a-0001");
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
google::ParseCommandLineFlags(&argc, &argv, true);
|
||||
bite_im::init_logger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);
|
||||
|
||||
auto db = bite_im::ODBFactory::create("root", "123456", "127.0.0.1", "bite_im", "utf8", 0, 1);
|
||||
bite_im::RelationTable rtb(db);
|
||||
bite_im::FriendApplyTable fatb(db);
|
||||
bite_im::ChatSessionTable cstb(db);
|
||||
|
||||
// r_insert_test(rtb);
|
||||
// r_select_test(rtb);
|
||||
// r_remove_test(rtb);
|
||||
// r_exists_test(rtb);
|
||||
// a_insert_test(fatb);
|
||||
// a_remove_test(fatb);
|
||||
// a_select_test(fatb);
|
||||
// a_exists_test(fatb);
|
||||
// c_insert_test(cstb);
|
||||
// c_select_test(cstb);
|
||||
// c_single_test(cstb);
|
||||
// std::cout << "--------------\n";
|
||||
// c_group_test(cstb);
|
||||
// c_remove_test(cstb);
|
||||
// c_remove_test2(cstb);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user