From 2fa42f24fd0e80adc16320a404e3e85ca6d1baa1 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Mon, 19 Aug 2013 21:00:31 +0100 Subject: Make it possible to disconnected a specified number of bots via the pCampbot console command "disconnect []" Bots disconnected are ascending from last in numeric order. Temporarily no way to reconnect bots. --- OpenSim/Tools/pCampBot/BotManager.cs | 101 +++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 46 deletions(-) (limited to 'OpenSim/Tools/pCampBot') diff --git a/OpenSim/Tools/pCampBot/BotManager.cs b/OpenSim/Tools/pCampBot/BotManager.cs index 5c0dc81..157c69c 100644 --- a/OpenSim/Tools/pCampBot/BotManager.cs +++ b/OpenSim/Tools/pCampBot/BotManager.cs @@ -80,7 +80,7 @@ namespace pCampBot /// /// Created bots, whether active or inactive. /// - protected List m_lBot; + protected List m_bots; /// /// Random number generator. @@ -130,35 +130,30 @@ namespace pCampBot } } - m_console.Commands.AddCommand("bot", false, "shutdown", - "shutdown", - "Shutdown bots and exit", HandleShutdown); + m_console.Commands.AddCommand( + "bot", false, "shutdown", "shutdown", "Shutdown bots and exit", HandleShutdown); - m_console.Commands.AddCommand("bot", false, "quit", - "quit", - "Shutdown bots and exit", - HandleShutdown); + m_console.Commands.AddCommand( + "bot", false, "quit", "quit", "Shutdown bots and exit", HandleShutdown); - m_console.Commands.AddCommand("bot", false, "disconnect", - "disconnect", - "Disconnect all bots", - HandleDisconnect); + m_console.Commands.AddCommand( + "bot", false, "disconnect", "disconnect []", "Disconnect bots", + "Disconnecting bots will interupt any bot connection process, including connection on startup.\n" + + "If an is given, then the last connected bots by postfix number are disconnected.\n" + + "If no is given, then all currently connected bots are disconnected.", + HandleDisconnect); - m_console.Commands.AddCommand("bot", false, "show regions", - "show regions", - "Show regions known to bots", - HandleShowRegions); + m_console.Commands.AddCommand( + "bot", false, "show regions", "show regions", "Show regions known to bots", HandleShowRegions); - m_console.Commands.AddCommand("bot", false, "show bots", - "show bots", - "Shows the status of all bots", - HandleShowStatus); + m_console.Commands.AddCommand( + "bot", false, "show bots", "show bots", "Shows the status of all bots", HandleShowStatus); // m_console.Commands.AddCommand("bot", false, "add bots", // "add bots ", // "Add more bots", HandleAddBots); - m_lBot = new List(); + m_bots = new List(); } /// @@ -196,7 +191,7 @@ namespace pCampBot for (int i = 0; i < botcount; i++) { - lock (m_lBot) + lock (m_bots) { if (DisconnectingBots) break; @@ -316,7 +311,7 @@ namespace pCampBot pb.OnConnected += handlebotEvent; pb.OnDisconnected += handlebotEvent; - m_lBot.Add(pb); + m_bots.Add(pb); Thread pbThread = new Thread(pb.startup); pbThread.Name = pb.Name; @@ -349,21 +344,6 @@ namespace pCampBot } /// - /// Shut down all bots - /// - /// - /// We launch each shutdown on its own thread so that a slow shutting down bot doesn't hold up all the others. - /// - private void ShutdownBots() - { - foreach (Bot bot in m_lBot) - { - Bot thisBot = bot; - Util.FireAndForget(o => thisBot.shutdown()); - } - } - - /// /// Standard CreateConsole routine /// /// @@ -374,21 +354,50 @@ namespace pCampBot private void HandleDisconnect(string module, string[] cmd) { - MainConsole.Instance.Output("Disconnecting bots"); - - lock (m_lBot) + lock (m_bots) { + int botsToDisconnect; + int connectedBots = m_bots.Count(b => b.ConnectionState == ConnectionState.Connected); + + if (cmd.Length == 1) + { + botsToDisconnect = connectedBots; + } + else + { + if (!ConsoleUtil.TryParseConsoleNaturalInt(MainConsole.Instance, cmd[1], out botsToDisconnect)) + return; + + botsToDisconnect = Math.Min(botsToDisconnect, connectedBots); + } + DisconnectingBots = true; - ShutdownBots(); + MainConsole.Instance.OutputFormat("Disconnecting {0} bots", botsToDisconnect); + + int disconnectedBots = 0; + + for (int i = m_bots.Count - 1; i >= 0; i--) + { + if (disconnectedBots >= botsToDisconnect) + break; + + Bot thisBot = m_bots[i]; + + if (thisBot.ConnectionState == ConnectionState.Connected) + { + Util.FireAndForget(o => thisBot.shutdown()); + disconnectedBots++; + } + } } } private void HandleShutdown(string module, string[] cmd) { - lock (m_lBot) + lock (m_bots) { - int connectedBots = m_lBot.Count(b => b.ConnectionState == ConnectionState.Connected); + int connectedBots = m_bots.Count(b => b.ConnectionState == ConnectionState.Connected); if (connectedBots > 0) { @@ -429,9 +438,9 @@ namespace pCampBot foreach (object o in Enum.GetValues(typeof(ConnectionState))) totals[(ConnectionState)o] = 0; - lock (m_lBot) + lock (m_bots) { - foreach (Bot pb in m_lBot) + foreach (Bot pb in m_bots) { Simulator currentSim = pb.Client.Network.CurrentSim; totals[pb.ConnectionState]++; -- cgit v1.1