Skip to content

Commit

Permalink
Merge pull request #8 from gaochuang/Add_plugin
Browse files Browse the repository at this point in the history
Add commonapidaemon
  • Loading branch information
gaochuang authored Sep 19, 2024
2 parents d3c3a36 + 32643e7 commit 359118d
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 8 deletions.
6 changes: 3 additions & 3 deletions commonapicontrollableprocess/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ CXXFLAGS = -Wall -Wextra -pthread -O2 -fPIC -std=c++17

LIBNAME = libcommonpicontrollableprocess

INCLUDES = -Iinclude/comapi/controllableProcess -Iinclude/comApi -Iinclude/plugin -Iplugins/commonapicontrollableprocess
INCLUDES = -Iinclude/comapi/controllableProcess -I../include

SRCS = src/controllableProcess.cpp

OBJS = $(SRCS:.cpp=.o)

STATIC_LIB = lib$(LIBNAME).a
SHARED_LIB = lib$(LIBNAME).so
STATIC_LIB = $(LIBNAME).a
SHARED_LIB = $(LIBNAME).so

all: $(STATIC_LIB) $(SHARED_LIB)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef COMMON_API_CONTROLLABLE_PROCESS_HPP_
#define COMMON_API_CONTROLLABLE_PROCESS_HPP_
#ifndef COMMON_API_CONTROLLABLE_PROCESS_HPP
#define COMMON_API_CONTROLLABLE_PROCESS_HPP

#include <functional>
#include <memory>

#include "plugin.hpp"
#include "plugin/plugin.hpp"

namespace reactorFramework
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef COMMON_API_CONTROLLABLE_PROCESS_PLUGIN_HPP
#define COMMON_API_CONTROLLABLE_PROCESS_PLUGIN_HPP


#include "defines.hpp"
#include "plugin/defines.hpp"
#include "controllableProcess.hpp"

#define COMMONAPICONTROLLABLEPROCESS_PLUGIN_CREATOR_NAME commonapi_controllable_process_create_plugin
Expand Down
33 changes: 33 additions & 0 deletions commonapidaemon/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
CXX = g++
CXXFLAGS = -Wall -Wextra -pthread -O2 -fPIC -std=c++17

LIBNAME = libcommonpidaemon

INCLUDES = -Iinclude/ -I../include/ -I../commonapicontrollableprocess/include

SRCS = src/Plugin.cpp

OBJS = $(SRCS:.cpp=.o)

STATIC_LIB = $(LIBNAME).a
SHARED_LIB = $(LIBNAME).so

all: $(STATIC_LIB) $(SHARED_LIB)

$(STATIC_LIB): $(OBJS)
@echo "Creating static library $@"
ar rcs $@ $(OBJS)

$(SHARED_LIB): $(OBJS)
@echo "Creating shared library $@"
$(CXX) -shared -o $@ $(OBJS)

src/%.o: src/%.cpp
@echo "Compiling $< into $@"
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

clean:
@echo "Cleaning up"
rm -f $(OBJS) $(STATIC_LIB) $(SHARED_LIB)

.PHONY: all clean
43 changes: 43 additions & 0 deletions commonapidaemon/include/Plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef COMMON_API_DAEMON_PLUGIN_HPP
#define COMMON_API_DAEMON_PLUGIN_HPP

#include "comapi/controllableProcess/controllableProcess.hpp"

#include "plugin/pluginServices.hpp"

#include <memory>

namespace commonapidaemon
{

class Plugin : public std::enable_shared_from_this<Plugin>,
public reactorFramework::controllableprocess::ControllableProcess
{
public:
Plugin(std::shared_ptr<reactorFramework::PluginServices>);
~Plugin();

void setTerminateCb(const TerminateCb& cb) override;
private:
void handleSigTerm();
void callTerminateCb();

enum class SignalState
{
NONE,
SIGNAL_RECEIVED,
SIGNAL_HANDLED,
};

std::shared_ptr<reactorFramework::PluginServices> pluginServices;
reactorFramework::FdMonitor& fdMonitor;
reactorFramework::signalMonitorService& signalMonitor;
reactorFramework::CallbackQueueService& callbackQueue;

SignalState signalState;
TerminateCb terminateCb;
};

}

#endif
94 changes: 94 additions & 0 deletions commonapidaemon/src/Plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "Plugin.hpp"

#include "plugin/callbackQueueService.hpp"
#include "plugin/fdMonitor.hpp"
#include "plugin/signalMonitorService.hpp"

#include <csignal>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <ostream>
#include <pthread.h>
#include <iostream>

using namespace commonapidaemon;

namespace
{
std::once_flag once;
void unblockSigTerm() noexcept
{
sigset_t sset;
sigemptyset(&sset);
sigaddset(&sset, SIGTERM);
pthread_sigmask(SIG_UNBLOCK, &sset, nullptr);
}

void unblockSigTermAtFork() noexcept
{
pthread_atfork(nullptr, nullptr, unblockSigTerm);
}
}

Plugin::Plugin(std::shared_ptr<reactorFramework::PluginServices> pluginServices):
pluginServices(pluginServices),
fdMonitor(pluginServices->getFdMonitor()),
signalMonitor(pluginServices->getSignalMonitor()),
callbackQueue(pluginServices->getEventCallbackQueue()),
signalState(SignalState::NONE)
{

signalMonitor.add(SIGTERM, std::bind(&Plugin::handleSigTerm, this));
std::call_once(once, unblockSigTermAtFork);
}


Plugin::~Plugin()
{
signalMonitor.del(SIGTERM);
}

void Plugin::setTerminateCb(const TerminateCb& cb)
{
terminateCb = cb;
if(SignalState::SIGNAL_RECEIVED != signalState)
{
return;
}
std::weak_ptr<Plugin> weak(shared_from_this());

callbackQueue.post(
[weak]()
{
if(auto plugin = weak.lock())
{
plugin->callTerminateCb();
}
}
);
}


void Plugin::handleSigTerm()
{
if (signalState != SignalState::NONE)
{
return;
}

signalState = SignalState::SIGNAL_RECEIVED;

callTerminateCb();
}

void Plugin::callTerminateCb()
{
if(!terminateCb)
{
return;
}

signalState = SignalState::SIGNAL_HANDLED;
terminateCb();
}

0 comments on commit 359118d

Please sign in to comment.