From 785113e5f8adc99ec6942391603e8cbdc4d9231e Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Mon, 27 Jul 2020 14:56:36 +0200 Subject: [PATCH] Fixed all compile errors within client.d --- source/app.d | 6 ++++-- source/client/client.d | 17 +++++++++-------- source/server/listener.d | 10 ++++++---- source/server/listeners.d | 13 +++++++++++++ source/server/server.d | 11 ++++++++--- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/source/app.d b/source/app.d index 562ef88..9512dc6 100644 --- a/source/app.d +++ b/source/app.d @@ -26,9 +26,11 @@ void main() /* Construct the listeners form the config file */ ButterflyListener[] listeners = constructListeners(config["listeners"]); - /* Start the server */ - //Address address = parseAddress(config["address"].str(), to!(ushort)(config["port"].str())); + /* Create the server */ ButterflyServer server = new ButterflyServer(listeners); + + /* Start the server */ + server.run(); } private ButterflyListener[] constructListeners(JSONValue listenersBlock) diff --git a/source/client/client.d b/source/client/client.d index 9fd131f..4a6f54f 100644 --- a/source/client/client.d +++ b/source/client/client.d @@ -13,13 +13,14 @@ import client.exceptions; import std.file; import std.exception; import std.datetime.systime : Clock, SysTime; +import server.listener : ButterflyListener; public final class ButterflyClient : Thread { /** - * The associated server + * The associated listener */ - private ButterflyServer server; + private ButterflyListener listener; /** * Socket of the client connection @@ -48,11 +49,11 @@ public final class ButterflyClient : Thread */ private Mailbox mailbox; - this(ButterflyServer server, Socket clientSocket) + this(ButterflyListener listener, Socket clientSocket) { super(&run); this.clientSocket = clientSocket; - this.server = server; + this.listener = listener; } private void run() @@ -490,7 +491,7 @@ public final class ButterflyClient : Thread * Check if the domain of this recipient is this server * or if it is a remote server. */ - if(cmp(domain, server.domain) == 0) + if(cmp(domain, listener.getDomain()) == 0) { writeln("Storing mail message to "~recipient~" ..."); @@ -518,7 +519,7 @@ public final class ButterflyClient : Thread private bool filterMailOutgoing(JSONValue* mailBlock) { /* Add the from field to the mail block */ - (*mailBlock)["from"] = mailbox.username~"@"~server.domain; + (*mailBlock)["from"] = mailbox.username~"@"~listener.getDomain(); /* Add the sent time stamp */ (*mailBlock)["sentTimestamp"] = Clock.currTime().toString(); @@ -574,7 +575,7 @@ public final class ButterflyClient : Thread * Check if the domain of this recipient is this server * or if it is a remote server. */ - if(server.isLocalDomain(domain)) + if(listener.getServer().isLocalDomain(domain)) { writeln("Local delivery occurring..."); @@ -688,7 +689,7 @@ public final class ButterflyClient : Thread { /* Create the error message */ JSONValue deliveryReport; - JSONValue[] errorRecipients = [JSONValue(mailbox.username~"@"~server.domain)]; + JSONValue[] errorRecipients = [JSONValue(mailbox.username~"@"~listener.getDomain())]; deliveryReport["recipients"] = errorRecipients; /* TODO: Make more indepth, and have copy of the mail that was tried to be sent */ diff --git a/source/server/listener.d b/source/server/listener.d index 6af887a..f647b07 100644 --- a/source/server/listener.d +++ b/source/server/listener.d @@ -19,16 +19,18 @@ public abstract class ButterflyListener : Thread this.domain = domain; } - public abstract void run() - { - - } + public abstract void run(); public void setServer(ButterflyServer server) { this.server = server; } + public ButterflyServer getServer() + { + return server; + } + public string getName() { return listenerName; diff --git a/source/server/listeners.d b/source/server/listeners.d index 0ac8d59..ea71e8e 100644 --- a/source/server/listeners.d +++ b/source/server/listeners.d @@ -4,6 +4,7 @@ import core.thread : Thread; import server.listener : ButterflyListener; import std.socket : Socket, Address, SocketType, ProtocolType; import std.json : JSONValue; +import client.client; public class IPv4Listener : ButterflyListener { @@ -27,6 +28,18 @@ public class IPv4Listener : ButterflyListener public override void run() { + serverSocket.listen(1); //TODO: backlog + while(true) + { + /* Accept the queued connection */ + Socket clientConnection = serverSocket.accept(); + + + ButterflyClient client = new ButterflyClient(); + + /* Start the client handler */ + butterflyClient.start(); + } } } \ No newline at end of file diff --git a/source/server/server.d b/source/server/server.d index 23c74c7..799705b 100644 --- a/source/server/server.d +++ b/source/server/server.d @@ -37,8 +37,13 @@ public final class ButterflyServer */ this.listeners = listeners; - /* Start accepting connections */ - run(); + /** + * Set the server of all listeners to this server + */ + foreach(ButterflyListener listener; listeners) + { + listener.setServer(this); + } } private void directoryCheck() @@ -84,7 +89,7 @@ public final class ButterflyServer } } - private void run() + public void run() { /* Start the listeners */ foreach(ButterflyListener listener; listeners)