BotManager.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 pCampBot
  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_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. OpenSim.Framework.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. /// I don't think the bots use this..
  169. /// </summary>
  170. /// <param name="commandParams"></param>
  171. /// <param name="pos"></param>
  172. /// <returns></returns>
  173. // TODO: unused: private string CombineParams(string[] commandParams, int pos)
  174. // TODO: unused: {
  175. // TODO: unused: string result = String.Empty;
  176. // TODO: unused: for (int i = pos; i < commandParams.Length; i++)
  177. // TODO: unused: {
  178. // TODO: unused: result += commandParams[i] + " ";
  179. // TODO: unused: }
  180. // TODO: unused: result = result.TrimEnd(' ');
  181. // TODO: unused: return result;
  182. // TODO: unused: }
  183. /// <summary>
  184. /// Command runnint tool.. Currently use it to add bots, shutdown and (dangerous)Forcequit
  185. /// </summary>
  186. /// <param name="command"></param>
  187. /// <param name="cmdparams"></param>
  188. public void RunCmd(string command, string[] cmdparams)
  189. {
  190. switch (command)
  191. {
  192. case "shutdown":
  193. m_console.Warn("BOTMANAGER", "Shutting down bots");
  194. doBotShutdown();
  195. break;
  196. case "quit":
  197. 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");
  198. Environment.Exit(0);
  199. break;
  200. case "addbots":
  201. int newbots = 0;
  202. Helpers.TryParse(cmdparams[0], out newbots);
  203. if (newbots > 0)
  204. addbots(newbots);
  205. break;
  206. case "help":
  207. 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");
  208. break;
  209. }
  210. }
  211. /// <summary>
  212. /// Required method to implement the conscmd_callback interface
  213. /// </summary>
  214. /// <param name="ShowWhat"></param>
  215. public void Show(string ShowWhat)
  216. {
  217. }
  218. }
  219. }