feat(scheduler): begin to implement task scheduler per #8

Signed-off-by: zontreck <tarapiccari@gmail.com>
This commit is contained in:
zontreck 2024-10-05 00:21:22 -07:00
parent 3d924423dd
commit 6fd72c2017
6 changed files with 131 additions and 21 deletions

View file

@ -3,22 +3,14 @@ LM(integer iSignal, string sOp, string sJsonArgs) {
llMessageLinked(LINK_SET, iSignal, llJsonSetValue(sJson, ["script"], llGetScriptName()), ""); llMessageLinked(LINK_SET, iSignal, llJsonSetValue(sJson, ["script"], llGetScriptName()), "");
} }
WriteSetting(string sKey, string sValue) { ScheduleCallback(string sTimerID, integer iSeconds, string sTarget) {
llLinksetDataWrite(sKey, sValue); LM(LINK_SIGNAL_CALLBACKS, OP_CALLBACK_SCHEDULE, llList2Json(JSON_OBJECT, ["timer", iSeconds, "target", sTarget, "id", sTimerID]));
SendSetting(sKey, sValue);
} }
ReadSetting(string sKey) { Callback(string sTarget, string sTimerID) {
string sVal = llLinksetDataRead(sKey); LM(LINK_SIGNAL_CALLBACKS, OP_CALLBACK, llList2Json(JSON_OBJECT, ["target", sTarget, "id", sTimerID]));
if(sVal == "") {
NoSetting(sKey);
}else SendSetting(sKey, sVal);
} }
SendSetting(string sKey, string sVal) { CancelCallback(string sTimerID) {
LM(LINK_SIGNAL_SETTINGS, OP_SETTINGS_RESPONSE, llList2Json(JSON_OBJECT, ["key", sKey, "value", sVal])); LM(LINK_SIGNAL_CALLBACKS, OP_CALLBACK_CANCEL, llList2Json(JSON_OBJECT, ["id", sTimerID]));
}
NoSetting(string sKey) {
LM(LINK_SIGNAL_SETTINGS, OP_SETTINGS_EMPTY, llList2Json(JSON_OBJECT, ["key", sKey]));
} }

View file

@ -1,11 +1,22 @@
integer LINK_SIGNAL_SETTINGS = 0904241; integer LINK_SIGNAL_SETTINGS = 0904241;
integer LINK_SIGNAL_REBOOT = 0904242; integer LINK_SIGNAL_REBOOT = 0904242;
integer LINK_SIGNAL_CALLBACKS = 0904243;
string OP_SETTINGS_WRITE = "settings_write"; string OP_SETTINGS_WRITE = "1";
string OP_SETTINGS_READ = "settings_read"; string OP_SETTINGS_READ = "2";
string OP_SETTINGS_REQUEST = "settings_req"; string OP_SETTINGS_REQUEST = "3";
string OP_SETTINGS_RESPONSE = "settings_resp"; string OP_SETTINGS_RESPONSE = "4";
string OP_SETTINGS_RESPONSE = "settings_empty"; string OP_SETTINGS_RESPONSE = "5";
string OP_SETTINGS_DELETE = "settings_del"; string OP_SETTINGS_DELETE = "6";
string OP_SETTINGS_RESET = "settings_reset"; string OP_SETTINGS_RESET = "7";
string OP_CALLBACK_SCHEDULE = "1";
string OP_CALLBACK_CANCEL = "2";
string OP_CALLBACK_CHK_STATUS = "3";
string OP_CALLBACK_READY = "4";
string OP_CALLBACK = "5";
string EMPTY_JSON = "{}";

View file

@ -0,0 +1,12 @@
/*
This file is a part of NuSystem (https://git.zontreck.com/AriasCreations/NuSystem)
NuSystem is licensed under the GPL.
Please see the Git Source Tree for structured information on changes to indivudual files
Initial Author: Aria (aria@zontreck.com)
*/
#include "../raw/scheduler.lsl"

74
src/raw/scheduler.lsl Normal file
View file

@ -0,0 +1,74 @@
#include "Variables.lsl"
#include "Functions.lsl"
#include "Version.lsl"
#include "../external/AriasCreations/Common.lsl"
#include "../external/AriasCreations/Helpers.lsl"
list g_lCallbacks = []; // TimerID, Timer, DestScript
integer g_iStride = 3;
default
{
state_entry()
{
g_lCallbacks = [];
LM(LINK_SIGNAL_CALLBACKS, OP_CALLBACK_READY, EMPTY_JSON);
llSetTimerEvent(0.5);
}
timer () {
integer i =0;
integer end = llGetListLength(g_lCallbacks);
for(i=0;i<end;i+=g_iStride) {
// Get the timer value, check for expiry
integer iExpires = llList2Integer(g_lCallbacks, i+1);
if(llGetUnixTime() > iExpires) {
// Send the callback signal
}
}
}
link_message(integer iSender, integer iNum, string sMsg, key kID)
{
DEBUG_FUNC(true, "link_message", [iNum, sMsg, kID]);
if(iNum == LINK_SIGNAL_CALLBACKS) {
string sOp = llJsonGetValue(sMsg, ["op"]);
switch(sOp) {
case OP_CALLBACK_SCHEDULE: {
DEBUG_FUNC(true, "OP_CALLBACK_SCHEDULE", []);
g_lCallbacks += [llJsonGetValue(sMsg, ["id"]), llGetUnixTime() + (integer)llJsonGetValue(sMsg, ["timer"]), llJsonGetValue(sMsg, ["target"])];
DEBUG_FUNC(false, "OP_CALLBACK_SCHEDULE", []);
break;
}
case OP_CALLBACK_CANCEL: {
DEBUG_FUNC(true, "OP_CALLBACK_CANCEL", []);
// Cancel the callback if it exists
integer iIndex = llListFindList(g_lCallbacks, [llJsonGetValue(sMsg, ["id"])]);
if(iIndex!=-1) {
// Cancel it
g_lCallbacks = llDeleteSubList(g_lCallbacks, iIndex, iIndex+g_iStride);
}
DEBUG_FUNC(false, "OP_CALLBACK_CANCEL", []);
break;
}
case OP_CALLBACK_CHK_STATUS: {
DEBUG_FUNC(true, "OP_CALLBACK_CHECK_STATUS", []);
LM(LINK_SIGNAL_CALLBACKS, OP_CALLBACK_READY, EMPTY_JSON);
DEBUG_FUNC(false, "OP_CALLBACK_CHECK_STATUS", []);
break;
}
}
}
DEBUG_FUNC(false, "link_message", []);
}
}

View file

@ -19,6 +19,27 @@ reqAllSettings(){
DEBUG_FUNC(false, "req_all_settings", []); DEBUG_FUNC(false, "req_all_settings", []);
} }
WriteSetting(string sKey, string sValue) {
llLinksetDataWrite("nusetting." + sKey, sValue);
SendSetting(sKey, sValue);
}
ReadSetting(string sKey) {
string sVal = llLinksetDataRead("nusetting." + sKey);
if(sVal == "") {
NoSetting(sKey);
}else SendSetting(sKey, sVal);
}
SendSetting(string sKey, string sVal) {
LM(LINK_SIGNAL_SETTINGS, OP_SETTINGS_RESPONSE, llList2Json(JSON_OBJECT, ["key", sKey, "value", sVal]));
}
NoSetting(string sKey) {
LM(LINK_SIGNAL_SETTINGS, OP_SETTINGS_EMPTY, llList2Json(JSON_OBJECT, ["key", sKey]));
}
default default
{ {
state_entry() { state_entry() {