BotManager.cs 8.1 KB

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