2020-04-24 17:56:57 +02:00
|
|
|
module handlers.response;
|
|
|
|
|
2020-04-24 18:30:02 +02:00
|
|
|
import std.json : JSONValue, JSONException;
|
|
|
|
import std.conv : to;
|
|
|
|
import utils.debugging : debugPrint;
|
|
|
|
import std.string : cmp;
|
|
|
|
import std.stdio : writeln;
|
2020-04-24 18:41:25 +02:00
|
|
|
import connection.connection;
|
2020-04-24 17:56:57 +02:00
|
|
|
|
2020-04-24 18:55:19 +02:00
|
|
|
/* The type of the command the message handler wants us to run */
|
|
|
|
private enum CommandType
|
|
|
|
{
|
2020-04-24 19:10:57 +02:00
|
|
|
SEND_CLIENTS, SEND_SERVERS, SEND_HANDLER
|
2020-04-24 18:55:19 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 19:19:53 +02:00
|
|
|
public final class HandlerResponse
|
2020-04-24 17:56:57 +02:00
|
|
|
{
|
2020-04-24 18:30:02 +02:00
|
|
|
/* The message-handler's response */
|
|
|
|
private JSONValue messageResponse;
|
|
|
|
|
2020-04-24 18:55:19 +02:00
|
|
|
/* The command to be executed */
|
|
|
|
private CommandType commandType;
|
|
|
|
|
2020-04-24 17:56:57 +02:00
|
|
|
this(JSONValue messageResponse)
|
|
|
|
{
|
2020-04-24 18:30:02 +02:00
|
|
|
/* Set the message-handler's response message */
|
|
|
|
this.messageResponse = messageResponse;
|
|
|
|
|
|
|
|
/* Attempt parsing the message and error checking it */
|
|
|
|
parse(messageResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void parse(JSONValue handlerResponse)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handles the response sent back to the server from the
|
|
|
|
* message handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Get the status */
|
|
|
|
ulong statusCode;
|
|
|
|
|
|
|
|
/* Error? */
|
|
|
|
bool error;
|
|
|
|
|
|
|
|
/* TODO: Bounds checking, type checking */
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/* Get the header block */
|
|
|
|
JSONValue headerBlock = handlerResponse["header"];
|
|
|
|
|
|
|
|
/* Get the status */
|
|
|
|
statusCode = to!(ulong)(headerBlock["status"].str());
|
|
|
|
debugPrint("Status code: " ~ to!(string)(statusCode));
|
2020-04-24 17:56:57 +02:00
|
|
|
|
2020-04-24 18:30:02 +02:00
|
|
|
/* If the status is 0, then it is all fine */
|
|
|
|
if(statusCode == 0)
|
|
|
|
{
|
|
|
|
debugPrint("Status is fine, the handler ran correctly");
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
2020-04-24 18:55:19 +02:00
|
|
|
/* Set the command type to SEND_CLIENTS */
|
|
|
|
commandType = CommandType.SEND_CLIENTS;
|
2020-04-24 19:10:57 +02:00
|
|
|
|
|
|
|
/* TODO: Error check and do accesses JSON that would be done in `.execute` */
|
2020-04-24 18:30:02 +02:00
|
|
|
}
|
|
|
|
else if(cmp(serverCommand, "sendServers") == 0)
|
|
|
|
{
|
2020-04-24 18:55:19 +02:00
|
|
|
/* Set the command type to SEND_SERVERS */
|
|
|
|
commandType = CommandType.SEND_SERVERS;
|
2020-04-24 19:10:57 +02:00
|
|
|
|
|
|
|
/* TODO: Error check and do accesses JSON that would be done in `.execute` */
|
2020-04-24 18:30:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* TODO: Error handling */
|
|
|
|
debugPrint("The message handler is using an invalid command");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the message handler returned a response in error */
|
|
|
|
debugPrint("Message handler returned an error code: " ~ to!(string)(statusCode));
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(JSONException exception)
|
|
|
|
{
|
|
|
|
debugPrint("<<< There was an error handling the response message >>>\n\n" ~ exception.toString());
|
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
{
|
|
|
|
throw new ResponseError(messageResponse, statusCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-24 18:41:25 +02:00
|
|
|
public void execute(BesterConnection originalRequester)
|
|
|
|
{
|
|
|
|
/* TODO: Implement me */
|
2020-04-24 18:55:19 +02:00
|
|
|
|
|
|
|
/* If the command is SEND_CLIENTS */
|
2020-04-24 19:10:57 +02:00
|
|
|
if(commandType == CommandType.SEND_CLIENTS)
|
2020-04-24 18:55:19 +02:00
|
|
|
{
|
|
|
|
/* Get the list of clients to send to */
|
|
|
|
string[] clients;
|
2020-04-24 19:12:26 +02:00
|
|
|
JSONValue[] clientList = messageResponse["header"]["command"]["data"].array();
|
2020-04-24 18:55:19 +02:00
|
|
|
for(ulong i = 0; i < clientList.length; i++)
|
|
|
|
{
|
|
|
|
clients ~= clientList[i].str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Implement me */
|
|
|
|
writeln("Users wanting to send to ", clients);
|
|
|
|
}
|
2020-04-24 19:10:57 +02:00
|
|
|
else if (commandType == CommandType.SEND_SERVERS)
|
|
|
|
{
|
|
|
|
/* Get the list of servers to send to */
|
|
|
|
string[] servers;
|
2020-04-24 19:12:26 +02:00
|
|
|
JSONValue[] serverList = messageResponse["header"]["command"]["data"].array();
|
2020-04-24 19:10:57 +02:00
|
|
|
for(ulong i = 0; i < serverList.length; i++)
|
|
|
|
{
|
|
|
|
servers ~= serverList[i].str();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Implement me */
|
|
|
|
writeln("Servers wanting to send to ", servers);
|
|
|
|
}
|
2020-04-24 19:19:53 +02:00
|
|
|
else if (commandType == CommandType.SEND_HANDLER)
|
|
|
|
{
|
|
|
|
/* TODO: Add me */
|
|
|
|
}
|
2020-04-24 18:41:25 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 18:30:02 +02:00
|
|
|
override public string toString()
|
|
|
|
{
|
|
|
|
return messageResponse.toPrettyString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final class ResponseError : Exception
|
|
|
|
{
|
|
|
|
this(JSONValue messageResponse, ulong statusCode)
|
|
|
|
{
|
|
|
|
/* TODO: Set message afterwards again */
|
|
|
|
super("");
|
2020-04-24 17:56:57 +02:00
|
|
|
}
|
|
|
|
}
|