1 module besterd;
2 
3 
4 
5 import server.server;
6 import std.conv : to;
7 import std.socket : SocketOSException, parseAddress, UnixAddress;
8 import utils.debugging : debugPrint;
9 import std.stdio : File, writeln;
10 import std.json : parseJSON, JSONValue;
11 import listeners.listener;
12 import listeners.types;
13 
14 unittest d
15 {
16 	writeln("Hello");
17 	main();
18 }
19 
20 void main()
21 {
22 	/* TODO: Change this */
23 	string address = "0.0.0.0";
24 	ushort port = 2222;
25 
26 	/* TODO: Add usage check and arguments before this */
27 	startServer("server.conf");
28 
29 	writeln("fdhjf he do be vibing though");
30 }
31 
32 
33 
34 
35 JSONValue getConfig(string configurationFilePath)
36 {
37 	/* TODO: Open the file here */
38 	File configFile;
39 	configFile.open(configurationFilePath);
40 
41 	/* The file buffer */
42 	byte[] fileBuffer;
43 
44 	/* Allocate the buffer to be the size of the file */
45 	fileBuffer.length = configFile.size();
46 
47 	/* Read the content of the file */
48 	/* TODO: Error handling ErrnoException */
49 	fileBuffer = configFile.rawRead(fileBuffer);
50 	configFile.close();
51 
52 	JSONValue config;
53 
54 	/* TODO: JSON error checking */
55 	config = parseJSON(cast(string)fileBuffer);
56 
57 	return config;	
58 }
59 
60 BesterListener[] getListeners(BesterServer server, JSONValue networkBlock)
61 {
62 	BesterListener[] listeners;
63 
64 	/* TODO: Error handling and get keys and clean up for formality */
65 
66 	/* Look for IPv4 TCP block */
67 	JSONValue inet4TCPBlock = networkBlock["tcp4"];
68 	debugPrint("<<< IPv4 TCP Block >>>\n" ~ inet4TCPBlock.toPrettyString());
69 	string inet4Address = inet4TCPBlock["address"].str();
70 	ushort inet4Port = to!(ushort)(inet4TCPBlock["port"].str());
71 	TCP4Listener tcp4Listener = new TCP4Listener(server, parseAddress(inet4Address, inet4Port));
72 	listeners ~= tcp4Listener;
73 
74 	/* Look for IPv6 TCP block */
75 	JSONValue inet6TCPBlock = networkBlock["tcp6"];
76 	debugPrint("<<< IPv6 TCP Block >>>\n" ~ inet6TCPBlock.toPrettyString());
77 	string inet6Address = inet6TCPBlock["address"].str();
78 	ushort inet6Port = to!(ushort)(inet6TCPBlock["port"].str());
79 	TCP6Listener tcp6Listener = new TCP6Listener(server, parseAddress(inet6Address, inet6Port));
80 	listeners ~= tcp6Listener;
81 
82 	/* Look for UNIX Domain block */
83 	JSONValue unixDomainBlock = networkBlock["unix"];
84 	debugPrint("<<< UNIX Domain Block >>>\n" ~ unixDomainBlock.toPrettyString());
85 	string unixAddress = unixDomainBlock["address"].str();
86 //	UNIXListener unixListener = new UNIXListener(server, new UnixAddress(unixAddress));
87 //	listeners ~= unixListener;
88 
89 	return listeners;
90 }
91 
92 void startServer(string configurationFilePath)
93 {
94 	/* The server configuration */
95 	JSONValue serverConfiguration = getConfig(configurationFilePath);
96 	debugPrint("<<< Bester.d configuration >>>\n" ~ serverConfiguration.toPrettyString());
97 
98 	try
99 	{
100 		/* The server */
101 		BesterServer server = null;
102 
103 		/* TODO: Bounds anc type checking */
104 
105 		/* Get the network block */
106 		JSONValue networkBlock = serverConfiguration["network"];
107 
108 		/* Create the Bester server */
109 		server = new BesterServer(serverConfiguration);
110 
111 		/* TODO: Get keys */
112 		BesterListener[] listeners = getListeners(server, networkBlock);
113 
114 		for(ulong i = 0; i < listeners.length; i++)
115 		{
116 			/* Add listener */
117 			server.addListener(listeners[i]);
118 		}
119 		
120 		/* Start running the server (starts the listeners) */
121 		server.run();
122 	}
123 	catch(SocketOSException exception)
124 	{
125 		debugPrint("Error binding: " ~ exception.toString());
126 	}
127 	
128 }