[MOD/WIP] Externalise and add errors to commands

Still need forms...
This commit is contained in:
LDA 2024-07-13 22:34:51 +02:00
commit a880769c48
9 changed files with 311 additions and 89 deletions

View file

@ -1565,78 +1565,6 @@ ParseeCongestion(void)
return congestion;
}
static void
StatusCallback(XMPPCommandManager *m, HashMap *data, XMLElement *out)
{
size_t alloc = MemoryAllocated();
size_t kb = alloc >> 10;
size_t mb = kb >> 10;
size_t gb = mb >> 10;
size_t min = gb ? gb : (mb ? mb : (kb ? kb : alloc));
char *unit = gb ? "GB" : (mb ? "MB" : (kb ? "KB" : "B"));
XMLElement *x = XMLCreateTag("x");
XMLElement *title = XMLCreateTag("title");
{
XMLElement *title_text = XMLCreateText("Parsee statistics");
XMLAddChild(title, title_text);
}
XMLAddChild(x, title);
XMLAddAttr(x, "xmlns", "jabber:x:data");
XMLAddAttr(x, "type", "result");
{
XMLElement *reported, *item, *field, *value, *txt;
reported = XMLCreateTag("reported");
XMLAddChild(x, reported);
#define Report(id, label) do \
{ \
field = XMLCreateTag("field"); \
XMLAddAttr(field, "var", id); \
XMLAddAttr(field, "label", label); \
XMLAddChild(reported, field); \
} \
while(0)
#define BeginItem() item = XMLCreateTag("item")
#define EndItem() XMLAddChild(x, item)
#define SetField(id, val) do \
{ \
field = XMLCreateTag("field"); \
value = XMLCreateTag("value"); \
txt = XMLCreateText(val); \
XMLAddAttr(field, "var", id); \
XMLAddChild(value, txt); \
XMLAddChild(field, value); \
XMLAddChild(item, field); \
} \
while(0)
/* Report */
Report("mem-alloc", "Heap allocated with Cytoplasm");
Report("xml-congest", "Unprocessed stanzas(congestion)");
/* Set */
BeginItem();
{
char *min_str = StrInt(min);
char *congest = StrInt(ParseeCongestion());
char *alloc = StrConcat(3, min_str, " ", unit);
SetField("mem-alloc", alloc);
SetField("xml-congest", congest);
Free(congest);
Free(min_str);
Free(alloc);
}
EndItem();
#undef SetField
#undef EndItem
#undef BeginItem
#undef Report
}
XMLAddChild(out, x);
}
void *
ParseeXMPPThread(void *argp)
{
@ -1649,15 +1577,17 @@ ParseeXMPPThread(void *argp)
await_table = HashMapCreate();
/* Initialise the command manager, and add all ad-hoc commands */
info.m = XMPPCreateManager();
info.m = XMPPCreateManager(args);
{
XMPPCommand *cmd;
cmd = XMPPBasicCmd(
"status", "Get status about Parsee",
StatusCallback
);
#define XMPP_COMMAND(f,n,t,s) \
cmd = XMPPBasicCmd( \
n, t, f \
); \
s \
XMPPRegisterCommand(info.m, cmd);
XMPPCOMMANDS
#undef XMPP_COMMAND
}
/* Initialise the FIFO */