2020-10-02 16:11:27 +02:00
|
|
|
/**
|
|
|
|
* DConfig
|
|
|
|
*
|
|
|
|
* Represents all configuration parameters
|
|
|
|
*/
|
|
|
|
module dnetd.dconfig;
|
|
|
|
|
|
|
|
import std.json;
|
|
|
|
import std.conv;
|
|
|
|
import std.socket : Address, parseAddress;
|
2020-12-20 23:48:52 +02:00
|
|
|
import gogga;
|
2021-01-29 14:28:52 +02:00
|
|
|
import dnetd.dlink : DLink;
|
|
|
|
import dnetd.dserver : DServer;
|
2020-10-02 16:11:27 +02:00
|
|
|
|
|
|
|
public final class DConfig
|
|
|
|
{
|
|
|
|
/* General configuration */
|
|
|
|
private DGeneralConfig generalConfig;
|
|
|
|
|
|
|
|
/* Link configuration */
|
|
|
|
private DLinkConfig linksConfig;
|
|
|
|
|
|
|
|
private this()
|
|
|
|
{
|
|
|
|
/* TODO: */
|
|
|
|
}
|
|
|
|
|
|
|
|
public DGeneralConfig getGeneral()
|
|
|
|
{
|
|
|
|
return generalConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DLinkConfig getLinks()
|
|
|
|
{
|
|
|
|
return linksConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DConfig getConfig(JSONValue json)
|
|
|
|
{
|
|
|
|
/* The newly created configuration */
|
|
|
|
DConfig config = new DConfig();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/* TODO: Parse */
|
|
|
|
|
|
|
|
/* Get the `general` block */
|
|
|
|
JSONValue generalBlock = json["general"];
|
|
|
|
config.generalConfig = DGeneralConfig.getConfig(generalBlock);
|
|
|
|
|
|
|
|
/* Get the `links` block */
|
2021-01-28 23:43:22 +02:00
|
|
|
JSONValue linksBlock = json["links"];
|
|
|
|
config.linksConfig = DLinkConfig.getConfig(linksBlock);
|
2020-10-02 16:11:27 +02:00
|
|
|
}
|
|
|
|
catch(JSONException e)
|
|
|
|
{
|
|
|
|
/* Set config to null (signals an error) */
|
|
|
|
config = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JSONValue saveConfig()
|
|
|
|
{
|
|
|
|
JSONValue config;
|
|
|
|
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final class DGeneralConfig
|
|
|
|
{
|
|
|
|
/* Addresses to bind sockets to */
|
2020-12-20 19:36:53 +02:00
|
|
|
private Address[] addresses;
|
2020-10-02 16:11:27 +02:00
|
|
|
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();
|
2020-12-20 23:48:52 +02:00
|
|
|
gprintln("Reading config:\n"~generalBlock.toPrettyString());
|
2020-10-02 16:11:27 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-12-20 19:36:53 +02:00
|
|
|
/* Set the addresses to bind to */
|
|
|
|
foreach(JSONValue bindBlock; generalBlock["binds"].array())
|
2020-10-02 16:11:27 +02:00
|
|
|
{
|
2020-12-20 19:36:53 +02:00
|
|
|
/* 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);
|
2020-10-02 16:11:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:20:46 +02:00
|
|
|
public Address[] getAddresses()
|
2020-10-02 16:11:27 +02:00
|
|
|
{
|
2020-12-20 19:36:53 +02:00
|
|
|
return addresses;
|
2020-10-02 16:11:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public final class DLinkConfig
|
|
|
|
{
|
2021-01-29 14:28:52 +02:00
|
|
|
/* Server links */
|
|
|
|
private DLink[] links;
|
|
|
|
private Address[] addresses;
|
|
|
|
|
|
|
|
public static DLinkConfig getConfig(DServer dserver, JSONValue linksBlock)
|
2021-01-28 23:43:22 +02:00
|
|
|
{
|
2021-01-29 14:28:52 +02:00
|
|
|
DLinkConfig dlinkConfig = new DLinkConfig();
|
2021-01-28 23:43:22 +02:00
|
|
|
|
2021-01-29 11:08:24 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
2020-10-02 16:11:27 +02:00
|
|
|
|
2021-01-29 14:28:52 +02:00
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
|
2021-01-28 23:43:22 +02:00
|
|
|
|
|
|
|
return dlinkConfig;
|
|
|
|
}
|
2020-10-02 16:11:27 +02:00
|
|
|
}
|