2017-01-09 20:52:56 +00:00
using System ;
2017-01-21 18:13:42 +00:00
using System.Threading.Tasks ;
2017-01-19 13:13:35 +00:00
2017-01-09 20:52:56 +00:00
using IotWeb.Server ;
using IotWeb.Common.Http ;
2017-01-19 13:13:35 +00:00
using Nibriboard.RippleSpace ;
2017-01-21 18:13:42 +00:00
using Nibriboard.Client ;
2017-01-19 13:13:35 +00:00
2017-01-09 20:52:56 +00:00
namespace Nibriboard
{
2017-01-28 18:49:26 +00:00
/// <summary>
/// The main Nibriboard server.
/// This class manages not only the connected clients, but also holds the master reference to the <see cref="Nibriboard.RippleSpace.RippleSpaceManager"/>.
/// </summary>
2017-01-10 19:52:27 +00:00
public class NibriboardServer
2017-01-09 20:52:56 +00:00
{
private HttpServer httpServer ;
2017-01-21 18:13:42 +00:00
private ClientSettings clientSettings ;
2017-01-19 13:13:35 +00:00
private RippleSpaceManager planeManager = new RippleSpaceManager ( ) ;
2017-01-09 20:52:56 +00:00
public readonly int Port = 31586 ;
2017-01-19 13:13:35 +00:00
public NibriboardServer ( int inPort = 31586 )
2017-01-09 20:52:56 +00:00
{
2017-01-19 13:13:35 +00:00
Port = inPort ;
2017-01-09 20:52:56 +00:00
2017-01-21 18:13:42 +00:00
clientSettings = new ClientSettings ( ) {
WebsocketHost = "localhost" ,
WebsocketPort = Port ,
WebsocketPath = "/RipplespaceLink"
} ;
// HTTP Server setup
2017-01-09 20:52:56 +00:00
httpServer = new HttpServer ( Port ) ;
httpServer . AddHttpRequestHandler (
2017-01-10 19:51:49 +00:00
"/" ,
2017-01-21 17:06:19 +00:00
new HttpEmbeddedFileHandler ( "Nibriboard.ClientFiles" )
/ * new HttpResourceHandler (
2017-01-10 19:51:49 +00:00
Assembly . GetExecutingAssembly ( ) ,
2017-01-21 17:06:19 +00:00
"ClientFiles" ,
2017-01-10 19:52:27 +00:00
"index.html"
2017-01-21 17:06:19 +00:00
) * /
2017-01-09 20:52:56 +00:00
) ;
2017-01-21 18:13:42 +00:00
httpServer . AddHttpRequestHandler (
"/Settings.json" ,
new HttpClientSettingsHandler ( clientSettings )
) ;
// Websocket setup
2017-01-10 19:52:27 +00:00
httpServer . AddWebSocketRequestHandler (
2017-01-21 18:13:42 +00:00
clientSettings . WebsocketPath ,
2017-01-21 18:38:52 +00:00
new NibriClientManager ( clientSettings )
2017-01-10 19:52:27 +00:00
) ;
2017-01-19 13:13:35 +00:00
}
public async Task Start ( )
{
2017-01-10 19:51:49 +00:00
httpServer . Start ( ) ;
2017-01-10 19:52:27 +00:00
Log . WriteLine ( "[NibriboardServer] Started on port {0}" , Port ) ;
2017-01-19 13:13:35 +00:00
await planeManager . StartMaintenanceMonkey ( ) ;
2017-01-10 19:52:27 +00:00
}
2017-01-09 20:52:56 +00:00
}
}