besterd/source/connection/connection.d

778 lines
20 KiB
D
Raw Normal View History

2020-04-24 17:48:22 +02:00
module connection.connection;
2020-04-16 15:02:34 +02:00
import utils.debugging : debugPrint;
import std.conv : to;
import std.socket : Socket, AddressFamily, SocketType, ProtocolType, parseAddress, SocketFlags, Address;
2020-04-16 17:05:56 +02:00
import core.thread : Thread;
2020-04-17 18:47:05 +02:00
import std.stdio : writeln, File;
2020-04-18 15:25:22 +02:00
import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON;
import std.string : cmp;
import server.handler;
2020-04-24 17:48:22 +02:00
import listeners.listener;
import server.server;
2020-04-24 17:48:22 +02:00
public class BesterConnection : Thread
2020-04-16 17:05:56 +02:00
{
/* The socket to the client */
private Socket clientConnection;
2020-04-16 23:25:22 +02:00
/* The server backend */
private BesterServer server;
2020-04-19 18:53:16 +02:00
/* The client's credentials */
private string authUsername;
private string authPassword;
2020-04-16 23:25:22 +02:00
this(Socket clientConnection, BesterServer server)
2020-04-16 17:05:56 +02:00
{
/* Save socket and set thread worker function pointer */
super(&run);
this.clientConnection = clientConnection;
2020-04-16 23:25:22 +02:00
this.server = server;
debugPrint("New client handler spawned for " ~ clientConnection.remoteAddress().toAddrString());
2020-04-16 17:05:56 +02:00
}
/* Read/send loop */
private void run()
{
/* Receive buffer */
byte[] buffer;
2020-04-16 16:39:31 +02:00
while(true)
{
2020-04-18 15:04:00 +02:00
/* Byte counter for loop-consumer */
uint currentByte = 0;
2020-04-18 15:04:00 +02:00
/* Bytes received counter */
long bytesReceived;
/* TODO: Add fix here to loop for bytes */
while(currentByte < 4)
{
2020-04-18 15:04:00 +02:00
/* Size buffer */
byte[4] tempBuffer;
/* Read at most 4 bytes */
bytesReceived = clientConnection.receive(tempBuffer);
if(!(bytesReceived > 0))
{
/* TODO: Handle error here */
debugPrint("Error with receiving");
return;
}
else
{
/**
* Read the bytes from the temp buffer (as many as was received)
* and append them to the *real* buffer.
*/
buffer ~= tempBuffer[0..bytesReceived];
/* Increment the byte counter */
currentByte += bytesReceived;
}
}
2020-04-18 15:04:00 +02:00
/* Get the message length */
int messageLength = *(cast(int*)buffer.ptr);
writeln("Message length: ", cast(uint)messageLength);
/* TODO: Testing locally ain't good as stuff arrives way too fast, although not as fast as I can type */
/* What must happen is a loop to loop and wait for data */
2020-04-16 18:26:53 +02:00
/* Full message buffer */
byte[] messageBuffer;
2020-04-16 18:30:47 +02:00
/* TODO: Add timeout if we haven't received a message in a certain amount of time */
2020-04-18 15:04:00 +02:00
/* Reset the current byte counter */
currentByte = 0;
2020-04-16 18:30:47 +02:00
2020-04-18 15:04:00 +02:00
while(currentByte < messageLength)
2020-04-16 18:26:53 +02:00
{
/**
* Receive 20 bytes (at most) at a time and don't dequeue from
* the kernel's TCP stack's buffer.
*/
2020-04-16 18:26:53 +02:00
byte[20] messageBufferPartial;
2020-04-18 15:04:00 +02:00
bytesReceived = clientConnection.receive(messageBufferPartial, SocketFlags.PEEK);
/* Check for receive error */
if(!(bytesReceived > 0))
{
debugPrint("Error receiving");
return;
}
else
{
/* TODO: Make sure we only take [0, messageLength) bytes */
if(cast(uint)bytesReceived+currentByte > messageLength)
{
byte[] remainingBytes;
remainingBytes.length = messageLength-currentByte;
clientConnection.receive(remainingBytes);
/* Increment counter of received bytes */
currentByte += remainingBytes.length;
/* Append the received bytes to the FULL message buffer */
messageBuffer ~= remainingBytes;
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
}
else
{
/* Increment counter of received bytes */
currentByte += bytesReceived;
/* Append the received bytes to the FULL message buffer */
messageBuffer ~= messageBufferPartial[0..bytesReceived];
/* TODO: Bug when over send, we must not allow this */
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
clientConnection.receive(messageBufferPartial);
}
}
2020-04-16 18:26:53 +02:00
}
2020-04-16 18:46:26 +02:00
/* Process the message */
processMessage(messageBuffer);
}
}
2020-04-17 00:07:41 +02:00
/* TODO: Pass in type and just payload or what */
2020-04-17 12:12:07 +02:00
private bool dispatch(string payloadType, JSONValue payload)
2020-04-17 00:07:41 +02:00
{
2020-04-17 12:12:07 +02:00
/* TODO: Implement me */
debugPrint("Dispatching payload [" ~ payloadType ~ "]");
debugPrint("Payload: " ~ payload.toPrettyString());
2020-04-17 13:46:54 +02:00
2020-04-18 19:30:43 +02:00
/* Status of dispatch */
bool dispatchStatus = true;
2020-04-17 22:24:08 +02:00
/* Lookup the payloadType handler */
MessageHandler chosenHandler;
for(uint i = 0; i < server.handlers.length; i++)
{
if(cmp(server.handlers[i].getPluginName(), payloadType) == 0)
2020-04-17 22:24:08 +02:00
{
chosenHandler = server.handlers[i];
break;
}
}
2020-04-18 15:04:00 +02:00
2020-04-17 22:24:08 +02:00
if(chosenHandler)
{
/* TODO: Send and receive data here */
2020-04-18 15:04:00 +02:00
/* Handler's UNIX domain socket */
Socket handlerSocket = chosenHandler.getSocket();
2020-04-18 15:25:22 +02:00
/* Get the payload as a string */
string payloadString = toJSON(payload);
/* Construct the data to send */
byte[] sendBuffer;
/* TODO: Add 4 bytes of payload length encded in little endian */
int payloadLength = cast(int)payloadString.length;
byte* lengthBytes = cast(byte*)&payloadLength;
sendBuffer ~= *(lengthBytes+0);
sendBuffer ~= *(lengthBytes+1);
sendBuffer ~= *(lengthBytes+2);
sendBuffer ~= *(lengthBytes+3);
/* Add the string bytes */
sendBuffer ~= cast(byte[])payloadString;
2020-04-17 22:24:08 +02:00
/* TODO: Send payload */
2020-04-18 15:25:22 +02:00
writeln("Send buffer: ", sendBuffer);
2020-04-18 15:04:00 +02:00
2020-04-17 22:24:08 +02:00
debugPrint("Sending payload over to handler for \"" ~ chosenHandler.getPluginName() ~ "\".");
handlerSocket.send(sendBuffer);
2020-04-18 15:04:00 +02:00
2020-04-17 22:24:08 +02:00
/* TODO: Get response */
debugPrint("Waiting for response from handler for \"" ~ chosenHandler.getPluginName() ~ "\".");
2020-04-18 19:30:43 +02:00
/* Construct a buffer to receive into */
byte[] receiveBuffer;
/* The current byte */
uint currentByte = 0;
/* The amount of bytes received */
long bytesReceived;
/* Loop consume the next 4 bytes */
while(currentByte < 4)
{
/* Temporary buffer */
byte[4] tempBuffer;
/* Read at-most 4 bytes */
bytesReceived = handlerSocket.receive(tempBuffer);
/* If there was an error reading from the socket */
if(!(bytesReceived > 0))
{
/* TODO: Error handling */
debugPrint("Error receiving from UNIX domain socket");
}
/* If there is no error reading from the socket */
else
{
/* Add the read bytes to the *real* buffer */
receiveBuffer ~= tempBuffer[0..bytesReceived];
/* Increment the byte counter */
currentByte += bytesReceived;
}
}
/* Response message length */
int messageLength = *cast(int*)receiveBuffer.ptr;
writeln("Message length is: ", cast(uint)messageLength);
/* Response message buffer */
byte[] fullMessage;
/* Reset the byte counter */
currentByte = 0;
while(currentByte < messageLength)
{
debugPrint("dhjkh");
/**
* Receive 20 bytes (at most) at a time and don't dequeue from
* the kernel's TCP stack's buffer.
*/
byte[20] tempBuffer;
bytesReceived = handlerSocket.receive(tempBuffer, SocketFlags.PEEK);
/* Check for an error whilst receiving */
if(!(bytesReceived > 0))
{
/* TODO: Error handling */
debugPrint("Error whilst receiving from unix domain socket");
}
else
{
/* TODO: Make sure we only take [0, messageLength) bytes */
if(cast(uint)bytesReceived+currentByte > messageLength)
{
byte[] remainingBytes;
remainingBytes.length = messageLength-currentByte;
handlerSocket.receive(remainingBytes);
/* Increment counter of received bytes */
currentByte += remainingBytes.length;
/* Append the received bytes to the FULL message buffer */
fullMessage ~= remainingBytes;
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
}
else
{
/* Increment counter of received bytes */
currentByte += bytesReceived;
/* Append the received bytes to the FULL message buffer */
fullMessage ~= tempBuffer[0..bytesReceived];
/* TODO: Bug when over send, we must not allow this */
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
handlerSocket.receive(tempBuffer);
}
}
2020-04-18 19:30:43 +02:00
}
writeln("MEssage ", fullMessage);
2020-04-18 19:30:43 +02:00
//int messageLength = 0;
/* TODO: Loop for collect message */
2020-04-18 19:30:43 +02:00
2020-04-18 20:10:16 +02:00
/* TODO: So now we have to think about what the hell it means
* for a response to be received, like cool and all, but we need
* the server to now do something.
*/
2020-04-18 19:30:43 +02:00
/* TODO: Set dispatchStatus */
2020-04-17 22:24:08 +02:00
}
else
{
/* TODO: Error handling */
debugPrint("No message handler for payload type \"" ~ payloadType ~ "\" found.");
2020-04-18 19:30:43 +02:00
dispatchStatus = false;
2020-04-17 22:24:08 +02:00
}
2020-04-17 12:12:07 +02:00
2020-04-17 00:07:41 +02:00
/* TODO: Set return value */
2020-04-18 19:30:43 +02:00
debugPrint("Dispatch status: " ~ to!(string)(dispatchStatus));
return dispatchStatus;
2020-04-17 00:07:41 +02:00
}
2020-04-19 14:36:55 +02:00
private JSONValue handlerRun(MessageHandler chosenHandler, JSONValue payload)
2020-04-19 14:36:55 +02:00
{
/* TODO: Send and receive data here */
2020-04-19 22:00:53 +02:00
/* Handler's UNIX domain socket */
Socket handlerSocket = chosenHandler.getSocket();
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Get the payload as a string */
string payloadString = toJSON(payload);
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Construct the data to send */
byte[] sendBuffer;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* TODO: Add 4 bytes of payload length encded in little endian */
int payloadLength = cast(int)payloadString.length;
byte* lengthBytes = cast(byte*)&payloadLength;
sendBuffer ~= *(lengthBytes+0);
sendBuffer ~= *(lengthBytes+1);
sendBuffer ~= *(lengthBytes+2);
sendBuffer ~= *(lengthBytes+3);
/* Add the string bytes */
sendBuffer ~= cast(byte[])payloadString;
/* TODO: Send payload */
writeln("Send buffer: ", sendBuffer);
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
debugPrint("Sending payload over to handler for \"" ~ chosenHandler.getPluginName() ~ "\".");
handlerSocket.send(sendBuffer);
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* TODO: Get response */
debugPrint("Waiting for response from handler for \"" ~ chosenHandler.getPluginName() ~ "\".");
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Construct a buffer to receive into */
byte[] receiveBuffer;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* The current byte */
uint currentByte = 0;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* The amount of bytes received */
long bytesReceived;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Loop consume the next 4 bytes */
while(currentByte < 4)
{
/* Temporary buffer */
byte[4] tempBuffer;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Read at-most 4 bytes */
bytesReceived = handlerSocket.receive(tempBuffer);
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* If there was an error reading from the socket */
if(!(bytesReceived > 0))
{
/* TODO: Error handling */
debugPrint("Error receiving from UNIX domain socket");
}
/* If there is no error reading from the socket */
else
{
/* Add the read bytes to the *real* buffer */
receiveBuffer ~= tempBuffer[0..bytesReceived];
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Increment the byte counter */
currentByte += bytesReceived;
}
}
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Response message length */
int messageLength = *cast(int*)receiveBuffer.ptr;
writeln("Message length is: ", cast(uint)messageLength);
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Response message buffer */
byte[] fullMessage;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Reset the byte counter */
currentByte = 0;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
while(currentByte < messageLength)
{
debugPrint("dhjkh");
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/**
* Receive 20 bytes (at most) at a time and don't dequeue from
* the kernel's TCP stack's buffer.
*/
byte[20] tempBuffer;
bytesReceived = handlerSocket.receive(tempBuffer, SocketFlags.PEEK);
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Check for an error whilst receiving */
if(!(bytesReceived > 0))
{
/* TODO: Error handling */
debugPrint("Error whilst receiving from unix domain socket");
}
else
{
/* TODO: Make sure we only take [0, messageLength) bytes */
if(cast(uint)bytesReceived+currentByte > messageLength)
{
byte[] remainingBytes;
remainingBytes.length = messageLength-currentByte;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
handlerSocket.receive(remainingBytes);
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Increment counter of received bytes */
currentByte += remainingBytes.length;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* Append the received bytes to the FULL message buffer */
fullMessage ~= remainingBytes;
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
}
else
{
/* Increment counter of received bytes */
currentByte += bytesReceived;
/* Append the received bytes to the FULL message buffer */
fullMessage ~= tempBuffer[0..bytesReceived];
/* TODO: Bug when over send, we must not allow this */
writeln("Received ", currentByte, "/", cast(uint)messageLength, " bytes");
handlerSocket.receive(tempBuffer);
}
}
}
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
writeln("MEssage ", fullMessage);
//int messageLength = 0;
/* TODO: Loop for collect message */
2020-04-19 14:36:55 +02:00
2020-04-19 22:00:53 +02:00
/* TODO: So now we have to think about what the hell it means
* for a response to be received, like cool and all, but we need
* the server to now do something.
*/
/* TODO: Set dispatchStatus */
return parseJSON(cast(string)fullMessage);
2020-04-19 14:36:55 +02:00
}
2020-04-19 18:53:16 +02:00
/**
* Handles the response sent back to the server from the
* message handler.
*/
2020-04-19 15:36:23 +02:00
private bool handleResponse(JSONValue handlerResponse)
{
2020-04-19 16:51:04 +02:00
/* TODO: Bounds checking, type checking */
try
{
/* Get the header block */
JSONValue headerBlock = handlerResponse["header"];
/* Get the status */
2020-04-24 17:12:25 +02:00
ulong statusCode = to!(ulong)(headerBlock["status"].str());
2020-04-19 16:51:04 +02:00
debugPrint("Status code: " ~ to!(string)(statusCode));
2020-04-19 18:53:16 +02:00
2020-04-24 17:12:25 +02:00
/* If the status is 0, then it is all fine */
if(statusCode == 0)
2020-04-19 18:53:16 +02:00
{
2020-04-24 17:12:25 +02:00
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-19 18:53:16 +02:00
{
2020-04-24 17:12:25 +02:00
/* 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);
2020-04-19 18:53:16 +02:00
}
2020-04-24 17:12:25 +02:00
else if(cmp(serverCommand, "sendServers") == 0)
2020-04-19 20:32:52 +02:00
{
2020-04-24 17:12:25 +02:00
/* 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");
2020-04-19 20:32:52 +02:00
}
2020-04-19 18:53:16 +02:00
}
else
{
2020-04-24 17:12:25 +02:00
/* If the message handler returned a response in error */
debugPrint("Message handler returned an error code: " ~ to!(string)(statusCode));
return false;
2020-04-19 18:53:16 +02:00
}
2020-04-19 16:51:04 +02:00
}
catch(JSONException exception)
{
debugPrint("<<< There was an error handling the response message >>>\n\n" ~ exception.toString());
return false;
}
2020-04-24 17:12:25 +02:00
/* If the handling went through fine */
2020-04-19 15:36:23 +02:00
return true;
}
2020-04-19 20:32:52 +02:00
public enum Scope
{
CLIENT,
SERVER,
UNKNOWN
}
2020-04-19 21:03:55 +02:00
private bool isAuthenticated()
{
return authUsername != null && authPassword != null;
}
2020-04-18 22:34:26 +02:00
/* TODO: Version 2 of message dispatcher */
2020-04-19 20:32:52 +02:00
private bool dispatchMessage(Scope scopeField, JSONValue payloadBlock)
2020-04-18 22:34:26 +02:00
{
/* Status of dispatch */
bool dispatchStatus = true;
/* TODO: Bounds checking, type checking */
/* Get the payload type */
string payloadType = payloadBlock["type"].str;
debugPrint("Payload type is \"" ~ payloadType ~ "\"");
/* Get the payload data */
JSONValue payloadData = payloadBlock["data"];
/* Lookup the payloadType handler */
2020-04-19 14:45:53 +02:00
MessageHandler chosenHandler = server.findHandler(payloadType);
2020-04-18 22:50:47 +02:00
/* Check if the payload is a built-in command */
2020-04-19 14:31:24 +02:00
if(cmp(payloadType, "builtin") == 0)
{
/* TODO: Implement me */
2020-04-19 14:33:44 +02:00
debugPrint("Built-in payload type");
2020-04-19 20:32:52 +02:00
/**
* Built-in commands follow the structure of
* "command" : {"type" : "cmdType", "command" : ...}
*/
JSONValue commandBlock = payloadData["command"];
string commandType = commandBlock["type"].str;
2020-04-19 21:55:08 +02:00
JSONValue command = commandBlock["args"];
2020-04-19 20:32:52 +02:00
/* Check if the command is the `login` */
if(cmp(commandType, "login") == 0)
{
debugPrint("User wants to login");
2020-04-19 21:03:55 +02:00
/* Get the username and password fields */
string username = command["username"].str(), password = command["password"].str();
debugPrint("Username: \"" ~ username ~ "\" Password: \"" ~ password ~ "\"");
/* Authenticate the user and get the status */
bool authenticationStatus = server.authenticate(username, password);
debugPrint("Authentication status: " ~ to!(string)(authenticationStatus));
/* If the authentication succeeded */
if(authenticationStatus)
{
/* Update this client's authentication status */
authUsername = username, authPassword = password;
debugPrint("User authenticated!");
/* TODO: Implement response */
}
else
{
debugPrint("User authentication FAILED!");
/* TODO: Implement response */
}
/* TODO: Implement me */
}
/* If the command is `close` */
else if(cmp(commandType, "close") == 0)
{
debugPrint("Closing socket...");
2020-04-19 21:55:08 +02:00
this.clientConnection.close();
2020-04-19 21:03:55 +02:00
}
else
{
debugPrint("Invalid built-in command type");
/* TODO: Generate error response */
2020-04-19 20:32:52 +02:00
}
}
/* If an external handler is found (i.e. not a built-in command) */
else if(chosenHandler)
{
/* TODO: Implement me */
2020-04-19 14:33:44 +02:00
debugPrint("Chosen handler for payload type \"" ~ payloadType ~ "\" is " ~ chosenHandler.getPluginName());
2020-04-19 14:36:55 +02:00
/* TODO: Collect return value */
JSONValue response = handlerRun(chosenHandler, payloadData);
debugPrint("<<< Message Handler [" ~ chosenHandler.getPluginName() ~ "] response >>>\n\n" ~ response.toPrettyString());
/* TODO: Handle response */
2020-04-24 17:12:25 +02:00
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
{
/* TODO: Implement error handling */
2020-04-19 14:33:44 +02:00
debugPrint("No handler available for payload type \"" ~ payloadType ~ "\"");
}
2020-04-18 22:34:26 +02:00
return dispatchStatus;
}
2020-04-19 16:59:09 +02:00
2020-04-18 22:34:26 +02:00
2020-04-16 18:46:26 +02:00
/* Process the received message */
private void processMessage(byte[] messageBuffer)
{
/* The message as a JSONValue struct */
JSONValue jsonMessage;
2020-04-18 22:34:26 +02:00
/* Attempt to convert the message to JSON */
try
{
/* Convert message to JSON */
jsonMessage = parseJSON(cast(string)messageBuffer);
debugPrint("<<< Received JSON >>>\n\n" ~ jsonMessage.toPrettyString());
/* TODO: Bounds checking, type checking */
/* Get the header */
JSONValue headerBlock = jsonMessage["header"];
2020-04-19 13:21:14 +02:00
/* Get the scope of the message */
2020-04-19 20:32:52 +02:00
Scope scopeField;
if(cmp(headerBlock["scope"].str, "client") == 0)
{
scopeField = Scope.CLIENT;
}
else if(cmp(headerBlock["scope"].str, "server") == 0)
{
scopeField = Scope.CLIENT;
}
else
{
scopeField = Scope.UNKNOWN;
}
2020-04-19 13:25:32 +02:00
/* Get the payload block */
JSONValue payloadBlock = jsonMessage["payload"];
debugPrint("<<< Payload is >>>\n\n" ~ payloadBlock.toPrettyString());
2020-04-19 13:21:14 +02:00
/* If the communication is client->server */
2020-04-19 20:32:52 +02:00
if(scopeField == Scope.CLIENT)
2020-04-19 13:21:14 +02:00
{
debugPrint("Client to server selected");
2020-04-18 22:34:26 +02:00
}
2020-04-19 13:22:26 +02:00
/* If the communication is server->server */
2020-04-19 20:32:52 +02:00
else if(scopeField == Scope.SERVER)
2020-04-19 13:22:26 +02:00
{
2020-04-19 18:53:16 +02:00
debugPrint("Server to server selected");
/* TODO: Implement me */
2020-04-19 13:22:26 +02:00
}
2020-04-18 22:34:26 +02:00
else
{
/* TODO: Error handling */
2020-04-19 20:32:52 +02:00
debugPrint("Unknown scope selected \"" ~ to!(string)(cast(uint)scopeField) ~ "\"");
2020-04-19 18:53:16 +02:00
return;
2020-04-18 22:34:26 +02:00
}
2020-04-19 18:53:16 +02:00
/* Dispatch the message */
2020-04-19 20:32:52 +02:00
bool dispatchStatus = dispatchMessage(scopeField, payloadBlock);
2020-04-19 18:53:16 +02:00
if(dispatchStatus)
2020-04-16 19:16:38 +02:00
{
2020-04-19 18:53:16 +02:00
debugPrint("Dispatch succeeded");
2020-04-16 19:16:38 +02:00
}
else
{
2020-04-19 18:53:16 +02:00
/* TODO: Error handling */
debugPrint("Dispatching failed...");
}
2020-04-16 18:46:26 +02:00
}
2020-04-19 18:53:16 +02:00
/* If thr attempt to convert the message to JSON fails */
2020-04-16 18:46:26 +02:00
catch(JSONException exception)
{
2020-04-19 18:53:16 +02:00
debugPrint("<<< There was an error whilst parsing the JSON message >>>\n\n"~exception.toString());
2020-04-16 16:39:31 +02:00
}
2020-04-19 18:53:16 +02:00
/* TODO: Return value */
}
2020-04-16 17:05:56 +02:00
2020-04-16 15:02:34 +02:00
2020-04-24 17:17:58 +02:00
}