BotManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using OpenMetaverse;
  32. using log4net;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. namespace OpenSim.TestSuite
  37. {
  38. /// <summary>
  39. /// Thread/Bot manager for the application
  40. /// </summary>
  41. public class BotManager
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. protected CommandConsole m_console;
  45. protected List<PhysicsBot> m_lBot;
  46. protected Thread[] m_td;
  47. protected bool m_verbose = true;
  48. protected Random somthing = new Random(Environment.TickCount);
  49. protected int numbots = 0;
  50. protected IConfig Previous_config;
  51. /// <summary>
  52. /// Constructor Creates MainConsole.Instance to take commands and provide the place to write data
  53. /// </summary>
  54. public BotManager()
  55. {
  56. m_log.Info("In bot manager");
  57. m_lBot = new List<PhysicsBot>();
  58. }
  59. /// <summary>
  60. /// Startup number of bots specified in the starting arguments
  61. /// </summary>
  62. /// <param name="botcount">How many bots to start up</param>
  63. /// <param name="cs">The configuration for the bots to use</param>
  64. public void dobotStartup(int botcount, IConfig cs)
  65. {
  66. Previous_config = cs;
  67. m_td = new Thread[botcount];
  68. for (int i = 0; i < botcount; i++)
  69. {
  70. startupBot(i, cs);
  71. }
  72. }
  73. /// <summary>
  74. /// Add additional bots (and threads) to our bot pool
  75. /// </summary>
  76. /// <param name="botcount">How Many of them to add</param>
  77. public void addbots(int botcount)
  78. {
  79. int len = m_td.Length;
  80. Thread[] m_td2 = new Thread[len + botcount];
  81. for (int i = 0; i < len; i++)
  82. {
  83. m_td2[i] = m_td[i];
  84. }
  85. m_td = m_td2;
  86. int newlen = len + botcount;
  87. for (int i = len; i < newlen; i++)
  88. {
  89. startupBot(i, Previous_config);
  90. }
  91. }
  92. /// <summary>
  93. /// This starts up the bot and stores the thread for the bot in the thread array
  94. /// </summary>
  95. /// <param name="pos">The position in the thread array to stick the bot's thread</param>
  96. /// <param name="cs">Configuration of the bot</param>
  97. public void startupBot(int pos, IConfig cs)
  98. {
  99. PhysicsBot pb = new PhysicsBot(cs);
  100. pb.OnConnected += handlebotEvent;
  101. pb.OnDisconnected += handlebotEvent;
  102. if (cs.GetString("firstname", "random") == "random") pb.firstname = CreateRandomName();
  103. if (cs.GetString("lastname", "random") == "random") pb.lastname = CreateRandomName();
  104. m_td[pos] = new Thread(pb.startup);
  105. m_td[pos].Name = "CampBot_" + pos;
  106. m_td[pos].IsBackground = true;
  107. m_td[pos].Start();
  108. m_lBot.Add(pb);
  109. }
  110. /// <summary>
  111. /// Creates a random name for the bot
  112. /// </summary>
  113. /// <returns></returns>
  114. private string CreateRandomName()
  115. {
  116. string returnstring = "";
  117. string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  118. for (int i = 0; i < 7; i++)
  119. {
  120. returnstring += chars.Substring(somthing.Next(chars.Length),1);
  121. }
  122. return returnstring;
  123. }
  124. /// <summary>
  125. /// High level connnected/disconnected events so we can keep track of our threads by proxy
  126. /// </summary>
  127. /// <param name="callbot"></param>
  128. /// <param name="eventt"></param>
  129. public void handlebotEvent(PhysicsBot callbot, EventType eventt)
  130. {
  131. switch (eventt)
  132. {
  133. case EventType.CONNECTED:
  134. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Connected");
  135. numbots++;
  136. break;
  137. case EventType.DISCONNECTED:
  138. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Disconnected");
  139. m_td[m_lBot.IndexOf(callbot)].Abort();
  140. numbots--;
  141. if (numbots > 1)
  142. Environment.Exit(0);
  143. break;
  144. }
  145. }
  146. /// <summary>
  147. /// Shutting down all bots
  148. /// </summary>
  149. public void doBotShutdown()
  150. {
  151. foreach (PhysicsBot pb in m_lBot)
  152. {
  153. pb.shutdown();
  154. }
  155. }
  156. }
  157. }