Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
feat: lua Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
gaowanlu committed Feb 13, 2024
1 parent ad6bd3d commit 913aebe
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 9 deletions.
5 changes: 3 additions & 2 deletions bin/config/main.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ max_conn = 2000
# using wait_time setting tick time, 10ms
wait_time = 10
http_static_dir = /tubekit_static
task_type = HTTP_TASK
lua_dir = ./lua
# task_type = HTTP_TASK
# task_type = STREAM_TASK
# task_type = WEBSOCKET_TASK
task_type = WEBSOCKET_TASK
# now support HTTP_TASK or STREAM_TASK or WEBSOCKET_TASK
daemon = 1
crt.pem = ./config/certificate.crt
Expand Down
7 changes: 7 additions & 0 deletions bin/lua/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function OnInit()
tubekit.Logger("[lua] main OnInit")
end

function OnExit()
tubekit.Logger("[lua] main OnExit")
end
4 changes: 2 additions & 2 deletions client/websocket.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

<script>
let pingpongcount = 0;
const socket = new WebSocket("wss://61.171.51.135:20023/");
//const socket = new WebSocket("ws://172.29.94.203:20023/");
//const socket = new WebSocket("wss://61.171.51.135:20023/");
const socket = new WebSocket("ws://172.29.94.203:20023/");
socket.addEventListener("open", (event) => {
console.log("WebSocket connection opened:", event);
//setTimeout(sendMessage, 0);
Expand Down
3 changes: 3 additions & 0 deletions src/app/http_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "utility/mime_type.h"
#include "utility/url.h"
#include "utility/singleton.h"
#include "app/lua_plugin.h"

using std::string;
using std::vector;
Expand Down Expand Up @@ -41,12 +42,14 @@ class html_loader
int http_app::on_init()
{
LOG_ERROR("http_app::on_init()");
utility::singleton<app::lua_plugin>::instance()->on_init();
return 0;
}

void http_app::on_stop()
{
LOG_ERROR("http_app::on_stop()");
utility::singleton<app::lua_plugin>::instance()->on_exit();
}

void http_app::on_tick()
Expand Down
71 changes: 67 additions & 4 deletions src/app/lua_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,98 @@
#include <string>
#include <iostream>

#include "server/server.h"
#include "utility/singleton.h"
#include "tubekit-log/logger.h"

using std::string;
using tubekit::app::lua_plugin;
using tubekit::server::server;
using tubekit::utility::singleton;

lua_plugin::lua_plugin() : lua_state(nullptr)
{
}

void lua_plugin::on_init()
{
// init lua vm
lua_state = luaL_newstate();
luaL_openlibs(lua_state);
// exe lua file
string filename = "./lua/init.lua";
string filename = singleton<server::server>::instance()->get_lua_dir() + "/init.lua";
int isok = luaL_dofile(lua_state, filename.data());

if (isok == 0)
{
// succ
LOG_ERROR("init.lua load succ");
mount();
exe_OnInit();
}
else
{
// std::cout << "err " << lua_tostring(luaState, -1) << std::endl;
LOG_ERROR("init.lua load failed, %s", lua_tostring(lua_state, -1));
}
LOG_ERROR("lua_plugin on_init");
}

void lua_plugin::on_exit()
{
// close lua vm
if (lua_state)
{
exe_OnExit();
lua_close(lua_state);
}
LOG_ERROR("lua_plugin on_exit");
}

lua_plugin::lua_plugin() : lua_state(nullptr)
void lua_plugin::exe_OnInit()
{
lua_getglobal(lua_state, "OnInit");
int isok = lua_pcall(lua_state, 0, 0, 0);
if (0 != isok)
{
LOG_ERROR("exe_OnInit failed %s", lua_tostring(lua_state, -1));
}
}

void lua_plugin::exe_OnExit()
{
lua_getglobal(lua_state, "OnExit");
int isok = lua_pcall(lua_state, 0, 0, 0);
if (0 != isok)
{
LOG_ERROR("exe_OnExit failed %s", lua_tostring(lua_state, -1));
}
}

void lua_plugin::mount()
{
static luaL_Reg lulibs[] = {
{"Logger", Logger},
{NULL, NULL}};
luaL_newlib(lua_state, lulibs);
lua_setglobal(lua_state, "tubekit");
}

int lua_plugin::Logger(lua_State *lua_state)
{
int num = lua_gettop(lua_state);
if (num != 1)
{
lua_pushinteger(lua_state, -1);
return -1;
}
if (lua_isstring(lua_state, 1) == 0)
{
lua_pushinteger(lua_state, -1);
return -1;
}
size_t len = 0;
const char *type = lua_tolstring(lua_state, 1, &len);
string str(type, len);
LOG_ERROR("%s", str.data());
lua_pushinteger(lua_state, 0);
return 0;
}
8 changes: 8 additions & 0 deletions src/app/lua_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ namespace tubekit::app
void on_init();
void on_exit();

void exe_OnInit();
void exe_OnExit();

void mount();

public:
static int Logger(lua_State *lua_state);

private:
lua_State *lua_state;
};
Expand Down
3 changes: 3 additions & 0 deletions src/app/stream_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "connection/connection_mgr.h"
#include "socket/socket.h"
#include "socket/socket_handler.h"
#include "app/lua_plugin.h"

using tubekit::app::stream_app;
using tubekit::connection::connection_mgr;
Expand Down Expand Up @@ -47,12 +48,14 @@ static int process_protocol(tubekit::connection::stream_connection &m_stream_con
int stream_app::on_init()
{
LOG_ERROR("stream_app::on_init()");
singleton<app::lua_plugin>::instance()->on_init();
return 0;
}

void stream_app::on_stop()
{
LOG_ERROR("stream_app::on_stop()");
singleton<app::lua_plugin>::instance()->on_exit();
}

void stream_app::on_tick()
Expand Down
3 changes: 3 additions & 0 deletions src/app/websocket_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "utility/singleton.h"
#include "connection/connection_mgr.h"
#include <arpa/inet.h>
#include "app/lua_plugin.h"

using namespace tubekit::app;
using namespace tubekit::utility;
Expand Down Expand Up @@ -33,12 +34,14 @@ enum class websocket_frame_type
int websocket_app::on_init()
{
LOG_ERROR("websocket_app::on_init()");
singleton<app::lua_plugin>::instance()->on_init();
return 0;
}

void websocket_app::on_stop()
{
LOG_ERROR("websocket_app::on_stop()");
singleton<app::lua_plugin>::instance()->on_exit();
}

void websocket_app::on_tick()
Expand Down
2 changes: 1 addition & 1 deletion src/app/websocket_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace tubekit
static void on_close_connection(tubekit::connection::websocket_connection &m_websocket_connection);
static void on_new_connection(tubekit::connection::websocket_connection &m_websocket_connection);
static bool send_packet(tubekit::connection::websocket_connection &m_websocket_connection, const char *data, size_t data_len, bool use_safe);

static int on_init();
static void on_stop();
static void on_tick();
Expand Down
12 changes: 12 additions & 0 deletions src/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,16 @@ const std::string &server::get_http_static_dir()
return m_http_static_dir;
}

void server::set_lua_dir(std::string lua_dir)
{
m_lua_dir = lua_dir;
}

const std::string &server::get_lua_dir()
{
return m_lua_dir;
}

void server::set_daemon(bool daemon)
{
m_daemon = daemon;
Expand Down Expand Up @@ -226,6 +236,7 @@ void server::config(const std::string &ip,
size_t wait_time,
std::string task_type,
std::string http_static_dir,
std::string lua_dir,
bool daemon, /*= false*/
std::string crt_pem /*= ""*/,
std::string key_pem /*= ""*/,
Expand All @@ -237,6 +248,7 @@ void server::config(const std::string &ip,
set_wait_time(wait_time);
set_task_type(task_type);
set_http_static_dir(http_static_dir);
set_lua_dir(lua_dir);

set_daemon(daemon);
set_use_ssl(use_ssl);
Expand Down
5 changes: 5 additions & 0 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ namespace tubekit
void set_http_static_dir(std::string http_static_dir);
const std::string &get_http_static_dir();

void set_lua_dir(std::string lua_dir);
const std::string &get_lua_dir();

void set_daemon(bool daemon);
void set_use_ssl(bool use_ssl);
void set_crt_pem(std::string set_crt_pem);
Expand All @@ -45,6 +48,7 @@ namespace tubekit
size_t wait_time,
std::string task_type,
std::string http_static_dir,
std::string lua_dir,
bool daemon = false,
std::string crt_pem = "",
std::string key_pem = "",
Expand All @@ -61,6 +65,7 @@ namespace tubekit
size_t m_connects;
size_t m_wait_time;
std::string m_http_static_dir;
std::string m_lua_dir;

std::string m_task_type;
bool m_daemon{false};
Expand Down
2 changes: 2 additions & 0 deletions src/system/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ void system::init()
const string task_type = (*ini)["server"]["task_type"];

const string http_static_dir = (*ini)["server"]["http_static_dir"];
const string lua_dir = (*ini)["server"]["lua_dir"];

const int use_ssl = (*ini)["server"]["use_ssl"];
const string crt_pem = (*ini)["server"]["crt.pem"];
Expand Down Expand Up @@ -72,6 +73,7 @@ void system::init()
wait_time,
task_type,
http_static_dir,
lua_dir,
daemon,
crt_pem,
key_pem,
Expand Down

0 comments on commit 913aebe

Please sign in to comment.