/// <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; xmds.UseDefaultCredentials = false; RegisterAgent.ProcessRegisterXml(xmds.RegisterDisplay(ApplicationSettings.Default.ServerKey, key.Key, ApplicationSettings.Default.DisplayName, "windows", ApplicationSettings.Default.ClientVersion, ApplicationSettings.Default.ClientCodeVersion, Environment.OSVersion.ToString(), key.MacAddress)); // Set the flag to indicate we have a connection to XMDS ApplicationSettings.Default.XmdsLastConnection = DateTime.Now; // Do we need to send a screenshot? if (ApplicationSettings.Default.ScreenShotRequested) { ApplicationSettings.Default.ScreenShotRequested = false; ScreenShot.TakeAndSend(); } } } 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.Error.ToString()); } catch (Exception ex) { // Log this message, but dont abort the thread Trace.WriteLine(new LogMessage("RegisterAgent - Run", "Exception in Run: " + ex.Message), LogType.Error.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()); }
/// <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> /// Create a schedule /// </summary> /// <param name="scheduleLocation"></param> public Schedule(string scheduleLocation, ref CacheManager cacheManager, ref ClientInfo clientInfoForm) { Trace.WriteLine(string.Format("XMDS Location: {0}", ApplicationSettings.Default.XiboClient_xmds_xmds)); // Get the key for this display _hardwareKey = new HardwareKey(); // Save the schedule location _scheduleLocation = scheduleLocation; // Create a new collection for the layouts in the schedule _layoutSchedule = new Collection<LayoutSchedule>(); // Set cachemanager _cacheManager = cacheManager; // Set client info form _clientInfoForm = clientInfoForm; // Create a Register Agent _registerAgent = new RegisterAgent(); _registerAgentThread = new Thread(new ThreadStart(_registerAgent.Run)); _registerAgentThread.Name = "RegisterAgentThread"; // Create a schedule manager _scheduleManager = new ScheduleManager(_cacheManager, scheduleLocation); _scheduleManager.OnNewScheduleAvailable += new ScheduleManager.OnNewScheduleAvailableDelegate(_scheduleManager_OnNewScheduleAvailable); _scheduleManager.OnRefreshSchedule += new ScheduleManager.OnRefreshScheduleDelegate(_scheduleManager_OnRefreshSchedule); _scheduleManager.ClientInfoForm = _clientInfoForm; // Create a schedule manager thread _scheduleManagerThread = new Thread(new ThreadStart(_scheduleManager.Run)); _scheduleManagerThread.Name = "ScheduleManagerThread"; // Create a Schedule Agent _scheduleAgent = new ScheduleAgent(); _scheduleAgent.CurrentScheduleManager = _scheduleManager; _scheduleAgent.ScheduleLocation = scheduleLocation; _scheduleAgent.HardwareKey = _hardwareKey.Key; _scheduleAgent.ClientInfoForm = _clientInfoForm; // Create a thread for the Schedule Agent to run in - but dont start it up yet. _scheduleAgentThread = new Thread(new ThreadStart(_scheduleAgent.Run)); _scheduleAgentThread.Name = "ScheduleAgentThread"; // Create a RequiredFilesAgent _requiredFilesAgent = new RequiredFilesAgent(); _requiredFilesAgent.CurrentCacheManager = cacheManager; _requiredFilesAgent.HardwareKey = _hardwareKey.Key; _requiredFilesAgent.ClientInfoForm = _clientInfoForm; _requiredFilesAgent.OnComplete += new RequiredFilesAgent.OnCompleteDelegate(LayoutFileModified); // Create a thread for the RequiredFiles Agent to run in - but dont start it up yet. _requiredFilesAgentThread = new Thread(new ThreadStart(_requiredFilesAgent.Run)); _requiredFilesAgentThread.Name = "RequiredFilesAgentThread"; // Library Agent _libraryAgent = new LibraryAgent(); _libraryAgent.CurrentCacheManager = _cacheManager; // Create a thread for the Library Agent to run in - but dont start it up yet. _libraryAgentThread = new Thread(new ThreadStart(_libraryAgent.Run)); _libraryAgentThread.Name = "LibraryAgent"; // Log Agent _logAgent = new LogAgent(); _logAgentThread = new Thread(new ThreadStart(_logAgent.Run)); _logAgentThread.Name = "LogAgent"; }
/// <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()); }