Skip to content

Commit

Permalink
Merge pull request #21 from lkpworkspace/dev
Browse files Browse the repository at this point in the history
v0.8.1
  • Loading branch information
lkpworkspace authored Aug 17, 2023
2 parents 3ff7ec6 + ba1f466 commit b5063d5
Show file tree
Hide file tree
Showing 25 changed files with 920 additions and 453 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- uses: actions/checkout@v3
- run: sudo apt update
- run: sudo apt install build-essential cmake ninja-build
- run: sudo apt install libjsoncpp-dev libgflags-dev libgtest-dev libgoogle-glog-dev libunwind-dev
- run: sudo apt install libjsoncpp-dev libgflags-dev libgoogle-glog-dev libunwind-dev

- name: Setup Ninja
uses: ashutoshvarma/setup-ninja@master
Expand Down
67 changes: 46 additions & 21 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(myframe)
project(myframe VERSION 0.8.1)

### gcc version
if (CMAKE_COMPILER_IS_GNUCC)
Expand Down Expand Up @@ -40,7 +40,6 @@ set(MYFRAME_CONF_DIR "conf")
### deps libs
find_package(jsoncpp REQUIRED)
find_package(gflags REQUIRED)
find_package(GTest REQUIRED)

link_libraries(
pthread dl rt m
Expand Down Expand Up @@ -78,26 +77,52 @@ install(DIRECTORY tools DESTINATION .)
install(DIRECTORY conf DESTINATION .)
install(DIRECTORY DESTINATION ${MYFRAME_LOG_DIR})
install(DIRECTORY DESTINATION ${MYFRAME_SERVICE_DIR})
execute_process(
COMMAND git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND git rev-parse HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_ID
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND git show -s --format=%ci HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_DATE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
install(CODE "
file(
WRITE ${CMAKE_INSTALL_PREFIX}/version.txt
\"${GIT_BRANCH}\n${GIT_COMMIT_ID}\n${GIT_COMMIT_DATE}\n\")
\"${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}\"
)
")

### package
include(InstallRequiredSystemLibraries)
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_VERSION_MAJOR "${${PROJECT_NAME}_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${${PROJECT_NAME}_VERSION_MINOR}")
set(CPACK_PACKAGE_VERSION_PATCH "${${PROJECT_NAME}_VERSION_PATCH}")
include(CPack)

### export cmake file
install(EXPORT "${PROJECT_NAME}Targets"
FILE "${PROJECT_NAME}Targets.cmake"
DESTINATION lib/cmake/${PROJECT_NAME}
)

include(CMakePackageConfigHelpers)
# generate the config file that is includes the exports
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "lib/cmake/${PROJECT_NAME}"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)

# generate the version file for the config file
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION "${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}"
COMPATIBILITY AnyNewerVersion
)

# install the configuration file
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION lib/cmake/${PROJECT_NAME}
)

# generate the export targets for the build tree
# needs to be after the install(TARGETS ) command
export(EXPORT "${PROJECT_NAME}Targets"
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake"
)
4 changes: 4 additions & 0 deletions Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

@PACKAGE_INIT@

include ( "${CMAKE_CURRENT_LIST_DIR}/myframeTargets.cmake" )
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 李柯鹏 <likepeng0418@163.com>
Copyright (c) 2019 李柯鹏 <likepeng0418@163.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 6 additions & 0 deletions doc/version_release.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 版本发布
* 修改CMakeLists.txt中版本号
* 提交修改
* 合入master
* 生成版本tag并发布
* 发布tag中详细描述版本更新内容
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ add_library(example_actor_serial SHARED example_actor_serial.cpp)
target_link_libraries(example_actor_serial ${PROJECT_NAME})
add_library(example_actor_concurrent SHARED example_actor_concurrent.cpp)
target_link_libraries(example_actor_concurrent ${PROJECT_NAME})
add_library(example_actor_subscribe SHARED example_actor_subscribe.cpp)
target_link_libraries(example_actor_subscribe ${PROJECT_NAME})

### worker
add_library(example_worker_publish SHARED example_worker_publish.cpp)
Expand Down Expand Up @@ -39,6 +41,7 @@ INSTALL(TARGETS
example_actor_timer
example_actor_serial
example_actor_concurrent
example_actor_subscribe
example_worker_actor_interactive
example_worker_publish
example_worker_talk
Expand Down
65 changes: 65 additions & 0 deletions examples/example_actor_subscribe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/****************************************************************************
Copyright (c) 2018, likepeng
All rights reserved.
Author: likepeng <likepeng0418@163.com>
****************************************************************************/
#include <chrono>
#include <random>
#include <thread>
#include <unordered_map>

#include <glog/logging.h>

#include "myframe/msg.h"
#include "myframe/actor.h"

class ExampleActorPub : public myframe::Actor {
public:
int Init(const char* param) override {
(void)param;
Timeout("1000ms", 100);
return 0;
}

void Proc(const std::shared_ptr<const myframe::Msg>& msg) override {
if (msg->GetType() == "SUBSCRIBE") {
pub_list_.push_back(msg->GetSrc());
return;
}
if (msg->GetType() == "TIMER") {
auto mailbox = GetMailbox();
for (size_t i = 0; i < pub_list_.size(); ++i) {
mailbox->Send(pub_list_[i],
std::make_shared<myframe::Msg>("pub msg"));
}
Timeout("1000ms", 100);
}
}
private:
std::vector<std::string> pub_list_;
};

class ExampleActorSub : public myframe::Actor {
public:
int Init(const char* param) override {
(void)param;
Subscribe("actor.example_b_pub.#1");
return 0;
}

void Proc(const std::shared_ptr<const myframe::Msg>& msg) override {
LOG(INFO) << "-----> get pub msg: " << *msg;
}
};

extern "C" std::shared_ptr<myframe::Actor> actor_create(
const std::string& actor_name) {
if (actor_name == "example_b_pub") {
return std::make_shared<ExampleActorPub>();
}
if (actor_name == "example_a_sub") {
return std::make_shared<ExampleActorSub>();
}
return nullptr;
}
18 changes: 18 additions & 0 deletions examples/example_actor_subscribe.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"type":"library",
"lib":"libexample_actor_subscribe.so",
"actor":{
"example_b_pub":[
{
"instance_name":"#1",
"instance_params":""
}
],
"example_a_sub":[
{
"instance_name":"#1",
"instance_params":""
}
]
}
}
10 changes: 7 additions & 3 deletions myframe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ aux_source_directory(. __srcs)

### lib
add_library(${PROJECT_NAME} SHARED ${__srcs})
target_include_directories(${PROJECT_NAME}
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)

### install
file(GLOB header_files
Expand All @@ -26,7 +31,6 @@ install(FILES
DESTINATION ${MYFRAME_INC_DIR}/${PROJECT_NAME}
)
install(TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION ${MYFRAME_LIB_DIR}
ARCHIVE DESTINATION ${MYFRAME_LIB_DIR}
RUNTIME DESTINATION ${MYFRAME_BIN_DIR}
DESTINATION lib
EXPORT "${PROJECT_NAME}Targets"
)
16 changes: 16 additions & 0 deletions myframe/actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Author: likepeng <likepeng0418@163.com>
#include "myframe/app.h"
#include "myframe/actor_context.h"
#include "myframe/worker_timer.h"
#include "myframe/mailbox.h"

namespace myframe {

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

bool Actor::Subscribe(const std::string& name) {
auto ctx = ctx_.lock();
if (ctx == nullptr) {
return false;
}
if (name == GetActorName()) {
return false;
}
auto msg = std::make_shared<Msg>();
msg->SetType("SUBSCRIBE");
auto mailbox = ctx->GetMailbox();
mailbox->Send(name, msg);
return true;
}

void Actor::SetContext(std::shared_ptr<ActorContext> c) { ctx_ = c; }

const Json::Value* Actor::GetConfig() const {
Expand Down
11 changes: 11 additions & 0 deletions myframe/actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ class Actor {
*/
int Timeout(const std::string& timer_name, int expired);

/**
* Subscribe() - 订阅actor或者worker的消息
* @name: 订阅actor或者worker的名称
*
* 被订阅的组件需要处理订阅消息,消息格式:
* msg->GetType() == "SUBSCRIBE" 确认是订阅消息
* msg->GetSrc() 确定是订阅组件名称
* @return: 成功返回true,失败返回false
*/
bool Subscribe(const std::string& name);

/**
* GetApp() - 获得应用实例
*
Expand Down
39 changes: 38 additions & 1 deletion myframe/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,34 @@ bool App::CreateActorContext(
bool App::CreateActorContext(
std::shared_ptr<Actor> mod_inst,
const std::string& params) {
auto actor_name = mod_inst->GetActorName();
auto ctx = std::make_shared<ActorContext>(shared_from_this(), mod_inst);
if (ctx->Init(params.c_str())) {
LOG(ERROR) << "init " << mod_inst->GetActorName() << " fail";
LOG(ERROR) << "init " << actor_name << " fail";
return false;
}
actor_ctx_mgr_->RegContext(ctx);
// 接收缓存中发给自己的消息
if (cache_msg_.find(actor_name) != cache_msg_.end()) {
auto& cache_list = cache_msg_[actor_name];
LOG(INFO) << actor_name
<< " recv msg from cache, size " << cache_list.size();
DispatchMsg(&cache_list);
cache_msg_.erase(actor_name);
}
// 目的地址不存在的暂时放到缓存消息队列
auto send_list = ctx->GetMailbox()->GetSendList();
for (auto it = send_list->begin(); it != send_list->end();) {
if (!HasUserInst((*it)->GetDst())) {
LOG(WARNING) << "can't found " << (*it)->GetDst()
<< ", cache this msg";
cache_msg_[(*it)->GetDst()].push_back(*it);
it = send_list->erase(it);
continue;
}
++it;
}
// 分发目的地址已经存在的消息
DispatchMsg(ctx);
return true;
}
Expand Down Expand Up @@ -710,4 +732,19 @@ void App::Quit() {
worker_ctx_mgr_->StopAllWorker();
}

bool App::HasUserInst(const std::string& name) {
if (name.substr(0, 6) == "worker") {
auto worker_ctx = ev_mgr_->Get<WorkerContext>(name);
if (worker_ctx != nullptr
&& worker_ctx->GetType() == Event::Type::kWorkerUser) {
return true;
}
} else if (name.substr(0, 5) == "actor") {
if (actor_ctx_mgr_->HasActor(name)) {
return true;
}
}
return false;
}

} // namespace myframe
5 changes: 5 additions & 0 deletions myframe/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class App final : public std::enable_shared_from_this<App> {
std::shared_ptr<Actor> inst,
const std::string& params);

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

bool LoadActors(
Expand Down Expand Up @@ -128,6 +129,10 @@ class App final : public std::enable_shared_from_this<App> {
std::atomic_bool quit_{true};
std::mutex dispatch_mtx_;
std::mutex local_mtx_;
/// 缓存消息列表
std::unordered_map<
std::string,
std::list<std::shared_ptr<Msg>>> cache_msg_;
/// poller
std::unique_ptr<Poller> poller_;
/// 模块管理对象
Expand Down
2 changes: 2 additions & 0 deletions myframe/event_conn_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ void EventConnManager::Notify(
std::shared_ptr<EventConn> ev = nullptr;
ev = ev_mgr_->Get<EventConn>(h);
if (ev == nullptr) {
LOG(ERROR) << "can't find handle " << h;
return;
}
if (ev->GetConnType() == EventConn::Type::kSend) {
LOG(WARNING) << "event " << ev->GetName() << " need't resp msg";
return;
}
// push msg to event_conn
Expand Down
4 changes: 2 additions & 2 deletions myframe/event_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Author: likepeng <likepeng0418@163.com>

#include <glog/logging.h>

#include <myframe/worker.h>
#include <myframe/event_conn.h>
#include "myframe/worker.h"
#include "myframe/event_conn.h"

namespace myframe {

Expand Down
1 change: 0 additions & 1 deletion templates/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ SET(MYFRAME_INSTALL_LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib)
#### dep 3rd lib
FIND_PACKAGE(jsoncpp REQUIRED)
FIND_PACKAGE(gflags REQUIRED)
FIND_PACKAGE(GTest REQUIRED)

#### include directory
INCLUDE_DIRECTORIES(${CMAKE_INSTALL_PREFIX}/include)
Expand Down
Loading

0 comments on commit b5063d5

Please sign in to comment.