From 8fec607e4f6a4d1b677f5f76301e727c09590367 Mon Sep 17 00:00:00 2001 From: Chris Josten Date: Tue, 2 Mar 2021 02:28:48 +0100 Subject: [PATCH] Voeg afsluitlogica toe --- ober/dub.json | 32 ++--- ober/dub.selections.json | 14 ++ ober/{aiuo-shutdown.service => mvoau.service} | 3 +- ober/source/app.d | 124 +++++++++++++----- ober/source/msgpackrpc/client.d | 1 + ober/source/msgpackrpc/protocol.d | 2 + ober/source/msgpackrpc/server.d | 1 + 7 files changed, 122 insertions(+), 55 deletions(-) create mode 100644 ober/dub.selections.json rename ober/{aiuo-shutdown.service => mvoau.service} (78%) diff --git a/ober/dub.json b/ober/dub.json index 5e9f9e2..0b52e3b 100644 --- a/ober/dub.json +++ b/ober/dub.json @@ -1,19 +1,15 @@ { - "name": "mvoau ober", - "authors": "Meson Team", - "copyright": "Copyright © 2018, Meson Team", - "license": [ - "MIT" - ], - "sourceFiles": [ - "source/msgpackrpc/client.d", - "source/msgpackrpc/package.d", - "source/msgpackrpc/protocol.d", - "source/msgpackrpc/server.d", - "source/app.d" - ], - "targetType": "executable", - "dependencies": { - "ddbus": "3.0.0" - } -} \ No newline at end of file + "authors": [ + "Chris Josten" + ], + "copyright": "Copyright © 2021, Chris Josten", + "dependencies": { + "ddbus": {"path": "/home/chris/Programmeren/D/ddbus"}, + "msgpack-d": "~>1.0.3", + "vibe-core": "~>1.13.0" + }, + "description": "Automatisch In-Uitschakelen Ober", + "license": "MIT", + "name": "mvoau", + "versions": [] +} diff --git a/ober/dub.selections.json b/ober/dub.selections.json new file mode 100644 index 0000000..299396f --- /dev/null +++ b/ober/dub.selections.json @@ -0,0 +1,14 @@ +{ + "fileVersion": 1, + "versions": { + "ddbus": {"path":"../../../D/ddbus"}, + "dunit": "1.0.16", + "eventcore": "0.9.13", + "libasync": "0.8.6", + "memutils": "1.0.4", + "msgpack-d": "1.0.3", + "stdx-allocator": "2.77.5", + "taggedalgebraic": "0.11.19", + "vibe-core": "1.13.0" + } +} diff --git a/ober/aiuo-shutdown.service b/ober/mvoau.service similarity index 78% rename from ober/aiuo-shutdown.service rename to ober/mvoau.service index 844138f..74ae63a 100644 --- a/ober/aiuo-shutdown.service +++ b/ober/mvoau.service @@ -4,8 +4,9 @@ Wants=network-online.target After=network-online.target [Service] +Environment="WACHTTIJD=15" Type=idle -ExecStart=aiuo-shutdown [wachttijd] [klant-ip] +ExecStart=mvoau $WACHTTIJD Restart=on-failure RestartSec=3 User=root diff --git a/ober/source/app.d b/ober/source/app.d index 8649b47..667a799 100644 --- a/ober/source/app.d +++ b/ober/source/app.d @@ -21,22 +21,31 @@ extern (C) void close(int fileNo); */ struct Toep { public: - immutable ushort PORT = 18_002; - immutable int m_waitingTime; - immutable string m_clientAddress; - immutable int CHECK_COUNT = 2; + immutable float m_waitingTime; + immutable int CHECK_COUNT = 4; Connection m_dbusCon; bool m_keepRunning = true; - version(WithRPC) Client m_client; - - this(int waitingTime, string clientAddress) { - this.m_waitingTime = waitingTime; - this.m_clientAddress = clientAddress; + + this(float waitingTime) { + this.m_waitingTime = waitingTime / CHECK_COUNT; this.m_dbusCon = connectToBus(DBusBusType.DBUS_BUS_SYSTEM); } + version(WithRPC) { + Client m_client; + immutable string m_clientAddress; + immutable ushort PORT = 18_002; + + this(float waitingTime, string clientAddress) { + this.m_waitingTime = waitingTime / CHECK_COUNT; + this.m_clientAddress = clientAddress; + this.m_dbusCon = connectToBus(DBusBusType.DBUS_BUS_SYSTEM); + } + } + int exec() { - info ("Can shut down: ", canShutdown()); + info ("Moet blokkeren:", moetBlokkeren()); + info ("Kan afsluiten:", kanAfsluiten()); version(WithRPC) { TCPConnection conn = connectTCP(m_clientAddress, PORT); Duration sleepTime = 500.msecs; @@ -57,7 +66,7 @@ public: } }); - runTask({ + auto taak = runTask({ FileDescriptor inhibitLock = FileDescriptor.none; version(WithRPC) { FileDescriptor sleepLock = FileDescriptor.none; @@ -136,62 +145,105 @@ public: } int checkCount = CHECK_COUNT; + int afsluitNakijkTeller = CHECK_COUNT; + + void werkAfsluitNakijkTellerBij() { + if (kanAfsluiten()) { + afsluitNakijkTeller--; + } else { + afsluitNakijkTeller = CHECK_COUNT; + } + } + while(m_keepRunning) { + sleep(seconds(cast(long) (m_waitingTime * 60))); // Check if we are already preventing shutdown if (inhibitLock != FileDescriptor.none) { - // We are. Check if we can shutdown (as in, no players are on the server) - if (canShutdown()) { - + // We zijn op dit moment het afsluiten niet aan het blokkeren. Kijken of dat wel moet. + if (moetBlokkeren()) { if (checkCount == 1) { - // Release the lock + // Laat het slot los releaseLock(inhibitLock); - info("Stop preventing shutdown"); + info("Afsluitslot losgelaten."); } else { - // Check 1? more time + // Kijk nog 1 of meer maal checkCount--; - tracef("Checking %d more time(s)", checkCount); + tracef("Nog %d maal kijken of we het afsluitslot los kunnen laten", checkCount); } } else { - // We cannot shut down. Reset the check counter. + // We kunnen niet afsluiten. De nakijkteller herstellen. checkCount = CHECK_COUNT; - tracef("Still players out there. Keeping lock"); + afsluitNakijkTeller = CHECK_COUNT; + tracef("Spelers zijn nog steeds actief. NIet afsluiten."); } - } else if (!canShutdown()) { + werkAfsluitNakijkTellerBij(); + } else if (moetBlokkeren()) { try { block(); + info("Afsluitslot verkregen."); } catch(DBusException e) { - warning("Could not take lock and prevent sleep/shutdown: ", e); + warning("Kon afsluitslot niet verkrijgen: ", e); + } + werkAfsluitNakijkTellerBij(); + } else if (kanAfsluiten()){ + if (afsluitNakijkTeller <= 1) { + trace("We kunnen afsluiten!"); + execute(["shutdown", "+1", "Systeem gaat afsluiten vanwege inactiviteit"]); + m_keepRunning = false; + exitEventLoop(); + } else { + afsluitNakijkTeller--; + tracef("Nog %s keer, dan kunnen we afsluiten!", afsluitNakijkTeller); } - info("Start preventing shutdown"); } else { - trace("Nothing to do"); + afsluitNakijkTeller = CHECK_COUNT; + trace("Niets om te doen."); } - sleep(seconds(m_waitingTime)); } }); int exitCode = runEventLoop(); // cleanup - info("Cleanup"); + info("Rotzooi opruimen."); return exitCode; } /** - * Checks if the system can shut down. + * Kijkt of we het afsluiten moeten blokkeren */ - bool canShutdown() { - auto result = execute(["ss", "-H", "--query=tcp", "state", "established", "sport", "=", ":minecraft"]); - return result.output.splitLines().length == 0; + bool moetBlokkeren() { + auto resultaat = execute(["ss", "-H", "--query=tcp", "state", "established", "sport", "=", ":minecraft"]); + return resultaat.output.splitLines().length != 0; + } + + /** + * Kijkt of we direct af kunnen sluiten. + */ + bool kanAfsluiten() { + auto resultaat = execute(["who"]); + return resultaat.output.splitLines().length != 0; } } int main(string[] args) { - scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]); - if (args.length < 3) { - stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]); - return -1; + version(WithRPC) { + scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]); + if (args.length < 3) { + stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]); + return -1; + } + } else { + scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD", args[0]); + if (args.length < 2) { + stderr.writefln("GEBRUIK: %s WACHTTIJD", args[0]); + return -1; + } + } + float waitingTime = to!float(args[1]); + version(WithRPC) { + return Toep(waitingTime, args[2]).exec(); + } else { + return Toep(waitingTime).exec(); } - int waitingTime = to!int(args[1]); - return Toep(waitingTime, args[2]).exec(); } diff --git a/ober/source/msgpackrpc/client.d b/ober/source/msgpackrpc/client.d index dcaf1db..eb9ea95 100644 --- a/ober/source/msgpackrpc/client.d +++ b/ober/source/msgpackrpc/client.d @@ -1,3 +1,4 @@ +module msgpackrpc.client; import core.atomic; import std.array; diff --git a/ober/source/msgpackrpc/protocol.d b/ober/source/msgpackrpc/protocol.d index 63153ae..0a46cd1 100644 --- a/ober/source/msgpackrpc/protocol.d +++ b/ober/source/msgpackrpc/protocol.d @@ -1,3 +1,5 @@ +module msgpackrpc.protocol; + import std.exception; import msgpack; diff --git a/ober/source/msgpackrpc/server.d b/ober/source/msgpackrpc/server.d index e69de29..648fa83 100644 --- a/ober/source/msgpackrpc/server.d +++ b/ober/source/msgpackrpc/server.d @@ -0,0 +1 @@ +module msgpackrpc.server;