BotManager.cs 8.4 KB

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