Make it possible to disconnected a specified number of bots via the pCampbot console command "disconnect [<n>]"

Bots disconnected are ascending from last in numeric order.
Temporarily no way to reconnect bots.
0.7.6-extended
Justin Clark-Casey (justincc) 2013-08-19 21:00:31 +01:00
parent 49b7cbda72
commit 2fa42f24fd
2 changed files with 82 additions and 48 deletions

View File

@ -179,8 +179,8 @@ namespace OpenSim.Framework.Console
/// Convert a console integer to an int, automatically complaining if a console is given. /// Convert a console integer to an int, automatically complaining if a console is given.
/// </summary> /// </summary>
/// <param name='console'>Can be null if no console is available.</param> /// <param name='console'>Can be null if no console is available.</param>
/// <param name='rawConsoleVector'>/param> /// <param name='rawConsoleInt'>/param>
/// <param name='vector'></param> /// <param name='i'></param>
/// <returns></returns> /// <returns></returns>
public static bool TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i) public static bool TryParseConsoleInt(ICommandConsole console, string rawConsoleInt, out int i)
{ {
@ -194,6 +194,31 @@ namespace OpenSim.Framework.Console
return true; return true;
} }
/// <summary>
/// Convert a console integer to a natural int, automatically complaining if a console is given.
/// </summary>
/// <param name='console'>Can be null if no console is available.</param>
/// <param name='rawConsoleInt'>/param>
/// <param name='i'></param>
/// <returns></returns>
public static bool TryParseConsoleNaturalInt(ICommandConsole console, string rawConsoleInt, out int i)
{
if (TryParseConsoleInt(console, rawConsoleInt, out i))
{
if (i < 0)
{
if (console != null)
console.OutputFormat("ERROR: {0} is not a positive integer", rawConsoleInt);
return false;
}
return true;
}
return false;
}
/// <summary> /// <summary>
/// Convert a minimum vector input from the console to an OpenMetaverse.Vector3 /// Convert a minimum vector input from the console to an OpenMetaverse.Vector3

View File

@ -80,7 +80,7 @@ namespace pCampBot
/// <summary> /// <summary>
/// Created bots, whether active or inactive. /// Created bots, whether active or inactive.
/// </summary> /// </summary>
protected List<Bot> m_lBot; protected List<Bot> m_bots;
/// <summary> /// <summary>
/// Random number generator. /// Random number generator.
@ -130,35 +130,30 @@ namespace pCampBot
} }
} }
m_console.Commands.AddCommand("bot", false, "shutdown", m_console.Commands.AddCommand(
"shutdown", "bot", false, "shutdown", "shutdown", "Shutdown bots and exit", HandleShutdown);
"Shutdown bots and exit", HandleShutdown);
m_console.Commands.AddCommand("bot", false, "quit", m_console.Commands.AddCommand(
"quit", "bot", false, "quit", "quit", "Shutdown bots and exit", HandleShutdown);
"Shutdown bots and exit",
HandleShutdown);
m_console.Commands.AddCommand("bot", false, "disconnect", m_console.Commands.AddCommand(
"disconnect", "bot", false, "disconnect", "disconnect [<n>]", "Disconnect bots",
"Disconnect all bots", "Disconnecting bots will interupt any bot connection process, including connection on startup.\n"
HandleDisconnect); + "If an <n> is given, then the last <n> connected bots by postfix number are disconnected.\n"
+ "If no <n> is given, then all currently connected bots are disconnected.",
HandleDisconnect);
m_console.Commands.AddCommand("bot", false, "show regions", m_console.Commands.AddCommand(
"show regions", "bot", false, "show regions", "show regions", "Show regions known to bots", HandleShowRegions);
"Show regions known to bots",
HandleShowRegions);
m_console.Commands.AddCommand("bot", false, "show bots", m_console.Commands.AddCommand(
"show bots", "bot", false, "show bots", "show bots", "Shows the status of all bots", HandleShowStatus);
"Shows the status of all bots",
HandleShowStatus);
// m_console.Commands.AddCommand("bot", false, "add bots", // m_console.Commands.AddCommand("bot", false, "add bots",
// "add bots <number>", // "add bots <number>",
// "Add more bots", HandleAddBots); // "Add more bots", HandleAddBots);
m_lBot = new List<Bot>(); m_bots = new List<Bot>();
} }
/// <summary> /// <summary>
@ -196,7 +191,7 @@ namespace pCampBot
for (int i = 0; i < botcount; i++) for (int i = 0; i < botcount; i++)
{ {
lock (m_lBot) lock (m_bots)
{ {
if (DisconnectingBots) if (DisconnectingBots)
break; break;
@ -316,7 +311,7 @@ namespace pCampBot
pb.OnConnected += handlebotEvent; pb.OnConnected += handlebotEvent;
pb.OnDisconnected += handlebotEvent; pb.OnDisconnected += handlebotEvent;
m_lBot.Add(pb); m_bots.Add(pb);
Thread pbThread = new Thread(pb.startup); Thread pbThread = new Thread(pb.startup);
pbThread.Name = pb.Name; pbThread.Name = pb.Name;
@ -348,21 +343,6 @@ namespace pCampBot
} }
} }
/// <summary>
/// Shut down all bots
/// </summary>
/// <remarks>
/// We launch each shutdown on its own thread so that a slow shutting down bot doesn't hold up all the others.
/// </remarks>
private void ShutdownBots()
{
foreach (Bot bot in m_lBot)
{
Bot thisBot = bot;
Util.FireAndForget(o => thisBot.shutdown());
}
}
/// <summary> /// <summary>
/// Standard CreateConsole routine /// Standard CreateConsole routine
/// </summary> /// </summary>
@ -374,21 +354,50 @@ namespace pCampBot
private void HandleDisconnect(string module, string[] cmd) private void HandleDisconnect(string module, string[] cmd)
{ {
MainConsole.Instance.Output("Disconnecting bots"); lock (m_bots)
lock (m_lBot)
{ {
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; 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) 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) if (connectedBots > 0)
{ {
@ -429,9 +438,9 @@ namespace pCampBot
foreach (object o in Enum.GetValues(typeof(ConnectionState))) foreach (object o in Enum.GetValues(typeof(ConnectionState)))
totals[(ConnectionState)o] = 0; 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; Simulator currentSim = pb.Client.Network.CurrentSim;
totals[pb.ConnectionState]++; totals[pb.ConnectionState]++;