diff --git a/src/Pepperdash Core/Comm/GenericSshClient.cs b/src/Pepperdash Core/Comm/GenericSshClient.cs index da864e6..880b872 100644 --- a/src/Pepperdash Core/Comm/GenericSshClient.cs +++ b/src/Pepperdash Core/Comm/GenericSshClient.cs @@ -231,7 +231,10 @@ namespace PepperDash.Core this.LogDebug("Attempting connect"); // Cancel reconnect if running. - ReconnectTimer.Stop(); + if (ReconnectTimer != null) + { + ReconnectTimer.Stop(); + } // Cleanup the old client if it already exists if (Client != null) @@ -248,8 +251,6 @@ namespace PepperDash.Core this.LogDebug("Creating new SshClient"); ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth); Client = new SshClient(connectionInfo); - - Client.ErrorOccurred -= Client_ErrorOccurred; Client.ErrorOccurred += Client_ErrorOccurred; //Attempt to connect @@ -258,6 +259,11 @@ namespace PepperDash.Core { Client.Connect(); TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534); + if (TheStream.DataAvailable) + { + // empty the buffer if there is data + string str = TheStream.Read(); + } TheStream.DataReceived += Stream_DataReceived; this.LogInformation("Connected"); ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED; @@ -336,7 +342,7 @@ namespace PepperDash.Core if (ReconnectTimer != null) { ReconnectTimer.Stop(); - ReconnectTimer = null; + // ReconnectTimer = null; } KillClient(SocketStatus.SOCKET_STATUS_BROKEN_LOCALLY); @@ -349,28 +355,72 @@ namespace PepperDash.Core { KillStream(); - if (Client != null) - { - Client.Disconnect(); - Client = null; - ClientStatus = status; - this.LogDebug("Disconnected"); + try + { + if (Client != null) + { + Client.ErrorOccurred -= Client_ErrorOccurred; + Client.Disconnect(); + Client.Dispose(); + Client = null; + ClientStatus = status; + Debug.Console(1, this, "Disconnected"); + } } - } + catch (Exception ex) + { + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception in Kill Client:{0}", ex); + } + } + + /// + /// Anything to do with reestablishing connection on failures + /// + void HandleConnectionFailure() + { + KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + + Debug.Console(1, this, "Client nulled due to connection failure. AutoReconnect: {0}, ConnectEnabled: {1}", AutoReconnect, ConnectEnabled); + if (AutoReconnect && ConnectEnabled) + { + Debug.Console(1, this, "Checking autoreconnect: {0}, {1}ms", AutoReconnect, AutoReconnectIntervalMs); + if (ReconnectTimer == null) + { + ReconnectTimer = new CTimer(o => + { + Connect(); + }, AutoReconnectIntervalMs); + Debug.Console(1, this, "Attempting connection in {0} seconds", + (float) (AutoReconnectIntervalMs/1000)); + } + else + { + Debug.Console(1, this, "{0} second reconnect cycle running", + (float) (AutoReconnectIntervalMs/1000)); + } + } + } /// /// Kills the stream /// void KillStream() { - if (TheStream != null) - { - TheStream.DataReceived -= Stream_DataReceived; - TheStream.Close(); - TheStream.Dispose(); - TheStream = null; - this.LogDebug("Disconnected stream"); - } + try + { + if (TheStream != null) + { + TheStream.DataReceived -= Stream_DataReceived; + TheStream.Close(); + TheStream.Dispose(); + TheStream = null; + this.LogDebug("Disconnected stream"); + } + } + catch (Exception ex) + { + this.LogException(ex, "Exception in Kill Stream:{0}"); + } } /// @@ -388,29 +438,33 @@ namespace PepperDash.Core /// void Stream_DataReceived(object sender, ShellDataEventArgs e) { - var bytes = e.Data; - if (bytes.Length > 0) + if (((ShellStream)sender).Length <= 0L) + { + return; + } + var response = ((ShellStream)sender).Read(); + + var bytesHandler = BytesReceived; + + if (bytesHandler != null) + { + var bytes = Encoding.UTF8.GetBytes(response); + if (StreamDebugging.RxStreamDebuggingIsEnabled) + { + this.LogInformation("Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length); + } + bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); + } + + var textHandler = TextReceived; + if (textHandler != null) { - var bytesHandler = BytesReceived; - if (bytesHandler != null) - { - if (StreamDebugging.RxStreamDebuggingIsEnabled) - { - this.LogInformation("Received {1} bytes: '{0}'", ComTextHelper.GetEscapedText(bytes), bytes.Length); - } - bytesHandler(this, new GenericCommMethodReceiveBytesArgs(bytes)); - } - - var textHandler = TextReceived; - if (textHandler != null) - { - var str = Encoding.GetEncoding(28591).GetString(bytes, 0, bytes.Length); - if (StreamDebugging.RxStreamDebuggingIsEnabled) - this.LogInformation("Received: '{0}'", ComTextHelper.GetDebugText(str)); + if (StreamDebugging.RxStreamDebuggingIsEnabled) + this.LogInformation("Received: '{0}'", ComTextHelper.GetDebugText(response)); - textHandler(this, new GenericCommMethodReceiveTextArgs(str)); - } - } + textHandler(this, new GenericCommMethodReceiveTextArgs(response)); + } + } @@ -460,22 +514,33 @@ namespace PepperDash.Core /// public void SendText(string text) { - try - { - if (Client != null && TheStream != null && IsConnected) - { - if (StreamDebugging.TxStreamDebuggingIsEnabled) - this.LogInformation("Sending {0} characters of text: '{1}'", text.Length, ComTextHelper.GetDebugText(text)); + try + { + if (Client != null && TheStream != null && IsConnected) + { + if (StreamDebugging.TxStreamDebuggingIsEnabled) + Debug.Console(0, + this, + "Sending {0} characters of text: '{1}'", + text.Length, + ComTextHelper.GetDebugText(text)); - TheStream.Write(text); - TheStream.Flush(); + TheStream.Write(text); + TheStream.Flush(); + } + else + { + Debug.Console(1, this, "Client is null or disconnected. Cannot Send Text"); + } + } + catch (ObjectDisposedException ex) + { + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message); + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace); - } - else - { - this.LogDebug("Client is null or disconnected. Cannot Send Text"); - } - } + KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + ReconnectTimer.Reset(); + } catch (Exception ex) { this.LogException(ex, "Exception sending text: {message}", text); @@ -503,10 +568,28 @@ namespace PepperDash.Core this.LogDebug("Client is null or disconnected. Cannot Send Bytes"); } } - catch (Exception ex) - { - this.LogException(ex, "Exception sending bytes: {message}", ComTextHelper.GetEscapedText(bytes)); + catch (ObjectDisposedException ex) + { + this.LogException(ex, "ObjectDisposedException sending {message}", ComTextHelper.GetEscapedText(bytes)); + + KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + ReconnectTimer.Reset(); } + catch (Exception ex) + { + this.LogException(ex, "Exception sending {message}", ComTextHelper.GetEscapedText(bytes)); + } + + KillClient(SocketStatus.SOCKET_STATUS_CONNECT_FAILED); + ReconnectTimer.Reset(); + } + catch (Exception ex) + { + Debug.Console(0, this, Debug.ErrorLogLevel.Notice, "Exception: {0}", ex.Message); + Debug.Console(1, this, Debug.ErrorLogLevel.Notice, "Stack Trace: {0}", ex.StackTrace); + + Debug.Console(1, this, Debug.ErrorLogLevel.Error, "Stream write failed"); + } } #endregion diff --git a/src/Pepperdash Core/Comm/GenericTcpIpClient.cs b/src/Pepperdash Core/Comm/GenericTcpIpClient.cs index e5f53b9..9529aa2 100644 --- a/src/Pepperdash Core/Comm/GenericTcpIpClient.cs +++ b/src/Pepperdash Core/Comm/GenericTcpIpClient.cs @@ -219,7 +219,8 @@ namespace PepperDash.Core /// public GenericTcpIpClient() : base(SplusKey) - { + { + StreamDebugging = new CommunicationStreamDebugging(SplusKey); CrestronEnvironment.ProgramStatusEventHandler += new ProgramStatusEventHandler(CrestronEnvironment_ProgramStatusEventHandler); AutoReconnectIntervalMs = 5000; BufferSize = 2000; diff --git a/src/Pepperdash Core/Web/RequestHandlers/DefaultRequestHandler.cs b/src/Pepperdash Core/Web/RequestHandlers/DefaultRequestHandler.cs index 57fa1de..ca19cf2 100644 --- a/src/Pepperdash Core/Web/RequestHandlers/DefaultRequestHandler.cs +++ b/src/Pepperdash Core/Web/RequestHandlers/DefaultRequestHandler.cs @@ -1,16 +1,17 @@ - +using Crestron.SimplSharp.WebScripting; + namespace PepperDash.Core.Web.RequestHandlers { - /// - /// Web API default request handler - /// - public class DefaultRequestHandler : WebApiBaseRequestHandler - { - /// - /// Constructor - /// - public DefaultRequestHandler() - : base(true) - { } - } -} + /// + /// Web API default request handler + /// + public class DefaultRequestHandler : WebApiBaseRequestHandler + { + /// + /// Constructor + /// + public DefaultRequestHandler() + : base(true) + { } + } +} \ No newline at end of file diff --git a/src/Pepperdash Core/Web/RequestHandlers/WebApiBaseRequestHandler.cs b/src/Pepperdash Core/Web/RequestHandlers/WebApiBaseRequestHandler.cs index caa6a7c..99e4aa9 100644 --- a/src/Pepperdash Core/Web/RequestHandlers/WebApiBaseRequestHandler.cs +++ b/src/Pepperdash Core/Web/RequestHandlers/WebApiBaseRequestHandler.cs @@ -4,162 +4,162 @@ using Crestron.SimplSharp.WebScripting; namespace PepperDash.Core.Web.RequestHandlers { - /// - /// CWS Base Handler, implements IHttpCwsHandler - /// - public abstract class WebApiBaseRequestHandler : IHttpCwsHandler - { - private readonly Dictionary> _handlers; - protected readonly bool EnableCors; + /// + /// CWS Base Handler, implements IHttpCwsHandler + /// + public abstract class WebApiBaseRequestHandler : IHttpCwsHandler + { + private readonly Dictionary> _handlers; + protected readonly bool EnableCors; - /// - /// Constructor - /// - protected WebApiBaseRequestHandler(bool enableCors) - { - EnableCors = enableCors; + /// + /// Constructor + /// + protected WebApiBaseRequestHandler(bool enableCors) + { + EnableCors = enableCors; - _handlers = new Dictionary> - { - {"CONNECT", HandleConnect}, - {"DELETE", HandleDelete}, - {"GET", HandleGet}, - {"HEAD", HandleHead}, - {"OPTIONS", HandleOptions}, - {"PATCH", HandlePatch}, - {"POST", HandlePost}, - {"PUT", HandlePut}, - {"TRACE", HandleTrace} - }; - } + _handlers = new Dictionary> + { + {"CONNECT", HandleConnect}, + {"DELETE", HandleDelete}, + {"GET", HandleGet}, + {"HEAD", HandleHead}, + {"OPTIONS", HandleOptions}, + {"PATCH", HandlePatch}, + {"POST", HandlePost}, + {"PUT", HandlePut}, + {"TRACE", HandleTrace} + }; + } - /// - /// Constructor - /// - protected WebApiBaseRequestHandler() - : this(false) - { - } + /// + /// Constructor + /// + protected WebApiBaseRequestHandler() + : this(false) + { + } - /// - /// Handles CONNECT method requests - /// - /// - protected virtual void HandleConnect(HttpCwsContext context) - { - context.Response.StatusCode = 501; - context.Response.StatusDescription = "Not Implemented"; - context.Response.End(); - } + /// + /// 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 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 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 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 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 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 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 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(); - } + /// + /// 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; + /// + /// Process request + /// + /// + public void ProcessRequest(HttpCwsContext context) + { + Action handler; - if (!_handlers.TryGetValue(context.Request.HttpMethod, out handler)) - { - return; - } + if (!_handlers.TryGetValue(context.Request.HttpMethod, out handler)) + { + return; + } - if (EnableCors) - { - context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); - context.Response.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); - } + if (EnableCors) + { + context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); + context.Response.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); + } - handler(context); - } - } + handler(context); + } + } } \ No newline at end of file diff --git a/src/Pepperdash Core/Web/WebApiServer.cs b/src/Pepperdash Core/Web/WebApiServer.cs index 17c737a..cf45b36 100644 --- a/src/Pepperdash Core/Web/WebApiServer.cs +++ b/src/Pepperdash Core/Web/WebApiServer.cs @@ -9,276 +9,276 @@ using PepperDash.Core.Web.RequestHandlers; namespace PepperDash.Core.Web { - /// - /// Web API server - /// - public class WebApiServer : IKeyName - { - private const string SplusKey = "Uninitialized Web API Server"; - private const string DefaultName = "Web API Server"; - private const string DefaultBasePath = "/api"; + /// + /// Web API server + /// + public class WebApiServer : IKeyName + { + private const string SplusKey = "Uninitialized Web API Server"; + private const string DefaultName = "Web API Server"; + private const string DefaultBasePath = "/api"; - private const uint DebugTrace = 0; - private const uint DebugInfo = 1; - private const uint DebugVerbose = 2; + private const uint DebugTrace = 0; + private const uint DebugInfo = 1; + private const uint DebugVerbose = 2; - private readonly CCriticalSection _serverLock = new CCriticalSection(); - private HttpCwsServer _server; + private readonly CCriticalSection _serverLock = new CCriticalSection(); + private HttpCwsServer _server; - /// - /// Web API server key - /// - public string Key { get; private set; } + /// + /// Web API server key + /// + public string Key { get; private set; } - /// - /// Web API server name - /// - public string Name { get; private set; } + /// + /// Web API server name + /// + public string Name { get; private set; } - /// - /// CWS base path, will default to "/api" if not set via initialize method - /// - public string BasePath { get; private set; } + /// + /// CWS base path, will default to "/api" if not set via initialize method + /// + public string BasePath { get; private set; } - /// - /// Indicates CWS is registered with base path - /// - public bool IsRegistered { get; private set; } + /// + /// Indicates CWS is registered with base path + /// + public bool IsRegistered { get; private set; } - /// - /// Http request handler - /// - //public IHttpCwsHandler HttpRequestHandler - //{ - // get { return _server.HttpRequestHandler; } - // set - // { - // if (_server == null) return; - // _server.HttpRequestHandler = value; - // } - //} + /// + /// Http request handler + /// + //public IHttpCwsHandler HttpRequestHandler + //{ + // get { return _server.HttpRequestHandler; } + // set + // { + // if (_server == null) return; + // _server.HttpRequestHandler = value; + // } + //} - /// - /// Received request event handler - /// - //public event EventHandler ReceivedRequestEvent - //{ - // add { _server.ReceivedRequestEvent += new HttpCwsRequestEventHandler(value); } - // remove { _server.ReceivedRequestEvent -= new HttpCwsRequestEventHandler(value); } - //} + /// + /// Received request event handler + /// + //public event EventHandler ReceivedRequestEvent + //{ + // add { _server.ReceivedRequestEvent += new HttpCwsRequestEventHandler(value); } + // remove { _server.ReceivedRequestEvent -= new HttpCwsRequestEventHandler(value); } + //} - /// - /// Constructor for S+. Make sure to set necessary properties using init method - /// - public WebApiServer() - : this(SplusKey, DefaultName, null) - { - } + /// + /// Constructor for S+. Make sure to set necessary properties using init method + /// + public WebApiServer() + : this(SplusKey, DefaultName, null) + { + } - /// - /// Constructor - /// - /// - /// - public WebApiServer(string key, string basePath) - : this(key, DefaultName, basePath) - { - } + /// + /// Constructor + /// + /// + /// + public WebApiServer(string key, string basePath) + : this(key, DefaultName, basePath) + { + } - /// - /// Constructor - /// - /// - /// - /// - public WebApiServer(string key, string name, string basePath) - { - Key = key; - Name = string.IsNullOrEmpty(name) ? DefaultName : name; - BasePath = string.IsNullOrEmpty(basePath) ? DefaultBasePath : basePath; + /// + /// Constructor + /// + /// + /// + /// + public WebApiServer(string key, string name, string basePath) + { + Key = key; + Name = string.IsNullOrEmpty(name) ? DefaultName : name; + BasePath = string.IsNullOrEmpty(basePath) ? DefaultBasePath : basePath; - if (_server == null) _server = new HttpCwsServer(BasePath); + if (_server == null) _server = new HttpCwsServer(BasePath); - _server.setProcessName(Key); - _server.HttpRequestHandler = new DefaultRequestHandler(); + _server.setProcessName(Key); + _server.HttpRequestHandler = new DefaultRequestHandler(); - CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler; - CrestronEnvironment.EthernetEventHandler += CrestronEnvironment_EthernetEventHandler; - } + CrestronEnvironment.ProgramStatusEventHandler += CrestronEnvironment_ProgramStatusEventHandler; + CrestronEnvironment.EthernetEventHandler += CrestronEnvironment_EthernetEventHandler; + } - /// - /// Program status event handler - /// - /// - void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType) - { - if (programEventType != eProgramStatusEventType.Stopping) return; + /// + /// Program status event handler + /// + /// + void CrestronEnvironment_ProgramStatusEventHandler(eProgramStatusEventType programEventType) + { + if (programEventType != eProgramStatusEventType.Stopping) return; - Debug.Console(DebugInfo, this, "Program stopping. stopping server"); + Debug.Console(DebugInfo, this, "Program stopping. stopping server"); - Stop(); - } + Stop(); + } - /// - /// Ethernet event handler - /// - /// - 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) - { - Debug.Console(DebugInfo, this, "Ethernet link up. Server is alreedy registered."); - return; - } + /// + /// Ethernet event handler + /// + /// + 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) + { + Debug.Console(DebugInfo, this, "Ethernet link up. Server is alreedy registered."); + return; + } - Debug.Console(DebugInfo, this, "Ethernet link up. Starting server"); + Debug.Console(DebugInfo, this, "Ethernet link up. Starting server"); - Start(); - } + Start(); + } - /// - /// Initializes CWS class - /// - public void Initialize(string key, string basePath) - { - Key = key; - BasePath = string.IsNullOrEmpty(basePath) ? DefaultBasePath : basePath; - } + /// + /// Initializes CWS class + /// + public void Initialize(string key, string basePath) + { + Key = key; + 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; - } + /// + /// 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); + _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; - } + /// + /// 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); - } + _server.Routes.Remove(route); + } - /// - /// Returns a list of the current routes - /// - public HttpCwsRouteCollection GetRouteCollection() - { - return _server.Routes; - } + /// + /// Returns a list of the current routes + /// + public HttpCwsRouteCollection GetRouteCollection() + { + return _server.Routes; + } - /// - /// Starts CWS instance - /// - public void Start() - { - try - { - _serverLock.Enter(); + /// + /// Starts CWS instance + /// + public void Start() + { + try + { + _serverLock.Enter(); - if (_server == null) - { - Debug.Console(DebugInfo, this, "Server is null, unable to start"); - return; - } + if (_server == null) + { + Debug.Console(DebugInfo, this, "Server is null, unable to start"); + return; + } - if (IsRegistered) - { - Debug.Console(DebugInfo, this, "Server has already been started"); - return; - } + if (IsRegistered) + { + Debug.Console(DebugInfo, this, "Server has already been started"); + return; + } - IsRegistered = _server.Register(); + IsRegistered = _server.Register(); - Debug.Console(DebugInfo, this, "Starting server, registration {0}", IsRegistered ? "was successful" : "failed"); - } - catch (Exception ex) - { - Debug.Console(DebugInfo, this, "Start Exception Message: {0}", ex.Message); - Debug.Console(DebugVerbose, this, "Start Exception StackTrace: {0}", ex.StackTrace); - if (ex.InnerException != null) - Debug.Console(DebugVerbose, this, "Start Exception InnerException: {0}", ex.InnerException); - } - finally - { - _serverLock.Leave(); - } - } + Debug.Console(DebugInfo, this, "Starting server, registration {0}", IsRegistered ? "was successful" : "failed"); + } + catch (Exception ex) + { + Debug.Console(DebugInfo, this, "Start Exception Message: {0}", ex.Message); + Debug.Console(DebugVerbose, this, "Start Exception StackTrace: {0}", ex.StackTrace); + if (ex.InnerException != null) + Debug.Console(DebugVerbose, this, "Start Exception InnerException: {0}", ex.InnerException); + } + finally + { + _serverLock.Leave(); + } + } - /// - /// Stop CWS instance - /// - public void Stop() - { - try - { - _serverLock.Enter(); + /// + /// Stop CWS instance + /// + public void Stop() + { + try + { + _serverLock.Enter(); - if (_server == null) - { - Debug.Console(DebugInfo, this, "Server is null or has already been stopped"); - return; - } + if (_server == null) + { + Debug.Console(DebugInfo, this, "Server is null or has already been stopped"); + return; + } - IsRegistered = _server.Unregister() == false; + IsRegistered = _server.Unregister() == false; - Debug.Console(DebugInfo, this, "Stopping server, unregistration {0}", IsRegistered ? "failed" : "was successful"); + Debug.Console(DebugInfo, this, "Stopping server, unregistration {0}", IsRegistered ? "failed" : "was successful"); - _server.Dispose(); - _server = null; - } - catch (Exception ex) - { - 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, "Server Stop Exception InnerException: {0}", ex.InnerException); - } - finally - { - _serverLock.Leave(); - } - } + _server.Dispose(); + _server = null; + } + catch (Exception ex) + { + 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, "Server Stop Exception InnerException: {0}", ex.InnerException); + } + finally + { + _serverLock.Leave(); + } + } - /// - /// Received request handler - /// - /// - /// This is here for development and testing - /// - /// - /// - public void ReceivedRequestEventHandler(object sender, HttpCwsRequestEventArgs args) - { - try - { - var j = JsonConvert.SerializeObject(args.Context, Formatting.Indented); - Debug.Console(DebugVerbose, this, "RecieveRequestEventHandler Context:\x0d\x0a{0}", j); - } - catch (Exception ex) - { - Debug.Console(DebugInfo, this, "ReceivedRequestEventHandler Exception Message: {0}", ex.Message); - Debug.Console(DebugVerbose, this, "ReceivedRequestEventHandler Exception StackTrace: {0}", ex.StackTrace); - if (ex.InnerException != null) - Debug.Console(DebugVerbose, this, "ReceivedRequestEventHandler Exception InnerException: {0}", ex.InnerException); - } - } - } + /// + /// Received request handler + /// + /// + /// This is here for development and testing + /// + /// + /// + public void ReceivedRequestEventHandler(object sender, HttpCwsRequestEventArgs args) + { + try + { + var j = JsonConvert.SerializeObject(args.Context, Formatting.Indented); + Debug.Console(DebugVerbose, this, "RecieveRequestEventHandler Context:\x0d\x0a{0}", j); + } + catch (Exception ex) + { + Debug.Console(DebugInfo, this, "ReceivedRequestEventHandler Exception Message: {0}", ex.Message); + Debug.Console(DebugVerbose, this, "ReceivedRequestEventHandler Exception StackTrace: {0}", ex.StackTrace); + if (ex.InnerException != null) + Debug.Console(DebugVerbose, this, "ReceivedRequestEventHandler Exception InnerException: {0}", ex.InnerException); + } + } + } } \ No newline at end of file