-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from gaochuang/Add_plugin
Add commonapidaemon
- Loading branch information
Showing
6 changed files
with
177 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
commonapicontrollableprocess/include/comapi/controllableProcess/controllableProcess.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...onapicontrollableprocess/include/comapi/controllableProcess/controllableProcessPlugin.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |