Change bot.IsConnected to be ConnectionState with Disconnected, Connecting, Connnected and Disconnecting states

0.7.3-extended
Justin Clark-Casey (justincc) 2012-05-11 00:53:21 +01:00
parent 9ec74f2098
commit ca22b5e2f0
2 changed files with 22 additions and 5 deletions

View File

@ -43,6 +43,14 @@ using Timer = System.Timers.Timer;
namespace pCampBot namespace pCampBot
{ {
public enum ConnectionState
{
Disconnected,
Connecting,
Connected,
Disconnecting
}
public class Bot public class Bot
{ {
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
@ -86,7 +94,7 @@ namespace pCampBot
/// <summary> /// <summary>
/// Is this bot connected to the grid? /// Is this bot connected to the grid?
/// </summary> /// </summary>
public bool IsConnected { get; private set; } public ConnectionState ConnectionState { get; private set; }
public string FirstName { get; private set; } public string FirstName { get; private set; }
public string LastName { get; private set; } public string LastName { get; private set; }
@ -130,6 +138,8 @@ namespace pCampBot
BotManager bm, List<IBehaviour> behaviours, BotManager bm, List<IBehaviour> behaviours,
string firstName, string lastName, string password, string loginUri) string firstName, string lastName, string password, string loginUri)
{ {
ConnectionState = ConnectionState.Disconnected;
behaviours.ForEach(b => b.Initialize(this)); behaviours.ForEach(b => b.Initialize(this));
Client = new GridClient(); Client = new GridClient();
@ -178,6 +188,8 @@ namespace pCampBot
/// </summary> /// </summary>
public void shutdown() public void shutdown()
{ {
ConnectionState = ConnectionState.Disconnecting;
if (m_actionThread != null) if (m_actionThread != null)
m_actionThread.Abort(); m_actionThread.Abort();
@ -209,9 +221,11 @@ namespace pCampBot
Client.Network.Disconnected += this.Network_OnDisconnected; Client.Network.Disconnected += this.Network_OnDisconnected;
Client.Objects.ObjectUpdate += Objects_NewPrim; Client.Objects.ObjectUpdate += Objects_NewPrim;
ConnectionState = ConnectionState.Connecting;
if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", "Your name")) if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", "Your name"))
{ {
IsConnected = true; ConnectionState = ConnectionState.Connected;
Thread.Sleep(Random.Next(1000, 10000)); Thread.Sleep(Random.Next(1000, 10000));
m_actionThread = new Thread(Action); m_actionThread = new Thread(Action);
@ -241,6 +255,8 @@ namespace pCampBot
} }
else else
{ {
ConnectionState = ConnectionState.Disconnected;
m_log.ErrorFormat( m_log.ErrorFormat(
"{0} {1} cannot login: {2}", FirstName, LastName, Client.Network.LoginMessage); "{0} {1} cannot login: {2}", FirstName, LastName, Client.Network.LoginMessage);
@ -439,6 +455,8 @@ namespace pCampBot
public void Network_OnDisconnected(object sender, DisconnectedEventArgs args) public void Network_OnDisconnected(object sender, DisconnectedEventArgs args)
{ {
ConnectionState = ConnectionState.Disconnected;
m_log.DebugFormat( m_log.DebugFormat(
"[BOT]: Bot {0} disconnected reason {1}, message {2}", Name, args.Reason, args.Message); "[BOT]: Bot {0} disconnected reason {1}, message {2}", Name, args.Reason, args.Message);
@ -456,7 +474,6 @@ namespace pCampBot
&& OnDisconnected != null) && OnDisconnected != null)
// if (OnDisconnected != null) // if (OnDisconnected != null)
{ {
IsConnected = false;
OnDisconnected(this, EventType.DISCONNECTED); OnDisconnected(this, EventType.DISCONNECTED);
} }
} }

View File

@ -242,7 +242,7 @@ namespace pCampBot
lock (m_lBot) lock (m_lBot)
{ {
if (m_lBot.TrueForAll(b => !b.IsConnected)) if (m_lBot.TrueForAll(b => b.ConnectionState == ConnectionState.Disconnected))
Environment.Exit(0); Environment.Exit(0);
break; break;
@ -306,7 +306,7 @@ namespace pCampBot
MainConsole.Instance.OutputFormat( MainConsole.Instance.OutputFormat(
outputFormat, outputFormat,
pb.Name, currentSim != null ? currentSim.Name : "(none)", pb.IsConnected ? "Connected" : "Disconnected"); pb.Name, currentSim != null ? currentSim.Name : "(none)", pb.ConnectionState);
} }
} }
} }