BotManager.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using System.IO;
  32. using libsecondlife;
  33. using libsecondlife.Packets;
  34. using Nini.Config;
  35. using System.Threading;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Console;
  38. namespace pCampBot
  39. {
  40. /// <summary>
  41. /// Thread/Bot manager for the application
  42. /// </summary>
  43. public class BotManager : conscmd_callback
  44. {
  45. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  46. protected ConsoleBase m_console;
  47. protected List<PhysicsBot> m_lBot;
  48. protected Thread[] m_td;
  49. protected bool m_verbose = true;
  50. protected Random somthing = new Random(System.Environment.TickCount);
  51. protected int numbots = 0;
  52. protected IConfig Previous_config;
  53. /// <summary>
  54. /// Constructor Creates MainConsole.Instance to take commands and provide the place to write data
  55. /// </summary>
  56. public BotManager()
  57. {
  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].Start();
  109. m_lBot.Add(pb);
  110. }
  111. /// <summary>
  112. /// Creates a random name for the bot
  113. /// </summary>
  114. /// <returns></returns>
  115. private string CreateRandomName()
  116. {
  117. string returnstring = "";
  118. string chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  119. for (int i = 0; i < 7; i++)
  120. {
  121. returnstring += chars.Substring(somthing.Next(chars.Length),1);
  122. }
  123. return returnstring;
  124. }
  125. /// <summary>
  126. /// High level connnected/disconnected events so we can keep track of our threads by proxy
  127. /// </summary>
  128. /// <param name="callbot"></param>
  129. /// <param name="eventt"></param>
  130. public void handlebotEvent(PhysicsBot callbot, EventType eventt)
  131. {
  132. switch (eventt)
  133. {
  134. case EventType.CONNECTED:
  135. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Connected");
  136. numbots++;
  137. break;
  138. case EventType.DISCONNECTED:
  139. m_log.Info("[ " + callbot.firstname + " " + callbot.lastname + "]: Disconnected");
  140. m_td[m_lBot.IndexOf(callbot)].Abort();
  141. numbots--;
  142. if (numbots >1)
  143. Environment.Exit(0);
  144. break;
  145. }
  146. }
  147. /// <summary>
  148. /// Shutting down all bots
  149. /// </summary>
  150. public void doBotShutdown()
  151. {
  152. foreach (PhysicsBot pb in m_lBot)
  153. {
  154. pb.shutdown();
  155. }
  156. }
  157. /// <summary>
  158. /// Standard CreateConsole routine
  159. /// </summary>
  160. /// <returns></returns>
  161. protected ConsoleBase CreateConsole()
  162. {
  163. return new ConsoleBase("Region", this);
  164. }
  165. /// <summary>
  166. /// I don't think the bots use this..
  167. /// </summary>
  168. /// <param name="commandParams"></param>
  169. /// <param name="pos"></param>
  170. /// <returns></returns>
  171. private string CombineParams(string[] commandParams, int pos)
  172. {
  173. string result = String.Empty;
  174. for (int i = pos; i < commandParams.Length; i++)
  175. {
  176. result += commandParams[i] + " ";
  177. }
  178. result = result.TrimEnd(' ');
  179. return result;
  180. }
  181. /// <summary>
  182. /// Command runnint tool.. Currently use it to add bots, shutdown and (dangerous)Forcequit
  183. /// </summary>
  184. /// <param name="command"></param>
  185. /// <param name="cmdparams"></param>
  186. public void RunCmd(string command, string[] cmdparams)
  187. {
  188. switch (command)
  189. {
  190. case "shutdown":
  191. m_console.Warn("BOTMANAGER", "Shutting down bots");
  192. doBotShutdown();
  193. break;
  194. case "quit":
  195. 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");
  196. Environment.Exit(0);
  197. break;
  198. case "addbots":
  199. int newbots = 0;
  200. Helpers.TryParse(cmdparams[0], out newbots);
  201. if (newbots > 0)
  202. addbots(newbots);
  203. break;
  204. case "help":
  205. 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");
  206. break;
  207. }
  208. }
  209. /// <summary>
  210. /// Required method to implement the conscmd_callback interface
  211. /// </summary>
  212. /// <param name="ShowWhat"></param>
  213. public void Show(string ShowWhat)
  214. {
  215. }
  216. }
  217. }