2017-01-09 20:52:56 +00:00
using System ;
2017-01-21 18:13:42 +00:00
using System.Threading.Tasks ;
2017-09-10 20:34:01 +00:00
using System.Net ;
using System.IO ;
2017-01-19 13:13:35 +00:00
using Nibriboard.RippleSpace ;
2017-01-21 18:13:42 +00:00
using Nibriboard.Client ;
2017-12-23 13:33:00 +00:00
using System.Reflection ;
using SBRL.Utilities ;
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
{
2017-12-23 13:33:00 +00:00
/// <summary>
/// Gets the version of this Nibriboard Server instance.
/// </summary>
/// <value>The version.</value>
public static string Version {
get {
Version asmVersion = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version ;
string commit = EmbeddedFiles . ReadAllText ( "Nibriboard.commit-hash.txt" ) ;
2017-12-23 17:24:20 +00:00
return $"v{asmVersion.Major}.{asmVersion.Minor}.{asmVersion.Build}-{commit.Substring(0, 7)}" ;
2017-12-23 13:33:00 +00:00
}
}
/// <summary>
/// Gets the date on which this Nibriboard server instance was built.
/// </summary>
/// <value>The build date.</value>
public static DateTime BuildDate {
get {
DateTime result = DateTime . MinValue ;
DateTime . TryParse ( EmbeddedFiles . ReadAllText ( "Nibriboard.build-date.txt" ) , out result ) ;
return result ;
}
}
2017-01-21 18:13:42 +00:00
private ClientSettings clientSettings ;
2017-12-16 12:16:43 +00:00
private CommandConsole commandServer ;
2017-01-19 13:13:35 +00:00
2017-07-20 12:15:11 +00:00
public readonly int CommandPort = 31587 ;
2017-01-09 20:52:56 +00:00
public readonly int Port = 31586 ;
2017-12-16 12:16:43 +00:00
public RippleSpaceManager PlaneManager ;
public NibriboardApp AppServer ;
2017-07-23 21:02:24 +00:00
public NibriboardServer ( string pathToRippleSpace , 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-07-23 21:02:24 +00:00
// Load the specified packed ripple space file if it exists - otherwise save it to disk
2017-12-15 23:36:25 +00:00
if ( File . Exists ( pathToRippleSpace ) )
{
PlaneManager = RippleSpaceManager . FromFile ( pathToRippleSpace ) . Result ;
}
else
{
2017-07-23 21:02:24 +00:00
Log . WriteLine ( "[NibriboardServer] Couldn't find packed ripple space at {0} - creating new ripple space instead." , pathToRippleSpace ) ;
2017-12-15 23:36:25 +00:00
PlaneManager = new RippleSpaceManager ( ) { SourceFilename = pathToRippleSpace } ;
2017-07-23 21:02:24 +00:00
}
2017-01-21 18:13:42 +00:00
clientSettings = new ClientSettings ( ) {
2017-05-04 18:53:38 +00:00
SecureWebSocket = false ,
WebSocketHost = "192.168.0.56" ,
WebSocketPort = Port ,
WebSocketPath = "/RipplespaceLink"
2017-01-21 18:13:42 +00:00
} ;
2017-12-22 14:46:32 +00:00
// HTTP / Websockets Server setup
SBRL . GlidingSquirrel . Log . LoggingLevel = SBRL . GlidingSquirrel . LogLevel . Debug ;
2017-12-16 12:16:43 +00:00
AppServer = new NibriboardApp ( new NibriboardAppStartInfo ( ) {
2017-11-09 21:30:03 +00:00
FilePrefix = "Nibriboard.obj.client_dist" ,
2017-09-10 20:34:01 +00:00
ClientSettings = clientSettings ,
2017-12-15 23:36:25 +00:00
SpaceManager = PlaneManager
2017-10-23 18:51:16 +00:00
} , IPAddress . Any , Port ) ;
2017-12-15 23:36:25 +00:00
// Command Console Server setup
commandServer = new CommandConsole ( this , CommandPort ) ;
2017-01-19 13:13:35 +00:00
}
public async Task Start ( )
{
2017-12-16 12:16:43 +00:00
await AppServer . 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
2017-12-15 23:36:25 +00:00
await PlaneManager . StartMaintenanceMonkey ( ) ;
2017-01-10 19:52:27 +00:00
}
2017-07-20 12:15:11 +00:00
/// <summary>
/// Starts the command listener.
/// The command listener is a light tcp-based command console that allows control
/// of the Nibriboard server, since C# doesn't currently have support for signal handling.
/// It listeners on [::1] _only_, to avoid security issues.
/// In the future, a simple secret might be required to use it to aid security further.
/// </summary>
public async Task StartCommandListener ( )
{
2017-12-15 23:36:25 +00:00
await commandServer . Start ( ) ;
2017-07-20 12:15:11 +00:00
}
2017-01-09 20:52:56 +00:00
}
}