mirror of
https://github.com/besterprotocol/besterd
synced 2023-12-13 21:00:32 +01:00
Compare commits
48 commits
Author | SHA1 | Date | |
---|---|---|---|
0f75841600 | |||
5fc42a6ef9 | |||
72139e70c3 | |||
ac56eb7689 | |||
d19b552997 | |||
261448159f | |||
c4a4d46264 | |||
c7f701e8f6 | |||
b783d98ea7 | |||
5c13fa3c77 | |||
60cd993472 | |||
aec7144106 | |||
9db02f607b | |||
c646b4bf20 | |||
88af27c3cb | |||
2b54142439 | |||
0b5ca124d6 | |||
71632ec1f3 | |||
80b0772fd8 | |||
32d35cec71 | |||
a22b65f7c4 | |||
615ebc823a | |||
8d0256d105 | |||
4e091e2f2d | |||
06c406ef86 | |||
00c16b68da | |||
1ade3365b1 | |||
95577698b8 | |||
be2953595d | |||
ea25aabf26 | |||
2993f74b05 | |||
cc934e5ec8 | |||
e0e7d5ffd0 | |||
89f51e5944 | |||
fc33bd0b69 | |||
955cc6d21d | |||
9f1e63367c | |||
a9ee569314 | |||
4442c62ae7 | |||
70df082f9a | |||
27ada909d6 | |||
1d7f0b8ac2 | |||
3e570e8066 | |||
79cfe50353 | |||
007e15caa4 | |||
cc359a14f0 | |||
d5e3a86842 | |||
bd4846bf61 |
11 changed files with 258 additions and 102 deletions
4
dub.json
4
dub.json
|
@ -5,8 +5,8 @@
|
|||
],
|
||||
"copyright": "Copyright © 2020, Tristan B. Kildaire",
|
||||
"dependencies": {
|
||||
"bformat": "1.0.4",
|
||||
"vibe-d": "0.9.0-alpha.4"
|
||||
"bformat": "1.0.8",
|
||||
"vibe-d": "0.9.0-beta.1"
|
||||
},
|
||||
"description": "Bester protocol daemon. ",
|
||||
"license": "AGPL v3",
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
{
|
||||
"fileVersion": 1,
|
||||
"versions": {
|
||||
"bformat": "1.0.4",
|
||||
"bformat": "1.0.8",
|
||||
"botan": "1.12.18",
|
||||
"botan-math": "1.0.3",
|
||||
"diet-ng": "1.7.2",
|
||||
"eventcore": "0.8.50",
|
||||
"eventcore": "0.9.6",
|
||||
"libasync": "0.8.6",
|
||||
"libevent": "2.0.2+2.0.16",
|
||||
"memutils": "1.0.4",
|
||||
"mir-linux-kernel": "1.0.1",
|
||||
"openssl": "1.1.6+1.0.1g",
|
||||
"stdx-allocator": "2.77.5",
|
||||
"taggedalgebraic": "0.11.13",
|
||||
"vibe-core": "1.9.1",
|
||||
"vibe-d": "0.9.0-alpha.4"
|
||||
"taggedalgebraic": "0.11.16",
|
||||
"vibe-core": "1.9.2",
|
||||
"vibe-d": "0.9.0-beta.1"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"network" : {
|
||||
"types" : ["unix", "tcp4", "tcp6"],
|
||||
"types" : ["tcp4", "tcp6"],
|
||||
"unix" : {
|
||||
"address" : "besterUNIXSock"
|
||||
},
|
||||
|
|
|
@ -63,6 +63,10 @@ public final class BesterConnection : Thread
|
|||
debugPrint("New client handler spawned for " ~ clientConnection.remoteAddress().toAddrString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the BesterConnection by stopping
|
||||
* the read-write loop and closing the socket.
|
||||
*/
|
||||
public void shutdown()
|
||||
{
|
||||
/* TODO: Send message posssibly, think about this for listeners and informers (etc.) too */
|
||||
|
@ -74,6 +78,9 @@ public final class BesterConnection : Thread
|
|||
return username ~ "@" ~ clientConnection.remoteAddress().toAddrString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of the username and password.
|
||||
*/
|
||||
public string[] getCredentials()
|
||||
{
|
||||
return [username, password];
|
||||
|
@ -83,7 +90,7 @@ public final class BesterConnection : Thread
|
|||
private void run()
|
||||
{
|
||||
debugPrint("<<< Begin read/send loop >>>");
|
||||
while(isActive) /*TODO: Remove and also make the stting of this kak not be closing socket */
|
||||
while(isActive)
|
||||
{
|
||||
/* Received JSON message */
|
||||
JSONValue receivedMessage;
|
||||
|
@ -103,7 +110,7 @@ public final class BesterConnection : Thread
|
|||
if(connectionType == Scope.SERVER)
|
||||
{
|
||||
debugPrint("Server connection done, closing BesterConnection.");
|
||||
isActive = false;
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
catch(BesterException exception)
|
||||
|
@ -146,19 +153,42 @@ public final class BesterConnection : Thread
|
|||
}
|
||||
|
||||
/* TODO: Comment [], rename [] */
|
||||
|
||||
/**
|
||||
* Dispatches the message to the correct message handler.
|
||||
*
|
||||
* Returns `true` on success or partial success, `false`
|
||||
* on fatal protocol error.
|
||||
*/
|
||||
private bool dispatchMessage(Scope scopeField, JSONValue payloadBlock)
|
||||
{
|
||||
/* Status of dispatch */
|
||||
bool dispatchStatus = true;
|
||||
/* The payload type */
|
||||
string payloadType;
|
||||
|
||||
/* TODO: Bounds checking, type checking */
|
||||
/* The payload data */
|
||||
JSONValue payloadData;
|
||||
|
||||
/* Get the payload type */
|
||||
string payloadType = payloadBlock["type"].str;
|
||||
debugPrint("Payload type is \"" ~ payloadType ~ "\"");
|
||||
/* The payload tag */
|
||||
string payloadTag;
|
||||
|
||||
/* Get the payload data */
|
||||
JSONValue payloadData = payloadBlock["data"];
|
||||
/* Attempt to parse protocol-critical fields */
|
||||
try
|
||||
{
|
||||
/* Get the payload type */
|
||||
payloadType = payloadBlock["type"].str;
|
||||
debugPrint("Payload type is \"" ~ payloadType ~ "\"");
|
||||
|
||||
/* Get the payload data */
|
||||
payloadData = payloadBlock["data"];
|
||||
|
||||
/* Get the payload tag */
|
||||
payloadTag = payloadBlock["id"].str();
|
||||
}
|
||||
catch(JSONException e)
|
||||
{
|
||||
debugPrint("Fatal error when processing packet, missing fields");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Lookup the payloadType handler */
|
||||
MessageHandler chosenHandler = server.findHandler(payloadType);
|
||||
|
@ -166,7 +196,36 @@ public final class BesterConnection : Thread
|
|||
/* Check if it is a dummy type */
|
||||
if(cmp(payloadType, "dummy") == 0)
|
||||
{
|
||||
/* TODO: Continue here, format think for everything */
|
||||
/* Construct a dummy response */
|
||||
JSONValue dummyMessage;
|
||||
|
||||
/* Construct a header block */
|
||||
JSONValue headerBlock;
|
||||
headerBlock["status"] = "0";
|
||||
|
||||
/* Attach the header block */
|
||||
dummyMessage["header"] = headerBlock;
|
||||
|
||||
/* Construct the payload block */
|
||||
JSONValue dummyPayloadBlock;
|
||||
dummyPayloadBlock["data"] = null;
|
||||
dummyPayloadBlock["type"] = payloadType;
|
||||
dummyPayloadBlock["id"] = payloadTag;
|
||||
|
||||
/* Attach the payload block */
|
||||
dummyMessage["payload"] = dummyPayloadBlock;
|
||||
|
||||
try
|
||||
{
|
||||
/* Send the message */
|
||||
sendMessage(clientConnection, dummyMessage);
|
||||
}
|
||||
catch(NetworkException e)
|
||||
{
|
||||
debugPrint("Error sending status message, fatal closing connection");
|
||||
/* TODO: We should deactivate the connection when this happens */
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/* Check if the payload is a built-in command */
|
||||
else if(cmp(payloadType, "builtin") == 0)
|
||||
|
@ -188,13 +247,15 @@ public final class BesterConnection : Thread
|
|||
debugPrint("Closing socket...");
|
||||
isActive = false;
|
||||
|
||||
sendStatus(0, JSONValue());
|
||||
// sendStatus(0, JSONValue());
|
||||
}
|
||||
else
|
||||
{
|
||||
debugPrint("Invalid built-in command type");
|
||||
/* TODO: Generate error response */
|
||||
dispatchStatus = false;
|
||||
// dispatchStatus = false;
|
||||
|
||||
/* TODO: Send a response as the "builtin" message handler */
|
||||
}
|
||||
}
|
||||
/* If an external handler is found (i.e. not a built-in command) */
|
||||
|
@ -211,54 +272,76 @@ public final class BesterConnection : Thread
|
|||
/* TODO: Continue here, we will make all error handling do on construction as to make this all more compact */
|
||||
debugPrint("<<< Message Handler [" ~ chosenHandler.getPluginName() ~ "] response >>>\n\n" ~ handlerResponse.toString());
|
||||
|
||||
/* Execute the message handler's command (as per its reply) */
|
||||
handlerResponse.execute(this);
|
||||
/* Execute the message handler's command (as per its reply) and pass in the tag */
|
||||
handlerResponse.execute(this, payloadTag);
|
||||
}
|
||||
catch(ResponseError e)
|
||||
{
|
||||
/* In the case of an error with the message handler, send an error to the client/server */
|
||||
|
||||
/* TODO: Send error here */
|
||||
//JSONValue errorResponse;
|
||||
//errorResponse["dd"] = 2;
|
||||
//debugPrint("Response error");
|
||||
dispatchStatus = false;
|
||||
/* TODO: Clean up comments */
|
||||
|
||||
/* Send error message to client */
|
||||
sendStatusReport(StatusType.FAILURE, payloadTag);
|
||||
}
|
||||
/* TODO: Be more specific with errors and reporting in the future */
|
||||
catch(Exception e)
|
||||
{
|
||||
/* TODO: Remove me */
|
||||
debugPrint("fhjhfsdjhfdjhgsdkjh UUUUH:" ~e.toString());
|
||||
dispatchStatus = false;
|
||||
|
||||
/* TODO: Add call to `sendStatus` here as the handler failed */
|
||||
/* Send error message to client */
|
||||
sendStatusReport(StatusType.FAILURE, payloadTag);
|
||||
}
|
||||
|
||||
debugPrint("Handler section done (for client)");
|
||||
/* TODO: Handle response */
|
||||
}
|
||||
/* If no message handler for the specified type could be found */
|
||||
else
|
||||
{
|
||||
/* TODO: Implement error handling */
|
||||
debugPrint("No handler available for payload type \"" ~ payloadType ~ "\"");
|
||||
|
||||
/* Send error message to client */
|
||||
JSONValue handlerName = payloadType;
|
||||
sendStatus(1, handlerName);
|
||||
dispatchStatus = false;
|
||||
sendStatusReport(StatusType.FAILURE, payloadTag);
|
||||
}
|
||||
|
||||
return dispatchStatus;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Send a status message to the client */
|
||||
public void sendStatus(uint code, JSONValue data)
|
||||
/**
|
||||
* Type of the status report.
|
||||
* Either 0 (for success) or 1 (for failure).
|
||||
*/
|
||||
public enum StatusType
|
||||
{
|
||||
/* Construct a status message */
|
||||
SUCCESS,
|
||||
FAILURE
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a status report for the message with id
|
||||
* `id` of type `StatusType`.
|
||||
*/
|
||||
public void sendStatusReport(StatusType statusType, string id)
|
||||
{
|
||||
/* Construct the response */
|
||||
JSONValue statusMessage;
|
||||
JSONValue statusBlock;
|
||||
statusBlock["code"] = to!(string)(code);
|
||||
statusBlock["data"] = data;
|
||||
statusMessage["status"] = statusBlock;
|
||||
|
||||
/* Construct the header block */
|
||||
JSONValue headerBlock;
|
||||
headerBlock["status"] = statusType == 0 ? "good" : "bad";
|
||||
headerBlock["messageType"] = "statusReport";
|
||||
|
||||
/* Attach the header block */
|
||||
statusMessage["header"] = headerBlock;
|
||||
|
||||
/* Create the payload block */
|
||||
JSONValue payloadBlock;
|
||||
payloadBlock["id"] = id;
|
||||
|
||||
/* Attach the payload block */
|
||||
statusMessage["payload"] = payloadBlock;
|
||||
|
||||
try
|
||||
{
|
||||
|
@ -295,23 +378,29 @@ public final class BesterConnection : Thread
|
|||
return Scope.UNKNOWN;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sends an error message on fatal error.
|
||||
* Used before client shutdown on such
|
||||
* an error.
|
||||
*/
|
||||
private void sendFatalMessage()
|
||||
{
|
||||
/* TODO: Implement me */
|
||||
}
|
||||
|
||||
/* Process the received message */
|
||||
private void processMessage(JSONValue jsonMessage)
|
||||
{
|
||||
|
||||
/* Attempt to convert the message to JSON */
|
||||
try
|
||||
{
|
||||
/* Convert message to JSON */
|
||||
debugPrint("<<< Received JSON >>>\n\n" ~ jsonMessage.toPrettyString());
|
||||
|
||||
/* TODO: Bounds checking, type checking */
|
||||
|
||||
/* Get the header */
|
||||
JSONValue headerBlock = jsonMessage["header"];
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check to see if this connection is currently "untyped".
|
||||
*
|
||||
|
@ -332,8 +421,11 @@ public final class BesterConnection : Thread
|
|||
|
||||
/* TODO: Send message back about an invalid scope */
|
||||
|
||||
/* TODO: End this here */
|
||||
isActive = false;
|
||||
/* Send fatal message */
|
||||
sendFatalMessage();
|
||||
|
||||
/* Stop the read/write loop */
|
||||
shutdown();
|
||||
return;
|
||||
}
|
||||
else if(scopeField == Scope.CLIENT)
|
||||
|
@ -369,7 +461,10 @@ public final class BesterConnection : Thread
|
|||
this.password = password;
|
||||
|
||||
/* Send error message to client */
|
||||
sendStatus(5, JSONValue());
|
||||
// sendStatus(5, JSONValue());
|
||||
|
||||
/* TODO: Send authentication success */
|
||||
sendStatusReport(StatusType.SUCCESS, "auth_special");
|
||||
}
|
||||
/* If authentication failed due to malformed message or incorrect details */
|
||||
else
|
||||
|
@ -380,11 +475,11 @@ public final class BesterConnection : Thread
|
|||
*/
|
||||
debugPrint("Authenticating the user failed, sending error and closing connection.");
|
||||
|
||||
/* Send error message to client */
|
||||
sendStatus(2, JSONValue());
|
||||
/* Send fatal message */
|
||||
sendFatalMessage();
|
||||
|
||||
/* Stop the read/write loop */
|
||||
isActive = false;
|
||||
shutdown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -395,35 +490,46 @@ public final class BesterConnection : Thread
|
|||
|
||||
/* Set the connection type to `scopeField` */
|
||||
connectionType = scopeField;
|
||||
|
||||
if(connectionType == Scope.CLIENT)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Attempt to get the payload block and dispatch the message */
|
||||
bool dispatchStatus;
|
||||
|
||||
|
||||
/* Get the `payload` block */
|
||||
JSONValue payloadBlock = jsonMessage["payload"];
|
||||
debugPrint("<<< Payload is >>>\n\n" ~ payloadBlock.toPrettyString());
|
||||
|
||||
/* Dispatch the message */
|
||||
dispatchStatus = dispatchMessage(connectionType, payloadBlock);
|
||||
|
||||
/* TODO: Catch error here and not inside dispatchMessage, gets rid of the need for this if statement */
|
||||
if(dispatchStatus)
|
||||
/**
|
||||
* Dispatch the message. If a fatal failure is
|
||||
* detected then the connection will be shutdown.
|
||||
*/
|
||||
if(dispatchMessage(connectionType, payloadBlock))
|
||||
{
|
||||
debugPrint("Dispatch succeeded");
|
||||
}
|
||||
else
|
||||
{
|
||||
/* TODO: Error handling */
|
||||
debugPrint("Dispatching failed...");
|
||||
debugPrint("Dispatch failed, deactivating connection...");
|
||||
|
||||
/* Send fatal message */
|
||||
sendFatalMessage();
|
||||
|
||||
/* Shutdown the connection */
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
/* If the attempt to convert the message to JSON fails */
|
||||
catch(JSONException exception)
|
||||
{
|
||||
debugPrint("General format error");
|
||||
sendStatus(3, JSONValue());
|
||||
debugPrint("Fatal format error, deactivating connection...");
|
||||
|
||||
/* Send fatal message */
|
||||
sendFatalMessage();
|
||||
|
||||
/* Shutdown the connection */
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
module handlers.response;
|
||||
|
||||
import std.json : JSONValue, JSONException, parseJSON;
|
||||
import std.json : JSONValue, JSONException, parseJSON, toJSON;
|
||||
import std.conv : to;
|
||||
import utils.debugging : debugPrint;
|
||||
import std.string : cmp;
|
||||
|
@ -14,7 +14,10 @@ import std.string : split;
|
|||
import server.server : BesterServer;
|
||||
import handlers.commands : Command;
|
||||
|
||||
/* The type of the command the message handler wants us to run */
|
||||
/**
|
||||
* The type of the command the message handler wants
|
||||
* us to run
|
||||
*/
|
||||
private enum CommandType : ubyte
|
||||
{
|
||||
/* Simple message flow (always end point) */
|
||||
|
@ -155,10 +158,8 @@ public final class HandlerResponse
|
|||
* Executes the command. Either `sendClients`, `sendServers`
|
||||
* or `sendHandler`.
|
||||
*/
|
||||
public void execute(BesterConnection originalRequester)
|
||||
public void execute(BesterConnection originalRequester, string messageID)
|
||||
{
|
||||
/* TODO: Implement me */
|
||||
|
||||
/* If the command is SEND_CLIENTS */
|
||||
if(commandType == CommandType.SEND_CLIENTS)
|
||||
{
|
||||
|
@ -176,16 +177,12 @@ public final class HandlerResponse
|
|||
BesterConnection[] connectionList = originalRequester.server.getClients(clients);
|
||||
//debugPrint("Users matched online on server: " ~ to!(string)(connectionList));
|
||||
|
||||
|
||||
/* The fully response message to send back */
|
||||
JSONValue clientPayload;
|
||||
|
||||
// /* Set the `handlerName` field of the header block */
|
||||
// JSONValue handlerName;
|
||||
// handlerName["handlerName"] = handler.getPluginName();
|
||||
|
||||
/* Set the header of the response */
|
||||
JSONValue headerBlock;
|
||||
headerBlock["messageType"] = "receivedMessage";
|
||||
clientPayload["header"] = headerBlock;
|
||||
|
||||
/* Set the payload of the response */
|
||||
|
@ -196,10 +193,11 @@ public final class HandlerResponse
|
|||
|
||||
|
||||
/**
|
||||
* Loop through each BesterConnection in connectionList and
|
||||
* send the message-handler payload response message to each
|
||||
* of them.
|
||||
*/
|
||||
* Loop through each BesterConnection in connectionList and
|
||||
* send the message-handler payload response message to each
|
||||
* of them.
|
||||
*/
|
||||
bool allSuccess = true;
|
||||
for(ulong i = 0; i < connectionList.length; i++)
|
||||
{
|
||||
/* Get the conneciton */
|
||||
|
@ -219,17 +217,23 @@ public final class HandlerResponse
|
|||
catch(SocketOSException exception)
|
||||
{
|
||||
/**
|
||||
* If there was an error sending to the client, this can happen
|
||||
* if the client has disconnected but hasn't yet been removed from
|
||||
* the connections array and hence we try to send on a dead socket
|
||||
* or get the remoteAddress on a dead socket, which causes a
|
||||
* SocketOSException to be called.
|
||||
*/
|
||||
debugPrint("Attempted interacting with dead socket");
|
||||
* If there was an error sending to the client, this can happen
|
||||
* if the client has disconnected but hasn't yet been removed from
|
||||
* the connections array and hence we try to send on a dead socket
|
||||
* or get the remoteAddress on a dead socket, which causes a
|
||||
* SocketOSException to be called.
|
||||
*/
|
||||
debugPrint("Attempted interacting with dead socket");
|
||||
allSuccess = false;
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint("SEND_CLIENTS: Completed run");
|
||||
|
||||
/**
|
||||
* Send a status report here.
|
||||
*/
|
||||
originalRequester.sendStatusReport(cast(BesterConnection.StatusType)!allSuccess, messageID);
|
||||
}
|
||||
else if (commandType == CommandType.SEND_SERVERS)
|
||||
{
|
||||
|
@ -248,12 +252,8 @@ public final class HandlerResponse
|
|||
/* The fully response message to send back */
|
||||
JSONValue serverPayload;
|
||||
|
||||
/* Set the `scope` field of the header block */
|
||||
JSONValue scopeField = "server";
|
||||
|
||||
/* Set the header of the response */
|
||||
JSONValue headerBlock;
|
||||
// headerBlock["handlerName"] = handler.getPluginName();
|
||||
headerBlock["scope"] = "server";
|
||||
serverPayload["header"] = headerBlock;
|
||||
|
||||
|
@ -265,6 +265,7 @@ public final class HandlerResponse
|
|||
|
||||
|
||||
/* Attempt connecting to each server and sending the payload */
|
||||
bool allSuccess = true;
|
||||
for(ulong i = 0; i < servers.length; i++)
|
||||
{
|
||||
/* Get the current server address and port */
|
||||
|
@ -294,10 +295,16 @@ public final class HandlerResponse
|
|||
{
|
||||
/* TODO: Be more specific with the above exception type */
|
||||
debugPrint("Error whilst sending payload to server: " ~ e.toString());
|
||||
allSuccess = false;
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint("SEND_SERVERS: Completed run");
|
||||
|
||||
/**
|
||||
* Send a status report here.
|
||||
*/
|
||||
originalRequester.sendStatusReport(cast(BesterConnection.StatusType)!allSuccess, messageID);
|
||||
}
|
||||
else if (commandType == CommandType.SEND_HANDLER)
|
||||
{
|
||||
|
@ -312,7 +319,7 @@ public final class HandlerResponse
|
|||
HandlerResponse handlerResponse = chosenHandler.handleMessage(messageResponse["data"]);
|
||||
|
||||
/* Execute the code (this here, recursive) */
|
||||
handlerResponse.execute(originalRequester);
|
||||
handlerResponse.execute(originalRequester, messageID);
|
||||
|
||||
debugPrint("SEND_HANDLER: Completed run");
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ public class BesterListener : Thread
|
|||
besterConnection.start();
|
||||
|
||||
/* Add this client to the list of connected clients */
|
||||
server.clients ~= besterConnection;
|
||||
server.addConnection(besterConnection);
|
||||
}
|
||||
|
||||
/* Close the socket */
|
||||
|
|
|
@ -3,7 +3,7 @@ module server.informer.client;
|
|||
import core.thread : Thread;
|
||||
import server.server : BesterServer;
|
||||
import std.socket;
|
||||
import bmessage;
|
||||
import utils.message : receiveMessage, sendMessage;
|
||||
import std.json;
|
||||
import utils.debugging;
|
||||
import std.string;
|
||||
|
@ -77,7 +77,6 @@ public final class BesterInformerClient : Thread
|
|||
active = false;
|
||||
result = null; /* TODO: JSOn default value */
|
||||
}
|
||||
/* TODO: Add any more new command here */
|
||||
/* If the command is invalid */
|
||||
else
|
||||
{
|
||||
|
|
|
@ -4,6 +4,7 @@ import utils.debugging : debugPrint;
|
|||
import std.conv : to;
|
||||
import std.socket : Socket, AddressFamily, SocketType, ProtocolType, parseAddress;
|
||||
import core.thread : Thread;
|
||||
import core.sync.mutex;
|
||||
import std.stdio : writeln, File;
|
||||
import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON;
|
||||
import std.string : cmp, strip;
|
||||
|
@ -40,6 +41,7 @@ public final class BesterServer
|
|||
* Connected clients.
|
||||
*/
|
||||
public BesterConnection[] clients;
|
||||
private Mutex clientsMutex;
|
||||
|
||||
/**
|
||||
* The informer server.
|
||||
|
@ -88,6 +90,31 @@ public final class BesterServer
|
|||
return matchedUsers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new Connection, `connection`, to the server.
|
||||
*/
|
||||
public void addConnection(BesterConnection connection)
|
||||
{
|
||||
/**
|
||||
* Lock the mutex so that only one listener thread
|
||||
* may access the array at a time.
|
||||
*/
|
||||
clientsMutex.lock();
|
||||
|
||||
/**
|
||||
* Append the connection to the array
|
||||
*/
|
||||
clients ~= connection;
|
||||
|
||||
/**
|
||||
* Release the mutex so other listeners can now append
|
||||
* to the array.
|
||||
*/
|
||||
clientsMutex.unlock();
|
||||
}
|
||||
|
||||
/* TODO: Add more thread sfaety here and abroad */
|
||||
|
||||
/**
|
||||
* Adds a listener, `listener`, to this server's
|
||||
* listener set.
|
||||
|
@ -111,6 +138,9 @@ public final class BesterServer
|
|||
debugPrint("Setting up message handlers...");
|
||||
setupHandlers(config["handlers"]);
|
||||
setupDatabase(config["database"]);
|
||||
|
||||
/* Initialize the `clients` array mutex */
|
||||
clientsMutex = new Mutex();
|
||||
}
|
||||
|
||||
/* TODO: Add comment, implement me */
|
||||
|
@ -229,7 +259,7 @@ public final class BesterServer
|
|||
bool authed = true;
|
||||
|
||||
/* Strip the username of whitespace (TODO: Should we?) */
|
||||
//username = strip(username);
|
||||
username = strip(username);
|
||||
|
||||
/* Make sure username and password are not empty */
|
||||
if(cmp(username, "") != 0 && cmp(password, "") != 0)
|
||||
|
|
|
@ -14,10 +14,15 @@ import bmessage : bformatreceiveMessage = receiveMessage, bformatsendMessage = s
|
|||
*/
|
||||
public void receiveMessage(Socket originator, ref JSONValue receiveMessage)
|
||||
{
|
||||
if(!bformatreceiveMessage(originator, receiveMessage))
|
||||
/* The received bytes */
|
||||
byte[] receivedBytes;
|
||||
|
||||
if(!bformatreceiveMessage(originator, receivedBytes))
|
||||
{
|
||||
throw new NetworkException(originator);
|
||||
}
|
||||
|
||||
receiveMessage = parseJSON(cast(string)receivedBytes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -31,8 +36,11 @@ public void receiveMessage(Socket originator, ref JSONValue receiveMessage)
|
|||
*/
|
||||
public void sendMessage(Socket recipient, JSONValue jsonMessage)
|
||||
{
|
||||
if(!bformatsendMessage(recipient, jsonMessage))
|
||||
if(!bformatsendMessage(recipient, cast(byte[])toJSON(jsonMessage)))
|
||||
{
|
||||
throw new NetworkException(recipient);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* TODO: Hash message: Next-gen implementation */
|
|
@ -8,18 +8,24 @@ def sendAs(username):
|
|||
d.connect(("127.0.0.1",2223))
|
||||
|
||||
# First do it and authenticate
|
||||
bys=json.dumps({"header":{"authentication":{"username":username, "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1"}})
|
||||
bys=json.dumps({"header":{"authentication":{"username":username, "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1", "id" : "lol"}})
|
||||
print(len(bys), bys)
|
||||
d.send(len(bys).to_bytes(4, "little"))
|
||||
d.send(bys.encode())
|
||||
|
||||
# Loop prompt and sending of message to tbk
|
||||
while True:
|
||||
bys=json.dumps({"header":{"authentication":{"username":"ddd", "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1"}})
|
||||
bys=json.dumps({"header":{"authentication":{"username":"ddd", "password":"passwd"}, "scope":"client"},"payload":{"data":{"bruhMsg":input("Enter message naaier: ")},"type":"type1", "id":"lol"}})
|
||||
print(len(bys), bys)
|
||||
d.send(len(bys).to_bytes(4, "little"))
|
||||
d.send(bys.encode())
|
||||
|
||||
length=int.from_bytes(list(d.recv(4)), "little")
|
||||
print(length)
|
||||
receivedDataBytes = d.recv(length)
|
||||
receivedData = list(receivedDataBytes)
|
||||
print(receivedDataBytes.decode())
|
||||
|
||||
|
||||
|
||||
# Now we can do it again (without authentication)
|
||||
|
|
|
@ -25,7 +25,7 @@ def runTest():
|
|||
"header" : {
|
||||
"status" : "0",
|
||||
"command" : {"type" : "sendClients", "data": ["deavmi"]}
|
||||
}, "data" : "length of your data: " + str(len(receivedBys)) + "\"" + receivedBys + "\"" })
|
||||
}, "data" : "length of your data: " + str(len(receivedBys)) + "\"" + str(receivedBys) + "\"" })
|
||||
print(s.send(len(bys).to_bytes(4, "little")))
|
||||
print(s.send(bys.encode()))
|
||||
|
||||
|
|
Loading…
Reference in a new issue