mirror of
https://github.com/deavmi/nostril
synced 2024-09-20 05:43:29 +02:00
App
- Removed server code Server - Placed server code within - Fixed module name Connection - Turned into a Fiber - Fixed module name
This commit is contained in:
parent
76fbb04d5a
commit
798d342984
58
source/app.d
58
source/app.d
@ -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();
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
26
source/nostril/app.d
Normal file
26
source/nostril/app.d
Normal file
@ -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();
|
||||||
|
}
|
104
source/nostril/connection.d
Normal file
104
source/nostril/connection.d
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
source/nostril/package.d
Normal file
1
source/nostril/package.d
Normal file
@ -0,0 +1 @@
|
|||||||
|
module nostril;
|
98
source/nostril/server.d
Normal file
98
source/nostril/server.d
Normal file
@ -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
|
||||||
|
}
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user