add spawn callback to Process
This commit is contained in:
parent
4c1480d364
commit
ef904f5403
|
@ -53,6 +53,8 @@ namespace nanosm {
|
||||||
} else {
|
} else {
|
||||||
// Parent: monitor FD
|
// Parent: monitor FD
|
||||||
eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
|
eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
|
||||||
|
if(onProcessSpawn)
|
||||||
|
eventLoop.Post(onProcessSpawn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,6 +79,10 @@ namespace nanosm {
|
||||||
kill(pid, SIGTERM);
|
kill(pid, SIGTERM);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Process::SetSpawnCallback(std::function<void()> f) {
|
||||||
|
onProcessSpawn = f;
|
||||||
|
}
|
||||||
|
|
||||||
void Process::SetExitCallback(std::function<void(int)> f) {
|
void Process::SetExitCallback(std::function<void(int)> f) {
|
||||||
onProcessExit = f;
|
onProcessExit = f;
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@ namespace nanosm {
|
||||||
|
|
||||||
void Kill();
|
void Kill();
|
||||||
|
|
||||||
|
|
||||||
|
void SetSpawnCallback(std::function<void()> f);
|
||||||
|
|
||||||
void SetExitCallback(std::function<void(int)> f);
|
void SetExitCallback(std::function<void(int)> f);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -35,6 +38,7 @@ namespace nanosm {
|
||||||
siginfo_t siginfo {};
|
siginfo_t siginfo {};
|
||||||
std::string commLine;
|
std::string commLine;
|
||||||
|
|
||||||
|
std::function<void()> onProcessSpawn;
|
||||||
std::function<void(int)> onProcessExit;
|
std::function<void(int)> onProcessExit;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,9 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "EventLoop.hpp"
|
#include "EventLoop.hpp"
|
||||||
#include "WordExp.hpp"
|
|
||||||
|
|
||||||
#include "Timer.hpp"
|
|
||||||
#include "Process.hpp"
|
#include "Process.hpp"
|
||||||
|
#include "Timer.hpp"
|
||||||
|
#include "WordExp.hpp"
|
||||||
|
|
||||||
nanosm::EventLoop ev;
|
nanosm::EventLoop ev;
|
||||||
|
|
||||||
|
@ -17,7 +16,7 @@ auto timer = std::make_shared<nanosm::Timer>(ev);
|
||||||
auto process = std::make_shared<nanosm::Process>(ev);
|
auto process = std::make_shared<nanosm::Process>(ev);
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
|
process->SetSpawnCallback([p = process]() {
|
||||||
// Do magic
|
// Do magic
|
||||||
process->SetExitCallback([p = process](int exitCode) {
|
process->SetExitCallback([p = process](int exitCode) {
|
||||||
printf("exited with %d exitcode\n", exitCode);
|
printf("exited with %d exitcode\n", exitCode);
|
||||||
|
@ -30,6 +29,7 @@ void test() {
|
||||||
// Start the timer to wait a bit before restarting the process
|
// Start the timer to wait a bit before restarting the process
|
||||||
timer->Arm(1);
|
timer->Arm(1);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
ev.AddObject(timer);
|
ev.AddObject(timer);
|
||||||
process->Spawn("xterm");
|
process->Spawn("xterm");
|
||||||
|
|
Loading…
Reference in New Issue