/** * Starts all message processors * * @param newSize the new thread poolsize */ private void shrinkThreadPool(int newSize) { while (messageProcessors.Count > newSize) { MessageProcessor mp = (MessageProcessor)messageProcessors[(0)]; messageProcessors.RemoveAt(0); mp.Stop(); } }
/** * Clears the faulty thread and tries to repair the damage and instanciate * a replacement. * * @param callingThread the thread where the error occurred. * @param message A description of the error * @param error The error itself */ public void HandleFatalError(Runnable callingThread, String message, Exception error) { if (callingThread is NetAccessPoint) { NetAccessPoint ap = (NetAccessPoint)callingThread; bool running = ap.IsRunning(); //make sure socket is closed RemoveNetAccessPoint(ap.GetDescriptor()); if (running) { try { Console.WriteLine("An access point has unexpectedly " + "stopped. AP: {0}", ap.ToString()); InstallNetAccessPoint(ap.GetDescriptor()); /** @todo: log fixing the error*/ } catch (StunException ex) { //make sure nothing's left and notify user RemoveNetAccessPoint(ap.GetDescriptor()); Console.WriteLine("Failed to relaunch accesspoint: {0} {1} {2}", ap, ex.Message, ex.StackTrace); /** @todo notify user and log */ } } return; } else if (callingThread is MessageProcessor) { MessageProcessor mp = (MessageProcessor)callingThread; //make sure the guy's dead. mp.Stop(); messageProcessors.Remove(mp); mp = new MessageProcessor(messageQueue, messageEventHandler, this); mp.Start(); Console.WriteLine("A message processor has been relaunched because of an error."); } Console.WriteLine("Error was: {0} {1}", error.Message, error.StackTrace); }
/** * Stops and deletes all message processors and access points. */ public virtual void ShutDown() { if (!isRunning) { return; } //stop access points ArrayList toRemove = new ArrayList(); foreach (NetAccessPointDescriptor napd in netAccessPoints.Keys) { toRemove.Add(napd); } foreach (NetAccessPointDescriptor napd in toRemove) { RemoveNetAccessPoint(napd); } //stop and empty thread pool while (messageProcessors.Count != 0) { MessageProcessor mp = (MessageProcessor)messageProcessors[0]; messageProcessors.RemoveAt(0); mp.Stop(); } //message processors should have been interrupted but it wouldn't hurt //notifying as well in case we decide to change one day. Monitor.Enter(messageQueue); try { Monitor.PulseAll(messageQueue); } finally { Monitor.Exit(messageQueue); } isRunning = false; }