/// <summary> /// Stops the service immediately, terminating any user activity. /// </summary> public void Stop() { lock (syncLock) { if (state == ServiceState.Stopped) { return; } SysLog.LogInformation("NeonSwitch Stop"); SysLog.Flush(); base.Close(); state = ServiceState.Stopped; if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } if (router != null) { router.Stop(); router = null; } } }
/// <summary> /// Immediately terminates the processing of all client messages. /// </summary> public void Stop() { if (router == null) { return; } using (TimedLock.Lock(syncLock)) { if (packageFolder != null) { packageFolder.Dispose(); packageFolder = null; } if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } if (cluster != null) { cluster.Stop(); cluster = null; } if (router != null) { router.Dispatcher.RemoveTarget(this); router = null; } } }
/// <summary> /// Immediately terminates the processing of all client messages. /// </summary> public void Stop() { if (!isRunning) { return; } using (TimedLock.Lock(syncLock)) { if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } if (httpServer != null) { httpServer.Stop(); httpServer = null; } router = null; isRunning = false; } }
private void FormServiceHost_Closing(object sender, System.ComponentModel.CancelEventArgs args) { switch (service.State) { case ServiceState.Starting: case ServiceState.Running: case ServiceState.Shutdown: try { service.Stop(); } catch (Exception e) { SysLog.LogException(e); } break; } SysLog.LogProvider = null; if (timer != null) { timer.Dispose(); timer = null; } Environment.Exit(0); }
/// <summary> /// Releases all resources associated with the instance. /// </summary> public void Close() { using (TimedLock.Lock(syncLock)) { if (syncLock == null) { return; } router.Dispatcher.RemoveTarget(this); if (packageFolder != null) { packageFolder.Dispose(); packageFolder = null; } if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } router = null; syncLock = null; } }
public void GatedTimer_Basic() { count = 0; state = null; wait = 1000; dispose = false; change = 0; timer = new GatedTimer(new TimerCallback(OnTimer), 10, TimeSpan.Zero, TimeSpan.FromMilliseconds(100)); Thread.Sleep(1000); timer.Dispose(); Assert.AreEqual(1, count); Assert.AreEqual(10, (int)state); count = 0; state = null; wait = 0; dispose = false; change = 0; timer = new GatedTimer(new TimerCallback(OnTimer), 10, TimeSpan.Zero, TimeSpan.FromMilliseconds(100)); Thread.Sleep(1000); timer.Dispose(); Assert.AreEqual(10, count); Assert.AreEqual(10, (int)state); }
/// <summary> /// Stops the server if it's currently running. /// </summary> public void Stop() { using (TimedLock.Lock(this)) { if (sock != null) { sock.Close(); } if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } devices = null; deviceMap = null; accounts = null; LogEvent = null; AuthenticateEvent = null; GetNasInfoEvent = null; isRunning = false; } Thread.Sleep(1000); // Give any pending authentication event calls a chance // to unwind. }
/// <summary> /// Stops the agent if it is running. /// </summary> public void Stop() { if (queueTimer != null) { queueTimer.Dispose(); queueTimer = null; } }
/// <summary> /// Removes a connection from the internal collection to /// detect timeouts. /// </summary> /// <param name="con">The connections.</param> internal static void RemoveConnection(HttpConnection con) { lock (syncLock) { if (sweeping) { return; // This is handled below by the sweep handler } clientCons.Remove(con); // If that was the last connection then stop the sweep timer. if (clientCons.Count == 0 && sweepTimer != null) { sweepTimer.Dispose(); sweepTimer = null; } } }
/// <summary> /// Stops the server, removing it from the cluster. /// </summary> /// <remarks> /// <note> /// It is not an error to call this method then the instance has already been closed. /// </note> /// </remarks> public void Close() { lock (syncLock) { if (closePending || socket == null) { return; } var packet = GetMessageBytes(UdpBroadcastMessageType.ServerUnregister); foreach (var server in settings.Servers) { if (!PauseNetwork) { socket.SendTo(packet, server); } } closePending = true; } // Sleep for a couple seconds so that any broadcast messages in transit // will still be retransmitted during the time it will take for the // other servers in the cluster to decide on a new master. Thread.Sleep(2000); socket.Close(); socket = null; if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } servers.Clear(); clients.Clear(); GC.SuppressFinalize(this); }
/// <summary> /// Stops the cache if it is running. /// </summary> public void Stop() { lock (syncLock) { if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } } }
/// <summary> /// Closes the instance, removing it from the broadcast group. /// </summary> /// <remarks> /// <note> /// It is not an error to call this method then the instance has already been closed. /// </note> /// </remarks> public void Close() { lock (syncLock) { if (socket != null) { var packet = GetMessageBytes(UdpBroadcastMessageType.ClientUnregister, settings.BroadcastGroup); foreach (var server in servers) { if (!PauseNetwork) { try { socket.SendTo(packet, server); } catch (Exception e) { SysLog.LogException(e); } } } socket.Close(); socket = null; } if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } } GC.SuppressFinalize(this); }
/// <summary> /// Immediately terminates the processing of all client messages. /// </summary> public void Stop() { if (!isRunning) { return; } lock (syncLock) { router.Dispatcher.RemoveTarget(this); if (clusterClient != null) { clusterClient.Close(); } if (clusterServer != null) { clusterServer.Close(); } if (fixCache != null) { fixCache.Stop(); } if (ipGeocoder != null) { ipGeocoder.Stop(); } if (archiver != null) { archiver.Stop(); archiver = null; } if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } isRunning = false; } }
private void OnTimer(object state) { this.count++; this.state = state; Thread.Sleep(wait); if (dispose) { timer.Dispose(); } if (change > 0) { timer.Change(TimeSpan.Zero, TimeSpan.FromMilliseconds(change)); } }
/// <summary> /// Releases any important resources associated with the instance. /// </summary> /// <param name="disposing">Pass <c>true</c> if the instance is being disposed as opposed to being finalized.</param> protected void Dispose(bool disposing) { if (disposing) { lock (cache) { if (cacheTimer != null) { cacheTimer.Dispose(); cacheTimer = null; } } GC.SuppressFinalize(this); } cacheTimer = null; }
/// <summary> /// Handles timeout processing. /// </summary> /// <param name="state">Not used.</param> private static void OnBkTimer(object state) { var delKeys = new List <int>(); var now = SysTime.Now; lock (syncLock) { // Walk the list of requests, looking for those that have timed out // and adding them to the deleted list. foreach (int key in requests.Keys) { var arDns = requests[key]; if (now >= arDns.TTD) { delKeys.Add(key); } } // Signal a timeout exception for each deleted request and // remove them from the requests table. for (int i = 0; i < delKeys.Count; i++) { var key = delKeys[i]; var arDns = requests[key]; requests.Remove(key); arDns.Notify(new TimeoutException()); } // Kill the timer if there are no outstanding requests and the // class has been idle for a while. if (requests.Count == 0 && now - lastQueryTime >= maxIdleTime) { bkTimer.Dispose(); bkTimer = null; } } }
/// <summary> /// Closes the client, releasing all resources. /// </summary> public void Close() { using (TimedLock.Lock(this)) { if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } if (ports != null) { foreach (RadiusClientPort port in ports) { port.Close(); } ports = null; } } }
/// <summary> /// Closes the authenticator instance if it's open. /// </summary> public void Close() { using (TimedLock.Lock(this)) { if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } if (router != null) { router.Dispatcher.RemoveTarget(this); router = null; } cache = null; publicKey = null; isOpen = false; } }
private void MainForm_FormClosed(object sender, FormClosedEventArgs args) { timer.Dispose(); stopButton_Click(null, null); }
/// <summary> /// Associates the service handler with a message router by registering /// the necessary application message handlers. /// </summary> /// <param name="router">The message router.</param> /// <param name="keyPrefix">The configuration key prefix or (null to use <b>LillTek.Datacenter.AppStore</b>).</param> /// <param name="perfCounters">The application's performance counter set (or <c>null</c>).</param> /// <param name="perfPrefix">The string to prefix any performance counter names (or <c>null</c>).</param> /// <remarks> /// <para> /// Applications that expose performance counters will pass a non-<c>null</c> <b>perfCounters</b> /// instance. The service handler should add any counters it implements to this set. /// If <paramref name="perfPrefix" /> is not <c>null</c> then any counters added should prefix their /// names with this parameter. /// </para> /// </remarks> public void Start(MsgRouter router, string keyPrefix, PerfCounterSet perfCounters, string perfPrefix) { var config = new Config(keyPrefix != null ? keyPrefix : ConfigPrefix); // Make sure the syncLock is set early. this.syncLock = router.SyncRoot; // Make sure that the LillTek.Datacenter message types have been // registered with the LillTek.Messaging subsystem. LillTek.Datacenter.Global.RegisterMsgTypes(); // Verify the router parameter if (router == null) { throw new ArgumentNullException("router", "Router cannot be null."); } if (this.router != null) { throw new InvalidOperationException("This handler has already been started."); } // General initialization mode = config.Get <AppStoreMode>("Mode", AppStoreMode.Primary); primaryBroadcast = config.Get("PrimaryBroadcast", true); packageScanInterval = config.Get("PackageScanInterval", TimeSpan.FromMinutes(5)); primaryPollInterval = config.Get("PrimaryPollInterval", TimeSpan.FromMinutes(15)); primaryPollTime = SysTime.Now; onTransfer = new AsyncCallback(OnTransfer); downloads = new Dictionary <AppRef, PendingDownload>(); forceSync = false; cDownloads = 0; netFail = false; // Initialize the package folder packageFolder = new AppPackageFolder(syncLock, config.Get("PackageFolder", "Packages")); packageFolder.ChangeEvent += new MethodArg1Invoker(OnPackageFolderChange); packageScanTime = SysTime.Now; // Initialize the performance counters startTime = DateTime.UtcNow; perf = new Perf(perfCounters, perfPrefix); // Crank up the background task timer. bkTimer = new GatedTimer(new TimerCallback(OnBkTimer), null, config.Get("BkTaskInterval", TimeSpan.FromSeconds(1))); try { // Initialize the router this.router = router; // Join the cluster, initializing this instance's state. cluster = new ClusterMember(router, ClusterMemberSettings.LoadConfig(config.KeyPrefix + "Cluster")); cluster["Mode"] = this.mode.ToString(); cluster.ClusterStatusUpdate += new ClusterMemberEventHandler(OnClusterStatusUpdate); cluster.Start(); // Rather than calling cluster.JoinWait() which could take a really long // time, I'm going to sleep for two seconds. There are three scenarios: // // 1. This is the first Application Store instance. // // 2. Other instances are running but they haven't // organized into a cluster. // // 3. A cluster is already running. // // If #1 is the current situation, then it will take a very long time // for JoinWait() to return because we have to go through the entire // missed master broadcast and election periods. Since we're the only // instance, we could have started serving content well before this. // // #2 won't be very common but if it is the case, the worst thing // that will happen is that it will take a while to discover the // primary store. // // If #3 is the case, then two seconds should be long enough for the // master to send the instance a cluster update. Thread.Sleep(2000); // Register the message handlers via the cluster member // so that the endpoint used will be the member's instanceEP. cluster.AddTarget(this, AppStoreHandler.DynamicScope); } catch { if (packageFolder != null) { packageFolder.Dispose(); packageFolder = null; } if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } router.Dispatcher.RemoveTarget(this); if (cluster != null) { cluster.Stop(); cluster = null; } throw; } }
/// <summary> /// Releases all resources associated with the instance. /// </summary> public void Close() { using (TimedLock.Lock(syncLock)) { if (!isOpen) { return; } // If we're running in UDP mode, then send a couple deregister messages // for all host entries to each known name server. This will ensure that // the DNS updated quickly when servers are shut down gracefully. Note // that I'm going to send duplicate messages to each server just to be // on the safe side. if (settings.Mode == DynDnsMode.Udp) { for (int i = 0; i < 2; i++) { DynDnsMessage message; byte[] packet; foreach (var entry in hosts.Values) { message = new DynDnsMessage(DynDnsMessageFlag.OpUnregister, entry); packet = message.ToArray(settings.SharedKey); foreach (var nameServer in settings.NameServers) { socket.SendTo(packet, nameServer); } Thread.Sleep(500); } } } // Shut down the client. if (cluster != null) { cluster.Stop(); cluster = null; } if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } if (socket != null) { socket.Close(); socket = null; } router = null; isOpen = false; } }
/// <summary> /// Starts the service, associating it with an <see cref="IServiceHost" /> instance. /// </summary> /// <param name="serviceHost">The service user interface.</param> /// <param name="args">Command line arguments.</param> public void Start(IServiceHost serviceHost, string[] args) { lock (syncLock) { if (router != null) { return; // Already started } // Global initialization startTime = DateTime.UtcNow; NetTrace.Start(); CoreApp.InstallPerfCounters(); // Service initialization this.serviceHost = serviceHost; try { SysLog.LogInformation("NeonSwitch v{0} Start", Helper.GetVersionString()); router = new LeafRouter(); router.Start(); state = ServiceState.Running; bkTimer = new GatedTimer(OnBkTimer, null, CoreApp.BkTaskInterval); SpeechEngine.Start(SpeechEngineSettings.LoadConfig("NeonSwitch.Speech")); #if DEBUG // $todo(jeff.lill): Delete this. SwitchTest.Test(); #endif // Indicate that the switch core service is open for business. NeonSwitch // application instance loaders will spin, waiting for this to be set before // calling the application's main function. Switch.SetGlobal(SwitchGlobal.NeonSwitchReady, "true"); } catch (Exception e) { SpeechEngine.Stop(); if (bkTimer != null) { bkTimer.Dispose(); bkTimer = null; } if (router != null) { router.Stop(); router = null; } SysLog.LogException(e); throw; } } }