BotManager.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.Text;
  30. using System.IO;
  31. using libsecondlife;
  32. using libsecondlife.Packets;
  33. using Nini.Config;
  34. using System.Threading;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. namespace OpenSim.TestSuite
  38. {
  39. /// <summary>
  40. /// Thread/Bot manager for the application
  41. /// </summary>
  42. public class BotManager : conscmd_callback
  43. {
  44. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  45. protected ConsoleBase m_console;
  46. protected List<PhysicsBot> m_lBot;
  47. protected Thread[] m_td;
  48. protected bool m_verbose = true;
  49. protected Random somthing = new Random(System.Environment.TickCount);
  50. protected int numbots = 0;
  51. protected IConfig Previous_config;
  52. /// <summary>
  53. /// Constructor Creates MainConsole.Instance to take commands and provide the place to write data
  54. /// </summary>
  55. public BotManager()
  56. {
  57. m_log.Info("In bot manager");
  58. // m_console = CreateConsole();
  59. // MainConsole.Instance = m_console;
  60. m_lBot = new List<PhysicsBot>();
  61. }
  62. /// <summary>
  63. /// Startup number of bots specified in the starting arguments
  64. /// </summary>
  65. /// <param name="botcount">How many bots to start up</param>
  66. /// <param name="cs">The configuration for the bots to use</param>
  67. public void dobotStartup(int botcount, IConfig cs)
  68. {
  69. Previous_config = cs;
  70. m_td = new Thread[botcount];
  71. for (int i = 0; i < botcount; i++)
  72. {
  73. startupBot(i, cs);
  74. }
  75. }
  76. /// <summary>
  77. /// Add additional bots (and threads) to our bot pool
  78. /// </summary>
  79. /// <param name="botcount">How Many of them to add</param>
  80. public void addbots(int botcount)
  81. {
  82. int len = m_td.Length;
  83. Thread[] m_td2 = new Thread[len + botcount];
  84. for (int i = 0; i < len; i++)
  85. {
  86. m_td2[i] = m_td[i];
  87. }
  88. m_td = m_td2;
  89. int newlen = len + botcount;
  90. for (int i = len; i < newlen; i++)
  91. {
  92. startupBot(i, Previous_config);
  93. }
  94. }
  95. /// <summary>
  96. /// This starts up the bot and stores the thread for the bot in the thread array
  97. /// </summary>
  98. /// <param name="pos">The position in the thread array to stick the bot's thread</param>
  99. /// <param name="cs">Configuration of the bot</param>
  100. public void startupBot(int pos, IConfig cs)
  101. {
  102. PhysicsBot pb = new PhysicsBot(cs);
  103. pb.OnConnected += handlebotEvent;
  104. pb.OnDisconnected += handlebotEvent;
  105. if (cs.GetString("firstname", "random") == "random") pb.firstname = CreateRandomName();
  106. if (cs.GetString("lastname", "random") == "random") pb.lastname = CreateRandomName();
  107. m_td[pos] = new Thread(pb.startup);
  108. m_td[pos].Name = "CampBot_" + pos;
  109. m_td[pos].IsBackground = true;
  110. m_td[pos].Start();
  111. m_lBot.Add(pb);
  112. OpenSim.Framework.ThreadTracker.Add(m_td[pos]);
  113. }
  114. /// <summary>
  115. /// Creates a random name for the bot
  116. /// </summary>
  117. /// <returns></returns>
  118. private string CreateRandomName()
  119. {
  120. string returnstring = "";
  121. string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  122. for (int i = 0; i < 7; i++)
  123. {
  124. returnstring += chars.Substring(somthing.Next(chars.Length),1);
  125. }
  126. return returnstring;
  127. }
  128. /// <summary>
  129. /// High level connnected/disconnected events so we can keep track of our threads by proxy
  130. /// </summary>
  131. /// <param name="callbot"></param>
  132. /// <param name="eventt"></param>
  133. public void handlebotEvent(PhysicsBot callbot, EventType eventt)
  134. {
  135. switch (eventt)
  136. {
  137. case EventType.CONNECTED:
  138. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Connected");
  139. numbots++;
  140. break;
  141. case EventType.DISCONNECTED:
  142. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Disconnected");
  143. m_td[m_lBot.IndexOf(callbot)].Abort();
  144. numbots--;
  145. if (numbots >1)
  146. Environment.Exit(0);
  147. break;
  148. }
  149. }
  150. /// <summary>
  151. /// Shutting down all bots
  152. /// </summary>
  153. public void doBotShutdown()
  154. {
  155. foreach (PhysicsBot pb in m_lBot)
  156. {
  157. pb.shutdown();
  158. }
  159. }
  160. /// <summary>
  161. /// Standard CreateConsole routine
  162. /// </summary>
  163. /// <returns></returns>
  164. protected ConsoleBase CreateConsole()
  165. {
  166. return new ConsoleBase("Region", this);
  167. }
  168. /// <summary>
  169. /// I don't think the bots use this..
  170. /// </summary>
  171. /// <param name="commandParams"></param>
  172. /// <param name="pos"></param>
  173. /// <returns></returns>
  174. // TODO: unused: private string CombineParams(string[] commandParams, int pos)
  175. // TODO: unused: {
  176. // TODO: unused: string result = String.Empty;
  177. // TODO: unused: for (int i = pos; i < commandParams.Length; i++)
  178. // TODO: unused: {
  179. // TODO: unused: result += commandParams[i] + " ";
  180. // TODO: unused: }
  181. // TODO: unused: result = result.TrimEnd(' ');
  182. // TODO: unused: return result;
  183. // TODO: unused: }
  184. /// <summary>
  185. /// Command runnint tool.. Currently use it to add bots, shutdown and (dangerous)Forcequit
  186. /// </summary>
  187. /// <param name="command"></param>
  188. /// <param name="cmdparams"></param>
  189. public void RunCmd(string command, string[] cmdparams)
  190. {
  191. switch (command)
  192. {
  193. case "shutdown":
  194. m_console.Warn("BOTMANAGER", "Shutting down bots");
  195. doBotShutdown();
  196. break;
  197. case "quit":
  198. 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");
  199. Environment.Exit(0);
  200. break;
  201. case "addbots":
  202. int newbots = 0;
  203. Helpers.TryParse(cmdparams[0], out newbots);
  204. if (newbots > 0)
  205. addbots(newbots);
  206. break;
  207. case "help":
  208. 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");
  209. break;
  210. }
  211. }
  212. /// <summary>
  213. /// Required method to implement the conscmd_callback interface
  214. /// </summary>
  215. /// <param name="ShowWhat"></param>
  216. public void Show(string ShowWhat)
  217. {
  218. }
  219. }
  220. }