2020-09-23 07:37:18 +00:00
|
|
|
import std.stdio;
|
|
|
|
import std.socket : parseAddress;
|
|
|
|
import dnetd.dserver : DServer;
|
2020-10-02 14:11:27 +00:00
|
|
|
import dnetd.dconfig : DConfig;
|
|
|
|
import std.json;
|
|
|
|
import std.exception;
|
2020-09-23 07:37:18 +00:00
|
|
|
|
2020-10-02 14:11:27 +00:00
|
|
|
void main(string[] args)
|
2020-09-23 07:37:18 +00:00
|
|
|
{
|
2020-10-02 14:11:27 +00:00
|
|
|
/* Configuration file */
|
|
|
|
string configFilename;
|
|
|
|
|
|
|
|
/* If there are no arguments */
|
2020-10-02 15:08:35 +00:00
|
|
|
if(args.length == 1)
|
2020-10-02 14:11:27 +00:00
|
|
|
{
|
|
|
|
/* Use the default file */
|
|
|
|
configFilename = "config.json";
|
|
|
|
}
|
|
|
|
/* If there is one argument */
|
2020-10-02 15:08:35 +00:00
|
|
|
else if(args.length == 2)
|
2020-10-02 14:11:27 +00:00
|
|
|
{
|
|
|
|
/* use the specified one */
|
|
|
|
configFilename = args[0];
|
|
|
|
}
|
|
|
|
/* Illegal amount of guns in one household (no such thing) */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writeln("Invalid number of arguments");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configuration file contents */
|
|
|
|
byte[] data;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/* Open the file for reading */
|
|
|
|
File config;
|
|
|
|
config.open(configFilename, "r");
|
|
|
|
|
|
|
|
/* Read the configuration file data */
|
|
|
|
data.length = config.size();
|
|
|
|
data = config.rawRead(data);
|
|
|
|
config.close();
|
|
|
|
}
|
|
|
|
catch(ErrnoException e)
|
|
|
|
{
|
|
|
|
writeln("Failure to use configuration file'"~configFilename~"' with error:\n\n"~e.toString());
|
|
|
|
return;
|
|
|
|
}
|
2020-09-23 07:37:18 +00:00
|
|
|
|
2020-10-02 14:11:27 +00:00
|
|
|
/* The JSON */
|
|
|
|
JSONValue json;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
/* Parse the configuration file */
|
|
|
|
json = parseJSON(cast(string)data);
|
|
|
|
}
|
|
|
|
catch(JSONException e)
|
|
|
|
{
|
|
|
|
writeln("Failure to parse configuration file'"~configFilename~"' with error:\n\n"~e.toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new configuration file and check configuration parameters */
|
|
|
|
DConfig config = DConfig.getConfig(json);
|
|
|
|
|
|
|
|
/* If the configuration reading was successful (valid JSON) */
|
|
|
|
if(config)
|
|
|
|
{
|
|
|
|
/* Start the server */
|
|
|
|
DServer dserver = new DServer(config);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writeln("Failure to read a valid dnetd configuration file'"~configFilename~"'");
|
|
|
|
}
|
|
|
|
|
2020-09-23 07:37:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|