Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit bc54ef5

Browse files
committed
adding event websocket
1 parent 8d50506 commit bc54ef5

File tree

13 files changed

+157
-76
lines changed

13 files changed

+157
-76
lines changed

engine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ find_package(LibArchive REQUIRED)
7878
find_package(tabulate CONFIG REQUIRED)
7979
find_package(CURL REQUIRED)
8080
find_package(SQLiteCpp REQUIRED)
81+
find_package(eventpp CONFIG REQUIRED)
8182

8283
add_executable(${TARGET_NAME} main.cc
8384
${CMAKE_CURRENT_SOURCE_DIR}/utils/cpuid/cpu_info.cc
@@ -94,6 +95,7 @@ target_link_libraries(${TARGET_NAME} PRIVATE CURL::libcurl)
9495
target_link_libraries(${TARGET_NAME} PRIVATE JsonCpp::JsonCpp Drogon::Drogon OpenSSL::SSL OpenSSL::Crypto yaml-cpp::yaml-cpp
9596
${CMAKE_THREAD_LIBS_INIT})
9697
target_link_libraries(${TARGET_NAME} PRIVATE SQLiteCpp)
98+
target_link_libraries(${TARGET_NAME} PRIVATE eventpp::eventpp)
9799

98100
# ##############################################################################
99101

engine/common/event.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace cortex::event {
6+
7+
struct Event {
8+
std::string message;
9+
};
10+
} // namespace cortex::event

engine/controllers/engine_controller.cc renamed to engine/controllers/engines.cc

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "engine_controller.h"
1+
#include "engines.h"
22
#include <utility>
33
#include "services/engine_service.h"
44
#include "utils/archive_utils.h"
55
#include "utils/cortex_utils.h"
66
#include "utils/logging_utils.h"
77

8-
void EngineController::InstallEngine(
8+
void Engines::InstallEngine(
99
const HttpRequestPtr& req,
1010
std::function<void(const HttpResponsePtr&)>&& callback,
1111
const std::string& engine) {
@@ -37,7 +37,7 @@ void EngineController::InstallEngine(
3737
}
3838
}
3939

40-
void EngineController::ListEngine(
40+
void Engines::ListEngine(
4141
const HttpRequestPtr& req,
4242
std::function<void(const HttpResponsePtr&)>&& callback) const {
4343
auto status_list = engine_service_->GetEngineInfoList();
@@ -64,10 +64,9 @@ void EngineController::ListEngine(
6464
callback(resp);
6565
}
6666

67-
void EngineController::GetEngine(
68-
const HttpRequestPtr& req,
69-
std::function<void(const HttpResponsePtr&)>&& callback,
70-
const std::string& engine) const {
67+
void Engines::GetEngine(const HttpRequestPtr& req,
68+
std::function<void(const HttpResponsePtr&)>&& callback,
69+
const std::string& engine) const {
7170
auto status = engine_service_->GetEngineInfo(engine);
7271
Json::Value ret;
7372
if (status.has_value()) {
@@ -90,7 +89,7 @@ void EngineController::GetEngine(
9089
}
9190
}
9291

93-
void EngineController::UninstallEngine(
92+
void Engines::UninstallEngine(
9493
const HttpRequestPtr& req,
9594
std::function<void(const HttpResponsePtr&)>&& callback,
9695
const std::string& engine) {

engine/controllers/engine_controller.h renamed to engine/controllers/engines.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
88

99
using namespace drogon;
1010

11-
class EngineController
12-
: public drogon::HttpController<EngineController, false> {
11+
class Engines : public drogon::HttpController<Engines, false> {
1312
public:
1413
METHOD_LIST_BEGIN
15-
METHOD_ADD(EngineController::InstallEngine, "/{1}/install", Post);
16-
METHOD_ADD(EngineController::UninstallEngine, "/{1}", Delete);
17-
METHOD_ADD(EngineController::ListEngine, "", Get);
18-
METHOD_ADD(EngineController::GetEngine, "/{1}", Get);
14+
METHOD_ADD(Engines::InstallEngine, "/{1}/install", Post);
15+
METHOD_ADD(Engines::UninstallEngine, "/{1}", Delete);
16+
METHOD_ADD(Engines::ListEngine, "", Get);
17+
METHOD_ADD(Engines::GetEngine, "/{1}", Get);
1918
METHOD_LIST_END
2019

21-
explicit EngineController(
22-
const std::shared_ptr<EngineService>& engine_service)
20+
explicit Engines(const std::shared_ptr<EngineService>& engine_service)
2321
: engine_service_(engine_service) {}
2422

2523
void InstallEngine(const HttpRequestPtr& req,

engine/controllers/event_controller.cc

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

engine/controllers/events.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "events.h"
2+
3+
void Events::broadcast(const std::string& message) {
4+
for (const auto& conn : connections_) {
5+
conn->send(message);
6+
}
7+
}
8+
9+
void Events::handleNewMessage(const WebSocketConnectionPtr& wsConnPtr,
10+
std::string&& message,
11+
const WebSocketMessageType& type) {}
12+
13+
void Events::handleNewConnection(const HttpRequestPtr& req,
14+
const WebSocketConnectionPtr& wsConnPtr) {
15+
LOG_DEBUG << "New connection";
16+
connections_.insert(wsConnPtr);
17+
}
18+
19+
void Events::handleConnectionClosed(const WebSocketConnectionPtr& wsConnPtr) {
20+
LOG_DEBUG << "Connection closed";
21+
connections_.erase(wsConnPtr);
22+
}

engine/controllers/events.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <drogon/HttpAppFramework.h>
2+
#include <drogon/HttpController.h>
3+
#include <drogon/PubSubService.h>
4+
#include <drogon/WebSocketController.h>
5+
#include <eventpp/eventqueue.h>
6+
#include "common/event.h"
7+
8+
using namespace drogon;
9+
10+
using Event = cortex::event::Event;
11+
using EventQueue = eventpp::EventQueue<std::string, void(const Event&)>;
12+
13+
class Events : public drogon::WebSocketController<Events, false> {
14+
15+
public:
16+
WS_PATH_LIST_BEGIN
17+
WS_PATH_ADD("/events", Get);
18+
WS_PATH_LIST_END
19+
20+
explicit Events(const std::shared_ptr<EventQueue> event_queue)
21+
: event_queue_{std::move(event_queue)} {};
22+
23+
void handleNewMessage(const WebSocketConnectionPtr& wsConnPtr,
24+
std::string&& message,
25+
const WebSocketMessageType& type) override;
26+
27+
void handleNewConnection(const HttpRequestPtr& req,
28+
const WebSocketConnectionPtr& wsConnPtr) override;
29+
30+
void handleConnectionClosed(const WebSocketConnectionPtr& wsConnPtr) override;
31+
32+
private:
33+
void broadcast(const std::string& message);
34+
35+
std::shared_ptr<EventQueue> event_queue_;
36+
std::set<WebSocketConnectionPtr> connections_;
37+
};

engine/controllers/model_controller.cc renamed to engine/controllers/models.cc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
#include "model_controller.h"
1+
#include "database/models.h"
22
#include <drogon/HttpTypes.h>
33
#include "config/gguf_parser.h"
44
#include "config/yaml_config.h"
5-
#include "database/models.h"
5+
#include "models.h"
66
#include "trantor/utils/Logger.h"
77
#include "utils/cortex_utils.h"
88
#include "utils/file_manager_utils.h"
99
#include "utils/http_util.h"
1010
#include "utils/logging_utils.h"
1111
#include "utils/string_utils.h"
1212

13-
void ModelController::PullModel(
14-
const HttpRequestPtr& req,
15-
std::function<void(const HttpResponsePtr&)>&& callback) {
13+
void Models::PullModel(const HttpRequestPtr& req,
14+
std::function<void(const HttpResponsePtr&)>&& callback) {
1615
if (!http_util::HasFieldInReq(req, callback, "modelId")) {
1716
return;
1817
}
@@ -57,7 +56,7 @@ void ModelController::PullModel(
5756
}
5857
}
5958

60-
void ModelController::ListModel(
59+
void Models::ListModel(
6160
const HttpRequestPtr& req,
6261
std::function<void(const HttpResponsePtr&)>&& callback) const {
6362
Json::Value ret;
@@ -104,7 +103,7 @@ void ModelController::ListModel(
104103
}
105104
}
106105

107-
void ModelController::GetModel(
106+
void Models::GetModel(
108107
const HttpRequestPtr& req,
109108
std::function<void(const HttpResponsePtr&)>&& callback) const {
110109
if (!http_util::HasFieldInReq(req, callback, "modelId")) {
@@ -148,10 +147,9 @@ void ModelController::GetModel(
148147
}
149148
}
150149

151-
void ModelController::DeleteModel(
152-
const HttpRequestPtr& req,
153-
std::function<void(const HttpResponsePtr&)>&& callback,
154-
const std::string& model_id) {
150+
void Models::DeleteModel(const HttpRequestPtr& req,
151+
std::function<void(const HttpResponsePtr&)>&& callback,
152+
const std::string& model_id) {
155153
auto result = model_service_->DeleteModel(model_id);
156154
if (result.has_error()) {
157155
Json::Value ret;
@@ -168,7 +166,7 @@ void ModelController::DeleteModel(
168166
}
169167
}
170168

171-
void ModelController::UpdateModel(
169+
void Models::UpdateModel(
172170
const HttpRequestPtr& req,
173171
std::function<void(const HttpResponsePtr&)>&& callback) const {
174172
if (!http_util::HasFieldInReq(req, callback, "modelId")) {
@@ -212,7 +210,7 @@ void ModelController::UpdateModel(
212210
}
213211
}
214212

215-
void ModelController::ImportModel(
213+
void Models::ImportModel(
216214
const HttpRequestPtr& req,
217215
std::function<void(const HttpResponsePtr&)>&& callback) const {
218216
if (!http_util::HasFieldInReq(req, callback, "modelId") ||
@@ -282,7 +280,7 @@ void ModelController::ImportModel(
282280
}
283281
}
284282

285-
void ModelController::SetModelAlias(
283+
void Models::SetModelAlias(
286284
const HttpRequestPtr& req,
287285
std::function<void(const HttpResponsePtr&)>&& callback) const {
288286
if (!http_util::HasFieldInReq(req, callback, "modelId") ||

engine/controllers/model_controller.h renamed to engine/controllers/models.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
using namespace drogon;
88

9-
class ModelController : public drogon::HttpController<ModelController, false> {
9+
class Models : public drogon::HttpController<Models, false> {
1010
public:
1111
METHOD_LIST_BEGIN
12-
METHOD_ADD(ModelController::PullModel, "/pull", Post);
13-
METHOD_ADD(ModelController::ListModel, "", Get);
14-
METHOD_ADD(ModelController::GetModel, "/get", Post);
15-
METHOD_ADD(ModelController::UpdateModel, "/update/", Post);
16-
METHOD_ADD(ModelController::ImportModel, "/import", Post);
17-
METHOD_ADD(ModelController::DeleteModel, "/{1}", Delete);
18-
METHOD_ADD(ModelController::SetModelAlias, "/alias", Post);
12+
METHOD_ADD(Models::PullModel, "/pull", Post);
13+
METHOD_ADD(Models::ListModel, "", Get);
14+
METHOD_ADD(Models::GetModel, "/get", Post);
15+
METHOD_ADD(Models::UpdateModel, "/update/", Post);
16+
METHOD_ADD(Models::ImportModel, "/import", Post);
17+
METHOD_ADD(Models::DeleteModel, "/{1}", Delete);
18+
METHOD_ADD(Models::SetModelAlias, "/alias", Post);
1919
METHOD_LIST_END
2020

21-
explicit ModelController(const std::shared_ptr<ModelService>& model_service)
21+
explicit Models(const std::shared_ptr<ModelService>& model_service)
2222
: model_service_(model_service) {}
2323

2424
void PullModel(const HttpRequestPtr& req,

engine/main.cc

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#include <drogon/drogon.h>
33
#include <memory>
44
#include "controllers/command_line_parser.h"
5-
#include "controllers/engine_controller.h"
6-
#include "controllers/model_controller.h"
5+
#include "controllers/engines.h"
6+
#include "controllers/events.h"
7+
#include "controllers/models.h"
78
#include "cortex-common/cortexpythoni.h"
89
#include "services/model_service.h"
910
#include "utils/archive_utils.h"
@@ -62,17 +63,27 @@ void RunServer() {
6263
LOG_INFO << "cortex.cpp version: undefined";
6364
#endif
6465

65-
auto download_service = std::make_shared<DownloadService>();
66+
using Event = cortex::event::Event;
67+
using EventQueue = eventpp::EventQueue<std::string, void(const Event&)>;
68+
69+
EventQueue event_queue;
70+
71+
auto download_service = std::make_shared<DownloadService>(
72+
std::make_shared<EventQueue>(event_queue));
6673

6774
// initialize custom controllers
6875
auto engine_service = std::make_shared<EngineService>(download_service);
69-
auto engine_ctl = std::make_shared<EngineController>(engine_service);
76+
auto engine_ctl = std::make_shared<Engines>(engine_service);
7077
drogon::app().registerController(engine_ctl);
7178

7279
auto model_service = std::make_shared<ModelService>(download_service);
73-
auto model_ctl = std::make_shared<ModelController>(model_service);
80+
auto model_ctl = std::make_shared<Models>(model_service);
7481
drogon::app().registerController(model_ctl);
7582

83+
auto event_ctl =
84+
std::make_shared<Events>(std::make_shared<EventQueue>(event_queue));
85+
drogon::app().registerController(event_ctl);
86+
7687
LOG_INFO << "Server started, listening at: " << config.apiServerHost << ":"
7788
<< config.apiServerPort;
7889
LOG_INFO << "Please load your model";

0 commit comments

Comments
 (0)