diff --git a/Step.CmsConnect/Builders/GatewayNewtorkConfigurationBuilder.cs b/Step.CmsConnectGateway/Builders/GatewayNewtorkConfigurationBuilder.cs similarity index 100% rename from Step.CmsConnect/Builders/GatewayNewtorkConfigurationBuilder.cs rename to Step.CmsConnectGateway/Builders/GatewayNewtorkConfigurationBuilder.cs diff --git a/Step.CmsConnect/Builders/GatewayProxyConfigurationBuilder.cs b/Step.CmsConnectGateway/Builders/GatewayProxyConfigurationBuilder.cs similarity index 100% rename from Step.CmsConnect/Builders/GatewayProxyConfigurationBuilder.cs rename to Step.CmsConnectGateway/Builders/GatewayProxyConfigurationBuilder.cs diff --git a/Step.CmsConnect/Builders/iBuilder.cs b/Step.CmsConnectGateway/Builders/iBuilder.cs similarity index 100% rename from Step.CmsConnect/Builders/iBuilder.cs rename to Step.CmsConnectGateway/Builders/iBuilder.cs diff --git a/Step.CmsConnect/Events/RebootEventHandlerArgs.cs b/Step.CmsConnectGateway/Events/GatewayRebootEventHandlerArgs.cs similarity index 74% rename from Step.CmsConnect/Events/RebootEventHandlerArgs.cs rename to Step.CmsConnectGateway/Events/GatewayRebootEventHandlerArgs.cs index 79e5d321..50c76768 100644 --- a/Step.CmsConnect/Events/RebootEventHandlerArgs.cs +++ b/Step.CmsConnectGateway/Events/GatewayRebootEventHandlerArgs.cs @@ -6,12 +6,12 @@ using System.Threading.Tasks; namespace Step.CmsConnectGateway.Events { - public class RebootEventHandlerArgs : EventArgs + public class GatewayRebootEventHandlerArgs : EventArgs { public Boolean errorStatus { get; internal set; } public String errorMessage { get; internal set; } - public RebootEventHandlerArgs() + public GatewayRebootEventHandlerArgs() { errorStatus = false; errorMessage = ""; diff --git a/Step.CmsConnect/Exceptions/GatewayException.cs b/Step.CmsConnectGateway/Exceptions/GatewayException.cs similarity index 100% rename from Step.CmsConnect/Exceptions/GatewayException.cs rename to Step.CmsConnectGateway/Exceptions/GatewayException.cs diff --git a/Step.CmsConnect/CmsConnectAdapter.cs b/Step.CmsConnectGateway/GatewayAdapter.cs similarity index 82% rename from Step.CmsConnect/CmsConnectAdapter.cs rename to Step.CmsConnectGateway/GatewayAdapter.cs index e4434bc1..406bed8d 100644 --- a/Step.CmsConnect/CmsConnectAdapter.cs +++ b/Step.CmsConnectGateway/GatewayAdapter.cs @@ -14,7 +14,7 @@ using System.Threading.Tasks; namespace Step.CmsConnectGateway { - public class CmsConnectAdapter : IDisposable + public class GatewayAdapter : IDisposable { //Internal Constant private const string SSH_SET_PROXY_COMMAND = "sudo ./setProxy.sh "; @@ -23,6 +23,7 @@ namespace Step.CmsConnectGateway private const string SSH_SET_NETWORK_COMMAND = "sudo ./setNetwork.sh "; private const string SSH_GET_NETWORK_COMMAND = "sudo ./getNetworkConfiguration.sh "; private const string SSH_GET_PROXY_COMMAND = "sudo ./getProxyConfiguration.sh "; + private const string SSH_TEST_CONNECTION_COMMAND = "sudo ./testConnection.sh "; private const string SSH_GW_REBOOT_COMMAND = "sudo ./gatewayReboot.sh "; const string IP_ADDR_LABEL = "IP_ADDRESS="; @@ -32,8 +33,12 @@ namespace Step.CmsConnectGateway const string PROXY_ADDR_LABEL = "PROXY="; const string NO_PROXY_LABEL = "NO_PROXY="; const string UNDEF_VALUE = "none"; + const string CONNECTION_OK_VALUE = "OK"; + const string CONNECTION_NOWEB_VALUE = "NO_WEB"; + const string CONNECTION_NOPORT_VALUE = "NO_PORTS"; const int REBOOT_MINUTES_MAX = 2; + const int REBOOT_MSWAIT_BETWEEN_OP = 500; //Internal Private Variable @@ -48,7 +53,7 @@ namespace Step.CmsConnectGateway /// Simple constructor. /// Create an instance with this configuration: hostname: localhost, username: root, password: root. /// - public CmsConnectAdapter() + public GatewayAdapter() { this.host = "localhost"; this.username = "root"; @@ -64,7 +69,7 @@ namespace Step.CmsConnectGateway /// Username for Gateway Login /// Password for Gateway Login /// Thrown when one parameter is null - public CmsConnectAdapter(string hostname, string username, string password) + public GatewayAdapter(string hostname, string username, string password) { setup(hostname, username, password); } @@ -138,19 +143,36 @@ namespace Step.CmsConnectGateway } + /// + /// Test connection to SCM/CMS Server, on Gateway. + /// + /// Seconds timeout for the request + /// + public GatewayConnectionStatus TestGatewayConnection(int timeout) + { + if (timeout < 0) + throw new ArgumentOutOfRangeException("Timeout must be > 0"); + + return ElaborateTestConnectionCommand(SendSSHCommand(SSH_TEST_CONNECTION_COMMAND + timeout)); + } + + /// /// Reboot asynchronously the Gateway. /// /// Serconds to delay before reboot /// Handler Callback when the operation is finished or an error occours. - /// See + /// See /// - public void RebootGatewayAsync(int delay, EventHandler handler) + public void RebootGatewayAsync(int delay, EventHandler handler) { + if (delay < 0) + throw new ArgumentOutOfRangeException("Delay must be > 0"); + //Create the Action - Action> action = (int del, EventHandler hand) => + Action> action = (int del, EventHandler hand) => { - RebootEventHandlerArgs ev = new RebootEventHandlerArgs(); + GatewayRebootEventHandlerArgs ev = new GatewayRebootEventHandlerArgs(); try { this.SendSSHReboot(del); @@ -176,6 +198,9 @@ namespace Step.CmsConnectGateway /// public void RebootGateway(int delay) { + if (delay < 0) + throw new ArgumentOutOfRangeException("Delay must be > 0"); + //Send Command this.SendSSHReboot(delay); } @@ -314,6 +339,17 @@ namespace Step.CmsConnectGateway return configuration; } + private GatewayConnectionStatus ElaborateTestConnectionCommand(List lines) + { + GatewayConnectionStatus status = new GatewayConnectionStatus(); + foreach (string line in lines) + { + if (!String.IsNullOrWhiteSpace(line)) + DecodeConnectionStatus(line.Trim(), ref status); + } + return status; + } + #endregion //--------------------------------------------------------------------------------------------------- @@ -512,6 +548,20 @@ namespace Step.CmsConnectGateway } } + + private void DecodeConnectionStatus(string stringValue, ref GatewayConnectionStatus status) + { + if (stringValue == CONNECTION_OK_VALUE) + status = GatewayConnectionStatus.OK; + else if (stringValue == CONNECTION_NOWEB_VALUE) + status = GatewayConnectionStatus.WEB_ERROR; + else if (stringValue == CONNECTION_NOPORT_VALUE) + status = GatewayConnectionStatus.PORT_ERROR; + else + throw new GatewayException("Internal Gateway Error during Connection-Test (bad format): " + stringValue); + } + + private bool EvaluateIPV4(string stringValue,out IPAddress address) { Regex rgx = new Regex(@"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); @@ -525,8 +575,6 @@ namespace Step.CmsConnectGateway address = new IPAddress(0); return false; } - - #endregion //--------------------------------------------------------------------------------------------------- @@ -617,16 +665,59 @@ namespace Step.CmsConnectGateway //Create the Instance SshClient _sshGateway = new SshClient(this.host, this.username, this.password); - bool connected = false; - //Try Connection Cycle + //Phase 1 Wait Disconnection Cycle + bool disconnected = false; + do + { + try + { + //If TimeSpan > TOT MIN -> Exception + if ((DateTime.Now - nowAfterReboot) > TimeSpan.FromMinutes(REBOOT_MINUTES_MAX)) + throw new GatewayException("Timeout Error during reboot - Gateway has never been rebooted"); + + //Try Connection + _sshGateway.Connect(); + Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP); + _sshGateway.Disconnect(); + } + catch (SocketException e) + { + //Not Connected + disconnected = true; + } + catch (SshConnectionException e) + { + //Not Connected + disconnected = true; + } + catch (SshException e) + { + //Not Connected + disconnected = true; + } + catch (GatewayException e) + { + //Error + throw new GatewayException(e.Message); + } + catch (Exception e) + { + //Error + throw new GatewayException("Unknown Error during reboot: " + e); + } + } while (!disconnected); + + + //Phase 2 Wait Re-connection Cycle + bool connected = false; do { try { //If TimeSpan > TOT MIN -> Exception if((DateTime.Now - nowAfterReboot) > TimeSpan.FromMinutes(REBOOT_MINUTES_MAX)) - throw new GatewayException("Timeout Error during reboot"); + throw new GatewayException("Timeout Error during reboot - Gateway not found during reboot"); //Try Connection _sshGateway.Connect(); @@ -635,17 +726,17 @@ namespace Step.CmsConnectGateway catch (SocketException e) { //Not Connected - Thread.Sleep(500); + Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP); } catch (SshConnectionException e) { //Not Connected - Thread.Sleep(500); + Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP); } catch (SshException e) { //Not Connected - Thread.Sleep(500); + Thread.Sleep(REBOOT_MSWAIT_BETWEEN_OP); } catch (GatewayException e) { diff --git a/Step.CmsConnect/Models/GatewayConfiguration.cs b/Step.CmsConnectGateway/Models/GatewayConfiguration.cs similarity index 90% rename from Step.CmsConnect/Models/GatewayConfiguration.cs rename to Step.CmsConnectGateway/Models/GatewayConfiguration.cs index 56b4bf03..f94a78de 100644 --- a/Step.CmsConnect/Models/GatewayConfiguration.cs +++ b/Step.CmsConnectGateway/Models/GatewayConfiguration.cs @@ -7,7 +7,9 @@ using System.Net; namespace Step.CmsConnectGateway { - + + + public enum GatewayConnectionStatus { OK = 0, WEB_ERROR = 1, PORT_ERROR = 2 }; public class GatewayNetworkConfiguration { diff --git a/Step.CmsConnect/Properties/AssemblyInfo.cs b/Step.CmsConnectGateway/Properties/AssemblyInfo.cs similarity index 100% rename from Step.CmsConnect/Properties/AssemblyInfo.cs rename to Step.CmsConnectGateway/Properties/AssemblyInfo.cs diff --git a/Step.CmsConnect/Step.CmsConnectGateway.csproj b/Step.CmsConnectGateway/Step.CmsConnectGateway.csproj similarity index 90% rename from Step.CmsConnect/Step.CmsConnectGateway.csproj rename to Step.CmsConnectGateway/Step.CmsConnectGateway.csproj index 406ede17..eaab3452 100644 --- a/Step.CmsConnect/Step.CmsConnectGateway.csproj +++ b/Step.CmsConnectGateway/Step.CmsConnectGateway.csproj @@ -7,8 +7,8 @@ {49B04D99-0ECD-4900-86D3-7098D61314D7} Library Properties - Step.CmsConnect - Step.CmsConnect + Step.CmsConnectGateway + Step.CmsConnectGateway v4.6.1 512 @@ -47,8 +47,8 @@ - - + + diff --git a/Step.CmsConnect/packages.config b/Step.CmsConnectGateway/packages.config similarity index 100% rename from Step.CmsConnect/packages.config rename to Step.CmsConnectGateway/packages.config diff --git a/Step.sln b/Step.sln index 6a8ee0a5..f4646718 100644 --- a/Step.sln +++ b/Step.sln @@ -37,7 +37,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.NC", "Step.NC\Step.NC. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Chromium", "Client.Chromium\Client.Chromium.csproj", "{F9F17F23-660E-488C-A7FA-6A5B35D64313}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.CmsConnectGateway", "Step.CmsConnect\Step.CmsConnectGateway.csproj", "{49B04D99-0ECD-4900-86D3-7098D61314D7}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step.CmsConnectGateway", "Step.CmsConnectGateway\Step.CmsConnectGateway.csproj", "{49B04D99-0ECD-4900-86D3-7098D61314D7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/Step/Step.csproj b/Step/Step.csproj index 0896d7e3..a803959c 100644 --- a/Step/Step.csproj +++ b/Step/Step.csproj @@ -16122,7 +16122,7 @@ - + {49b04d99-0ecd-4900-86d3-7098d61314d7} Step.CmsConnectGateway