Skip to content

Commit 78c6116

Browse files
committed
WIP: pybind wrapper App
1 parent 1c79258 commit 78c6116

File tree

6 files changed

+165
-49
lines changed

6 files changed

+165
-49
lines changed

python/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ project(pymyframe)
1111
include(${SWIG_USE_FILE})
1212

1313
set(dot_i_srcs
14-
swig/test.i
14+
swig/pymyframe.i
1515
)
1616
set_property(SOURCE ${dot_i_srcs} PROPERTY CPLUSPLUS ON)
1717
swig_add_library(${PROJECT_NAME}
@@ -20,15 +20,18 @@ swig_add_library(${PROJECT_NAME}
2020
SOURCES ${dot_i_srcs}
2121
)
2222
set_property(TARGET ${PROJECT_NAME} PROPERTY SWIG_INCLUDE_DIRECTORIES
23+
${CMAKE_SOURCE_DIR}
2324
${CMAKE_CURRENT_SOURCE_DIR}
2425
)
2526
target_include_directories(${PROJECT_NAME}
2627
PRIVATE
28+
${CMAKE_SOURCE_DIR}
2729
${CMAKE_CURRENT_SOURCE_DIR}
2830
${Python_INCLUDE_DIRS}
2931
)
3032
target_link_libraries(${PROJECT_NAME}
3133
PRIVATE
34+
myframe
3235
${Python_LIBRARIES}
3336
)
3437

@@ -40,8 +43,9 @@ install(TARGETS
4043
)
4144
file(TOUCH "${CMAKE_CURRENT_BINARY_DIR}/__init__.py")
4245
install(FILES
43-
"${CMAKE_CURRENT_BINARY_DIR}/myframe.py"
46+
"${CMAKE_CURRENT_BINARY_DIR}/pymyframe.py"
4447
"${CMAKE_CURRENT_BINARY_DIR}/__init__.py"
48+
"${CMAKE_CURRENT_SOURCE_DIR}/launcher.py"
4549
PERMISSIONS
4650
OWNER_READ OWNER_WRITE
4751
GROUP_READ

python/launcher.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/python3
2+
# -*- coding: UTF-8 -*-
3+
import time
4+
import pymyframe as myframe
5+
6+
app = myframe.App()
7+
app_conf = myframe.AppConf()
8+
9+
res = app.init(app_conf)
10+
if res == False:
11+
exit(-1)
12+
13+
res = app.loadServiceFromDir("service")
14+
if res == False:
15+
exit(-1)
16+
17+
app.start()
18+
19+
while True:
20+
time.sleep(1)

python/py_test.h

Lines changed: 0 additions & 40 deletions
This file was deleted.

python/pymyframe.h

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/****************************************************************************
2+
Copyright (c) 2019, 李柯鹏
3+
All rights reserved.
4+
5+
Author: 李柯鹏 <likepeng0418@163.com>
6+
****************************************************************************/
7+
#pragma once
8+
#include <iostream>
9+
#include <thread>
10+
#include "myframe/common.h"
11+
#include "myframe/log.h"
12+
#include "myframe/app.h"
13+
14+
namespace pymyframe {
15+
16+
class App;
17+
class AppConf {
18+
friend class App;
19+
public:
20+
AppConf() {
21+
log_dir_ = myframe::Common::GetAbsolutePath(log_dir_);
22+
std::cout << "appconf construct\n";
23+
}
24+
~AppConf() {
25+
std::cout << "appconf deconstruct\n";
26+
}
27+
28+
void setProcessName(const std::string& name) {
29+
process_name_ = name;
30+
}
31+
32+
void setLibDir(const std::string& dir) {
33+
args_.SetStr(MYFRAME_KEY_SERVICE_LIB_DIR,
34+
myframe::Common::GetAbsolutePath(dir));
35+
}
36+
37+
void setLogDir(const std::string& dir) {
38+
log_dir_ = myframe::Common::GetAbsolutePath(dir);
39+
}
40+
41+
void setLogMaxSizeMB(int sz) {
42+
log_max_size_ = sz;
43+
}
44+
45+
void setThreadPoolSize(int sz) {
46+
args_.SetInt(MYFRAME_KEY_THREAD_POOL_SIZE, sz);
47+
}
48+
49+
void setPendingQueueSize(int sz) {
50+
args_.SetInt(MYFRAME_KEY_PENDING_QUEUE_SIZE, sz);
51+
}
52+
53+
void setRunQueueSize(int sz) {
54+
args_.SetInt(MYFRAME_KEY_RUN_QUEUE_SIZE, sz);
55+
}
56+
57+
private:
58+
int log_max_size_{100};
59+
std::string log_dir_{"log"};
60+
std::string process_name_{"launcher"};
61+
myframe::Arguments args_;
62+
};
63+
64+
class App {
65+
public:
66+
App() {
67+
std::cout << "pyapp construct\n";
68+
}
69+
70+
~App() {
71+
if (app_) {
72+
app_->Quit();
73+
}
74+
if (th_.joinable()) {
75+
th_.join();
76+
}
77+
std::cout << "pyapp deconstruct\n";
78+
}
79+
80+
bool init(const AppConf& conf) {
81+
myframe::InitLog(
82+
conf.log_dir_,
83+
conf.process_name_,
84+
conf.log_max_size_);
85+
app_ = std::make_shared<myframe::App>();
86+
int res = app_->Init(conf.args_);
87+
if (res == false) {
88+
return false;
89+
}
90+
return res;
91+
}
92+
93+
bool loadServiceFromDir(const std::string& path) {
94+
if (app_ == nullptr) {
95+
return false;
96+
}
97+
return app_->LoadServiceFromDir(
98+
myframe::Common::GetAbsolutePath(path)
99+
);
100+
}
101+
102+
bool loadServiceFromFile(const std::string& filepath) {
103+
if (app_ == nullptr) {
104+
return false;
105+
}
106+
return app_->LoadServiceFromFile(
107+
myframe::Common::GetAbsolutePath(filepath)
108+
);
109+
}
110+
111+
// int send(msg);
112+
// msg sendRequest(msg);
113+
// bool addActor(PyObject* obj);
114+
115+
void start() {
116+
if (app_ == nullptr) {
117+
return;
118+
}
119+
th_ = std::thread([this](){
120+
app_->Exec();
121+
});
122+
}
123+
124+
private:
125+
std::shared_ptr<myframe::App> app_;
126+
std::thread th_;
127+
};
128+
129+
} // namespace pymyframe

python/swig/pymyframe.i

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
%module pymyframe
2+
3+
%{
4+
#include "pymyframe.h"
5+
%}
6+
7+
%include <std_shared_ptr.i>
8+
%include <std_string.i>
9+
10+
%include "pymyframe.h"

python/swig/test.i

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)