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