Begin implementing nanosm main logic
This commit is contained in:
parent
753164b393
commit
44160204e2
|
@ -14,7 +14,11 @@ A more robust solution that's still small and easy to setup (read: Not written i
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
TODO
|
```bash
|
||||||
|
$ cmake -Wno-dev -GNinja -Bbuild -DCMAKE_INSTALL_PREFIX=/usr
|
||||||
|
$ ninja -C build
|
||||||
|
# ninja -C build install
|
||||||
|
```
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
|
|
||||||
|
|
|
@ -2,17 +2,13 @@
|
||||||
|
|
||||||
[nanosm]
|
[nanosm]
|
||||||
|
|
||||||
# Controls if nanosm should be more verbose. Defaults to false.
|
# The time in seconds nanosm will wait before restarting any app which crashes.
|
||||||
verbose = false
|
|
||||||
|
|
||||||
# The time in seconds nanosm will wait before restarting any app which exits.
|
|
||||||
restart-time = 1
|
restart-time = 1
|
||||||
|
|
||||||
|
|
||||||
# Any applications you want to run at startup.
|
# Any applications you want to run at startup.
|
||||||
# Note that applications are executed in the order they are declared, but
|
# Note that applications are executed in the order they are declared
|
||||||
# this will not hold true if any (or all apps) crash (they will be restarted
|
# only once they are first started, and will from then on start in the
|
||||||
# effectively in a psuedorandom order).
|
# order that they happen to crash. This may not be great but is how it be for now.
|
||||||
[nanosm.apps]
|
[nanosm.apps]
|
||||||
# The window manager you want to use.
|
# The window manager you want to use.
|
||||||
window-manager = { command = "/path/to/wm/binary --any-additional-args-here" }
|
window-manager = { command = "/path/to/wm/binary --any-additional-args-here" }
|
||||||
|
|
|
@ -51,7 +51,7 @@ namespace nanosm {
|
||||||
|
|
||||||
execvp(exp.words[0].data(), argv.data());
|
execvp(exp.words[0].data(), argv.data());
|
||||||
} else {
|
} else {
|
||||||
// Parent: monitor FD
|
// Parent: monitor the pidfd by adding ourselves to the event loop now
|
||||||
eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
|
eventLoop.AddObject(IoObject::Ptr(shared_from_this()));
|
||||||
if(onProcessSpawn)
|
if(onProcessSpawn)
|
||||||
eventLoop.Post(onProcessSpawn);
|
eventLoop.Post(onProcessSpawn);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
namespace nanosm {
|
namespace nanosm {
|
||||||
|
|
||||||
|
/// A timerfd backed timer. Is always oneshot.
|
||||||
struct Timer : nanosm::EventLoop::IoObject, std::enable_shared_from_this<Timer> {
|
struct Timer : nanosm::EventLoop::IoObject, std::enable_shared_from_this<Timer> {
|
||||||
Timer(nanosm::EventLoop& ev);
|
Timer(nanosm::EventLoop& ev);
|
||||||
|
|
||||||
|
@ -15,8 +16,12 @@ namespace nanosm {
|
||||||
|
|
||||||
void OnReady(int eventMask) override;
|
void OnReady(int eventMask) override;
|
||||||
|
|
||||||
|
/// Set the callback used to notify when the timer expires.
|
||||||
void SetExpiryCallback(std::function<void()> expire);
|
void SetExpiryCallback(std::function<void()> expire);
|
||||||
|
|
||||||
|
/// Arm and start the timer.
|
||||||
void Arm(u32 durationSeconds);
|
void Arm(u32 durationSeconds);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int timerFd { -1 };
|
int timerFd { -1 };
|
||||||
std::function<void()> cb;
|
std::function<void()> cb;
|
||||||
|
|
69
src/main.cpp
69
src/main.cpp
|
@ -1,33 +1,68 @@
|
||||||
|
|
||||||
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "EventLoop.hpp"
|
#include "EventLoop.hpp"
|
||||||
#include "Process.hpp"
|
|
||||||
#include "RestartingProcess.hpp"
|
#include "RestartingProcess.hpp"
|
||||||
#include "Timer.hpp"
|
|
||||||
|
|
||||||
nanosm::EventLoop ev;
|
struct NanoSm {
|
||||||
|
struct App : std::enable_shared_from_this<App> {
|
||||||
|
App(nanosm::EventLoop& ev, const std::string& tomlId, const std::string& commandLine)
|
||||||
|
: process(std::make_shared<nanosm::RestartingProcess>(ev)),
|
||||||
|
id(tomlId),
|
||||||
|
commandLine(commandLine) {
|
||||||
|
}
|
||||||
|
|
||||||
auto process = std::make_shared<nanosm::RestartingProcess>(ev);
|
void Spawn() {
|
||||||
|
process->SetSpawnCallback([self = shared_from_this()](pid_t pid) {
|
||||||
|
printf("\"%s\": command spawned as pid %d successfully\n", self->id.c_str(), pid);
|
||||||
|
|
||||||
// tests stuff :)
|
// Set exit callback too
|
||||||
void test() {
|
self->process->SetExitCallback([self](pid_t pid, int exitCode) {
|
||||||
process->SetSpawnCallback([](pid_t pid) {
|
printf("\"%s\": pid %d exited with %d exitcode\n", self->id.c_str(), pid, exitCode);
|
||||||
printf("\"%s\" spawned as pid %d successfully\n", process->GetCommandLine().c_str(), pid);
|
});
|
||||||
// Do magic
|
});
|
||||||
process->SetExitCallback([](pid_t pid, int exitCode) {
|
|
||||||
printf("\"%s\" pid %d exited with %d exitcode\n", process->GetCommandLine().c_str(), pid, exitCode);
|
process->Spawn(commandLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<nanosm::RestartingProcess> process;
|
||||||
|
std::string id;
|
||||||
|
std::string commandLine;
|
||||||
|
};
|
||||||
|
|
||||||
|
void AddApp(const std::string& id, const std::string& cmdLine) {
|
||||||
|
apps.push_back(std::make_shared<App>(ev, id, cmdLine));
|
||||||
|
}
|
||||||
|
|
||||||
|
void SpawnAllApps() {
|
||||||
|
for(auto& app : apps)
|
||||||
|
app->Spawn();
|
||||||
|
}
|
||||||
|
|
||||||
|
int Run() {
|
||||||
|
ev.Post([this]() {
|
||||||
|
this->SpawnAllApps();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
process->Spawn("xterm");
|
ev.Run();
|
||||||
}
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
nanosm::EventLoop ev;
|
||||||
|
std::vector<std::shared_ptr<App>> apps;
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
ev.Post(test);
|
NanoSm nanosm;
|
||||||
|
|
||||||
ev.Run();
|
// Run a few hardcoded apps (as a test)
|
||||||
return 0;
|
nanosm.AddApp("Xterm", "xterm");
|
||||||
|
nanosm.AddApp("Xterm2", "xterm");
|
||||||
|
|
||||||
|
return nanosm.Run();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue