/// <summary> /// Runs the agent /// </summary> public void Run() { Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Thread Started"), LogType.Info.ToString()); int retryAfterSeconds = 0; while (!_forceStop) { lock (_locker) { try { // If we are restarting, reset _manualReset.Reset(); // Reset backOff retryAfterSeconds = 0; HardwareKey key = new HardwareKey(); Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Thread Woken and Lock Obtained"), LogType.Info.ToString()); using (xmds.xmds xmds = new xmds.xmds()) { xmds.Credentials = null; xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds + "&method=registerDisplay"; xmds.UseDefaultCredentials = false; // Store the XMR address string xmrAddress = ApplicationSettings.Default.XmrNetworkAddress; RegisterAgent.ProcessRegisterXml(callRegister(xmds, key)); // Set the flag to indicate we have a connection to XMDS ApplicationSettings.Default.XmdsLastConnection = DateTime.Now; // Has the XMR address changed? if (xmrAddress != ApplicationSettings.Default.XmrNetworkAddress) { OnXmrReconfigure(); } // Notify Status NotifyStatus(); // Have we been asked to move CMS instance? // CMS MOVE // -------- if (!string.IsNullOrEmpty(ApplicationSettings.Default.NewCmsAddress) && !string.IsNullOrEmpty(ApplicationSettings.Default.NewCmsKey) && ApplicationSettings.Default.NewCmsAddress != ApplicationSettings.Default.ServerUri ) { // Make a call using the new details, and see if it works. string oldUri = ApplicationSettings.Default.ServerUri; string oldKey = ApplicationSettings.Default.ServerKey; ApplicationSettings.Default.ServerUri = ApplicationSettings.Default.NewCmsAddress; ApplicationSettings.Default.ServerKey = ApplicationSettings.Default.NewCmsKey; Trace.WriteLine(new LogMessage("RegisterAgent - Run", "We have been asked to move to a new CMS. " + ApplicationSettings.Default.NewCmsAddress), LogType.Info.ToString()); // Try it and see. try { xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds + "&method=registerDisplay"; string xml = callRegister(xmds, key); // If that worked (no errors), update our settings ApplicationSettings.Default.NewCmsAddress = ""; ApplicationSettings.Default.NewCmsKey = ""; // ServerUri/Key will be updated too. ApplicationSettings.Default.Save(); ProcessRegisterXml(xml); } catch (Exception e) { Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Error swapping to new CMS. E = " + e.Message.ToString()), LogType.Error.ToString()); // Switch back to the old values for subsequent tries ApplicationSettings.Default.ServerUri = oldUri; ApplicationSettings.Default.ServerKey = oldKey; } } // Have we been asked to switch to HTTPS? // HTTPS MOVE // ---------- if (ApplicationSettings.Default.ForceHttps && xmds.Url.ToLowerInvariant().StartsWith("http://")) { Trace.WriteLine(new LogMessage("RegisterAgent - Run", "We have been asked to move to HTTPS from our current HTTP."), LogType.Info.ToString()); // Try it and see. try { string url = ApplicationSettings.Default.XiboClient_xmds_xmds + "&method=registerDisplay"; xmds.Url = url.Replace("http://", "https://"); callRegister(xmds, key); // If that worked (no errors), update our setting ApplicationSettings.Default.ServerUri = ApplicationSettings.Default.ServerUri.Replace("http://", "https://"); ApplicationSettings.Default.Save(); } catch (Exception e) { Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Error swapping to HTTPS. E = " + e.Message.ToString()), LogType.Error.ToString()); } } } } catch (WebException webEx) when(webEx.Response is HttpWebResponse httpWebResponse && (int)httpWebResponse.StatusCode == 429) { // Get the header for how long we ought to wait retryAfterSeconds = webEx.Response.Headers["Retry-After"] != null?int.Parse(webEx.Response.Headers["Retry-After"]) : 120; // Log it. Trace.WriteLine(new LogMessage("LogAgent", "Run: 429 received, waiting for " + retryAfterSeconds + " seconds."), LogType.Info.ToString()); } catch (WebException webEx) { // Increment the quantity of XMDS failures and bail out ApplicationSettings.Default.IncrementXmdsErrorCount(); // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("RegisterAgent - Run", "WebException in Run: " + webEx.Message), LogType.Info.ToString()); } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Exception in Run: " + ex.Message), LogType.Info.ToString()); } } if (retryAfterSeconds > 0) { // Sleep this thread until we've fulfilled our try after _manualReset.WaitOne(retryAfterSeconds * 1000); } else { // Sleep this thread until the next collection interval _manualReset.WaitOne((int)(ApplicationSettings.Default.CollectInterval * ApplicationSettings.Default.XmdsCollectionIntervalFactor() * 1000)); } } Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Thread Stopped"), LogType.Info.ToString()); }
/// <summary> /// Runs the agent /// </summary> public void Run() { Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Thread Started"), LogType.Info.ToString()); while (!_forceStop) { lock (_locker) { try { // If we are restarting, reset _manualReset.Reset(); HardwareKey key = new HardwareKey(); Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Thread Woken and Lock Obtained"), LogType.Info.ToString()); using (xmds.xmds xmds = new xmds.xmds()) { xmds.Credentials = null; xmds.Url = ApplicationSettings.Default.XiboClient_xmds_xmds + "&method=registerDisplay"; xmds.UseDefaultCredentials = false; // Store the XMR address string xmrAddress = ApplicationSettings.Default.XmrNetworkAddress; RegisterAgent.ProcessRegisterXml(xmds.RegisterDisplay( ApplicationSettings.Default.ServerKey, key.Key, ApplicationSettings.Default.DisplayName, "windows", ApplicationSettings.Default.ClientVersion, ApplicationSettings.Default.ClientCodeVersion, Environment.OSVersion.ToString(), key.MacAddress, key.Channel, key.getXmrPublicKey())); // Set the flag to indicate we have a connection to XMDS ApplicationSettings.Default.XmdsLastConnection = DateTime.Now; // Has the XMR address changed? if (xmrAddress != ApplicationSettings.Default.XmrNetworkAddress) { OnXmrReconfigure(); } // Is the timezone empty? if (string.IsNullOrEmpty(ApplicationSettings.Default.DisplayTimeZone)) { reportTimezone(); } } } catch (WebException webEx) { // Increment the quantity of XMDS failures and bail out ApplicationSettings.Default.IncrementXmdsErrorCount(); // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("RegisterAgent - Run", "WebException in Run: " + webEx.Message), LogType.Info.ToString()); } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Exception in Run: " + ex.Message), LogType.Info.ToString()); } } // Sleep this thread until the next collection interval _manualReset.WaitOne((int)(ApplicationSettings.Default.CollectInterval * ApplicationSettings.Default.XmdsCollectionIntervalFactor() * 1000)); } Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Thread Stopped"), LogType.Info.ToString()); }