diff --git a/source/app.d b/source/app.d index b0c7169..a7ac1d6 100644 --- a/source/app.d +++ b/source/app.d @@ -1,7 +1,15 @@ -module app; +module besterd; + + -import std.stdio; import server.server; +import std.conv : to; +import std.socket : SocketOSException, parseAddress, UnixAddress; +import utils.debugging : debugPrint; +import std.stdio : File, writeln; +import std.json : parseJSON, JSONValue; +import listeners.listener; +import listeners.types; unittest d { @@ -20,3 +28,101 @@ void main() writeln("fdhjf he do be vibing though"); } + + + + +JSONValue getConfig(string configurationFilePath) +{ + /* TODO: Open the file here */ + File configFile; + configFile.open(configurationFilePath); + + /* The file buffer */ + byte[] fileBuffer; + + /* Allocate the buffer to be the size of the file */ + fileBuffer.length = configFile.size(); + + /* Read the content of the file */ + /* TODO: Error handling ErrnoException */ + fileBuffer = configFile.rawRead(fileBuffer); + configFile.close(); + + JSONValue config; + + /* TODO: JSON error checking */ + config = parseJSON(cast(string)fileBuffer); + + return config; +} + +BesterListener[] getListeners(BesterServer server, JSONValue networkBlock) +{ + BesterListener[] listeners; + + /* TODO: Error handling and get keys and clean up for formality */ + + /* Look for IPv4 TCP block */ + JSONValue inet4TCPBlock = networkBlock["tcp4"]; + debugPrint("<<< IPv4 TCP Block >>>\n" ~ inet4TCPBlock.toPrettyString()); + string inet4Address = inet4TCPBlock["address"].str(); + ushort inet4Port = to!(ushort)(inet4TCPBlock["port"].str()); + TCP4Listener tcp4Listener = new TCP4Listener(server, parseAddress(inet4Address, inet4Port)); + listeners ~= tcp4Listener; + + /* Look for IPv6 TCP block */ + JSONValue inet6TCPBlock = networkBlock["tcp6"]; + debugPrint("<<< IPv6 TCP Block >>>\n" ~ inet6TCPBlock.toPrettyString()); + string inet6Address = inet6TCPBlock["address"].str(); + ushort inet6Port = to!(ushort)(inet6TCPBlock["port"].str()); + TCP6Listener tcp6Listener = new TCP6Listener(server, parseAddress(inet6Address, inet6Port)); + listeners ~= tcp6Listener; + + /* Look for UNIX Domain block */ + JSONValue unixDomainBlock = networkBlock["unix"]; + debugPrint("<<< UNIX Domain Block >>>\n" ~ unixDomainBlock.toPrettyString()); + string unixAddress = unixDomainBlock["address"].str(); +// UNIXListener unixListener = new UNIXListener(server, new UnixAddress(unixAddress)); +// listeners ~= unixListener; + + return listeners; +} + +void startServer(string configurationFilePath) +{ + /* The server configuration */ + JSONValue serverConfiguration = getConfig(configurationFilePath); + debugPrint("<<< Bester.d configuration >>>\n" ~ serverConfiguration.toPrettyString()); + + try + { + /* The server */ + BesterServer server = null; + + /* TODO: Bounds anc type checking */ + + /* Get the network block */ + JSONValue networkBlock = serverConfiguration["network"]; + + /* Create the Bester server */ + server = new BesterServer(serverConfiguration); + + /* TODO: Get keys */ + BesterListener[] listeners = getListeners(server, networkBlock); + + for(ulong i = 0; i < listeners.length; i++) + { + /* Add listener */ + server.addListener(listeners[i]); + } + + /* Start running the server (starts the listeners) */ + server.run(); + } + catch(SocketOSException exception) + { + debugPrint("Error binding: " ~ exception.toString()); + } + +} diff --git a/source/bester/server.d b/source/bester/server.d deleted file mode 100644 index 67c4725..0000000 --- a/source/bester/server.d +++ /dev/null @@ -1,104 +0,0 @@ -module server.server; - -import server.types; -import std.conv : to; -import std.socket : SocketOSException, parseAddress, UnixAddress; -import utils.debugging : debugPrint; -import std.stdio : File, writeln; -import std.json : parseJSON, JSONValue; -import server.listeners; - -JSONValue getConfig(string configurationFilePath) -{ - /* TODO: Open the file here */ - File configFile; - configFile.open(configurationFilePath); - - /* The file buffer */ - byte[] fileBuffer; - - /* Allocate the buffer to be the size of the file */ - fileBuffer.length = configFile.size(); - - /* Read the content of the file */ - /* TODO: Error handling ErrnoException */ - fileBuffer = configFile.rawRead(fileBuffer); - configFile.close(); - - JSONValue config; - - /* TODO: JSON error checking */ - config = parseJSON(cast(string)fileBuffer); - - return config; -} - -BesterListener[] getListeners(BesterServer server, JSONValue networkBlock) -{ - BesterListener[] listeners; - - /* TODO: Error handling and get keys and clean up for formality */ - - /* Look for IPv4 TCP block */ - JSONValue inet4TCPBlock = networkBlock["tcp4"]; - debugPrint("<<< IPv4 TCP Block >>>\n" ~ inet4TCPBlock.toPrettyString()); - string inet4Address = inet4TCPBlock["address"].str(); - ushort inet4Port = to!(ushort)(inet4TCPBlock["port"].str()); - TCP4Listener tcp4Listener = new TCP4Listener(server, parseAddress(inet4Address, inet4Port)); - listeners ~= tcp4Listener; - - /* Look for IPv6 TCP block */ - JSONValue inet6TCPBlock = networkBlock["tcp6"]; - debugPrint("<<< IPv6 TCP Block >>>\n" ~ inet6TCPBlock.toPrettyString()); - string inet6Address = inet6TCPBlock["address"].str(); - ushort inet6Port = to!(ushort)(inet6TCPBlock["port"].str()); - TCP6Listener tcp6Listener = new TCP6Listener(server, parseAddress(inet6Address, inet6Port)); - listeners ~= tcp6Listener; - - /* Look for UNIX Domain block */ - JSONValue unixDomainBlock = networkBlock["unix"]; - debugPrint("<<< UNIX Domain Block >>>\n" ~ unixDomainBlock.toPrettyString()); - string unixAddress = unixDomainBlock["address"].str(); -// UNIXListener unixListener = new UNIXListener(server, new UnixAddress(unixAddress)); -// listeners ~= unixListener; - - return listeners; -} - -void startServer(string configurationFilePath) -{ - /* The server configuration */ - JSONValue serverConfiguration = getConfig(configurationFilePath); - debugPrint("<<< Bester.d configuration >>>\n" ~ serverConfiguration.toPrettyString()); - - try - { - /* The server */ - BesterServer server = null; - - /* TODO: Bounds anc type checking */ - - /* Get the network block */ - JSONValue networkBlock = serverConfiguration["network"]; - - /* Create the Bester server */ - server = new BesterServer(serverConfiguration); - - /* TODO: Get keys */ - BesterListener[] listeners = getListeners(server, networkBlock); - - for(ulong i = 0; i < listeners.length; i++) - { - /* Add listener */ - server.addListener(listeners[i]); - } - - /* Start running the server (starts the listeners) */ - server.run(); - } - catch(SocketOSException exception) - { - debugPrint("Error binding: " ~ exception.toString()); - } - -} \ No newline at end of file diff --git a/source/bester/types/connection.d b/source/connection/connection.d similarity index 99% rename from source/bester/types/connection.d rename to source/connection/connection.d index 112b787..9a08d7f 100644 --- a/source/bester/types/connection.d +++ b/source/connection/connection.d @@ -1,4 +1,4 @@ -module server.types.connection; +module connection.connection; import utils.debugging : debugPrint; import std.conv : to; @@ -8,10 +8,11 @@ import std.stdio : writeln, File; import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON; import std.string : cmp; import server.handler; -import server.types.listeners; +import listeners.listener; +import server.server; -private class BesterConnection : Thread +public class BesterConnection : Thread { /* The socket to the client */ diff --git a/source/bester/types/listener.d b/source/listeners/listener.d similarity index 95% rename from source/bester/types/listener.d rename to source/listeners/listener.d index 809e5b2..521c2b1 100644 --- a/source/bester/types/listener.d +++ b/source/listeners/listener.d @@ -1,4 +1,4 @@ -module server.types.listener; +module listeners.listener; import utils.debugging : debugPrint; import std.conv : to; @@ -8,6 +8,8 @@ import std.stdio : writeln, File; import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON; import std.string : cmp; import server.handler; +import server.server; +import connection.connection; /* TODO: Implement me */ diff --git a/source/bester/types/listeners/listeners.d b/source/listeners/types.d similarity index 93% rename from source/bester/types/listeners/listeners.d rename to source/listeners/types.d index 7bfbe8b..4ac7235 100644 --- a/source/bester/types/listeners/listeners.d +++ b/source/listeners/types.d @@ -1,6 +1,8 @@ -module server.types.listeners.listeners; +module listeners.types; + +import listeners.listener; +import server.server; -import server.types; import std.socket : Socket, Address, AddressFamily, SocketType; public class UNIXListener : BesterListener diff --git a/source/bester/handler.d b/source/server/handler.d similarity index 100% rename from source/bester/handler.d rename to source/server/handler.d diff --git a/source/bester/types/server.d b/source/server/server.d similarity index 96% rename from source/bester/types/server.d rename to source/server/server.d index b3b99f2..ca0e2d5 100644 --- a/source/bester/types/server.d +++ b/source/server/server.d @@ -1,4 +1,4 @@ -module server.types.server; +module server.server; import utils.debugging : debugPrint; import std.conv : to; @@ -8,7 +8,8 @@ import std.stdio : writeln, File; import std.json : JSONValue, parseJSON, JSONException, JSONType, toJSON; import std.string : cmp; import server.handler; -import server.types.listeners; +import listeners.listener; +import connection.connection; public class BesterServer { @@ -18,7 +19,7 @@ public class BesterServer * Associative array of `payloadType (string)`:`MessageHandler` * TODO: Implement this */ - private MessageHandler[] handlers; + public MessageHandler[] handlers; /* The server's socket */ private Socket serverSocket;