Create a separate pCampbot "disconnect" console command which disconnects connected bots.

"quit" console command now requires bots to be separate disconnected first before quitting.
This is to prepare the way for disconnecting/reconnecting different numbers of bots in a pCampbot session.
And hopefully resolves bug where console appears not to be reset if Environment.Exit(0) is called on a different thread
0.7.6-extended
Justin Clark-Casey (justincc) 2013-08-19 20:29:17 +01:00
parent ef63abe9b1
commit 49b7cbda72
1 changed files with 67 additions and 43 deletions

View File

@ -52,9 +52,9 @@ namespace pCampBot
public const int DefaultLoginDelay = 5000; public const int DefaultLoginDelay = 5000;
/// <summary> /// <summary>
/// True if pCampbot is in the process of shutting down. /// Is pCampbot in the process of disconnecting bots?
/// </summary> /// </summary>
public bool ShuttingDown { get; private set; } public bool DisconnectingBots { get; private set; }
/// <summary> /// <summary>
/// Delay between logins of multiple bots. /// Delay between logins of multiple bots.
@ -139,6 +139,11 @@ namespace pCampBot
"Shutdown bots and exit", "Shutdown bots and exit",
HandleShutdown); HandleShutdown);
m_console.Commands.AddCommand("bot", false, "disconnect",
"disconnect",
"Disconnect all bots",
HandleDisconnect);
m_console.Commands.AddCommand("bot", false, "show regions", m_console.Commands.AddCommand("bot", false, "show regions",
"show regions", "show regions",
"Show regions known to bots", "Show regions known to bots",
@ -191,7 +196,9 @@ namespace pCampBot
for (int i = 0; i < botcount; i++) for (int i = 0; i < botcount; i++)
{ {
if (ShuttingDown) lock (m_lBot)
{
if (DisconnectingBots)
break; break;
string lastName = string.Format("{0}_{1}", lastNameStem, i + fromBotNumber); string lastName = string.Format("{0}_{1}", lastNameStem, i + fromBotNumber);
@ -217,6 +224,10 @@ namespace pCampBot
StartBot(this, behaviours, firstName, lastName, password, loginUri, startUri, wearSetting); StartBot(this, behaviours, firstName, lastName, password, loginUri, startUri, wearSetting);
} }
// Stagger logins
Thread.Sleep(LoginDelay);
}
} }
/// <summary> /// <summary>
@ -305,7 +316,6 @@ namespace pCampBot
pb.OnConnected += handlebotEvent; pb.OnConnected += handlebotEvent;
pb.OnDisconnected += handlebotEvent; pb.OnDisconnected += handlebotEvent;
lock (m_lBot)
m_lBot.Add(pb); m_lBot.Add(pb);
Thread pbThread = new Thread(pb.startup); Thread pbThread = new Thread(pb.startup);
@ -313,9 +323,6 @@ namespace pCampBot
pbThread.IsBackground = true; pbThread.IsBackground = true;
pbThread.Start(); pbThread.Start();
// Stagger logins
Thread.Sleep(LoginDelay);
} }
/// <summary> /// <summary>
@ -328,16 +335,14 @@ namespace pCampBot
switch (eventt) switch (eventt)
{ {
case EventType.CONNECTED: case EventType.CONNECTED:
{
m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Connected"); m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Connected");
break; break;
}
case EventType.DISCONNECTED: case EventType.DISCONNECTED:
m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Disconnected");
lock (m_lBot)
{ {
if (m_lBot.TrueForAll(b => b.ConnectionState == ConnectionState.Disconnected)) m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Disconnected");
Environment.Exit(0);
break; break;
} }
} }
@ -349,9 +354,7 @@ namespace pCampBot
/// <remarks> /// <remarks>
/// We launch each shutdown on its own thread so that a slow shutting down bot doesn't hold up all the others. /// We launch each shutdown on its own thread so that a slow shutting down bot doesn't hold up all the others.
/// </remarks> /// </remarks>
public void doBotShutdown() private void ShutdownBots()
{
lock (m_lBot)
{ {
foreach (Bot bot in m_lBot) foreach (Bot bot in m_lBot)
{ {
@ -359,7 +362,6 @@ namespace pCampBot
Util.FireAndForget(o => thisBot.shutdown()); Util.FireAndForget(o => thisBot.shutdown());
} }
} }
}
/// <summary> /// <summary>
/// Standard CreateConsole routine /// Standard CreateConsole routine
@ -370,12 +372,34 @@ namespace pCampBot
return new LocalConsole("pCampbot"); return new LocalConsole("pCampbot");
} }
private void HandleDisconnect(string module, string[] cmd)
{
MainConsole.Instance.Output("Disconnecting bots");
lock (m_lBot)
{
DisconnectingBots = true;
ShutdownBots();
}
}
private void HandleShutdown(string module, string[] cmd) private void HandleShutdown(string module, string[] cmd)
{ {
lock (m_lBot)
{
int connectedBots = m_lBot.Count(b => b.ConnectionState == ConnectionState.Connected);
if (connectedBots > 0)
{
MainConsole.Instance.OutputFormat("Please disconnect {0} connected bots first", connectedBots);
return;
}
}
MainConsole.Instance.Output("Shutting down"); MainConsole.Instance.Output("Shutting down");
ShuttingDown = true; Environment.Exit(0);
doBotShutdown();
} }
private void HandleShowRegions(string module, string[] cmd) private void HandleShowRegions(string module, string[] cmd)