Skip to content

Commit b5063d5

Browse files
authored
Merge pull request #21 from lkpworkspace/dev
v0.8.1
2 parents 3ff7ec6 + ba1f466 commit b5063d5

25 files changed

+920
-453
lines changed

.github/workflows/linux.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- uses: actions/checkout@v3
2020
- run: sudo apt update
2121
- run: sudo apt install build-essential cmake ninja-build
22-
- run: sudo apt install libjsoncpp-dev libgflags-dev libgtest-dev libgoogle-glog-dev libunwind-dev
22+
- run: sudo apt install libjsoncpp-dev libgflags-dev libgoogle-glog-dev libunwind-dev
2323

2424
- name: Setup Ninja
2525
uses: ashutoshvarma/setup-ninja@master

CMakeLists.txt

+46-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(myframe)
2+
project(myframe VERSION 0.8.1)
33

44
### gcc version
55
if (CMAKE_COMPILER_IS_GNUCC)
@@ -40,7 +40,6 @@ set(MYFRAME_CONF_DIR "conf")
4040
### deps libs
4141
find_package(jsoncpp REQUIRED)
4242
find_package(gflags REQUIRED)
43-
find_package(GTest REQUIRED)
4443

4544
link_libraries(
4645
pthread dl rt m
@@ -78,26 +77,52 @@ install(DIRECTORY tools DESTINATION .)
7877
install(DIRECTORY conf DESTINATION .)
7978
install(DIRECTORY DESTINATION ${MYFRAME_LOG_DIR})
8079
install(DIRECTORY DESTINATION ${MYFRAME_SERVICE_DIR})
81-
execute_process(
82-
COMMAND git rev-parse --abbrev-ref HEAD
83-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
84-
OUTPUT_VARIABLE GIT_BRANCH
85-
OUTPUT_STRIP_TRAILING_WHITESPACE
86-
)
87-
execute_process(
88-
COMMAND git rev-parse HEAD
89-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
90-
OUTPUT_VARIABLE GIT_COMMIT_ID
91-
OUTPUT_STRIP_TRAILING_WHITESPACE
92-
)
93-
execute_process(
94-
COMMAND git show -s --format=%ci HEAD
95-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
96-
OUTPUT_VARIABLE GIT_COMMIT_DATE
97-
OUTPUT_STRIP_TRAILING_WHITESPACE
98-
)
9980
install(CODE "
10081
file(
10182
WRITE ${CMAKE_INSTALL_PREFIX}/version.txt
102-
\"${GIT_BRANCH}\n${GIT_COMMIT_ID}\n${GIT_COMMIT_DATE}\n\")
83+
\"${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}\"
84+
)
10385
")
86+
87+
### package
88+
include(InstallRequiredSystemLibraries)
89+
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
90+
set(CPACK_PACKAGE_VERSION_MAJOR "${${PROJECT_NAME}_VERSION_MAJOR}")
91+
set(CPACK_PACKAGE_VERSION_MINOR "${${PROJECT_NAME}_VERSION_MINOR}")
92+
set(CPACK_PACKAGE_VERSION_PATCH "${${PROJECT_NAME}_VERSION_PATCH}")
93+
include(CPack)
94+
95+
### export cmake file
96+
install(EXPORT "${PROJECT_NAME}Targets"
97+
FILE "${PROJECT_NAME}Targets.cmake"
98+
DESTINATION lib/cmake/${PROJECT_NAME}
99+
)
100+
101+
include(CMakePackageConfigHelpers)
102+
# generate the config file that is includes the exports
103+
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
104+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
105+
INSTALL_DESTINATION "lib/cmake/${PROJECT_NAME}"
106+
NO_SET_AND_CHECK_MACRO
107+
NO_CHECK_REQUIRED_COMPONENTS_MACRO
108+
)
109+
110+
# generate the version file for the config file
111+
write_basic_package_version_file(
112+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
113+
VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}"
114+
COMPATIBILITY AnyNewerVersion
115+
)
116+
117+
# install the configuration file
118+
install(FILES
119+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
120+
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
121+
DESTINATION lib/cmake/${PROJECT_NAME}
122+
)
123+
124+
# generate the export targets for the build tree
125+
# needs to be after the install(TARGETS ) command
126+
export(EXPORT "${PROJECT_NAME}Targets"
127+
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake"
128+
)

Config.cmake.in

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
@PACKAGE_INIT@
3+
4+
include ( "${CMAKE_CURRENT_LIST_DIR}/myframeTargets.cmake" )

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 李柯鹏 <[email protected]>
3+
Copyright (c) 2019 李柯鹏 <[email protected]>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

doc/version_release.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 版本发布
2+
* 修改CMakeLists.txt中版本号
3+
* 提交修改
4+
* 合入master
5+
* 生成版本tag并发布
6+
* 发布tag中详细描述版本更新内容

examples/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ add_library(example_actor_serial SHARED example_actor_serial.cpp)
99
target_link_libraries(example_actor_serial ${PROJECT_NAME})
1010
add_library(example_actor_concurrent SHARED example_actor_concurrent.cpp)
1111
target_link_libraries(example_actor_concurrent ${PROJECT_NAME})
12+
add_library(example_actor_subscribe SHARED example_actor_subscribe.cpp)
13+
target_link_libraries(example_actor_subscribe ${PROJECT_NAME})
1214

1315
### worker
1416
add_library(example_worker_publish SHARED example_worker_publish.cpp)
@@ -39,6 +41,7 @@ INSTALL(TARGETS
3941
example_actor_timer
4042
example_actor_serial
4143
example_actor_concurrent
44+
example_actor_subscribe
4245
example_worker_actor_interactive
4346
example_worker_publish
4447
example_worker_talk

examples/example_actor_subscribe.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/****************************************************************************
2+
Copyright (c) 2018, likepeng
3+
All rights reserved.
4+
5+
Author: likepeng <[email protected]>
6+
****************************************************************************/
7+
#include <chrono>
8+
#include <random>
9+
#include <thread>
10+
#include <unordered_map>
11+
12+
#include <glog/logging.h>
13+
14+
#include "myframe/msg.h"
15+
#include "myframe/actor.h"
16+
17+
class ExampleActorPub : public myframe::Actor {
18+
public:
19+
int Init(const char* param) override {
20+
(void)param;
21+
Timeout("1000ms", 100);
22+
return 0;
23+
}
24+
25+
void Proc(const std::shared_ptr<const myframe::Msg>& msg) override {
26+
if (msg->GetType() == "SUBSCRIBE") {
27+
pub_list_.push_back(msg->GetSrc());
28+
return;
29+
}
30+
if (msg->GetType() == "TIMER") {
31+
auto mailbox = GetMailbox();
32+
for (size_t i = 0; i < pub_list_.size(); ++i) {
33+
mailbox->Send(pub_list_[i],
34+
std::make_shared<myframe::Msg>("pub msg"));
35+
}
36+
Timeout("1000ms", 100);
37+
}
38+
}
39+
private:
40+
std::vector<std::string> pub_list_;
41+
};
42+
43+
class ExampleActorSub : public myframe::Actor {
44+
public:
45+
int Init(const char* param) override {
46+
(void)param;
47+
Subscribe("actor.example_b_pub.#1");
48+
return 0;
49+
}
50+
51+
void Proc(const std::shared_ptr<const myframe::Msg>& msg) override {
52+
LOG(INFO) << "-----> get pub msg: " << *msg;
53+
}
54+
};
55+
56+
extern "C" std::shared_ptr<myframe::Actor> actor_create(
57+
const std::string& actor_name) {
58+
if (actor_name == "example_b_pub") {
59+
return std::make_shared<ExampleActorPub>();
60+
}
61+
if (actor_name == "example_a_sub") {
62+
return std::make_shared<ExampleActorSub>();
63+
}
64+
return nullptr;
65+
}

examples/example_actor_subscribe.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"type":"library",
3+
"lib":"libexample_actor_subscribe.so",
4+
"actor":{
5+
"example_b_pub":[
6+
{
7+
"instance_name":"#1",
8+
"instance_params":""
9+
}
10+
],
11+
"example_a_sub":[
12+
{
13+
"instance_name":"#1",
14+
"instance_params":""
15+
}
16+
]
17+
}
18+
}

myframe/CMakeLists.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ aux_source_directory(. __srcs)
55

66
### lib
77
add_library(${PROJECT_NAME} SHARED ${__srcs})
8+
target_include_directories(${PROJECT_NAME}
9+
INTERFACE
10+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
11+
$<INSTALL_INTERFACE:include>
12+
)
813

914
### install
1015
file(GLOB header_files
@@ -26,7 +31,6 @@ install(FILES
2631
DESTINATION ${MYFRAME_INC_DIR}/${PROJECT_NAME}
2732
)
2833
install(TARGETS ${PROJECT_NAME}
29-
LIBRARY DESTINATION ${MYFRAME_LIB_DIR}
30-
ARCHIVE DESTINATION ${MYFRAME_LIB_DIR}
31-
RUNTIME DESTINATION ${MYFRAME_BIN_DIR}
34+
DESTINATION lib
35+
EXPORT "${PROJECT_NAME}Targets"
3236
)

myframe/actor.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Author: likepeng <[email protected]>
1212
#include "myframe/app.h"
1313
#include "myframe/actor_context.h"
1414
#include "myframe/worker_timer.h"
15+
#include "myframe/mailbox.h"
1516

1617
namespace myframe {
1718

@@ -71,6 +72,21 @@ int Actor::Timeout(const std::string& timer_name, int expired) {
7172
return timer_worker->SetTimeout(GetActorName(), timer_name, expired);
7273
}
7374

75+
bool Actor::Subscribe(const std::string& name) {
76+
auto ctx = ctx_.lock();
77+
if (ctx == nullptr) {
78+
return false;
79+
}
80+
if (name == GetActorName()) {
81+
return false;
82+
}
83+
auto msg = std::make_shared<Msg>();
84+
msg->SetType("SUBSCRIBE");
85+
auto mailbox = ctx->GetMailbox();
86+
mailbox->Send(name, msg);
87+
return true;
88+
}
89+
7490
void Actor::SetContext(std::shared_ptr<ActorContext> c) { ctx_ = c; }
7591

7692
const Json::Value* Actor::GetConfig() const {

myframe/actor.h

+11
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ class Actor {
8585
*/
8686
int Timeout(const std::string& timer_name, int expired);
8787

88+
/**
89+
* Subscribe() - 订阅actor或者worker的消息
90+
* @name: 订阅actor或者worker的名称
91+
*
92+
* 被订阅的组件需要处理订阅消息,消息格式:
93+
* msg->GetType() == "SUBSCRIBE" 确认是订阅消息
94+
* msg->GetSrc() 确定是订阅组件名称
95+
* @return: 成功返回true,失败返回false
96+
*/
97+
bool Subscribe(const std::string& name);
98+
8899
/**
89100
* GetApp() - 获得应用实例
90101
*

myframe/app.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,34 @@ bool App::CreateActorContext(
355355
bool App::CreateActorContext(
356356
std::shared_ptr<Actor> mod_inst,
357357
const std::string& params) {
358+
auto actor_name = mod_inst->GetActorName();
358359
auto ctx = std::make_shared<ActorContext>(shared_from_this(), mod_inst);
359360
if (ctx->Init(params.c_str())) {
360-
LOG(ERROR) << "init " << mod_inst->GetActorName() << " fail";
361+
LOG(ERROR) << "init " << actor_name << " fail";
361362
return false;
362363
}
363364
actor_ctx_mgr_->RegContext(ctx);
365+
// 接收缓存中发给自己的消息
366+
if (cache_msg_.find(actor_name) != cache_msg_.end()) {
367+
auto& cache_list = cache_msg_[actor_name];
368+
LOG(INFO) << actor_name
369+
<< " recv msg from cache, size " << cache_list.size();
370+
DispatchMsg(&cache_list);
371+
cache_msg_.erase(actor_name);
372+
}
373+
// 目的地址不存在的暂时放到缓存消息队列
374+
auto send_list = ctx->GetMailbox()->GetSendList();
375+
for (auto it = send_list->begin(); it != send_list->end();) {
376+
if (!HasUserInst((*it)->GetDst())) {
377+
LOG(WARNING) << "can't found " << (*it)->GetDst()
378+
<< ", cache this msg";
379+
cache_msg_[(*it)->GetDst()].push_back(*it);
380+
it = send_list->erase(it);
381+
continue;
382+
}
383+
++it;
384+
}
385+
// 分发目的地址已经存在的消息
364386
DispatchMsg(ctx);
365387
return true;
366388
}
@@ -710,4 +732,19 @@ void App::Quit() {
710732
worker_ctx_mgr_->StopAllWorker();
711733
}
712734

735+
bool App::HasUserInst(const std::string& name) {
736+
if (name.substr(0, 6) == "worker") {
737+
auto worker_ctx = ev_mgr_->Get<WorkerContext>(name);
738+
if (worker_ctx != nullptr
739+
&& worker_ctx->GetType() == Event::Type::kWorkerUser) {
740+
return true;
741+
}
742+
} else if (name.substr(0, 5) == "actor") {
743+
if (actor_ctx_mgr_->HasActor(name)) {
744+
return true;
745+
}
746+
}
747+
return false;
748+
}
749+
713750
} // namespace myframe

myframe/app.h

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class App final : public std::enable_shared_from_this<App> {
9292
std::shared_ptr<Actor> inst,
9393
const std::string& params);
9494

95+
bool HasUserInst(const std::string& name);
9596
std::shared_ptr<WorkerTimer> GetTimerWorker();
9697

9798
bool LoadActors(
@@ -128,6 +129,10 @@ class App final : public std::enable_shared_from_this<App> {
128129
std::atomic_bool quit_{true};
129130
std::mutex dispatch_mtx_;
130131
std::mutex local_mtx_;
132+
/// 缓存消息列表
133+
std::unordered_map<
134+
std::string,
135+
std::list<std::shared_ptr<Msg>>> cache_msg_;
131136
/// poller
132137
std::unique_ptr<Poller> poller_;
133138
/// 模块管理对象

myframe/event_conn_manager.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ void EventConnManager::Notify(
7070
std::shared_ptr<EventConn> ev = nullptr;
7171
ev = ev_mgr_->Get<EventConn>(h);
7272
if (ev == nullptr) {
73+
LOG(ERROR) << "can't find handle " << h;
7374
return;
7475
}
7576
if (ev->GetConnType() == EventConn::Type::kSend) {
77+
LOG(WARNING) << "event " << ev->GetName() << " need't resp msg";
7678
return;
7779
}
7880
// push msg to event_conn

myframe/event_manager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Author: likepeng <[email protected]>
99

1010
#include <glog/logging.h>
1111

12-
#include <myframe/worker.h>
13-
#include <myframe/event_conn.h>
12+
#include "myframe/worker.h"
13+
#include "myframe/event_conn.h"
1414

1515
namespace myframe {
1616

templates/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ SET(MYFRAME_INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib)
1414
#### dep 3rd lib
1515
FIND_PACKAGE(jsoncpp REQUIRED)
1616
FIND_PACKAGE(gflags REQUIRED)
17-
FIND_PACKAGE(GTest REQUIRED)
1817

1918
#### include directory
2019
INCLUDE_DIRECTORIES(${CMAKE_INSTALL_PREFIX}/include)

0 commit comments

Comments
 (0)