From 5f443cfa2c063edd441bc58fd6955994b7347aa1 Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Fri, 29 Jan 2021 14:28:52 +0200 Subject: [PATCH] Configure at start up (server linking) --- source/app.d | 10 ++++- source/dnetd/dconfig.d | 29 ++++++++++++- source/dnetd/dlink.d | 92 ++++++++++++++++++++++++++++++++++-------- testing/1.json | 6 +-- testing/2.json | 2 +- 5 files changed, 115 insertions(+), 24 deletions(-) diff --git a/source/app.d b/source/app.d index 30e010a..e32b6d3 100644 --- a/source/app.d +++ b/source/app.d @@ -1,7 +1,7 @@ import std.stdio; import std.socket : parseAddress; import dnetd.dserver : DServer; -import dnetd.dconfig : DConfig; +import dnetd.dconfig : DConfig, DLinkConfig; import std.json; import std.exception; import gogga; @@ -72,6 +72,14 @@ void main(string[] args) { /* Start the server */ DServer dserver = new DServer(config); + + /* Now configure the the linking */ + DLinkConfig linkConfig = DLinkConfig.getConfig(dserver, json["links"]); + + /* TODO: COnfigure here */ + + + /* TODO: Start meyer here (remove from inside DServer) */ } else { diff --git a/source/dnetd/dconfig.d b/source/dnetd/dconfig.d index f057c3f..b81fdb6 100644 --- a/source/dnetd/dconfig.d +++ b/source/dnetd/dconfig.d @@ -9,6 +9,8 @@ import std.json; import std.conv; import std.socket : Address, parseAddress; import gogga; +import dnetd.dlink : DLink; +import dnetd.dserver : DServer; public final class DConfig { @@ -131,9 +133,13 @@ public final class DGeneralConfig public final class DLinkConfig { - public static DLinkConfig getConfig(JSONValue linksBlock) + /* Server links */ + private DLink[] links; + private Address[] addresses; + + public static DLinkConfig getConfig(DServer dserver, JSONValue linksBlock) { - DLinkConfig dlinkConfig; + DLinkConfig dlinkConfig = new DLinkConfig(); /* Get the active servers */ string[] activeServers; @@ -145,6 +151,25 @@ public final class DLinkConfig activeServers ~= server; } + /* Parse each link and add it to the link-list */ + foreach(string server; activeServers) + { + /* Get the peer block */ + JSONValue peerBlock = linksBlock[server]; + + /* Get the name */ + string name = peerBlock["name"].str(); + + /* Get the address */ + string address = peerBlock["address"].str(); + + /* Get the port */ + ushort port = to!(ushort)(peerBlock["port"].str()); + + /* Add the address and port tuple to the list of addresses to bind to */ + dlinkConfig.links ~= new DLink(server, name, parseAddress(address, port)); + } + return dlinkConfig; } diff --git a/source/dnetd/dlink.d b/source/dnetd/dlink.d index d4ad372..4b347d9 100644 --- a/source/dnetd/dlink.d +++ b/source/dnetd/dlink.d @@ -6,6 +6,8 @@ import std.stdio; import std.conv; import dnetd.dserver; import dnetd.dconfig; +import std.socket : Address; +import core.thread : Thread; /** * DLink @@ -14,7 +16,7 @@ import dnetd.dconfig; * with information about what this link * knows and can tell us */ -public final class DLink +public final class DLidnk { /* The directly attached peer */ private DConnection directPeer; @@ -34,6 +36,58 @@ public final class DLink } } +/** +* Represents a server link +* +* Either used for inbound or outbound +*/ +public final class DLink : Thread +{ + /* Associated server */ + private DServer server; + + /* The connection */ + private DConnection connection; + + /** + * Links details + */ + private string name; + private Address address; + + /** + * Constructs a DLink for an outbound peering + */ + this(DServer server, string name, Address address) + { + /* Create an outbound connection */ + /* TODO: Fuuuuuuuuuuuuuuuuuuuck handling of shit here bababooey and not in dconnection.d as we would have done below */ + + /* Initialize a new outbound connection */ + initializeOutboundConnection(); + } + + /** + * Initializes a new outbound connection + */ + private void initializeOutboundConnection() + { + /* Open a connection to the server */ + // connection = new DConnection(); + } + + /** + * Constructs a DLink for an inbound peering + */ + this(DServer server, string name, Address address, DConnection connection) + { + /* Save name and address */ + this(server, name, address); + + /* Save connection */ + } +} + public final class DMeyer { /* Direct peers */ @@ -49,6 +103,10 @@ public final class DMeyer /* Initialize the locks */ initLocks(); + + /* Open a connection to the server */ + + /* TODO: Open connections to all servers we are yet to open a connection to (check the `links` array) */ } /* Initialize locks */ @@ -57,32 +115,32 @@ public final class DMeyer linksMutex = new Mutex(); } - /* Attach a direct peer */ - public void attachDirectPeer(DConnection peer) - { - /* TODO: Add to `directPeers` */ - linksMutex.lock(); + // /* Attach a direct peer */ + // public void attachDirectPeer(DConnection peer) + // { + // /* TODO: Add to `directPeers` */ + // linksMutex.lock(); - links ~= new DLink(peer); - writeln("Attached direct peer: "~to!(string)(peer)); + // links ~= new DLink(peer); + // writeln("Attached direct peer: "~to!(string)(peer)); - linksMutex.unlock(); - } + // linksMutex.unlock(); + // } /* Get a list of all servers we know of */ - public DLink getLink(DConnection peer) - { - DLink link; + // public DLink getLink(DConnection peer) + // { + // DLink link; - linksMutex.lock(); + // linksMutex.lock(); - linksMutex.unlock(); + // linksMutex.unlock(); - return link; - } + // return link; + // } } diff --git a/testing/1.json b/testing/1.json index 85a699f..ea094ea 100644 --- a/testing/1.json +++ b/testing/1.json @@ -11,13 +11,13 @@ }, ], "network" : "aBasedIRCNetwork", - "name" : "MyBrandSpankingNewIRCServer", + "name" : "server1", "motd" : "Welcome to my generic dnet chat server!" }, "links" : { - "server1" : { - "name" : "server2", + "server2" : { + "name": "server2", "address" : "127.0.0.1", "port" : "6666" }, diff --git a/testing/2.json b/testing/2.json index 06ebc29..dc4bf89 100644 --- a/testing/2.json +++ b/testing/2.json @@ -11,7 +11,7 @@ } ], "network" : "aBasedIRCNetwork", - "name" : "MyBrandSpankingNewIRCServer", + "name" : "server2", "motd" : "Welcome to my generic dnet chat server!" },