1
0
mirror of https://github.com/deavminet/dnetd synced 2024-09-21 17:53:39 +02:00
dnetd_old/source/dnetd/dconfig.d
2021-01-29 17:00:22 +02:00

121 lines
3.1 KiB
D

/**
* DConfig
*
* Represents all configuration parameters
*/
module dnetd.dconfig;
import std.json;
import std.conv;
import std.socket : Address, parseAddress;
import gogga;
import dnetd.dlink : DLink;
import dnetd.dserver : DServer;
public final class DGeneralConfig
{
/* Addresses to bind sockets to */
private Address[] addresses;
private ushort port;
/* Server information */
private string network;
private string name;
private string motd;
public static DGeneralConfig getConfig(JSONValue generalBlock)
{
/* The generated general config */
DGeneralConfig config = new DGeneralConfig();
gprintln("Reading config:\n"~generalBlock.toPrettyString());
try
{
/* Set the addresses to bind to */
foreach(JSONValue bindBlock; generalBlock["binds"].array())
{
/* Get the address */
string address = bindBlock["address"].str();
/* Get the port */
ushort port = to!(ushort)(bindBlock["port"].str());
/* Add the address and port tuple to the list of addresses to bind to */
config.addresses ~= parseAddress(address, port);
}
/* Set the network name */
config.network = generalBlock["network"].str();
/* Set the server name */
config.name = generalBlock["name"].str();
/* Set the message of the day */
config.motd = generalBlock["motd"].str();
}
catch(JSONException e)
{
/* Set the config to null (signals an error) */
config = null;
}
return config;
}
public string getMotd()
{
return motd;
}
public Address[] getAddresses()
{
return addresses;
}
}
public final class DLinkConfig
{
/* Server links */
private DLink[] links;
public static DLinkConfig getConfig(DServer dserver, JSONValue linksBlock)
{
DLinkConfig dlinkConfig = new DLinkConfig();
/* Get the active servers */
string[] activeServers;
JSONValue[] serversActive = linksBlock["active"].array();
foreach(JSONValue activeServer; serversActive)
{
string server = activeServer.str();
gprintln("Found active server to be linked with \""~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(dserver, name, parseAddress(address, port));
}
return dlinkConfig;
}
public DLink[] getLinks()
{
return links;
}
}