Skip to content

Commit 4c8bbf1

Browse files
添加websocket动态库实现
1 parent e28bb1b commit 4c8bbf1

File tree

6 files changed

+100
-31
lines changed

6 files changed

+100
-31
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ target_link_directories(dlms
4343
target_link_libraries(dlms
4444
PUBLIC glog)
4545

46+
# 编译依赖,如果添加依赖编译时会将对应的目标也进行编译
47+
add_dependencies(dlms websocket)
48+
4649
add_subdirectory(test)
4750

4851

bin/dlms.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"DLMS" : {
3+
"Worker" : {
4+
"ThreadNumber" : 10
5+
}
6+
7+
},
8+
"Plugins" : {
9+
"Websocket" : {
10+
"Client" : {
11+
12+
},
13+
"Server" : {
14+
15+
}
16+
}
17+
18+
}
19+
}

src/dlms/dlms.cpp

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Created by andrew on 2022/2/13.
33
//
44
#include <iostream>
5+
#include <csignal>
56
#include <glog/logging.h>
67

78
#include "dlms.h"
@@ -10,12 +11,17 @@
1011
#include "dlms_error.h"
1112
#include "thread_manager.h"
1213

13-
using json=nlohmann::json;
14+
using json = nlohmann::json;
1415

1516
using namespace std;
1617

18+
bool running = true;
1719

18-
int main(int argc, char* argv[]) {
20+
static void SignalInterrupt(int instruction) {
21+
running = false;
22+
}
23+
24+
int main(int argc, char *argv[]) {
1925

2026
google::InitGoogleLogging(argv[0]);
2127
FLAGS_log_dir = "./log";
@@ -24,10 +30,16 @@ int main(int argc, char* argv[]) {
2430
CContext context;
2531
// 1. 解析命令行参数
2632
context.ParseCommandLine(argc, argv);
33+
// 2. 加载配置
34+
context.LoadConfig();
2735

2836
context.Init();
2937
context.Start();
3038

39+
while (running) {
40+
std::this_thread::sleep_for(std::chrono::seconds(1));
41+
}
42+
3143
context.Stop();
3244
context.Reset();
3345

@@ -38,15 +50,14 @@ int main(int argc, char* argv[]) {
3850
google::ShutdownGoogleLogging();
3951

4052

41-
4253
return 0;
4354
}
4455

4556

4657
Status CContext::Init() {
4758

4859
// 具体的线程个数从配置文件中获取
49-
lpThreadManager = new (std::nothrow) ThreadManager(10);
60+
lpThreadManager = new(std::nothrow) ThreadManager(10);
5061
if (nullptr == lpThreadManager) {
5162

5263
return Status::OutOfMemory("");
@@ -58,6 +69,8 @@ Status CContext::Init() {
5869

5970
Status CContext::Start() {
6071

72+
// 注册信号
73+
signal(SIGINT, SignalInterrupt);
6174

6275
return Status::Ok();
6376
}
@@ -92,6 +105,15 @@ Status CContext::Dispatch(Func workFunction) {
92105
return Status::InvalidArgument("");
93106
}
94107

108+
Status CContext::LoadConfig() {
109+
auto status = config.LoadJsonFromFile();
110+
if (!status.ok()) {
111+
return status;
112+
}
113+
114+
return Status::Ok();
115+
}
116+
95117
#define GET_CASE_INFO(X) \
96118
case X: \
97119
configMap.emplace(std::make_pair(X, POINTER_SAFE(optarg))); \
@@ -102,10 +124,8 @@ Status CConfig::ParseCommandLine(int32_t argc, char *argv[]) {
102124
int ch;
103125
std::string opts = "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z:"
104126
"A:B:C:D:E:F:G:H:I:J:K:L:M:N:O:P:Q:R:S:T:U:V:W:X:Y:Z:";
105-
while( (ch = getopt(argc, argv, opts.c_str())) != -1 )
106-
{
107-
switch(ch)
108-
{
127+
while ((ch = getopt(argc, argv, opts.c_str())) != -1) {
128+
switch (ch) {
109129
GET_CASE_INFO('a');
110130
GET_CASE_INFO('b');
111131
GET_CASE_INFO('c');
@@ -136,9 +156,33 @@ Status CConfig::ParseCommandLine(int32_t argc, char *argv[]) {
136156
configMap['?'] = "Bad option!";
137157
return Status::InvalidArgument("");
138158
default:
139-
printf("default, result=%c\n",ch);
159+
printf("default, result=%c\n", ch);
140160
break;
141161
}
142162
}
143163
return Status::Ok();
144164
}
165+
166+
Status CConfig::LoadJsonFromFile() {
167+
168+
auto iter = configMap.find('j');
169+
if (iter == std::end(configMap)) {
170+
return Status::NotFound("Config json is not found");
171+
}
172+
173+
std::string path = get_current_dir_name();
174+
std::string filename = iter->second;
175+
176+
lpJsonProxy = new JsonProxy(path, filename);
177+
if (nullptr == lpJsonProxy) {
178+
return Status::OutOfMemory("Failed to create json proxy");
179+
}
180+
181+
//std::cout << lpJsonProxy->GetJsonObj().dump() << std::endl;
182+
183+
return Status::Ok();
184+
}
185+
186+
CConfig::~CConfig() {
187+
delete lpJsonProxy;
188+
}

src/dlms/dlms.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#ifndef DLMS_DLMS_H
66
#define DLMS_DLMS_H
7+
78
#include <string>
89
#include <map>
910
#include <unordered_map>
@@ -15,17 +16,17 @@
1516
#include "thread_manager.h"
1617

1718

18-
1919
class CConfig : public IConfig {
2020
public:
21-
~CConfig() override = default;
21+
~CConfig() override;
2222

23-
Status ParseCommandLine(int32_t argc, char* argv[]);
23+
Status ParseCommandLine(int32_t argc, char *argv[]);
2424

25+
Status LoadJsonFromFile();
2526

2627
private:
2728
std::unordered_map<char, std::string> configMap;
28-
JsonProxy* jsonProxy;
29+
JsonProxy *lpJsonProxy;
2930
};
3031

3132
class CContext : public IContext {
@@ -40,22 +41,24 @@ class CContext : public IContext {
4041

4142
Status Reset() override;
4243

43-
Plugin* GetPlugin(std::string& pluginName) override;
44-
45-
Status ParseCommandLine(int32_t argc, char* argv[]);
44+
Plugin *GetPlugin(std::string &pluginName) override;
4645

4746
// 将需要工作的线程放到Push里面
4847
Status Dispatch(Func workFunction) override;
48+
4949
// 如果某些函数不需要再执行了就Pop掉,之后主框架就不在执行该函数了
50+
Status ParseCommandLine(int32_t argc, char *argv[]);
51+
52+
Status LoadConfig();
53+
54+
public:
5055

5156

5257
private:
5358
PluginManager pluginManager;
54-
CConfig config;
59+
CConfig config;
5560
ThreadManager *lpThreadManager{nullptr};
5661
};
5762

5863

59-
60-
6164
#endif //DLMS_DLMS_H

utils/json_proxy.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
#ifndef DLMS_JSON_PROXY_H
66
#define DLMS_JSON_PROXY_H
7+
78
#include <utility>
89
#include <iostream>
910
#include <fstream>
11+
#include <unistd.h>
1012

1113
#include "json.hpp"
1214
#include "status.h"
@@ -15,25 +17,23 @@ using json = nlohmann::json;
1517

1618
class JsonProxy {
1719
public:
18-
explicit JsonProxy(json json) : jsonObj(std::move(json)) {}
19-
explicit JsonProxy(json& json) : jsonObj(json) {}
20-
explicit JsonProxy(json&& json) : jsonObj(json) {}
20+
explicit JsonProxy(json json) : jsonObj(std::move(json)) {}
21+
22+
explicit JsonProxy(json &json) : jsonObj(json) {}
2123

22-
JsonProxy(std::string& path, std::string& filename) {
23-
std::string file = path + filename;
24+
explicit JsonProxy(json &&json) : jsonObj(json) {}
25+
26+
JsonProxy(std::string &path, std::string &filename) {
27+
std::string file = path + "/" + filename;
2428
std::ifstream(file) >> jsonObj;
2529
}
2630

27-
explicit JsonProxy(std::string& filename) {}
28-
31+
explicit JsonProxy(std::string &filename) {}
2932

30-
json& GetJsonObj() {
33+
json &GetJsonObj() {
3134
return jsonObj;
3235
}
3336

34-
35-
36-
3737
private:
3838
json jsonObj;
3939
};

utils/thread_manager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ThreadManager {
6060
while (Running == threadStatus) {
6161
cv.wait(lock, [&]{return (!queueFunc.empty() || Quit());});
6262
if (!Quit() && !queueFunc.empty()) {
63-
auto op = std::move(queueFunc.front());
63+
auto &op = queueFunc.front();
6464
queueFunc.pop();
6565
lock.unlock();
6666
op();

0 commit comments

Comments
 (0)