/// <summary>
        /// Führt eine Anfrage aus.
        /// </summary>
        /// <typeparam name="TResult">Die Art des erwarteten Ergebnisses.</typeparam>
        /// <param name="uri">Die aufzurufende Adresse.</param>
        /// <param name="method">Die Methode zum Aufruf.</param>
        /// <returns>Das Ergebnis des Aufrufs.</returns>
        private static TResult CallServer <TResult>(string uri, string method = "GET")
        {
            // Be safe
            try
            {
                // Create request
                var request = WebRequest.Create(uri);

                // Configure
                request.UseDefaultCredentials = true;
                request.ContentLength         = 0;
                request.Timeout = 10000;
                request.Method  = method;

                // Load the response
                var response = request.GetResponse();
                try
                {
                    // Process
                    using (var status = response.GetResponseStream())
                        using (var reader = new StreamReader(status))
                            return((TResult)s_Deserializer.Deserialize(reader, typeof(TResult)));
                }
                finally
                {
                    // Done
                    response.Close();
                }
            }
            catch (Exception e)
            {
                // See if this is a timeout
                var webException = e as WebException;
                if (webException != null)
                {
                    if (webException.Status == WebExceptionStatus.Timeout)
                    {
                        VCRNETControl.Log("Timeout calling {1} ({0})", method, uri);
                    }
                }

                // Forward
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Wird periodisch aufgerufen und frage den aktuellen Zustand des Dienstes auf einem Rechner ab.
        /// </summary>
        /// <param name="state">Wird ignoriert.</param>
        public void CheckServer(object state)
        {
            // Create a processing clone
            var settings   = new VCRNETRestProxy.ServiceInformation();
            var serverInfo = View.ServerInfo;

            // Safe process
            try
            {
                // Report
                VCRNETControl.Log("Checking {0}:{1}", ServerName, ServerPort);

                // Try to resolve MAC
                View.Settings.DetectMAC();

                // Read the server settings
                settings = VCRNETRestProxy.GetInformation(EndPoint);

                // Load version once
                if (string.IsNullOrEmpty(serverInfo.Version))
                {
                    serverInfo.Version = settings.version;
                }

                // Check mode
                bool hasNext = false, near = false;

                // Find all activities
                var activities = VCRNETRestProxy.GetActivities(EndPoint);

                // All profiles
                foreach (var profile in settings.profileNames)
                {
                    // Report
                    VCRNETControl.Log("Processing Profile {2} for {0}:{1}", ServerName, ServerPort, profile);

                    // Attach to the profile
                    var info = serverInfo[profile];
                    var profileActivities = activities.Where(activity => StringComparer.InvariantCultureIgnoreCase.Equals(activity.device, profile)).ToArray();
                    var profileCurrent    = profileActivities.Where(activity => activity.IsActive).ToArray();

                    // Check for a current recording
                    if (profileCurrent.Length > 0)
                    {
                        // Remember
                        info.CurrentRecordings = profileCurrent;

                        // Done
                        continue;
                    }

                    // Find the next recording
                    var next = profileActivities.FirstOrDefault();
                    if (next == null)
                    {
                        continue;
                    }
                    if (!next.start.HasValue)
                    {
                        continue;
                    }
                    if (next.start.Value == DateTime.MinValue)
                    {
                        continue;
                    }

                    // At least there are recordings
                    hasNext = true;

                    // Check delta
                    var delta = next.start.Value - DateTime.UtcNow;
                    if (delta.TotalMinutes < Math.Max(5, settings.sleepMinimum))
                    {
                        near = true;
                    }
                }

                // Check mode
                if (serverInfo.State == TrayColors.Red)
                {
                    if (near)
                    {
                        serverInfo.State = TrayColors.Yellow;
                    }
                    else if (hasNext)
                    {
                        serverInfo.State = TrayColors.Green;
                    }
                    else
                    {
                        serverInfo.State = TrayColors.Standard;
                    }
                }
            }
            catch
            {
            }

            // Update
            View.ServerInfo = serverInfo;

            // Report
            if (!m_Context.ProcessStateAndCheckHibernation(this, serverInfo.State, settings.hibernationPending, View.Settings.IsLocal && settings.extensionsRunning))
            {
                return;
            }

            // Safe tell the server that we take over hibernation control.
            try
            {
                // Process
                VCRNETRestProxy.ResetPendingHibernation(EndPoint);
            }
            catch
            {
            }
        }