Esempio n. 1
0
        private async Task PingAndCheckServer(ServerSettings serverSettings)
        {
            var proxy = new CoreServiceProxy();

            int apiComparison;

            try
            {
                apiComparison = await proxy.Ping(Constants.RestApiVersion).ConfigureAwait(false);
            }
            catch (ArgusTVException ex)
            {
                throw new ArgusTVNotFoundException(ex, "'{0}:{1}' not found, is the service running?", serverSettings.ServerName, serverSettings.Port);
            }
            if (apiComparison < 0)
            {
                throw new ArgusTVException("ARGUS TV Recorder on server is more recent, upgrade client");
            }
            else if (apiComparison > 0)
            {
                throw new ArgusTVException("ARGUS TV Recorder on server is too old, upgrade server");
            }
            serverSettings.WakeOnLan.MacAddresses = String.Join(";", await proxy.GetMacAddresses().ConfigureAwait(false));
            serverSettings.WakeOnLan.IPAddress    = WakeOnLan.GetIPAddress(serverSettings.ServerName);
        }
Esempio n. 2
0
        /// <exclude/>
        protected async Task <HttpResponseMessage> ExecuteRequestAsync(HttpRequestMessage request, bool logError = true)
        {
            try
            {
                if (request.Method != HttpMethod.Get && request.Content == null)
                {
                    // Work around current Mono bug.
                    request.Content = new ByteArrayContent(new byte[0]);
                }
                var response = await _client.SendAsync(request).ConfigureAwait(false);

                if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
                {
                    var error = SimpleJson.DeserializeObject <RestError>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
                    throw new ArgusTVException(error.detail);
                }
                if (response.StatusCode >= HttpStatusCode.BadRequest)
                {
                    throw new ArgusTVException(response.ReasonPhrase);
                }
                return(response);
            }
            catch (AggregateException ex)
            {
                if (IsConnectionError(ex.InnerException))
                {
                    WakeOnLan.EnsureServerAwake(Proxies.ServerSettings);

                    throw new ArgusTVNotFoundException(ex.InnerException.InnerException.Message);
                }
                throw;
            }
            catch (ArgusTVException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (logError)
                {
                    Proxies.Logger.Error(ex.ToString());
                }
                throw new ArgusTVUnexpectedErrorException("An unexpected error occured.");
            }
        }
Esempio n. 3
0
        private async Task <bool> InternalInitialize(ServerSettings serverSettings, bool throwError, IServiceProxyLogger logger, SourceLevels logLevel = SourceLevels.Error)
#endif
        {
#if DOTNET4
            lock (Instance._syncLock)
#else
            using (await _asyncLock.LockAsync().ConfigureAwait(false))
#endif
            {
                ServerSettings      previousServerSettings = _serverSettings;
                bool                previousIsInitialized  = _isInitialized;
                IServiceProxyLogger previousLogger         = _logger;

                try
                {
                    _isInitialized  = false;
                    _serverSettings = serverSettings;
                    _logger         = logger ?? new DefaultLogger();

                    WakeOnLan.EnsureServerAwake(serverSettings);

                    DateTime firstTryTime = DateTime.Now;
                    for (; ;)
                    {
                        try
                        {
#if DOTNET4
                            PingAndCheckServer(serverSettings).Wait();
#else
                            await PingAndCheckServer(serverSettings).ConfigureAwait(false);
#endif
                            break;
                        }
                        catch
                        {
                            TimeSpan span = DateTime.Now - firstTryTime;
                            if (!serverSettings.WakeOnLan.Enabled ||
                                span.TotalSeconds >= serverSettings.WakeOnLan.TimeoutSeconds)
                            {
                                throw;
                            }
                            System.Threading.Thread.Sleep(1000);
                        }
                    }
                }
                catch
                {
                    _serverSettings = previousServerSettings;
                    _logger         = previousLogger;
                    _isInitialized  = previousIsInitialized;
                    if (!throwError)
                    {
                        return(false);
                    }
                    throw;
                }

                ResetProxies();
                _isInitialized = true;
                return(true);
            }
        }