[go: up one dir, main page]

Skip to content

Commit

Permalink
[services]: some bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
muqiuhan committed Jun 24, 2024
1 parent ffca235 commit 7fe2c46
Show file tree
Hide file tree
Showing 15 changed files with 977 additions and 1,195 deletions.
18 changes: 9 additions & 9 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ BasedOnStyle: LLVM
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
AfterClass: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterStruct: true
AfterUnion: true
AfterClass: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
BeforeCatch: true
BeforeElse: true
AfterControlStatement: true
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
IndentBraces: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Custom
Expand Down
14 changes: 7 additions & 7 deletions build.ninja
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ rule cxx
depfile = $out.d
description = compiling.debug $in

rule as
rule mxx
command = /usr/bin/gcc $ARGS -MMD -MF $out.d -o $out -c $in
deps = gcc
depfile = $out.d
Expand All @@ -33,20 +33,24 @@ rule cc
depfile = $out.d
description = compiling.debug $in

rule mm
rule as
command = /usr/bin/gcc $ARGS -MMD -MF $out.d -o $out -c $in
deps = gcc
depfile = $out.d
description = compiling.debug $in

rule mxx
rule mm
command = /usr/bin/gcc $ARGS -MMD -MF $out.d -o $out -c $in
deps = gcc
depfile = $out.d
description = compiling.debug $in


# rules for linker
rule ar
command = /usr/bin/ar $ARGS $out $in
description = archiving.debug $out

rule sh
command = /usr/bin/g++ -o $out $in $ARGS
description = linking.debug $out
Expand All @@ -55,10 +59,6 @@ rule ld
command = /usr/bin/g++ -o $out $in $ARGS
description = linking.debug $out

rule ar
command = /usr/bin/ar $ARGS $out $in
description = archiving.debug $out


# build targets

Expand Down
16 changes: 8 additions & 8 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@ VV=@
endif

CXX=/usr/bin/gcc
AS=/usr/bin/gcc
MXX=/usr/bin/gcc
RC=/home/muqiu/.cargo/bin/rustc
CU=/usr/bin/clang
CC=/usr/bin/gcc
AS=/usr/bin/gcc
MM=/usr/bin/gcc
MXX=/usr/bin/gcc
RC=/home/muqiu/.cargo/bin/rustc

RCSH=/home/muqiu/.cargo/bin/rustc
AR=/usr/bin/ar
RCAR=/home/muqiu/.cargo/bin/rustc
SH=/usr/bin/g++
RCLD=/home/muqiu/.cargo/bin/rustc
RCSH=/home/muqiu/.cargo/bin/rustc
LD=/usr/bin/g++
RCAR=/home/muqiu/.cargo/bin/rustc
AR=/usr/bin/ar
RCLD=/home/muqiu/.cargo/bin/rustc

autumnbot_LD=/usr/bin/g++
autumnbot_CXX=/usr/bin/gcc
autumnbot_CXX=/usr/bin/gcc

autumnbot_CXXFLAGS=-m64 -g -O0 -std=c++20 -Isrc -DOPENSSL_LOAD_CONF -isystem /home/muqiu/.xmake/packages/s/spdlog/v1.14.1/469f312ffeff47d9aa249e3ed8e400dc/include -isystem /home/muqiu/.xmake/packages/n/nlohmann_json/v3.11.3/ec1183311a224b34b5c138edda96fdde/include -isystem /home/muqiu/.xmake/packages/o/opencv/4.10.0/07fff2ddbc8647d392e888cc2a12d4a4/include/opencv4 -isystem /home/muqiu/.xmake/packages/f/ffmpeg/7.0/927a7605f9ef47db9de73f45b4a37225/include -isystem /home/muqiu/.xmake/packages/l/libdrm/2.4.118/1b3643c7b5084929bfbf9a3bd621b192/include -isystem /home/muqiu/.xmake/packages/l/libdrm/2.4.118/1b3643c7b5084929bfbf9a3bd621b192/include/libdrm -isystem /home/muqiu/.xmake/packages/c/cpr/1.10.5/815208ce0ebc48d69649b40cf563b7c0/include -isystem /home/muqiu/.xmake/packages/l/libcurl/8.7.1/eb037cbc8bf3460aa31aea7c571a7311/include -static
autumnbot_CXXFLAGS=
autumnbot_CXXFLAGS=-m64 -g -O0 -std=c++20 -Isrc -DOPENSSL_LOAD_CONF -isystem /home/muqiu/.xmake/packages/s/spdlog/v1.14.1/469f312ffeff47d9aa249e3ed8e400dc/include -isystem /home/muqiu/.xmake/packages/n/nlohmann_json/v3.11.3/ec1183311a224b34b5c138edda96fdde/include -isystem /home/muqiu/.xmake/packages/o/opencv/4.10.0/07fff2ddbc8647d392e888cc2a12d4a4/include/opencv4 -isystem /home/muqiu/.xmake/packages/f/ffmpeg/7.0/927a7605f9ef47db9de73f45b4a37225/include -isystem /home/muqiu/.xmake/packages/l/libdrm/2.4.118/1b3643c7b5084929bfbf9a3bd621b192/include -isystem /home/muqiu/.xmake/packages/l/libdrm/2.4.118/1b3643c7b5084929bfbf9a3bd621b192/include/libdrm -isystem /home/muqiu/.xmake/packages/c/cpr/1.10.5/815208ce0ebc48d69649b40cf563b7c0/include -isystem /home/muqiu/.xmake/packages/l/libcurl/8.7.1/eb037cbc8bf3460aa31aea7c571a7311/include -static
autumnbot_LDFLAGS=-m64 -L/home/muqiu/.xmake/packages/o/opencv/4.10.0/07fff2ddbc8647d392e888cc2a12d4a4/lib -L/home/muqiu/.xmake/packages/o/opencv/4.10.0/07fff2ddbc8647d392e888cc2a12d4a4/lib/opencv4/3rdparty -L/home/muqiu/.xmake/packages/f/ffmpeg/7.0/927a7605f9ef47db9de73f45b4a37225/lib -L/home/muqiu/.xmake/packages/l/libdrm/2.4.118/1b3643c7b5084929bfbf9a3bd621b192/lib -L/home/muqiu/.xmake/packages/c/cpr/1.10.5/815208ce0ebc48d69649b40cf563b7c0/lib -L/home/muqiu/.xmake/packages/l/libcurl/8.7.1/eb037cbc8bf3460aa31aea7c571a7311/lib -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_saliency -lopencv_wechat_qrcode -lopencv_mcc -lopencv_face -lopencv_img_hash -lopencv_videostab -lopencv_structured_light -lopencv_intensity_transform -lopencv_ccalib -lopencv_line_descriptor -lopencv_stereo -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_fuzzy -lopencv_hfs -lopencv_rapid -lopencv_bgsegm -lopencv_bioinspired -lopencv_rgbd -lopencv_dpm -lopencv_aruco -lopencv_reg -lopencv_tracking -lopencv_datasets -lopencv_xfeatures2d -lopencv_shape -lopencv_superres -lopencv_plot -lopencv_quality -lopencv_text -lopencv_optflow -lopencv_ximgproc -lopencv_xobjdetect -lopencv_xphoto -lopencv_stitching -lopencv_ml -lopencv_photo -lopencv_gapi -lopencv_objdetect -lopencv_highgui -lopencv_videoio -lopencv_video -lopencv_calib3d -lopencv_dnn -lopencv_features2d -lopencv_flann -lopencv_imgcodecs -lopencv_imgproc -lopencv_core -lade -lzlib -llibjpeg-turbo -llibtiff -llibwebp -llibpng -llibprotobuf -lquirc -lavfilter -lavdevice -lavformat -lavcodec -lswscale -lswresample -lavutil -ldrm -ldrm_nouveau -ldrm_radeon -ldrm_amdgpu -lz -lcpr -lcurl -lssl -lcrypto -ldl -lpthread -Wl,-Bsymbolic

default: autumnbot
Expand Down
12 changes: 6 additions & 6 deletions src/errors/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

#include "root/root.hpp"

namespace autumnbot::errors
{
namespace autumnbot::errors {
/** Errors is the parent class of all errors in autumnbot.
** autumnbot globally disables exceptions and uses result<T, E> to handle errors. */
class Error
{
** autumnbot globally disables exceptions and uses result<T, E> to handle
*errors. */
class Error {
public:
explicit Error(std::string msg) : Msg(std::move(msg)) {}
explicit Error(std::string msg)
: Msg(std::move(msg)) {}

virtual ~Error() = default;

Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include <cstdlib>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>

#include "plugins/logger/logger.hpp"
#include "plugins/plugins.hpp"
#include "services/camera/camera.hpp"
#include "services/ollama/ollama.hpp"
#include "services/services.hpp"
#include <opencv2/opencv.hpp>

using namespace autumnbot;

Expand All @@ -17,8 +18,7 @@ const static std::map<std::string, services::Service *> SERVICES = {
{"Camera", reinterpret_cast<services::Service *>(new services::camera::Camera{})},
{"Ollama", reinterpret_cast<services::Service *>(new services::ollama::Ollama{})}};

int main(int argc, char **argv)
{
int main(int argc, char **argv) {
plugins::PluginManager pluginManager{PLUGINS};
services::ServiceManager servicesManager{SERVICES};

Expand Down
16 changes: 6 additions & 10 deletions src/plugins/logger/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include "logger.hpp"

#include <exception>
#include <memory>

namespace autumnbot::plugins::logger
{
auto Logger::Mount() noexcept -> result<void, errors::Error>
{
namespace autumnbot::plugins::logger {
auto Logger::Mount() noexcept -> result<void, errors::Error> {
logging::info("[plugin] <Logger>: Mount");
try
{
Expand All @@ -14,14 +13,11 @@ namespace autumnbot::plugins::logger

return {};
}
catch (const std::exception & exn)
{
return fail(Error(exn.what()));
}
catch (const std::exception &exn)
{ return fail(Error(exn.what())); }
}

auto Logger::Umount() noexcept -> result<void, errors::Error>
{
auto Logger::Umount() noexcept -> result<void, errors::Error> {
logging::info("[plugin] <Logger>: Unount");
return {};
}
Expand Down
15 changes: 5 additions & 10 deletions src/plugins/logger/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@
#include "root/root.hpp"
#include "spdlog/spdlog.h"

namespace autumnbot::plugins::logger
{
class Error : public plugins::Error
{
namespace autumnbot::plugins::logger {
class Error : public plugins::Error {
public:
explicit Error(std::string msg)
: plugins::Error(std::format("[plugin] <Logger> {}", msg))
{}
: plugins::Error(std::format("[plugin] <Logger> {}", msg)) {}
};

class Logger : private plugins::Plugin
{
class Logger : private plugins::Plugin {
public:
explicit Logger(std::string moduleName, std::string pluginName)
: ModuleName(std::move(moduleName))
, plugins::Plugin(std::move(pluginName))
{}
, plugins::Plugin(std::move(pluginName)) {}

auto Mount() noexcept -> result<void, errors::Error> override;
auto Umount() noexcept -> result<void, errors::Error> override;
Expand Down
49 changes: 23 additions & 26 deletions src/plugins/plugins.hpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,69 @@
#ifndef AUTUMNBOT_PLUGINS_HPP
#define AUTUMNBOT_PLUGINS_HPP

#include "root/root.hpp"
#include "errors/errors.hpp"
#include "root/root.hpp"

namespace autumnbot::plugins
{
namespace autumnbot::plugins {

/** All errors that occur in the autumnbot::plugins inherit from this class. */
class Error : public errors::Error
{
/** All errors that occur in the autumnbot::plugins inherit from this class.
*/
class Error : public errors::Error {
public:
explicit Error(std::string msg)
: errors::Error(std::format("[plugin] {}", msg))
{}
: errors::Error(std::format("[plugin] {}", msg)) {}
};

/** Plugin is an abstraction of various built-in plugins in autumnbot,
** such as log plugins, etc. */
class Plugin
{
class Plugin {
public:
/** Mount a plugin. Returns void if successful, or Error with error information if failed. */
/** Mount a plugin. Returns void if successful, or Error with error
* information if failed. */
virtual auto Mount() noexcept -> result<void, errors::Error> = 0;

/** Umount a plugin. Returns void if successful, or Error with error information if failed. */
/** Umount a plugin. Returns void if successful, or Error with error
* information if failed. */
virtual auto Umount() noexcept -> result<void, errors::Error> = 0;

virtual ~Plugin() = default;

explicit Plugin(std::string pluginName)
: PluginName(std::move(pluginName))
{}
: PluginName(std::move(pluginName)) {}

protected:
const std::string PluginName;
};

/** PluginManager is used to unify the mount and umount plugins. */
class PluginManager
{
class PluginManager {
public:
explicit PluginManager(const std::vector<Plugin *> &plugins)
: Plugins(plugins)
{
: Plugins(plugins) {
logging::info("[plugin] <PluginManager>: mount plugins...");

for (const auto &plugin : Plugins)
plugin->Mount()
.map_error([&](const auto &error) {
logging::error(error.Msg);
return error;
}).expect("[plugin] <PluginManager>: error.");
logging::error(error.Msg);
return error;
})
.expect("[plugin] <PluginManager>: error.");

logging::info("[plugin] <PluginManager>: done");
}

~PluginManager()
{
~PluginManager() {
logging::info("[plugin] <PluginManager>: umount plugins...");

for (const auto &plugin : Plugins)
{
plugin->Umount()
.map_error([&](const auto &error) {
logging::error(error.Msg);
return error;
}).expect("[plugin] <PluginManager>: error.");
logging::error(error.Msg);
return error;
})
.expect("[plugin] <PluginManager>: error.");

delete plugin;
}
Expand Down
Loading

0 comments on commit 7fe2c46

Please sign in to comment.