1
0
mirror of https://github.com/deavminet/dnetd synced 2024-09-21 17:53:39 +02:00

Configure at start up (server linking)

This commit is contained in:
Tristan B. Kildaire 2021-01-29 14:28:52 +02:00
parent 4e4605ff4c
commit 5f443cfa2c
5 changed files with 115 additions and 24 deletions

View File

@ -1,7 +1,7 @@
import std.stdio; import std.stdio;
import std.socket : parseAddress; import std.socket : parseAddress;
import dnetd.dserver : DServer; import dnetd.dserver : DServer;
import dnetd.dconfig : DConfig; import dnetd.dconfig : DConfig, DLinkConfig;
import std.json; import std.json;
import std.exception; import std.exception;
import gogga; import gogga;
@ -72,6 +72,14 @@ void main(string[] args)
{ {
/* Start the server */ /* Start the server */
DServer dserver = new DServer(config); 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 else
{ {

View File

@ -9,6 +9,8 @@ import std.json;
import std.conv; import std.conv;
import std.socket : Address, parseAddress; import std.socket : Address, parseAddress;
import gogga; import gogga;
import dnetd.dlink : DLink;
import dnetd.dserver : DServer;
public final class DConfig public final class DConfig
{ {
@ -131,9 +133,13 @@ public final class DGeneralConfig
public final class DLinkConfig 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 */ /* Get the active servers */
string[] activeServers; string[] activeServers;
@ -145,6 +151,25 @@ public final class DLinkConfig
activeServers ~= server; 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; return dlinkConfig;
} }

View File

@ -6,6 +6,8 @@ import std.stdio;
import std.conv; import std.conv;
import dnetd.dserver; import dnetd.dserver;
import dnetd.dconfig; import dnetd.dconfig;
import std.socket : Address;
import core.thread : Thread;
/** /**
* DLink * DLink
@ -14,7 +16,7 @@ import dnetd.dconfig;
* with information about what this link * with information about what this link
* knows and can tell us * knows and can tell us
*/ */
public final class DLink public final class DLidnk
{ {
/* The directly attached peer */ /* The directly attached peer */
private DConnection directPeer; 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 public final class DMeyer
{ {
/* Direct peers */ /* Direct peers */
@ -49,6 +103,10 @@ public final class DMeyer
/* Initialize the locks */ /* Initialize the locks */
initLocks(); 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 */ /* Initialize locks */
@ -57,32 +115,32 @@ public final class DMeyer
linksMutex = new Mutex(); linksMutex = new Mutex();
} }
/* Attach a direct peer */ // /* Attach a direct peer */
public void attachDirectPeer(DConnection peer) // public void attachDirectPeer(DConnection peer)
{ // {
/* TODO: Add to `directPeers` */ // /* TODO: Add to `directPeers` */
linksMutex.lock(); // linksMutex.lock();
links ~= new DLink(peer); // links ~= new DLink(peer);
writeln("Attached direct peer: "~to!(string)(peer)); // writeln("Attached direct peer: "~to!(string)(peer));
linksMutex.unlock(); // linksMutex.unlock();
} // }
/* Get a list of all servers we know of */ /* Get a list of all servers we know of */
public DLink getLink(DConnection peer) // public DLink getLink(DConnection peer)
{ // {
DLink link; // DLink link;
linksMutex.lock(); // linksMutex.lock();
linksMutex.unlock(); // linksMutex.unlock();
return link; // return link;
} // }
} }

View File

@ -11,12 +11,12 @@
}, },
], ],
"network" : "aBasedIRCNetwork", "network" : "aBasedIRCNetwork",
"name" : "MyBrandSpankingNewIRCServer", "name" : "server1",
"motd" : "Welcome to my generic dnet chat server!" "motd" : "Welcome to my generic dnet chat server!"
}, },
"links" : { "links" : {
"server1" : { "server2" : {
"name": "server2", "name": "server2",
"address" : "127.0.0.1", "address" : "127.0.0.1",
"port" : "6666" "port" : "6666"

View File

@ -11,7 +11,7 @@
} }
], ],
"network" : "aBasedIRCNetwork", "network" : "aBasedIRCNetwork",
"name" : "MyBrandSpankingNewIRCServer", "name" : "server2",
"motd" : "Welcome to my generic dnet chat server!" "motd" : "Welcome to my generic dnet chat server!"
}, },