public void Start() { lock (_locker) { if (_running) { return; } else { _running = true; } if (ProtocolRunningChanged != null) { ProtocolRunningChanged(this, _running); } if (_firstStartTimestamp == DateTimeOffset.MinValue) { _firstStartTimestamp = DateTimeOffset.UtcNow; } // let the service helper know that the current protocol is running (saves helper) SensusServiceHelper.Get().RegisterProtocol(this); SensusServiceHelper.Get().AddRunningProtocolId(_id); SensusServiceHelper.Get().Logger.Log("Starting probes for protocol " + _name + ".", LoggingLevel.Normal, GetType()); int probesStarted = 0; foreach (Probe probe in _probes) { if (probe.Enabled) { try { probe.Start(); probesStarted++; } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Failed to start probe \"" + probe.GetType().FullName + "\":" + ex.Message, LoggingLevel.Normal, GetType()); } } } bool stopProtocol = false; if (probesStarted > 0) { try { _localDataStore.Start(); try { _remoteDataStore.Start(); } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Remote data store failed to start: " + ex.Message, LoggingLevel.Normal, GetType()); stopProtocol = true; } } catch (Exception ex) { SensusServiceHelper.Get().Logger.Log("Local data store failed to start: " + ex.Message, LoggingLevel.Normal, GetType()); stopProtocol = true; } } else { SensusServiceHelper.Get().Logger.Log("No probes were started.", LoggingLevel.Normal, GetType()); stopProtocol = true; } if (stopProtocol) { Stop(); } } }
public void Start() { lock (_locker) { if (_running) { return; } else { _running = true; } if (ProtocolRunningChanged != null) { ProtocolRunningChanged(this, _running); } // let the service helper know that the current protocol is running (saves helper) SensusServiceHelper.Get().RegisterProtocol(this); SensusServiceHelper.Get().AddRunningProtocolId(_id); bool stopProtocol = false; // start local data store try { if (_localDataStore == null) { throw new Exception("Local data store not defined."); } _localDataStore.Start(); // start remote data store try { if (_remoteDataStore == null) { throw new Exception("Remote data store not defined."); } _remoteDataStore.Start(); // start probes try { SensusServiceHelper.Get().Logger.Log("Starting probes for protocol " + _name + ".", LoggingLevel.Normal, GetType()); int probesEnabled = 0; int probesStarted = 0; foreach (Probe probe in _probes) { if (probe.Enabled) { ++probesEnabled; try { probe.Start(); probesStarted++; } catch (Exception ex) { // stop probe to clean up any inconsistent state information try { probe.Stop(); } catch (Exception ex2) { SensusServiceHelper.Get().Logger.Log("Failed to stop probe after failing to start it: " + ex2.Message, LoggingLevel.Normal, GetType()); } string message = "Failed to start probe \"" + probe.GetType().FullName + "\": " + ex.Message; SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType()); SensusServiceHelper.Get().FlashNotificationAsync(message); // disable probe if it is not supported on the device if (ex is NotSupportedException) { probe.Enabled = false; } } } } if (probesEnabled == 0) { throw new Exception("No probes were enabled."); } else if (probesStarted == 0) { throw new Exception("No probes started."); } } catch (Exception ex) { string message = "Failure while starting probes: " + ex.Message; SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType()); SensusServiceHelper.Get().FlashNotificationAsync(message); stopProtocol = true; } } catch (Exception ex) { string message = "Remote data store failed to start: " + ex.Message; SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType()); SensusServiceHelper.Get().FlashNotificationAsync(message); stopProtocol = true; } } catch (Exception ex) { string message = "Local data store failed to start: " + ex.Message; SensusServiceHelper.Get().Logger.Log(message, LoggingLevel.Normal, GetType()); SensusServiceHelper.Get().FlashNotificationAsync(message); stopProtocol = true; } if (stopProtocol) { Stop(); } } }