Voeg afsluitlogica toe
This commit is contained in:
parent
ae97f07a41
commit
8fec607e4f
|
@ -1,19 +1,15 @@
|
||||||
{
|
{
|
||||||
"name": "mvoau ober",
|
"authors": [
|
||||||
"authors": "Meson Team",
|
"Chris Josten"
|
||||||
"copyright": "Copyright © 2018, Meson Team",
|
|
||||||
"license": [
|
|
||||||
"MIT"
|
|
||||||
],
|
],
|
||||||
"sourceFiles": [
|
"copyright": "Copyright © 2021, Chris Josten",
|
||||||
"source/msgpackrpc/client.d",
|
|
||||||
"source/msgpackrpc/package.d",
|
|
||||||
"source/msgpackrpc/protocol.d",
|
|
||||||
"source/msgpackrpc/server.d",
|
|
||||||
"source/app.d"
|
|
||||||
],
|
|
||||||
"targetType": "executable",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ddbus": "3.0.0"
|
"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": []
|
||||||
}
|
}
|
14
ober/dub.selections.json
Normal file
14
ober/dub.selections.json
Normal file
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,8 +4,9 @@ Wants=network-online.target
|
||||||
After=network-online.target
|
After=network-online.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
|
Environment="WACHTTIJD=15"
|
||||||
Type=idle
|
Type=idle
|
||||||
ExecStart=aiuo-shutdown [wachttijd] [klant-ip]
|
ExecStart=mvoau $WACHTTIJD
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
RestartSec=3
|
RestartSec=3
|
||||||
User=root
|
User=root
|
|
@ -21,22 +21,31 @@ extern (C) void close(int fileNo);
|
||||||
*/
|
*/
|
||||||
struct Toep {
|
struct Toep {
|
||||||
public:
|
public:
|
||||||
immutable ushort PORT = 18_002;
|
immutable float m_waitingTime;
|
||||||
immutable int m_waitingTime;
|
immutable int CHECK_COUNT = 4;
|
||||||
immutable string m_clientAddress;
|
|
||||||
immutable int CHECK_COUNT = 2;
|
|
||||||
Connection m_dbusCon;
|
Connection m_dbusCon;
|
||||||
bool m_keepRunning = true;
|
bool m_keepRunning = true;
|
||||||
version(WithRPC) Client m_client;
|
|
||||||
|
|
||||||
this(int waitingTime, string clientAddress) {
|
this(float waitingTime) {
|
||||||
this.m_waitingTime = waitingTime;
|
this.m_waitingTime = waitingTime / CHECK_COUNT;
|
||||||
this.m_clientAddress = clientAddress;
|
|
||||||
this.m_dbusCon = connectToBus(DBusBusType.DBUS_BUS_SYSTEM);
|
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() {
|
int exec() {
|
||||||
info ("Can shut down: ", canShutdown());
|
info ("Moet blokkeren:", moetBlokkeren());
|
||||||
|
info ("Kan afsluiten:", kanAfsluiten());
|
||||||
version(WithRPC) {
|
version(WithRPC) {
|
||||||
TCPConnection conn = connectTCP(m_clientAddress, PORT);
|
TCPConnection conn = connectTCP(m_clientAddress, PORT);
|
||||||
Duration sleepTime = 500.msecs;
|
Duration sleepTime = 500.msecs;
|
||||||
|
@ -57,7 +66,7 @@ public:
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
runTask({
|
auto taak = runTask({
|
||||||
FileDescriptor inhibitLock = FileDescriptor.none;
|
FileDescriptor inhibitLock = FileDescriptor.none;
|
||||||
version(WithRPC) {
|
version(WithRPC) {
|
||||||
FileDescriptor sleepLock = FileDescriptor.none;
|
FileDescriptor sleepLock = FileDescriptor.none;
|
||||||
|
@ -136,62 +145,105 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
int checkCount = CHECK_COUNT;
|
int checkCount = CHECK_COUNT;
|
||||||
|
int afsluitNakijkTeller = CHECK_COUNT;
|
||||||
|
|
||||||
|
void werkAfsluitNakijkTellerBij() {
|
||||||
|
if (kanAfsluiten()) {
|
||||||
|
afsluitNakijkTeller--;
|
||||||
|
} else {
|
||||||
|
afsluitNakijkTeller = CHECK_COUNT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while(m_keepRunning) {
|
while(m_keepRunning) {
|
||||||
|
sleep(seconds(cast(long) (m_waitingTime * 60)));
|
||||||
// Check if we are already preventing shutdown
|
// Check if we are already preventing shutdown
|
||||||
if (inhibitLock != FileDescriptor.none) {
|
if (inhibitLock != FileDescriptor.none) {
|
||||||
// We are. Check if we can shutdown (as in, no players are on the server)
|
// We zijn op dit moment het afsluiten niet aan het blokkeren. Kijken of dat wel moet.
|
||||||
if (canShutdown()) {
|
if (moetBlokkeren()) {
|
||||||
|
|
||||||
if (checkCount == 1) {
|
if (checkCount == 1) {
|
||||||
// Release the lock
|
// Laat het slot los
|
||||||
releaseLock(inhibitLock);
|
releaseLock(inhibitLock);
|
||||||
info("Stop preventing shutdown");
|
info("Afsluitslot losgelaten.");
|
||||||
} else {
|
} else {
|
||||||
// Check 1? more time
|
// Kijk nog 1 of meer maal
|
||||||
checkCount--;
|
checkCount--;
|
||||||
tracef("Checking %d more time(s)", checkCount);
|
tracef("Nog %d maal kijken of we het afsluitslot los kunnen laten", checkCount);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// We cannot shut down. Reset the check counter.
|
// We kunnen niet afsluiten. De nakijkteller herstellen.
|
||||||
checkCount = CHECK_COUNT;
|
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 {
|
try {
|
||||||
block();
|
block();
|
||||||
|
info("Afsluitslot verkregen.");
|
||||||
} catch(DBusException e) {
|
} catch(DBusException e) {
|
||||||
warning("Could not take lock and prevent sleep/shutdown: ", e);
|
warning("Kon afsluitslot niet verkrijgen: ", e);
|
||||||
}
|
}
|
||||||
info("Start preventing shutdown");
|
werkAfsluitNakijkTellerBij();
|
||||||
|
} else if (kanAfsluiten()){
|
||||||
|
if (afsluitNakijkTeller <= 1) {
|
||||||
|
trace("We kunnen afsluiten!");
|
||||||
|
execute(["shutdown", "+1", "Systeem gaat afsluiten vanwege inactiviteit"]);
|
||||||
|
m_keepRunning = false;
|
||||||
|
exitEventLoop();
|
||||||
} else {
|
} else {
|
||||||
trace("Nothing to do");
|
afsluitNakijkTeller--;
|
||||||
|
tracef("Nog %s keer, dan kunnen we afsluiten!", afsluitNakijkTeller);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
afsluitNakijkTeller = CHECK_COUNT;
|
||||||
|
trace("Niets om te doen.");
|
||||||
}
|
}
|
||||||
sleep(seconds(m_waitingTime));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
int exitCode = runEventLoop();
|
int exitCode = runEventLoop();
|
||||||
// cleanup
|
// cleanup
|
||||||
info("Cleanup");
|
info("Rotzooi opruimen.");
|
||||||
return exitCode;
|
return exitCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the system can shut down.
|
* Kijkt of we het afsluiten moeten blokkeren
|
||||||
*/
|
*/
|
||||||
bool canShutdown() {
|
bool moetBlokkeren() {
|
||||||
auto result = execute(["ss", "-H", "--query=tcp", "state", "established", "sport", "=", ":minecraft"]);
|
auto resultaat = execute(["ss", "-H", "--query=tcp", "state", "established", "sport", "=", ":minecraft"]);
|
||||||
return result.output.splitLines().length == 0;
|
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) {
|
int main(string[] args) {
|
||||||
|
version(WithRPC) {
|
||||||
scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
|
scope(failure) stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
|
||||||
if (args.length < 3) {
|
if (args.length < 3) {
|
||||||
stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
|
stderr.writefln("GEBRUIK: %s WACHTTIJD KLANTADRES", args[0]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
int waitingTime = to!int(args[1]);
|
} else {
|
||||||
return Toep(waitingTime, args[2]).exec();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
module msgpackrpc.client;
|
||||||
import core.atomic;
|
import core.atomic;
|
||||||
|
|
||||||
import std.array;
|
import std.array;
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
module msgpackrpc.protocol;
|
||||||
|
|
||||||
import std.exception;
|
import std.exception;
|
||||||
|
|
||||||
import msgpack;
|
import msgpack;
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
module msgpackrpc.server;
|
Loading…
Reference in a new issue