corrade-lsl-templates – Blame information for rev 42

Subversion Repositories:
Rev:
Rev Author Line No. Line
27 office 1 ///////////////////////////////////////////////////////////////////////////
29 office 2 // Copyright (C) Wizardry and Steamworks 2016 - License: CC BY 2.0 //
27 office 3 ///////////////////////////////////////////////////////////////////////////
4 //
5 // A module that sends the current Corrade heartbeat to group chat.
6 //
7 ///////////////////////////////////////////////////////////////////////////
8  
9 ///////////////////////////////////////////////////////////////////////////
33 office 10 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 11 ///////////////////////////////////////////////////////////////////////////
12 string wasKeyValueGet(string k, string data) {
13 if(llStringLength(data) == 0) return "";
14 if(llStringLength(k) == 0) return "";
41 office 15 list a = llParseStringKeepNulls(data, ["&", "="], []);
33 office 16 integer i = llListFindList(llList2ListStrided(a, 0, -1, 2), [ k ]);
17 if(i != -1) return llList2String(a, 2*i+1);
27 office 18 return "";
19 }
42 office 20  
27 office 21 ///////////////////////////////////////////////////////////////////////////
29 office 22 // Copyright (C) 2013 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 23 ///////////////////////////////////////////////////////////////////////////
24 string wasKeyValueEncode(list data) {
25 list k = llList2ListStrided(data, 0, -1, 2);
26 list v = llList2ListStrided(llDeleteSubList(data, 0, 0), 0, -1, 2);
27 data = [];
28 do {
29 data += llList2String(k, 0) + "=" + llList2String(v, 0);
30 k = llDeleteSubList(k, 0, 0);
31 v = llDeleteSubList(v, 0, 0);
32 } while(llGetListLength(k) != 0);
33 return llDumpList2String(data, "&");
34 }
35  
36 ///////////////////////////////////////////////////////////////////////////
29 office 37 // Copyright (C) 2011 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 38 ///////////////////////////////////////////////////////////////////////////
39 // http://was.fm/secondlife/wanderer
40 vector wasCirclePoint(float radius) {
41 float x = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
42 float y = llPow(-1, 1 + (integer) llFrand(2)) * llFrand(radius*2);
43 if(llPow(x,2) + llPow(y,2) <= llPow(radius,2))
44 return <x, y, 0>;
45 return wasCirclePoint(radius);
46 }
47  
48 ///////////////////////////////////////////////////////////////////////////
29 office 49 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 50 ///////////////////////////////////////////////////////////////////////////
51 // escapes a string in conformance with RFC1738
52 string wasURLEscape(string i) {
53 string o = "";
54 do {
55 string c = llGetSubString(i, 0, 0);
56 i = llDeleteSubString(i, 0, 0);
57 if(c == "") jump continue;
58 if(c == " ") {
59 o += "+";
60 jump continue;
61 }
62 if(c == "\n") {
63 o += "%0D" + llEscapeURL(c);
64 jump continue;
65 }
66 o += llEscapeURL(c);
67 @continue;
68 } while(i != "");
69 return o;
70 }
71  
72 ///////////////////////////////////////////////////////////////////////////
29 office 73 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 74 ///////////////////////////////////////////////////////////////////////////
75 list wasCSVToList(string csv) {
76 list l = [];
77 list s = [];
78 string m = "";
79 do {
80 string a = llGetSubString(csv, 0, 0);
81 csv = llDeleteSubString(csv, 0, 0);
82 if(a == ",") {
83 if(llList2String(s, -1) != "\"") {
84 l += m;
85 m = "";
86 jump continue;
87 }
88 m += a;
89 jump continue;
90 }
91 if(a == "\"" && llGetSubString(csv, 0, 0) == a) {
92 m += a;
93 csv = llDeleteSubString(csv, 0, 0);
94 jump continue;
95 }
96 if(a == "\"") {
97 if(llList2String(s, -1) != a) {
98 s += a;
99 jump continue;
100 }
101 s = llDeleteSubList(s, -1, -1);
102 jump continue;
103 }
104 m += a;
105 @continue;
106 } while(csv != "");
107 // postcondition: length(s) = 0
108 return l + m;
109 }
110  
111 ///////////////////////////////////////////////////////////////////////////
29 office 112 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 113 ///////////////////////////////////////////////////////////////////////////
114 string wasListToCSV(list l) {
115 list v = [];
116 do {
117 string a = llDumpList2String(
118 llParseStringKeepNulls(
119 llList2String(
42 office 120 l,
27 office 121  
42 office 122 ),
123 ["\""],
27 office 124 []
125 ),
126 "\"\""
127 );
128 if(llParseStringKeepNulls(
42 office 129 a,
27 office 130 [" ", ",", "\n", "\""], []
42 office 131 ) !=
27 office 132 (list) a
133 ) a = "\"" + a + "\"";
134 v += a;
135 l = llDeleteSubList(l, 0, 0);
136 } while(l != []);
137 return llDumpList2String(v, ",");
138 }
139  
140 ///////////////////////////////////////////////////////////////////////////
29 office 141 // Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 142 ///////////////////////////////////////////////////////////////////////////
143 // unescapes a string in conformance with RFC1738
144 string wasURLUnescape(string i) {
145 return llUnescapeURL(
146 llDumpList2String(
147 llParseString2List(
148 llDumpList2String(
149 llParseString2List(
42 office 150 i,
151 ["+"],
27 office 152 []
42 office 153 ),
27 office 154 " "
42 office 155 ),
156 ["%0D%0A"],
27 office 157 []
42 office 158 ),
27 office 159 "\n"
160 )
161 );
162 }
163  
164 ///////////////////////////////////////////////////////////////////////////
29 office 165 // Copyright (C) 2017 Wizardry and Steamworks - License: CC BY 2.0 //
27 office 166 ///////////////////////////////////////////////////////////////////////////
167 list wasSetIntersect(list a, list b) {
168 if(llGetListLength(a) == 0) return [];
169 string i = llList2String(a, 0);
170 a = llDeleteSubList(a, 0, 0);
171 if(llListFindList(b, (list)i) == -1)
172 return wasSetIntersect(a, b);
173 return i + wasSetIntersect(a, b);
174 }
175  
176 // configuration data
177 string configuration = "";
178 // callback URL
179 string URL = "";
180 // store message over state.
181 string data = "";
182  
183 default {
184 state_entry() {
185 llOwnerSay("[Heartbeat] Starting...");
186 llSetTimerEvent(10);
187 }
188 link_message(integer sender, integer num, string message, key id) {
189 if(id != "configuration") return;
190 llOwnerSay("[Heartbeat] Got configuration...");
191 configuration = message;
192 state listen_group;
193 }
194 timer() {
195 llOwnerSay("[Heartbeat] Requesting configuration...");
196 llMessageLinked(LINK_THIS, 0, "configuration", NULL_KEY);
197 }
198 on_rez(integer num) {
199 llResetScript();
200 }
201 changed(integer change) {
42 office 202 if((change & CHANGED_INVENTORY) ||
203 (change & CHANGED_REGION_START) ||
27 office 204 (change & CHANGED_OWNER)) {
205 llResetScript();
206 }
207 }
208 state_exit() {
209 llSetTimerEvent(0);
210 }
211 }
212  
213 state listen_group {
214 state_entry() {
215 // DEBUG
216 llOwnerSay("[Heartbeat] Waiting for group messages...");
217 }
218 link_message(integer sender, integer num, string message, key id) {
219 // We only care about notifications now.
220 if(id != "notification")
221 return;
42 office 222  
27 office 223 // This script only processes group notifications.
33 office 224 if(wasKeyValueGet("type", message) != "group" ||
225 (wasKeyValueGet("type", message) == "group" &&
42 office 226 wasURLUnescape(wasKeyValueGet("group", message)) !=
33 office 227 wasKeyValueGet("group", configuration)))
27 office 228 return;
42 office 229  
27 office 230 // Get the sent message.
231 data = wasURLUnescape(
232 wasKeyValueGet(
42 office 233 "message",
27 office 234 message
235 )
236 );
42 office 237  
27 office 238 // Check if this is an eggdrop command.
42 office 239 if(llGetSubString(data, 0, 0) !=
27 office 240 wasKeyValueGet("command", configuration))
241 return;
42 office 242  
27 office 243 // Check if the command matches the current module.
244 list command = llParseString2List(data, [" "], []);
42 office 245 if(llList2String(command, 0) !=
27 office 246 wasKeyValueGet("command", configuration) + "heartbeat")
247 return;
42 office 248  
27 office 249 // Remove command.
250 command = llDeleteSubList(command, 0, 0);
251  
42 office 252 // Get heartbeat.
253 state heartbeat;
27 office 254 }
255 on_rez(integer num) {
256 llResetScript();
257 }
258 changed(integer change) {
42 office 259 if((change & CHANGED_INVENTORY) ||
260 (change & CHANGED_REGION_START) ||
27 office 261 (change & CHANGED_OWNER)) {
262 llResetScript();
263 }
264 }
265 }
266  
42 office 267 state heartbeat {
27 office 268 state_entry() {
269 // DEBUG
270 llOwnerSay("[Heartbeat] Getting heartbeat data...");
271 llInstantMessage(
272 wasKeyValueGet(
42 office 273 "corrade",
27 office 274 configuration
42 office 275 ),
27 office 276 wasKeyValueEncode(
277 [
278 "command", "getheartbeatdata",
279 "group", wasURLEscape(
280 wasKeyValueGet(
42 office 281 "group",
27 office 282 configuration
283 )
284 ),
285 "password", wasURLEscape(
286 wasKeyValueGet(
42 office 287 "password",
27 office 288 configuration
289 )
290 ),
291 "data", wasListToCSV(
292 [
293 "AverageCPUUsage",
33 office 294 "AverageRAMUsage",
42 office 295 "AverageNETUsage",
33 office 296 "Uptime"
27 office 297 ]
298 ),
42 office 299 "callback", wasURLEscape(
300 wasKeyValueGet(
301 "URL",
302 configuration
303 )
304 )
27 office 305 ]
306 )
307 );
308 llSetTimerEvent(60);
309 }
42 office 310 link_message(integer sender, integer num, string body, key id) {
311 // Only process callbacks for the database command.
312 if(id != "callback" || wasKeyValueGet("command", body) != "getheartbeatdata")
313 return;
314  
315 if(wasKeyValueGet("success", body) != "True") {
27 office 316 // DEBUG
42 office 317 llOwnerSay("[Heartbeat] Unable to get heartbeat data: " +
27 office 318 wasURLUnescape(
319 wasKeyValueGet("error", body)
320 )
321 );
322 state listen_group;
323 }
42 office 324  
27 office 325 list stats = wasCSVToList(
326 wasURLUnescape(
327 wasKeyValueGet(
328 "data",
329 body
330 )
331 )
332 );
42 office 333  
27 office 334 string CPU = llList2String(
335 stats,
336 llListFindList(
42 office 337 stats,
27 office 338 ["AverageCPUUsage"]
339 ) + 1
340 );
42 office 341  
27 office 342 // AverageRAMUsage is returned in bytes,
343 // so convert the value to MiB and round.
344 string RAM = (string)llRound(
345 llList2Float(
346 stats,
347 llListFindList(
42 office 348 stats,
27 office 349 ["AverageRAMUsage"]
350 ) + 1
351 ) / 1024 / 1024
352 );
42 office 353  
354 // AverageNETUsage is returned in bytes,
355 // so convert the value to MiB and round.
356 string NET = (string)llRound(
357 llList2Float(
358 stats,
359 llListFindList(
360 stats,
361 ["AverageNETUsage"]
362 ) + 1
363 ) / 1024 / 1024
364 );
365  
33 office 366 string uptime = llList2String(
367 stats,
368 llListFindList(
42 office 369 stats,
33 office 370 ["Uptime"]
371 ) + 1
372 );
42 office 373  
374 data = "RAM: " + RAM + "MiB" + " CPU: " + CPU + "%" + " NET: " + NET + "MiB" + " Uptime: " + uptime;
375  
27 office 376 state tell;
377 }
378 timer() {
379 state listen_group;
380 }
381 on_rez(integer num) {
382 llResetScript();
383 }
384 changed(integer change) {
42 office 385 if((change & CHANGED_INVENTORY) ||
386 (change & CHANGED_REGION_START) ||
27 office 387 (change & CHANGED_OWNER)) {
388 llResetScript();
389 }
390 }
391 state_exit() {
392 llSetTimerEvent(0);
393 }
394 }
395  
396 state tell {
397 state_entry() {
398 // DEBUG
399 llOwnerSay("[Heartbeat] Sending to group.");
400 llInstantMessage(
401 wasKeyValueGet(
42 office 402 "corrade",
27 office 403 configuration
42 office 404 ),
27 office 405 wasKeyValueEncode(
406 [
407 "command", "tell",
408 "group", wasURLEscape(
409 wasKeyValueGet(
42 office 410 "group",
27 office 411 configuration
412 )
413 ),
414 "password", wasURLEscape(
415 wasKeyValueGet(
42 office 416 "password",
27 office 417 configuration
418 )
419 ),
420 "entity", "group",
421 "message", wasURLEscape(data)
422 ]
423 )
424 );
425 state listen_group;
426 }
427 }