ARFlightController: Optimized a bit, and only search for ConnectionStatus every now and then.
ARFlightController: Optimized a bit, and only search for ConnectionStatus every now and then.

--- a/ARFlightController.cs
+++ b/ARFlightController.cs
@@ -46,6 +46,9 @@
 		protected IButton toolbarButton;
 
 		protected ApplicationLauncherButton appLauncherButton;
+		protected Tools.DebugLogger log;
+
+		protected System.Diagnostics.Stopwatch updateTimer;
 		#endregion
 
 		#region Properties
@@ -116,6 +119,10 @@
 		protected void Awake()
 		{
 			this.lockID = "ARConnectionRequired";
+
+			this.log = Tools.DebugLogger.New(this);
+
+			this.updateTimer = new System.Diagnostics.Stopwatch();
 
 			this.connectionTextures = new Dictionary<ConnectionStatus, string>();
 
@@ -161,7 +168,7 @@
 				);
 			}
 
-			Tools.DebugLogger log = Tools.DebugLogger.New(this);
+			this.log.Clear();
 
 			VesselCommand availableCommand;
 
@@ -209,7 +216,23 @@
 				// ...unlock the controls.
 				InputLockManager.RemoveControlLock(this.lockID);
 			}
-				
+
+			log.Print();
+		}
+
+		protected void Update()
+		{
+			if (!this.updateTimer.IsRunning || this.updateTimer.ElapsedMilliseconds > 125L)
+			{
+				this.updateTimer.Reset();
+			}
+			else
+			{
+				return;
+			}
+
+			this.log.Clear();
+
 			if (
 				(this.toolbarButton != null || this.appLauncherButton != null) &&
 				HighLogic.LoadedSceneIsFlight &&
@@ -218,62 +241,7 @@
 			{
 				log.Append("Checking vessel relay status.\n");
 
-				List<ModuleLimitedDataTransmitter> relays =
-					FlightGlobals.ActiveVessel.getModulesOfType<ModuleLimitedDataTransmitter>();
-
-				log.AppendFormat("\t...found {0} relays\n", relays.Count);
-
-				bool vesselCanTransmit = false;
-				bool vesselHasOptimalRelay = false;
-
-				foreach (ModuleLimitedDataTransmitter relay in relays)
-				{
-					log.AppendFormat("\tvesselCanTransmit: {0}, vesselHasOptimalRelay: {1}\n",
-						vesselCanTransmit, vesselHasOptimalRelay);
-
-					log.AppendFormat("\tChecking relay {0}\n" +
-						"\t\tCanTransmit: {1}, transmitDistance: {2}, nominalRange: {3}\n",
-						relay,
-						relay.CanTransmit(),
-						relay.transmitDistance,
-						relay.nominalRange
-					);
-
-					bool relayCanTransmit = relay.CanTransmit();
-
-					if (!vesselCanTransmit && relayCanTransmit)
-					{
-						vesselCanTransmit = true;
-					}
-
-					if (!vesselHasOptimalRelay &&
-						relayCanTransmit &&
-						relay.transmitDistance <= (double)relay.nominalRange)
-					{
-						vesselHasOptimalRelay = true;
-					}
-
-					if (vesselCanTransmit && vesselHasOptimalRelay)
-					{
-						break;
-					}
-				}
-
-				log.AppendFormat("Done checking.  vesselCanTransmit: {0}, vesselHasOptimalRelay: {1}\n",
-					vesselCanTransmit, vesselHasOptimalRelay);
-
-				if (vesselHasOptimalRelay)
-				{
-					this.currentConnectionStatus = ConnectionStatus.Optimal;
-				}
-				else if (vesselCanTransmit)
-				{
-					this.currentConnectionStatus = ConnectionStatus.Suboptimal;
-				}
-				else
-				{
-					this.currentConnectionStatus = ConnectionStatus.None;
-				}
+				this.currentConnectionStatus = FlightGlobals.ActiveVessel.GetConnectionStatus();
 
 				log.AppendFormat("currentConnectionStatus: {0}, setting texture to {1}",
 					this.currentConnectionStatus, this.currentConnectionTexture);
@@ -339,13 +307,6 @@
 			InputLockManager.RemoveControlLock(this.lockID);
 		}
 		#endregion
-
-		public enum ConnectionStatus
-		{
-			None,
-			Suboptimal,
-			Optimal
-		}
 	}
 }
 

--- a/AntennaRelay.cs
+++ b/AntennaRelay.cs
@@ -522,7 +522,7 @@
 			this.moduleRef = module;
 
 			this.searchTimer = new System.Diagnostics.Stopwatch();
-			this.millisecondsBetweenSearches = 125;
+			this.millisecondsBetweenSearches = 125L;
 		}
 	}
 }

--- a/RelayExtensions.cs
+++ b/RelayExtensions.cs
@@ -109,6 +109,39 @@
 
 			return false;
 		}
+
+		public static ConnectionStatus GetConnectionStatus(this Vessel vessel)
+		{
+			bool canTransmit = false;
+
+			foreach (IAntennaRelay relay in RelayDatabase.Instance[vessel].Values)
+			{
+				if (relay.CanTransmit())
+				{
+					canTransmit = true;
+					if (relay.transmitDistance <= relay.nominalTransmitDistance)
+					{
+						return ConnectionStatus.Optimal;
+					}
+				}
+			}
+
+			if (canTransmit)
+			{
+				return ConnectionStatus.Suboptimal;
+			}
+			else
+			{
+				return ConnectionStatus.None;
+			}
+		}
+	}
+
+	public enum ConnectionStatus
+	{
+		None,
+		Suboptimal,
+		Optimal
 	}
 }