Exemplo n.º 1
0
        private async Task <IWatcherCheckResult> EnsureAsync(ConnectionInfo connectionInfo)
        {
            var isValid       = true;
            var portSpecified = _configuration.Port > 0;

            if (_configuration.EnsureThatAsync != null)
            {
                isValid = await _configuration.EnsureThatAsync?.Invoke(connectionInfo);
            }

            isValid = isValid && (_configuration.EnsureThat?.Invoke(connectionInfo) ?? true);

            return(ServerWatcherCheckResult.Create(this, isValid, connectionInfo,
                                                   $"Successfully connected to the hostname '{_configuration.Hostname}' " +
                                                   $"{(portSpecified ? $"using port: {_configuration.Port}" : string.Empty)}."));
        }
Exemplo n.º 2
0
        private async Task <IWatcherCheckResult> ExecuteCheckAsync()
        {
            using (var servicesProvider = GetServicesProviders())
            {
                var ipStatusAndAddressAndOpenedPort = await TryConnectAsync(servicesProvider);

                var ipStatus        = ipStatusAndAddressAndOpenedPort.Item1;
                var ipAddress       = ipStatusAndAddressAndOpenedPort.Item2;
                var portOpened      = ipStatusAndAddressAndOpenedPort.Item3;
                var ipStatusMessage = _pingStatusMessages[ipStatus];
                var portSpecified   = _configuration.Port > 0;
                var connectionInfo  = ConnectionInfo.Create(_configuration.Hostname,
                                                            ipAddress, _configuration.Port, portOpened, ipStatus, ipStatusMessage);

                if (ipStatus == IPStatus.Unknown)
                {
                    return(ServerWatcherCheckResult.Create(this, false, connectionInfo,
                                                           $"Could not resolve the hostname '{_configuration.Hostname}' " +
                                                           $"{(portSpecified ? $"using port: {_configuration.Port}" : string.Empty)}. " +
                                                           $"Ping status: '{ipStatusMessage}'"));
                }
                if (ipStatus != IPStatus.Success)
                {
                    return(ServerWatcherCheckResult.Create(this, false, connectionInfo,
                                                           $"Unable to connect to the hostname '{_configuration.Hostname}' " +
                                                           $"{(portSpecified ? $"using port: {_configuration.Port}" : string.Empty)}. " +
                                                           $"Ping status: '{ipStatusMessage}'"));
                }
                if (portSpecified && !portOpened)
                {
                    return(ServerWatcherCheckResult.Create(this, false, connectionInfo,
                                                           $"Unable to connect to the hostname '{_configuration.Hostname}' " +
                                                           $"using port: {_configuration.Port}. " +
                                                           $"Ping status: '{ipStatusMessage}'"));
                }

                return(await EnsureAsync(connectionInfo));
            }
        }
Exemplo n.º 3
0
        public async Task <IWatcherCheckResult> ExecuteAsync()
        {
            try
            {
                return(await ExecuteCheckAsync());
            }
            catch (TaskCanceledException exception)
            {
                var connectionInfo = ConnectionInfo.Create(_configuration.Hostname,
                                                           IPAddress.None, _configuration.Port, false, IPStatus.Unknown, string.Empty);

                return(ServerWatcherCheckResult.Create(this, false, connectionInfo,
                                                       "A connection timeout has occurred while trying to access the hostname: " +
                                                       $"'{_configuration.Hostname}' using port: {_configuration.Port}."));
            }
            catch (Exception exception)
            {
                throw new WatcherException("There was an error while trying to access the hostname: " +
                                           $"'{_configuration.Hostname}' using port: {_configuration.Port}.",
                                           exception);
            }
        }