diff --git a/Pepperdash Core/Pepperdash Core/CrestronWebServer/CwsBaseHandler.cs b/Pepperdash Core/Pepperdash Core/CrestronWebServer/CwsBaseHandler.cs
new file mode 100644
index 0000000..f9e25bf
--- /dev/null
+++ b/Pepperdash Core/Pepperdash Core/CrestronWebServer/CwsBaseHandler.cs
@@ -0,0 +1,149 @@
+using System;
+using System.Collections.Generic;
+using Crestron.SimplSharp.WebScripting;
+using Newtonsoft.Json;
+
+namespace PepperDash.Core
+{
+ ///
+ /// CWS Base Handler, implements IHttpCwsHandler
+ ///
+ public abstract class CwsBaseHandler : IHttpCwsHandler
+ {
+ private readonly Dictionary> _handlers;
+
+ ///
+ /// Constructor
+ ///
+ protected CwsBaseHandler()
+ {
+ _handlers = new Dictionary>
+ {
+ {"CONNECT", HandleConnect},
+ {"DELETE", HandleDelete},
+ {"GET", HandleGet},
+ {"HEAD", HandleHead},
+ {"OPTIONS", HandleOptions},
+ {"PATCH", HandlePatch},
+ {"POST", HandlePost},
+ {"PUT", HandlePut},
+ {"TRACE", HandleTrace}
+ };
+ }
+
+ ///
+ /// Handles CONNECT method requests
+ ///
+ ///
+ protected virtual void HandleConnect(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles DELETE method requests
+ ///
+ ///
+ protected virtual void HandleDelete(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles GET method requests
+ ///
+ ///
+ protected virtual void HandleGet(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles HEAD method requests
+ ///
+ ///
+ protected virtual void HandleHead(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles OPTIONS method requests
+ ///
+ ///
+ protected virtual void HandleOptions(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles PATCH method requests
+ ///
+ ///
+ protected virtual void HandlePatch(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles POST method requests
+ ///
+ ///
+ protected virtual void HandlePost(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles PUT method requests
+ ///
+ ///
+ protected virtual void HandlePut(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles TRACE method requests
+ ///
+ ///
+ protected virtual void HandleTrace(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Process request
+ ///
+ ///
+ public void ProcessRequest(HttpCwsContext context)
+ {
+ Action handler;
+
+ if (!_handlers.TryGetValue(context.Request.HttpMethod, out handler))
+ {
+ return;
+ }
+
+ handler(context);
+ }
+ }
+}
\ No newline at end of file
diff --git a/Pepperdash Core/Pepperdash Core/CrestronWebServer/CwsDefaultRequestHandler.cs b/Pepperdash Core/Pepperdash Core/CrestronWebServer/CwsDefaultRequestHandler.cs
new file mode 100644
index 0000000..3a91114
--- /dev/null
+++ b/Pepperdash Core/Pepperdash Core/CrestronWebServer/CwsDefaultRequestHandler.cs
@@ -0,0 +1,106 @@
+using Crestron.SimplSharp.WebScripting;
+
+namespace PepperDash.Core
+{
+ public class CwsDefaultRequestHandler : CwsBaseHandler
+ {
+ ///
+ /// Handles CONNECT method requests
+ ///
+ ///
+ protected override void HandleConnect(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles DELETE method requests
+ ///
+ ///
+ protected override void HandleDelete(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles GET method requests
+ ///
+ ///
+ protected override void HandleGet(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles HEAD method requests
+ ///
+ ///
+ protected override void HandleHead(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles OPTIONS method requests
+ ///
+ ///
+ protected override void HandleOptions(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles PATCH method requests
+ ///
+ ///
+ protected override void HandlePatch(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles POST method requests
+ ///
+ ///
+ protected override void HandlePost(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles PUT method requests
+ ///
+ ///
+ protected override void HandlePut(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+
+ ///
+ /// Handles TRACE method requests
+ ///
+ ///
+ protected override void HandleTrace(HttpCwsContext context)
+ {
+ context.Response.StatusCode = 501;
+ context.Response.StatusDescription = "Not Implemented";
+ context.Response.End();
+ }
+ }
+}
\ No newline at end of file
diff --git a/Pepperdash Core/Pepperdash Core/CrestronWebServer/GenericCwsBase.cs b/Pepperdash Core/Pepperdash Core/CrestronWebServer/GenericCwsBase.cs
index 9446e6a..0bfbb63 100644
--- a/Pepperdash Core/Pepperdash Core/CrestronWebServer/GenericCwsBase.cs
+++ b/Pepperdash Core/Pepperdash Core/CrestronWebServer/GenericCwsBase.cs
@@ -44,7 +44,6 @@ namespace PepperDash.Core
public GenericCwsBase(string key, string basePath)
: base(key)
{
-
BasePath = string.IsNullOrEmpty(basePath) ? DefaultBasePath : basePath;
}
@@ -57,7 +56,6 @@ namespace PepperDash.Core
public GenericCwsBase(string key, string name, string basePath)
: base(key, name)
{
-
BasePath = string.IsNullOrEmpty(basePath) ? DefaultBasePath : basePath;
}
@@ -67,8 +65,7 @@ namespace PepperDash.Core
///
void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType)
{
- if (programEventType != eProgramStatusEventType.Stopping)
- return;
+ if (programEventType != eProgramStatusEventType.Stopping) return;
Debug.Console(DebugInfo, this, "Program stopping. Disabling Server");
@@ -82,13 +79,15 @@ namespace PepperDash.Core
void CrestronEnvironment_EthernetEventHandler(EthernetEventArgs ethernetEventArgs)
{
// Re-enable the server if the link comes back up and the status should be connected
- if (ethernetEventArgs.EthernetEventType == eEthernetEventType.LinkUp
- && IsRegistered)
+ if (ethernetEventArgs.EthernetEventType == eEthernetEventType.LinkUp && IsRegistered)
{
- Debug.Console(DebugInfo, this, "Ethernet link up. Starting server");
-
- Start();
+ Debug.Console(DebugInfo, this, "Ethernet link up. Server is alreedy registered.");
+ return;
}
+
+ Debug.Console(DebugInfo, this, "Ethernet link up. Starting server");
+
+ Start();
}
///
@@ -97,7 +96,37 @@ namespace PepperDash.Core
public void Initialize(string key, string basePath)
{
Key = key;
- BasePath = string.IsNullOrEmpty(basePath) ? DefaultBasePath : basePath;
+ BasePath = string.IsNullOrEmpty(basePath) ? DefaultBasePath : basePath;
+ }
+
+ ///
+ /// Adds a route to CWS
+ ///
+ public void AddRoute(HttpCwsRoute route)
+ {
+ if (route == null)
+ {
+ Debug.Console(DebugInfo, this, "Failed to add route, route parameter is null");
+ return;
+ }
+
+ _server.Routes.Add(route);
+
+ }
+
+ ///
+ /// Removes a route from CWS
+ ///
+ ///
+ public void RemoveRoute(HttpCwsRoute route)
+ {
+ if (route == null)
+ {
+ Debug.Console(DebugInfo, this, "Failed to remote route, orute parameter is null");
+ return;
+ }
+
+ _server.Routes.Remove(route);
}
///
@@ -119,7 +148,7 @@ namespace PepperDash.Core
_server = new HttpCwsServer(BasePath)
{
- HttpRequestHandler = new RequestHandlerUnknown()
+ HttpRequestHandler = new CwsDefaultRequestHandler()
};
IsRegistered = _server.Register();
@@ -148,29 +177,26 @@ namespace PepperDash.Core
if (_server == null)
{
- Debug.Console(DebugInfo, this, "Servier has already been stopped");
+ Debug.Console(DebugInfo, this, "Server has already been stopped");
return;
}
- if (_server.Unregister())
- {
- IsRegistered = false;
- }
-
- Dispose(true);
+ IsRegistered = _server.Unregister() == false;
+ _server.Dispose();
+ _server = null;
}
catch (Exception ex)
{
- Debug.Console(DebugInfo, this, "ServerStop Exception Message: {0}", ex.Message);
- Debug.Console(DebugVerbose, this, "ServerStop Exception StackTrace: {0}", ex.StackTrace);
+ Debug.Console(DebugInfo, this, "Server Stop Exception Message: {0}", ex.Message);
+ Debug.Console(DebugVerbose, this, "Server Stop Exception StackTrace: {0}", ex.StackTrace);
if (ex.InnerException != null)
- Debug.Console(DebugVerbose, this, "ServerStop Exception InnerException: {0}", ex.InnerException);
+ Debug.Console(DebugVerbose, this, "Server Stop Exception InnerException: {0}", ex.InnerException);
}
finally
{
_serverLock.Leave();
}
- }
+ }
///
/// Received request handler
@@ -184,7 +210,6 @@ namespace PepperDash.Core
{
try
{
- // TODO [ ] Add logic for received requests
Debug.Console(DebugInfo, this, @"RecieveRequestEventHandler
Method: {0}
Path: {1}
@@ -206,7 +231,6 @@ UserHostName: {9}",
args.Context.Request.UserAgent,
args.Context.Request.UserHostAddress,
args.Context.Request.UserHostName);
-
}
catch (Exception ex)
{
@@ -215,52 +239,6 @@ UserHostName: {9}",
if (ex.InnerException != null)
Debug.Console(DebugVerbose, this, "ReceivedRequestEventHandler Exception InnerException: {0}", ex.InnerException);
}
- }
-
- ///
- /// Tracks if CWS is disposed
- ///
- public bool Disposed
- {
- get
- {
- return (_server == null);
- }
- }
-
- ///
- /// Disposes CWS instance
- ///
- public void Dispose()
- {
- Dispose(true);
- CrestronEnvironment.GC.SuppressFinalize(this);
- }
-
- ///
- /// Disposes CWS instance
- ///
- ///
- protected void Dispose(bool disposing)
- {
- if (Disposed)
- {
- Debug.Console(DebugInfo, this, "Server has already been disposed");
- return;
- }
-
- if (!disposing) return;
-
- if (_server != null)
- {
- _server.Dispose();
- _server = null;
- }
- }
-
- ~GenericCwsBase()
- {
- Dispose(true);
- }
+ }
}
}
\ No newline at end of file
diff --git a/Pepperdash Core/Pepperdash Core/CrestronWebServer/RequestHandlerUnknown.cs b/Pepperdash Core/Pepperdash Core/CrestronWebServer/RequestHandlerUnknown.cs
deleted file mode 100644
index 5a2adac..0000000
--- a/Pepperdash Core/Pepperdash Core/CrestronWebServer/RequestHandlerUnknown.cs
+++ /dev/null
@@ -1,16 +0,0 @@
-using Crestron.SimplSharp.WebScripting;
-
-namespace PepperDash.Core
-{
- public class RequestHandlerUnknown : IHttpCwsHandler
- {
- public void ProcessRequest(HttpCwsContext context)
- {
- // TODO [ ] Modify unknown request handler
- context.Response.StatusCode = 418;
- context.Response.StatusDescription = "I'm a teapot";
- context.Response.ContentType = "application/json";
- context.Response.Write(string.Format("{0} {1}", context.Request.HttpMethod, context.Request.RawUrl), true);
- }
- }
-}
\ No newline at end of file
diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj
index 8e49d82..55c4a20 100644
--- a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj
+++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj
@@ -7,7 +7,7 @@
{87E29B4C-569B-4368-A4ED-984AC1440C96}
Library
Properties
- PepperDash_Core
+ PepperDash.Core
PepperDash_Core
{0B4745B0-194B-4BB6-8E21-E9057CA92500};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
WindowsCE
@@ -93,8 +93,9 @@
+
-
+
diff --git a/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj.DotSettings b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj.DotSettings
new file mode 100644
index 0000000..8107a4f
--- /dev/null
+++ b/Pepperdash Core/Pepperdash Core/PepperDash_Core.csproj.DotSettings
@@ -0,0 +1,2 @@
+
+ True
\ No newline at end of file