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