Skip to content

Commit bb4c129

Browse files
完善插件信息
1 parent c587ee4 commit bb4c129

File tree

8 files changed

+134
-13
lines changed

8 files changed

+134
-13
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ message(STATUS ${CPACK_PACKAGE_VERSION})
1919

2020
add_subdirectory(3rd/glog)
2121
#add_subdirectory(3rd/civetweb)
22+
add_subdirectory(plugins)
2223

2324

2425

inc/dlms_bash.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
#define DLMS_DLMS_BASH_H
77
#include <iostream>
88
#include <functional>
9+
#include "thread_manager.h"
910

1011
class Plugin;
1112
class IDlms {
1213
public:
13-
typedef typename std::function<int32_t ()> Func;
14+
1415

1516
public:
1617
virtual ~IDlms() = default;
@@ -51,9 +52,7 @@ class IDlms {
5152
virtual Plugin *GetPlugin(std::string &pluginName) = 0;
5253

5354
// 将需要工作的线程放到Push里面
54-
virtual uint32_t PushWorker(Func workFunction) = 0;
55-
// 如果某些函数不需要再执行了就Pop掉,之后主框架就不在执行该函数了
56-
virtual int32_t PopWorker(uint32_t Index) = 0;
55+
virtual uint32_t Dispatch(Func workFunction) = 0;
5756
};
5857

5958

inc/plugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Plugin {
3636
PLUGIN_INIT = 1,
3737
PLUGIN_RUNNING = 2,
3838
PLUGIN_STOP = 3,
39-
PLUGIN_Reset = 4,
39+
PLUGIN_Reset = 4,
4040
};
4141
//
4242

plugins/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
# 设置静态库文件目录
4+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
5+
# 动态库文件目录
6+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/plugins)
7+
# 可执行文件目录
8+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
9+
10+
11+
12+
add_library(websocket SHARED
13+
websocket/websocket_plugin.cpp
14+
websocket/websocket_server.cpp
15+
websocket/websocket_client.cpp)
16+
target_link_libraries(websocket
17+
PUBLIC pthread)
18+
target_include_directories(websocket
19+
PUBLIC ${PROJECT_SOURCE_DIR}/inc
20+
PUBLIC ${PROJECT_SOURCE_DIR}/3rd/civetweb)
21+
22+
23+
24+

src/dlms/dlms.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "dlms.h"
88
#include "json.hpp"
99
#include "dlms_macro.h"
10+
#include "dlms_error.h"
1011

1112
using json=nlohmann::json;
1213

@@ -23,6 +24,10 @@ int main(int argc, char* argv[]) {
2324
// 1. 解析命令行参数
2425
dlms.ParseCommandLine(argc, argv);
2526

27+
dlms.Init();
28+
dlms.Start();
29+
dlms.Stop();
30+
dlms.Reset();
2631

2732

2833

@@ -42,6 +47,12 @@ int main(int argc, char* argv[]) {
4247

4348
int32_t CDlms::Init() {
4449

50+
// 具体的线程个数从配置文件中获取
51+
lpThreadManager = new (std::nothrow) ThreadManager(10);
52+
if (nullptr == lpThreadManager) {
53+
54+
return ERROR;
55+
}
4556

4657

4758
return 0;
@@ -56,6 +67,9 @@ int32_t CDlms::Stop() {
5667
}
5768

5869
int32_t CDlms::Reset() {
70+
71+
delete lpThreadManager;
72+
5973
return 0;
6074
}
6175

@@ -74,11 +88,7 @@ int32_t CDlms::ParseCommandLine(int32_t argc, char **argv) {
7488
return config.ParseCommandLine(argc, argv);
7589
}
7690

77-
uint32_t CDlms::PushWorker(IDlms::Func workFunction) {
78-
return 0;
79-
}
80-
81-
int32_t CDlms::PopWorker(uint32_t Index) {
91+
uint32_t CDlms::Dispatch(Func workFunction) {
8292
return 0;
8393
}
8494

src/dlms/dlms.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <dlms_bash.h>
1313
#include "plugin_manager.h"
1414
#include "json_proxy.h"
15+
#include "thread_manager.h"
1516

1617

1718

@@ -44,14 +45,14 @@ class CDlms : public IDlms {
4445
int32_t ParseCommandLine(int32_t argc, char* argv[]);
4546

4647
// 将需要工作的线程放到Push里面
47-
uint32_t PushWorker(Func workFunction) override;
48+
uint32_t Dispatch(Func workFunction) override;
4849
// 如果某些函数不需要再执行了就Pop掉,之后主框架就不在执行该函数了
49-
int32_t PopWorker(uint32_t Index) override;
5050

5151

5252
private:
5353
PluginManager pluginManager;
5454
CConfig config;
55+
ThreadManager *lpThreadManager{nullptr};
5556
};
5657

5758

utils/status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Status {
146146
state_ = result;
147147
}
148148

149-
static const char *CopyState(const char *s) {
149+
static const char *CopyState(const char *state) {
150150
uint32_t size;
151151
std::memcpy(&size, state, sizeof(size));
152152
char *result = new char[size + 5];

utils/thread_manager.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//
2+
// Created by andrew on 2022/6/29.
3+
//
4+
5+
#ifndef DLMS_THREAD_MANAGER_H
6+
#define DLMS_THREAD_MANAGER_H
7+
#include <thread>
8+
#include <mutex>
9+
#include <queue>
10+
#include <atomic>
11+
#include <condition_variable>
12+
13+
typedef typename std::function<int32_t ()> Func;
14+
15+
class ThreadManager {
16+
public:
17+
enum ThreadStatus {
18+
Initializing = 0,
19+
Running = 1,
20+
Stopping = 2,
21+
};
22+
23+
public:
24+
explicit ThreadManager(uint32_t threadNumber) {
25+
threadStatus = Initializing;
26+
this->Start(threadNumber);
27+
}
28+
~ThreadManager() {
29+
Stop();
30+
}
31+
32+
void Start(uint32_t threadNumber) {
33+
threadStatus = Running;
34+
for (uint32_t i = 0; i < threadNumber; i++) {
35+
this->threadPool.emplace_back(&ThreadManager::Run, this);
36+
}
37+
}
38+
39+
void Dispatch(const Func& func) {
40+
std::unique_lock<std::mutex> lock(mux);
41+
queueFunc.push(func);
42+
lock.unlock();
43+
cv.notify_one();
44+
}
45+
46+
void Stop() {
47+
std::unique_lock<std::mutex> lock(mux);
48+
threadStatus = Stopping;
49+
lock.unlock();
50+
cv.notify_all();
51+
for (auto& dispatchThread : threadPool) {
52+
dispatchThread.join();
53+
}
54+
}
55+
56+
private:
57+
void Run() {
58+
59+
std::unique_lock<std::mutex> lock(mux);
60+
while (Running == threadStatus) {
61+
cv.wait(lock, [&]{return (!queueFunc.empty() || Quit());});
62+
if (!Quit() && !queueFunc.empty()) {
63+
auto op = std::move(queueFunc.front());
64+
queueFunc.pop();
65+
lock.unlock();
66+
op();
67+
lock.lock();
68+
}
69+
}
70+
}
71+
bool Quit() {
72+
return threadStatus != Running;
73+
}
74+
75+
private:
76+
std::vector<std::thread> threadPool{}; // 线程池
77+
ThreadStatus threadStatus{Initializing}; //
78+
std::mutex mux;
79+
std::queue<Func> queueFunc;
80+
std::condition_variable cv;
81+
std::atomic<bool> shouldContinue{};
82+
};
83+
84+
85+
86+
#endif //DLMS_THREAD_MANAGER_H

0 commit comments

Comments
 (0)