From 33f3c44d6ec0d4375e5fa75453cd50da7eb2229a Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 19 Jan 2017 13:13:35 +0000
Subject: [PATCH] Set up core basic framework that calls the main code

---
 Nibriboard/Client/index.js                   |   2 +
 Nibriboard/Env.cs                            |  15 ++
 Nibriboard/Log.cs                            |   3 +-
 Nibriboard/NibriClient.cs                    |   2 +
 Nibriboard/Nibriboard.csproj                 | 160 ++++++++++---------
 Nibriboard/NibriboardServer.cs               |  20 ++-
 Nibriboard/Program.cs                        |  10 +-
 Nibriboard/RippleSpace/Plane.cs              |   5 +
 Nibriboard/RippleSpace/RippleSpaceManager.cs |  59 +++++++
 README.md                                    |   2 +-
 10 files changed, 191 insertions(+), 87 deletions(-)
 create mode 100644 Nibriboard/Env.cs
 create mode 100644 Nibriboard/RippleSpace/RippleSpaceManager.cs

diff --git a/Nibriboard/Client/index.js b/Nibriboard/Client/index.js
index e69de29..23e72fd 100644
--- a/Nibriboard/Client/index.js
+++ b/Nibriboard/Client/index.js
@@ -0,0 +1,2 @@
+"use strict";
+
diff --git a/Nibriboard/Env.cs b/Nibriboard/Env.cs
new file mode 100644
index 0000000..a1c2b08
--- /dev/null
+++ b/Nibriboard/Env.cs
@@ -0,0 +1,15 @@
+﻿using System;
+namespace Nibriboard
+{
+	public static class Env
+	{
+		public static readonly DateTime ServerStartTime = DateTime.Now;
+
+		public static double SecondsSinceStart {
+			get {
+				TimeSpan timeSinceStart = DateTime.Now - ServerStartTime;
+				return timeSinceStart.TotalSeconds;
+			}
+		}
+	}
+}
diff --git a/Nibriboard/Log.cs b/Nibriboard/Log.cs
index b3f7d69..0d4c010 100644
--- a/Nibriboard/Log.cs
+++ b/Nibriboard/Log.cs
@@ -3,9 +3,10 @@ namespace Nibriboard
 {
 	public static class Log
 	{
+
 		public static int WriteLine(string text, params object[] args)
 		{
-			string outputText = $"[{DateTime.Now}] " + string.Format(text, args);
+			string outputText = $"[{Env.SecondsSinceStart}] " + string.Format(text, args);
 			Console.WriteLine(outputText);
 			return outputText.Length;
 		}
diff --git a/Nibriboard/NibriClient.cs b/Nibriboard/NibriClient.cs
index b5753ab..c852530 100644
--- a/Nibriboard/NibriClient.cs
+++ b/Nibriboard/NibriClient.cs
@@ -26,6 +26,8 @@ namespace Nibriboard
 
 				//Task.Run(async () => await onMessage(frame)).Wait();
 			};
+
+			
 		}
 
 		private async Task onMessage(string frame)
diff --git a/Nibriboard/Nibriboard.csproj b/Nibriboard/Nibriboard.csproj
index f58bbaa..fa7650a 100644
--- a/Nibriboard/Nibriboard.csproj
+++ b/Nibriboard/Nibriboard.csproj
@@ -1,79 +1,81 @@
-<?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
-  <PropertyGroup>
-    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
-    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
-    <ProjectGuid>{B7F806D9-9C50-4BA8-A803-0FC2EC5F5932}</ProjectGuid>
-    <OutputType>Exe</OutputType>
-    <RootNamespace>Nibriboard</RootNamespace>
-    <AssemblyName>Nibriboard</AssemblyName>
-    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
-    <DebugSymbols>true</DebugSymbols>
-    <DebugType>full</DebugType>
-    <Optimize>false</Optimize>
-    <OutputPath>bin\Debug</OutputPath>
-    <DefineConstants>DEBUG;</DefineConstants>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-    <ExternalConsole>true</ExternalConsole>
-    <PlatformTarget>x86</PlatformTarget>
-    <CustomCommands>
-      <CustomCommands>
-        <Command>
-          <type>BeforeBuild</type>
-          <command>npm run build</command>
-          <workingdir>${ProjectDir}/Client</workingdir>
-        </Command>
-      </CustomCommands>
-    </CustomCommands>
-  </PropertyGroup>
-  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
-    <Optimize>true</Optimize>
-    <OutputPath>bin\Release</OutputPath>
-    <ErrorReport>prompt</ErrorReport>
-    <WarningLevel>4</WarningLevel>
-    <ExternalConsole>true</ExternalConsole>
-    <PlatformTarget>x86</PlatformTarget>
-  </PropertyGroup>
-  <ItemGroup>
-    <Reference Include="System" />
-    <Reference Include="IotWeb.Common">
-      <HintPath>..\packages\IotWeb.0.8.6\lib\net45\IotWeb.Common.dll</HintPath>
-    </Reference>
-    <Reference Include="IotWeb.Server">
-      <HintPath>..\packages\IotWeb.0.8.6\lib\net45\IotWeb.Server.dll</HintPath>
-    </Reference>
-  </ItemGroup>
-  <ItemGroup>
-    <Compile Include="Program.cs" />
-    <Compile Include="Properties\AssemblyInfo.cs" />
-    <Compile Include="RippleSpace\Plane.cs" />
-    <Compile Include="RippleSpace\Chunk.cs" />
-    <Compile Include="RippleSpace\LocationReference.cs" />
-    <Compile Include="RippleSpace\ChunkReference.cs" />
-    <Compile Include="RippleSpace\DrawnLine.cs" />
-    <Compile Include="RippleSpace\Reference.cs" />
-    <Compile Include="Utilities.cs" />
-    <Compile Include="NibriboardServer.cs" />
-    <Compile Include="NibriClientManager.cs" />
-    <Compile Include="Log.cs" />
-    <Compile Include="Utilities\EmbeddedFiles.cs" />
-    <Compile Include="NibriClient.cs" />
-  </ItemGroup>
-  <ItemGroup>
-  	<EmbeddedResource Include="Client\index.html" />
-  	<EmbeddedResource Include="Client\NibriClient.js" />
-  	<EmbeddedResource Include="Client\Nibri.css" />
-  </ItemGroup>
-  <ItemGroup>
-    <Folder Include="RippleSpace\" />
-    <Folder Include="Client\" />
-    <Folder Include="Utilities\" />
-  </ItemGroup>
-  <ItemGroup>
-    <None Include="packages.config" />
-  </ItemGroup>
-  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
-</Project>
+﻿<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+    <ProjectGuid>{B7F806D9-9C50-4BA8-A803-0FC2EC5F5932}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>Nibriboard</RootNamespace>
+    <AssemblyName>Nibriboard</AssemblyName>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG;</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <ExternalConsole>true</ExternalConsole>
+    <PlatformTarget>x86</PlatformTarget>
+    <CustomCommands>
+      <CustomCommands>
+        <Command>
+          <type>BeforeBuild</type>
+          <command>npm run build</command>
+          <workingdir>${ProjectDir}/Client</workingdir>
+        </Command>
+      </CustomCommands>
+    </CustomCommands>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <ExternalConsole>true</ExternalConsole>
+    <PlatformTarget>x86</PlatformTarget>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="IotWeb.Common">
+      <HintPath>..\packages\IotWeb.0.8.6\lib\net45\IotWeb.Common.dll</HintPath>
+    </Reference>
+    <Reference Include="IotWeb.Server">
+      <HintPath>..\packages\IotWeb.0.8.6\lib\net45\IotWeb.Server.dll</HintPath>
+    </Reference>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RippleSpace\Plane.cs" />
+    <Compile Include="RippleSpace\Chunk.cs" />
+    <Compile Include="RippleSpace\LocationReference.cs" />
+    <Compile Include="RippleSpace\ChunkReference.cs" />
+    <Compile Include="RippleSpace\DrawnLine.cs" />
+    <Compile Include="RippleSpace\Reference.cs" />
+    <Compile Include="Utilities.cs" />
+    <Compile Include="NibriboardServer.cs" />
+    <Compile Include="NibriClient.cs" />
+    <Compile Include="NibriClientManager.cs" />
+    <Compile Include="Log.cs" />
+    <Compile Include="Utilities\EmbeddedFiles.cs" />
+    <Compile Include="Env.cs" />
+    <Compile Include="RippleSpace\RippleSpaceManager.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <EmbeddedResource Include="Client\index.html" />
+    <EmbeddedResource Include="Client\NibriClient.js" />
+    <EmbeddedResource Include="Client\Nibri.css" />
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="RippleSpace\" />
+    <Folder Include="Client\" />
+    <Folder Include="Utilities\" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+    <None Include="Client\index.js" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file
diff --git a/Nibriboard/NibriboardServer.cs b/Nibriboard/NibriboardServer.cs
index 4f5ef49..e5bab44 100644
--- a/Nibriboard/NibriboardServer.cs
+++ b/Nibriboard/NibriboardServer.cs
@@ -1,22 +1,26 @@
 ﻿using System;
 using System.Reflection;
+
 using IotWeb.Server;
 using IotWeb.Common.Http;
-using System.Net;
+
+using Nibriboard.RippleSpace;
+using System.Threading.Tasks;
+
 namespace Nibriboard
 {
 	public class NibriboardServer
 	{
 		private HttpServer httpServer;
 
+		private RippleSpaceManager planeManager = new RippleSpaceManager();
+
 		public readonly int Port = 31586;
 
-		public NibriboardServer()
+		public NibriboardServer(int inPort = 31586)
 		{
-		}
+			Port = inPort;
 
-		public void Setup()
-		{
 			httpServer = new HttpServer(Port);
 			httpServer.AddHttpRequestHandler(
 				"/",
@@ -30,8 +34,14 @@ namespace Nibriboard
 				"/RipplespaceConnection",
 				new NibriClientManager()
 			);
+		}
+
+		public async Task Start()
+		{
 			httpServer.Start();
 			Log.WriteLine("[NibriboardServer] Started on port {0}", Port);
+
+			await planeManager.StartMaintenanceMonkey();
 		}
 	}
 }
diff --git a/Nibriboard/Program.cs b/Nibriboard/Program.cs
index da9a953..6b5306a 100644
--- a/Nibriboard/Program.cs
+++ b/Nibriboard/Program.cs
@@ -1,5 +1,6 @@
 ﻿using System;
 using System.Reflection;
+using System.Threading.Tasks;
 using SBRLUtilities;
 
 namespace Nibriboard
@@ -8,8 +9,15 @@ namespace Nibriboard
 	{
 		public static void Main(string[] args)
 		{
-			Console.WriteLine("Hello World!");
+			Log.WriteLine("[core] Starting Nibriboard.");
+
+			Log.WriteLine("[core] Detected embedded files: ");
 			EmbeddedFiles.WriteResourceList();
+
+			NibriboardServer server = new NibriboardServer();
+			Task.WaitAll(
+				server.Start()
+			);
 		}
 	}
 }
diff --git a/Nibriboard/RippleSpace/Plane.cs b/Nibriboard/RippleSpace/Plane.cs
index 192281e..21172ea 100644
--- a/Nibriboard/RippleSpace/Plane.cs
+++ b/Nibriboard/RippleSpace/Plane.cs
@@ -82,5 +82,10 @@ namespace Nibriboard.RippleSpace
 				containingChunk.Add(newLineSegment);
 			}
 		}
+
+		public async Task PerformMaintenance()
+		{
+			// TODO: Perform maintenance here
+		}
 	}
 }
diff --git a/Nibriboard/RippleSpace/RippleSpaceManager.cs b/Nibriboard/RippleSpace/RippleSpaceManager.cs
new file mode 100644
index 0000000..4cbdaad
--- /dev/null
+++ b/Nibriboard/RippleSpace/RippleSpaceManager.cs
@@ -0,0 +1,59 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using System.Diagnostics;
+
+namespace Nibriboard.RippleSpace
+{
+	public class RippleSpaceManager
+	{
+		/// <summary>
+		/// The master list of planes that this PlaneManager is in charge of. 
+		/// </summary>
+		public List<Plane> Planes = new List<Plane>();
+
+		/// <summary>
+		/// The number of milliseconds between each maintenance run.
+		/// </summary>
+		public readonly int MaintenanceInternal = 5000;
+
+		/// <summary>
+		/// The number of milliseconds the last maintenance run took.
+		/// </summary>
+		public long LastMaintenanceDuration = 0;
+
+		public RippleSpaceManager()
+		{
+			Log.WriteLine("[RippleSpace] New blank ripplespace initialised.");
+		}
+
+		public Plane GetById(string targetName)
+		{
+			foreach (Plane plane in Planes)
+			{
+				if (plane.Name == targetName)
+					return plane;
+			}
+
+			return null;
+		}
+
+		public async Task StartMaintenanceMonkey()
+		{
+			Log.WriteLine("[RippleSpace/Maintenance] Automated maintenance monkey created.");
+
+			while (true)
+			{
+				Stopwatch maintenanceStopwatch = Stopwatch.StartNew();
+
+				foreach (Plane plane in Planes)
+					await plane.PerformMaintenance();
+
+				LastMaintenanceDuration = maintenanceStopwatch.ElapsedMilliseconds;
+
+				await Task.Delay(MaintenanceInternal);
+			}
+
+		}
+	}
+}
diff --git a/README.md b/README.md
index 1d7118a..17da64f 100644
--- a/README.md
+++ b/README.md
@@ -5,4 +5,4 @@ An infinite whiteboard for recording those big ideas.
 ## Credits
  - Main code - [Starbeamrainbowlabs](https://starbeamrainbowlabs.com/)
  - [Paper.js](http://paperjs.org/) - Client-side rendering
- - [IotWeb](http://sensaura.org/pages/tools/iotweb/) - Underlying HTTP / WebSocket server
\ No newline at end of file
+ - [IotWeb](http://sensaura.org/pages/tools/iotweb/) - Underlying HTTP / WebSocket server
