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