Compare commits

...

19 Commits

Author SHA1 Message Date
Tristan B. Kildaire 0f75841600 Upgraded 2020-07-20 19:45:25 +02:00
Tristan B. Kildaire 5fc42a6ef9 ACTUALLY updated bformat 2020-06-17 08:25:23 +02:00
Tristan B. Kildaire 72139e70c3 Updated bformat. 2020-06-17 08:24:48 +02:00
Tristan B. Kildaire ac56eb7689 Added TODO for further thread safety changes to come. 2020-06-16 18:50:50 +02:00
Tristan B. Kildaire d19b552997 Migrated successfully to new bformat. 2020-06-16 18:46:54 +02:00
Tristan B. Kildaire 261448159f Had a mistake migrating to the new bformat, now migrated almost. 2020-06-16 18:46:26 +02:00
Tristan B. Kildaire c4a4d46264 Upgraded to new bformat v1.0.5 2020-06-16 18:46:07 +02:00
Tristan B. Kildaire c7f701e8f6 Use correct bformat version. 2020-06-16 18:39:50 +02:00
Tristan B. Kildaire b783d98ea7 `addConnection` is now threadsafe. 2020-06-16 18:35:00 +02:00
Tristan B. Kildaire 5c13fa3c77 Using new `addConnection` which aims to be thread safe. 2020-06-16 18:32:17 +02:00
Tristan B. Kildaire 60cd993472 Added stub function `addConnection` to the BesterServer type for thread safe Connection object array appending. 2020-06-16 18:29:42 +02:00
Tristan B. Kildaire aec7144106 Converted missing bmessage `sendMessage` call. 2020-06-16 18:20:45 +02:00
Tristan B. Kildaire 9db02f607b Updated to new bmessage API. 2020-06-16 18:19:46 +02:00
Tristan B. Kildaire c646b4bf20 Updated vibe-d 2020-06-16 18:10:18 +02:00
Tristan B. Kildaire 88af27c3cb Stripepd whitespace from username. 2020-05-15 16:47:18 +02:00
Tristan B. Kildaire 2b54142439 Cleaned up and added TODOs 2020-05-15 16:31:35 +02:00
Tristan B. Kildaire 0b5ca124d6 Use new shutdown and send a fatal message. 2020-05-15 16:06:40 +02:00
Tristan B. Kildaire 71632ec1f3 Added comment.
Remove dunused function.
2020-05-15 15:54:37 +02:00
Tristan B. Kildaire 80b0772fd8 Removed some TODOs 2020-05-15 15:52:33 +02:00
8 changed files with 80 additions and 50 deletions

View File

@ -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",

View File

@ -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"
}
}

View File

@ -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)
@ -272,15 +279,12 @@ public final class BesterConnection : Thread
{
/* 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 */
@ -291,7 +295,6 @@ public final class BesterConnection : Thread
}
debugPrint("Handler section done (for client)");
/* TODO: Handle response */
}
/* If no message handler for the specified type could be found */
else
@ -306,6 +309,10 @@ public final class BesterConnection : Thread
return true;
}
/**
* Type of the status report.
* Either 0 (for success) or 1 (for failure).
*/
public enum StatusType
{
SUCCESS,
@ -347,27 +354,6 @@ public final class BesterConnection : Thread
}
}
/* Send a status message to the client */
public void sendStatus(uint code, JSONValue data)
{
/* Construct a status message */
JSONValue statusMessage;
JSONValue statusBlock;
statusBlock["code"] = to!(string)(code);
statusBlock["data"] = data;
statusMessage["status"] = statusBlock;
try
{
/* Send the message */
sendMessage(clientConnection, statusMessage);
}
catch(NetworkException e)
{
debugPrint("Error sending status message");
}
}
/**
* Given the headerBlock, this returns the requested scope
* of the connection.
@ -392,10 +378,15 @@ public final class BesterConnection : Thread
return Scope.UNKNOWN;
}
/* TODO: Implement me */
/**
* 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 */
@ -430,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)

View File

@ -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) */
@ -157,8 +160,6 @@ public final class HandlerResponse
*/
public void execute(BesterConnection originalRequester, string messageID)
{
/* TODO: Implement me */
/* If the command is SEND_CLIENTS */
if(commandType == CommandType.SEND_CLIENTS)
{
@ -180,7 +181,7 @@ public final class HandlerResponse
JSONValue clientPayload;
/* Set the header of the response */
JSONValue headerBlock; /* TODO: Do something with this */
JSONValue headerBlock;
headerBlock["messageType"] = "receivedMessage";
clientPayload["header"] = headerBlock;

View File

@ -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 */

View File

@ -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;

View File

@ -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)

View File

@ -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,7 +36,7 @@ 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);
}