diff --git a/source/app.d b/source/app.d deleted file mode 100644 index b45b546..0000000 --- a/source/app.d +++ /dev/null @@ -1,58 +0,0 @@ -import std.stdio; - -import vibe.vibe; -import std.json; - -import gogga; - -// TODO: Investigate if we need the belowe (I copied it from Birchwood) -__gshared GoggaLogger logger; -__gshared static this() -{ - logger = new GoggaLogger(); -} - - -/** - * Handles an incoming websocket connection - - * Params: - * socket = the web socket to the client - */ -void websocketHandler(scope WebSocket socket) -{ - logger.print("Handling web socket: "~to!(string)(socket)~"\n",DebugType.INFO); - - HTTPServerRequest httpRequest = cast(HTTPServerRequest)socket.request(); - logger.print("New connection from: "~to!(string)(httpRequest.peer)~"\n",DebugType.INFO); - - string receivedText = socket.receiveText(); - logger.print(receivedText~"\n", DebugType.INFO); - - - JSONValue jsonReceived; - jsonReceived = parseJSON(receivedText); - - logger.print(jsonReceived.toPrettyString()~"\n", DebugType.INFO); -} - -void main() -{ - // Setup where to listen - HTTPServerSettings httpSettings = new HTTPServerSettings(); - - // TODO: Customize these with a config file or environment variables - httpSettings.port = 8082; - - // Setup a websocket negotiater with a handler attached - auto websocketNegotiater = handleWebSockets(&websocketHandler); - - // Handle `/` as the web socket path - URLRouter router = new URLRouter(); - router.get("/", websocketNegotiater); - - // Bind the router to the server - listenHTTP(httpSettings, router); - - runApplication(); -} diff --git a/source/connection.d b/source/connection.d deleted file mode 100644 index 62e07f0..0000000 --- a/source/connection.d +++ /dev/null @@ -1,59 +0,0 @@ -module nostril.connection; - -import gogga; - -// TODO: Investigate if we need the belowe (I copied it from Birchwood) -__gshared GoggaLogger logger; -__gshared static this() -{ - logger = new GoggaLogger(); -} - - -import vibe.vibe : WebSocket; -import core.thread : Thread; -import std.json; - -public class Connection : Thread -{ - /* Client socket */ - private WebSocket ws; - - this(WebSocket ws) - { - super(&worker); - this.ws = ws; - } - - private void worker() - { - // TODO: Add this - } - - /** - * Handles received data - * - * Params: - * text = received data - */ - private void handler(string text) - { - string receivedText = text; - logger.print(receivedText~"\n", DebugType.INFO); - - - JSONValue jsonReceived; - try - { - jsonReceived = parseJSON(receivedText); - logger.print(jsonReceived.toPrettyString()~"\n", DebugType.INFO); - - // TODO: Add handling here - - } - catch(JSONException e) - { - logger.print("There was an error parsing the client's JSON\n", DebugType.ERROR); - } - } -} \ No newline at end of file diff --git a/source/nostril/app.d b/source/nostril/app.d new file mode 100644 index 0000000..84ba44c --- /dev/null +++ b/source/nostril/app.d @@ -0,0 +1,26 @@ +module nostril.app; + +import std.stdio; + +import vibe.vibe; +import vibe.http.dist; +import std.json; + +import gogga; + +// TODO: Investigate if we need the belowe (I copied it from Birchwood) +__gshared GoggaLogger logger; +__gshared static this() +{ + logger = new GoggaLogger(); +} + +import nostril.server; + +void main() +{ + string[] bindAddresseds = ["::"]; + int bindPort = 8082; + Server server = new Server(bindAddresseds, bindPort); + server.startServer(); +} diff --git a/source/nostril/connection.d b/source/nostril/connection.d new file mode 100644 index 0000000..cd914fc --- /dev/null +++ b/source/nostril/connection.d @@ -0,0 +1,104 @@ +module nostril.connection; + + +import std.stdio; + +import vibe.vibe; +import vibe.http.dist; +import std.json; + +import gogga; +import gogga; + +// TODO: Investigate if we need the belowe (I copied it from Birchwood) +__gshared GoggaLogger logger; +__gshared static this() +{ + logger = new GoggaLogger(); +} + + +import vibe.vibe : WebSocket, WebSocketException; +import core.thread.fiber : Fiber; +import std.json; + +// TODO: This won't work with Thread, must be a fiber +// ... because of how vibe.d works +public class Connection : Fiber +{ + /* Client socket */ + private WebSocket socket; + + this(WebSocket ws) + { + super(&worker); + this.socket = ws; + } + + private void worker() + { + logger.print("Handling web socket: "~to!(string)(socket)~"\n",DebugType.INFO); + + HTTPServerRequest httpRequest = cast(HTTPServerRequest)socket.request(); + logger.print("New connection from: "~to!(string)(httpRequest.peer)~"\n",DebugType.INFO); + + + + while(socket.waitForData()) + { + string data; + + try + { + import std.stdio; + writeln("Ha"); + + data = socket.receiveText(); + writeln("Hello receve done"); + } + catch(WebSocketException e) + { + logger.print("Error in receive text\n", DebugType.ERROR); + } + + try + { + handler(data); + } + catch(Exception e) + { + logger.print("Error in handler\n", DebugType.ERROR); + } + + } + + logger.print("Web socket connection closing...\n", DebugType.WARNING); + } + + /** + * Handles received data + * + * Params: + * text = received data + */ + private void handler(string text) + { + string receivedText = text; + logger.print(receivedText~"\n", DebugType.INFO); + + + JSONValue jsonReceived; + try + { + jsonReceived = parseJSON(receivedText); + logger.print(jsonReceived.toPrettyString()~"\n", DebugType.INFO); + + // TODO: Add handling here + + } + catch(JSONException e) + { + logger.print("There was an error parsing the client's JSON\n", DebugType.ERROR); + } + } +} \ No newline at end of file diff --git a/source/nostril/package.d b/source/nostril/package.d new file mode 100644 index 0000000..c82a961 --- /dev/null +++ b/source/nostril/package.d @@ -0,0 +1 @@ +module nostril; \ No newline at end of file diff --git a/source/nostril/server.d b/source/nostril/server.d new file mode 100644 index 0000000..22322a1 --- /dev/null +++ b/source/nostril/server.d @@ -0,0 +1,98 @@ +module nostril.server; + +import gogga; + +// TODO: Investigate if we need the belowe (I copied it from Birchwood) +__gshared GoggaLogger logger; +__gshared static this() +{ + logger = new GoggaLogger(); +} + +import core.thread : Thread; + +import vibe.vibe : URLRouter, HTTPServerSettings; +import vibe.vibe : WebSocket, handleWebSockets; + +import vibe.vibe; + +import nostril.connection; + +/** + * Server + * + * A thread which manages all of the vibe.d fibers + * for running a server which accepts client connections + */ +public class Server +{ + /** + * HTTP server + */ + private HTTPServerSettings httpSettings; + private URLRouter router; + private @safe void delegate(scope HTTPServerRequest, HTTPServerResponse) websocketNegotiater; + + + + this(string[] bindAddresses, int bindPort) + { + // Setup where to listen + this.httpSettings = new HTTPServerSettings(); + httpSettings.port = 8082; + httpSettings.bindAddresses = bindAddresses; + + // Setup a websocket negotiater with a handler attached + this.websocketNegotiater = handleWebSockets(&websocketHandler); + + // Handle `/` as the web socket path + this.router = new URLRouter(); + router.get("/", this.websocketNegotiater); + } + + public void startServer() + { + // Bind the router to the server + // TODO: Investigate multi-threaded listener rather + listenHTTP(httpSettings, router); + // listenHTTPDist(httpSettings, toDelegate(&threadHandler), "[]::]", 8082); + + runApplication(); + } + + + public static Server createServer() + { + Server newServer; + + + + return newServer; + } + + /** + * Handles an incoming websocket connection + * + * Params: + * socket = the web socket to the client + */ + void websocketHandler(scope WebSocket socket) + { + new Connection(socket).call(); + } + + + +} + +/** + * BackingStore + * + * Represents the backing storage where + * events are to be read from and written + * to + */ +public abstract class BackingStore +{ + // TODO: Add a queue here +} \ No newline at end of file diff --git a/source/server.d b/source/server.d deleted file mode 100644 index f6aefe3..0000000 --- a/source/server.d +++ /dev/null @@ -1,51 +0,0 @@ -module nostril.server; - -import gogga; - -// TODO: Investigate if we need the belowe (I copied it from Birchwood) -__gshared GoggaLogger logger; -__gshared static this() -{ - logger = new GoggaLogger(); -} - - - -import vibe.vibe : URLRouter; - -public class Server -{ - private URLRouter router; - - private this() - { - - } - - public void startServer() - { - // TODO: Make call to `runApplication()` - } - - - public static Server createServer() - { - Server newServer; - - - - return newServer; - } -} - -/** - * BackingStore - * - * Represents the backing storage where - * events are to be read from and written - * to - */ -public abstract class BackingStore -{ - // TODO: Add a queue here -} \ No newline at end of file