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" : {
"types" : ["unix", "tcp4", "tcp6"],
"unix" : {
"address" : "besterUNIXSock"
},
"tcp4" : {
"port" : "2228",
"port" : "2220",
"address" : "0.0.0.0"
},
"tcp6" : {
"port" : "2229",
"port" : "2221",
"address" : "::"
}
},

View File

@ -679,52 +679,63 @@ private class BesterConnection : Thread
JSONValue headerBlock = handlerResponse["header"];
/* Get the status */
ulong statusCode = to!(ulong)(headerBlock["status"].str);
ulong statusCode = to!(ulong)(headerBlock["status"].str());
debugPrint("Status code: " ~ to!(string)(statusCode));
/* The command block */
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)
/* If the status is 0, then it is all fine */
if(statusCode == 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();
}
debugPrint("Status is fine, the handler ran correctly");
/* TODO: Implement me */
writeln("Users wanting to send to ", clients);
/* The command block */
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
{
/* TODO: Error handling */
debugPrint("The message handler is using an invalid command");
/* If the message handler returned a response in error */
debugPrint("Message handler returned an error code: " ~ to!(string)(statusCode));
return false;
}
}
catch(JSONException exception)
{
@ -732,6 +743,7 @@ private class BesterConnection : Thread
return false;
}
/* If the handling went through fine */
return true;
}
@ -832,7 +844,15 @@ private class BesterConnection : Thread
debugPrint("<<< Message Handler [" ~ chosenHandler.getPluginName() ~ "] response >>>\n\n" ~ response.toPrettyString());
/* 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
{

View File

@ -4,7 +4,7 @@ import json
def basicTest():
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"}})
print(bys)
d.send(bytes([len(bys),0,0,0]))
@ -13,7 +13,7 @@ def basicTest():
def commandBuiltInClose():
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": {
"data": {
"command" : {"type" : "close", "args" : None}
@ -25,7 +25,7 @@ def commandBuiltInClose():
def commandAuthTest():
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": {
"data": {
"command" : {"type" : "login", "args" : {"username" :"1", "password":"2"}}