BotManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 OpenSim 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 libsecondlife;
  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 : conscmd_callback
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. protected ConsoleBase 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_console = CreateConsole();
  58. // MainConsole.Instance = m_console;
  59. m_lBot = new List<PhysicsBot>();
  60. }
  61. /// <summary>
  62. /// Startup number of bots specified in the starting arguments
  63. /// </summary>
  64. /// <param name="botcount">How many bots to start up</param>
  65. /// <param name="cs">The configuration for the bots to use</param>
  66. public void dobotStartup(int botcount, IConfig cs)
  67. {
  68. Previous_config = cs;
  69. m_td = new Thread[botcount];
  70. for (int i = 0; i < botcount; i++)
  71. {
  72. startupBot(i, cs);
  73. }
  74. }
  75. /// <summary>
  76. /// Add additional bots (and threads) to our bot pool
  77. /// </summary>
  78. /// <param name="botcount">How Many of them to add</param>
  79. public void addbots(int botcount)
  80. {
  81. int len = m_td.Length;
  82. Thread[] m_td2 = new Thread[len + botcount];
  83. for (int i = 0; i < len; i++)
  84. {
  85. m_td2[i] = m_td[i];
  86. }
  87. m_td = m_td2;
  88. int newlen = len + botcount;
  89. for (int i = len; i < newlen; i++)
  90. {
  91. startupBot(i, Previous_config);
  92. }
  93. }
  94. /// <summary>
  95. /// This starts up the bot and stores the thread for the bot in the thread array
  96. /// </summary>
  97. /// <param name="pos">The position in the thread array to stick the bot's thread</param>
  98. /// <param name="cs">Configuration of the bot</param>
  99. public void startupBot(int pos, IConfig cs)
  100. {
  101. PhysicsBot pb = new PhysicsBot(cs);
  102. pb.OnConnected += handlebotEvent;
  103. pb.OnDisconnected += handlebotEvent;
  104. if (cs.GetString("firstname", "random") == "random") pb.firstname = CreateRandomName();
  105. if (cs.GetString("lastname", "random") == "random") pb.lastname = CreateRandomName();
  106. m_td[pos] = new Thread(pb.startup);
  107. m_td[pos].Name = "CampBot_" + pos;
  108. m_td[pos].IsBackground = true;
  109. m_td[pos].Start();
  110. m_lBot.Add(pb);
  111. ThreadTracker.Add(m_td[pos]);
  112. }
  113. /// <summary>
  114. /// Creates a random name for the bot
  115. /// </summary>
  116. /// <returns></returns>
  117. private string CreateRandomName()
  118. {
  119. string returnstring = "";
  120. string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  121. for (int i = 0; i < 7; i++)
  122. {
  123. returnstring += chars.Substring(somthing.Next(chars.Length),1);
  124. }
  125. return returnstring;
  126. }
  127. /// <summary>
  128. /// High level connnected/disconnected events so we can keep track of our threads by proxy
  129. /// </summary>
  130. /// <param name="callbot"></param>
  131. /// <param name="eventt"></param>
  132. public void handlebotEvent(PhysicsBot callbot, EventType eventt)
  133. {
  134. switch (eventt)
  135. {
  136. case EventType.CONNECTED:
  137. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Connected");
  138. numbots++;
  139. break;
  140. case EventType.DISCONNECTED:
  141. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Disconnected");
  142. m_td[m_lBot.IndexOf(callbot)].Abort();
  143. numbots--;
  144. if (numbots > 1)
  145. Environment.Exit(0);
  146. break;
  147. }
  148. }
  149. /// <summary>
  150. /// Shutting down all bots
  151. /// </summary>
  152. public void doBotShutdown()
  153. {
  154. foreach (PhysicsBot pb in m_lBot)
  155. {
  156. pb.shutdown();
  157. }
  158. }
  159. /// <summary>
  160. /// Standard CreateConsole routine
  161. /// </summary>
  162. /// <returns></returns>
  163. protected ConsoleBase CreateConsole()
  164. {
  165. return new ConsoleBase("Region", this);
  166. }
  167. /// <summary>
  168. /// Command runnint tool.. Currently use it to add bots, shutdown and (dangerous)Forcequit
  169. /// </summary>
  170. /// <param name="command"></param>
  171. /// <param name="cmdparams"></param>
  172. public void RunCmd(string command, string[] cmdparams)
  173. {
  174. switch (command)
  175. {
  176. case "shutdown":
  177. m_console.Warn("BOTMANAGER", "Shutting down bots");
  178. doBotShutdown();
  179. break;
  180. case "quit":
  181. m_console.Warn("DANGER", "This should only be used to quit the program if you've already used the shutdown command and the program hasn't quit");
  182. Environment.Exit(0);
  183. break;
  184. case "addbots":
  185. int newbots;
  186. Helpers.TryParse(cmdparams[0], out newbots);
  187. if (newbots > 0)
  188. addbots(newbots);
  189. break;
  190. case "help":
  191. m_console.Notice("HELP", "\nshutdown - graceful shutdown\naddbots <n> - adds n bots to the test\nquit - forcequits, dangerous if you have not already run shutdown");
  192. break;
  193. }
  194. }
  195. /// <summary>
  196. /// Required method to implement the conscmd_callback interface
  197. /// </summary>
  198. /// <param name="ShowWhat"></param>
  199. public void Show(string ShowWhat)
  200. {
  201. }
  202. }
  203. }