WIP: Response handling

This commit is contained in:
Tristan B. Kildaire 2020-04-24 17:12:25 +02:00
parent 836e4cdc80
commit 16795c3be5
4 changed files with 65 additions and 44 deletions

BIN
besterd-test-library Executable file

Binary file not shown.

View File

@ -1,14 +1,15 @@
{ {
"network" : { "network" : {
"types" : ["unix", "tcp4", "tcp6"],
"unix" : { "unix" : {
"address" : "besterUNIXSock" "address" : "besterUNIXSock"
}, },
"tcp4" : { "tcp4" : {
"port" : "2228", "port" : "2220",
"address" : "0.0.0.0" "address" : "0.0.0.0"
}, },
"tcp6" : { "tcp6" : {
"port" : "2229", "port" : "2221",
"address" : "::" "address" : "::"
} }
}, },

View File

@ -679,52 +679,63 @@ private class BesterConnection : Thread
JSONValue headerBlock = handlerResponse["header"]; JSONValue headerBlock = handlerResponse["header"];
/* Get the status */ /* Get the status */
ulong statusCode = to!(ulong)(headerBlock["status"].str); ulong statusCode = to!(ulong)(headerBlock["status"].str());
debugPrint("Status code: " ~ to!(string)(statusCode)); debugPrint("Status code: " ~ to!(string)(statusCode));
/* The command block */ /* If the status is 0, then it is all fine */
JSONValue commandBlock = headerBlock["command"]; if(statusCode == 0)
/**
* Get the command that the message handler wants the
* server to run.
*/
string serverCommand = commandBlock["type"].str;
debugPrint("Handler->Server command: \"" ~ serverCommand ~ "\"");
/* Check the command to be run */
if(cmp(serverCommand, "sendClients") == 0)
{ {
/* Get the list of clients to send to */ debugPrint("Status is fine, the handler ran correctly");
string[] clients;
JSONValue[] clientList = commandBlock["data"].array();
for(ulong i = 0; i < clientList.length; i++)
{
clients ~= clientList[i].str();
}
/* TODO: Implement me */
writeln("Users wanting to send to ", clients);
}
else if(cmp(serverCommand, "sendServers") == 0)
{
/* Get the list of clients to send to */
string[] clients;
JSONValue[] clientList = commandBlock["data"].array();
for(ulong i = 0; i < clientList.length; i++)
{
clients ~= clientList[i].str();
}
/* TODO: Implement me */ /* The command block */
writeln("Users wanting to send to ", clients); JSONValue commandBlock = headerBlock["command"];
/**
* Get the command that the message handler wants the
* server to run.
*/
string serverCommand = commandBlock["type"].str;
debugPrint("Handler->Server command: \"" ~ serverCommand ~ "\"");
/* Check the command to be run */
if(cmp(serverCommand, "sendClients") == 0)
{
/* Get the list of clients to send to */
string[] clients;
JSONValue[] clientList = commandBlock["data"].array();
for(ulong i = 0; i < clientList.length; i++)
{
clients ~= clientList[i].str();
}
/* TODO: Implement me */
writeln("Users wanting to send to ", clients);
}
else if(cmp(serverCommand, "sendServers") == 0)
{
/* Get the list of clients to send to */
string[] clients;
JSONValue[] clientList = commandBlock["data"].array();
for(ulong i = 0; i < clientList.length; i++)
{
clients ~= clientList[i].str();
}
/* TODO: Implement me */
writeln("Users wanting to send to ", clients);
}
else
{
/* TODO: Error handling */
debugPrint("The message handler is using an invalid command");
}
} }
else else
{ {
/* TODO: Error handling */ /* If the message handler returned a response in error */
debugPrint("The message handler is using an invalid command"); debugPrint("Message handler returned an error code: " ~ to!(string)(statusCode));
return false;
} }
} }
catch(JSONException exception) catch(JSONException exception)
{ {
@ -732,6 +743,7 @@ private class BesterConnection : Thread
return false; return false;
} }
/* If the handling went through fine */
return true; return true;
} }
@ -832,7 +844,15 @@ private class BesterConnection : Thread
debugPrint("<<< Message Handler [" ~ chosenHandler.getPluginName() ~ "] response >>>\n\n" ~ response.toPrettyString()); debugPrint("<<< Message Handler [" ~ chosenHandler.getPluginName() ~ "] response >>>\n\n" ~ response.toPrettyString());
/* TODO: Handle response */ /* TODO: Handle response */
handleResponse(response); bool handleStatus = handleResponse(response);
/* Check if the response was handled unsuccessfully */
if(!handleStatus)
{
debugPrint("Response from message handler was erroneous, sending error to user...");
/* TODO: Implement this */
}
} }
else else
{ {

View File

@ -4,7 +4,7 @@ import json
def basicTest(): def basicTest():
d=socket.socket(socket.AF_INET, socket.SOCK_STREAM) d=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
d.connect(("127.0.0.1",2229)) d.connect(("127.0.0.1",2221))
bys=json.dumps({"header":{"authentication":{"username":"tbk", "password":"passwd"}, "scope":"client"},"payload":{"data":"ABBA","type":"type1"}}) bys=json.dumps({"header":{"authentication":{"username":"tbk", "password":"passwd"}, "scope":"client"},"payload":{"data":"ABBA","type":"type1"}})
print(bys) print(bys)
d.send(bytes([len(bys),0,0,0])) d.send(bytes([len(bys),0,0,0]))
@ -13,7 +13,7 @@ def basicTest():
def commandBuiltInClose(): def commandBuiltInClose():
d=socket.socket(socket.AF_INET, socket.SOCK_STREAM) d=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
d.connect(("127.0.0.1",2229)) d.connect(("127.0.0.1",2221))
bys=json.dumps({"header": {"scope" : "client"},"payload": { bys=json.dumps({"header": {"scope" : "client"},"payload": {
"data": { "data": {
"command" : {"type" : "close", "args" : None} "command" : {"type" : "close", "args" : None}
@ -25,7 +25,7 @@ def commandBuiltInClose():
def commandAuthTest(): def commandAuthTest():
d=socket.socket(socket.AF_INET, socket.SOCK_STREAM) d=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
d.connect(("127.0.0.1",2229)) d.connect(("127.0.0.1",2221))
bys=json.dumps({"header": {"scope" : "client"},"payload": { bys=json.dumps({"header": {"scope" : "client"},"payload": {
"data": { "data": {
"command" : {"type" : "login", "args" : {"username" :"1", "password":"2"}} "command" : {"type" : "login", "args" : {"username" :"1", "password":"2"}}