123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Threading;
- using OpenMetaverse;
- using log4net;
- using log4net.Appender;
- using log4net.Core;
- using log4net.Repository;
- using Nini.Config;
- using OpenSim.Framework;
- using OpenSim.Framework.Console;
- using pCampBot.Interfaces;
- namespace pCampBot
- {
-
-
-
- public class BotManager
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- public const int DefaultLoginDelay = 5000;
-
-
-
-
- public int LoginDelay { get; set; }
-
-
-
- protected CommandConsole m_console;
-
-
-
- protected List<Bot> m_lBot;
-
-
-
- public Random Rng { get; private set; }
-
-
-
- public IConfig Config { get; private set; }
-
-
-
- public Dictionary<UUID, bool> AssetsReceived { get; private set; }
-
-
-
- public Dictionary<ulong, GridRegion> RegionsKnown { get; private set; }
-
-
-
- public BotManager()
- {
- LoginDelay = DefaultLoginDelay;
- Rng = new Random(Environment.TickCount);
- AssetsReceived = new Dictionary<UUID, bool>();
- RegionsKnown = new Dictionary<ulong, GridRegion>();
- m_console = CreateConsole();
- MainConsole.Instance = m_console;
-
-
- ILoggerRepository repository = LogManager.GetRepository();
- IAppender[] appenders = repository.GetAppenders();
- OpenSimAppender consoleAppender = null;
- foreach (IAppender appender in appenders)
- {
- if (appender.Name == "Console")
- {
- consoleAppender = (OpenSimAppender)appender;
- consoleAppender.Console = m_console;
- break;
- }
- }
- m_console.Commands.AddCommand("bot", false, "shutdown",
- "shutdown",
- "Shutdown bots and exit", HandleShutdown);
- m_console.Commands.AddCommand("bot", false, "quit",
- "quit",
- "Shutdown bots and exit",
- HandleShutdown);
- m_console.Commands.AddCommand("bot", false, "show regions",
- "show regions",
- "Show regions known to bots",
- HandleShowRegions);
- m_console.Commands.AddCommand("bot", false, "show bots",
- "show bots",
- "Shows the status of all bots",
- HandleShowStatus);
- m_lBot = new List<Bot>();
- }
-
-
-
-
-
- public void dobotStartup(int botcount, IConfig cs)
- {
- Config = cs;
- string firstName = cs.GetString("firstname");
- string lastNameStem = cs.GetString("lastname");
- string password = cs.GetString("password");
- string loginUri = cs.GetString("loginuri");
- HashSet<string> behaviourSwitches = new HashSet<string>();
- Array.ForEach<string>(
- cs.GetString("behaviours", "p").Split(new char[] { ',' }), b => behaviourSwitches.Add(b));
- MainConsole.Instance.OutputFormat(
- "[BOT MANAGER]: Starting {0} bots connecting to {1}, named {2} {3}_<n>",
- botcount,
- loginUri,
- firstName,
- lastNameStem);
- MainConsole.Instance.OutputFormat("[BOT MANAGER]: Delay between logins is {0}ms", LoginDelay);
- for (int i = 0; i < botcount; i++)
- {
- string lastName = string.Format("{0}_{1}", lastNameStem, i);
-
- List<IBehaviour> behaviours = new List<IBehaviour>();
-
-
- if (behaviourSwitches.Contains("p"))
- behaviours.Add(new PhysicsBehaviour());
-
- if (behaviourSwitches.Contains("g"))
- behaviours.Add(new GrabbingBehaviour());
-
- if (behaviourSwitches.Contains("t"))
- behaviours.Add(new TeleportBehaviour());
-
- if (behaviourSwitches.Contains("c"))
- behaviours.Add(new CrossBehaviour());
- StartBot(this, behaviours, firstName, lastName, password, loginUri);
- }
- }
-
-
-
-
-
-
-
-
-
- public void StartBot(
- BotManager bm, List<IBehaviour> behaviours,
- string firstName, string lastName, string password, string loginUri)
- {
- MainConsole.Instance.OutputFormat(
- "[BOT MANAGER]: Starting bot {0} {1}, behaviours are {2}",
- firstName, lastName, string.Join(",", behaviours.ConvertAll<string>(b => b.Name).ToArray()));
- Bot pb = new Bot(bm, behaviours, firstName, lastName, password, loginUri);
- pb.OnConnected += handlebotEvent;
- pb.OnDisconnected += handlebotEvent;
- lock (m_lBot)
- m_lBot.Add(pb);
- Thread pbThread = new Thread(pb.startup);
- pbThread.Name = pb.Name;
- pbThread.IsBackground = true;
- pbThread.Start();
-
- Thread.Sleep(LoginDelay);
- }
-
-
-
-
-
- private void handlebotEvent(Bot callbot, EventType eventt)
- {
- switch (eventt)
- {
- case EventType.CONNECTED:
- m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Connected");
- break;
- case EventType.DISCONNECTED:
- m_log.Info("[" + callbot.FirstName + " " + callbot.LastName + "]: Disconnected");
- lock (m_lBot)
- {
- if (m_lBot.TrueForAll(b => b.ConnectionState == ConnectionState.Disconnected))
- Environment.Exit(0);
- break;
- }
- }
- }
-
-
-
-
-
-
- public void doBotShutdown()
- {
- lock (m_lBot)
- {
- foreach (Bot bot in m_lBot)
- {
- Bot thisBot = bot;
- Util.FireAndForget(o => thisBot.shutdown());
- }
- }
- }
-
-
-
-
- protected CommandConsole CreateConsole()
- {
- return new LocalConsole("pCampbot");
- }
- private void HandleShutdown(string module, string[] cmd)
- {
- m_log.Info("[BOTMANAGER]: Shutting down bots");
- doBotShutdown();
- }
- private void HandleShowRegions(string module, string[] cmd)
- {
- string outputFormat = "{0,-30} {1, -20} {2, -5} {3, -5}";
- MainConsole.Instance.OutputFormat(outputFormat, "Name", "Handle", "X", "Y");
- lock (RegionsKnown)
- {
- foreach (GridRegion region in RegionsKnown.Values)
- {
- MainConsole.Instance.OutputFormat(
- outputFormat, region.Name, region.RegionHandle, region.X, region.Y);
- }
- }
- }
- private void HandleShowStatus(string module, string[] cmd)
- {
- string outputFormat = "{0,-30} {1, -30} {2,-14}";
- MainConsole.Instance.OutputFormat(outputFormat, "Name", "Region", "Status");
- lock (m_lBot)
- {
- foreach (Bot pb in m_lBot)
- {
- Simulator currentSim = pb.Client.Network.CurrentSim;
- MainConsole.Instance.OutputFormat(
- outputFormat,
- pb.Name, currentSim != null ? currentSim.Name : "(none)", pb.ConnectionState);
- }
- }
- }
-
- internal void Grid_GridRegion(object o, GridRegionEventArgs args)
- {
- lock (RegionsKnown)
- {
- GridRegion newRegion = args.Region;
- if (RegionsKnown.ContainsKey(newRegion.RegionHandle))
- {
- return;
- }
- else
- {
- m_log.DebugFormat(
- "[BOT MANAGER]: Adding {0} {1} to known regions", newRegion.Name, newRegion.RegionHandle);
- RegionsKnown[newRegion.RegionHandle] = newRegion;
- }
- }
- }
- }
- }
|