/*
  * Starts the server processes.  This function scans for all implementations of the Site interface.
  * Following that, it then creates the required Tcp Listeners for the sites found.
  * One all listeners have been created and the sites loaded into them appropriately,
  * start all listeners which starts all sites.  Following that, start the background runner 
  * thread to handle all cron jobs that run in the background.
  */
 public static void Start()
 {
     Monitor.Enter(_lock);
     if (_started)
         throw new Exception(Messages.Current["Org.Reddragonit.EmbeddedWebServer.ServerControl.Errors.ServerStarted"]);
     else
     {
         MT19937 _rand = new MT19937(DateTime.Now.Ticks);
         _listeners = new List<PortListener>();
         foreach (Type t in Utility.LocateTypeInstances(typeof(Site)))
         {
             Site s = (Site)t.GetConstructor(Type.EmptyTypes).Invoke(new object[] { });
             s.ID = SessionManager.GenerateSessionID();
             foreach (sIPPortPair ipp in s.ListenOn)
             {
                 bool add = true;
                 foreach (PortListener pt in _listeners)
                 {
                     if (pt.Port == ipp.Port)
                     {
                         pt.AttachSite(s,ipp);
                         add = false;
                     }
                 }
                 if (add)
                     _listeners.Add(new PortListener(s,ipp));
             }
         }
         foreach (PortListener pt in _listeners)
             pt.Start();
         _backgroundRunner = new BackgroundOperationRunner();
         _backgroundRunner.Start();
         _started = true;
     }
     Monitor.Exit(_lock);
 }
 static EventController()
 {
     _rand = new MT19937(DateTime.Now.Ticks);
     _handlers = new List<IEventHandler>();
 }
 public BackgroundOperationRunner() {
     _preCalls = new List<ServerControl.delPreBackgroundCall>();
     _postCalls = new List<ServerControl.delPostBackgroundCall>();
     _rand = new MT19937(DateTime.Now.Ticks);
     _calls = new List<sCall>();
     Logger.LogMessage(DiagnosticsLevels.TRACE, "Constructing list of background operation calls");
     foreach (Type t in Utility.LocateTypeInstances(typeof(IBackgroundOperationContainer)))
     {
         foreach (MethodInfo mi in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
         {
             Logger.LogMessage(DiagnosticsLevels.TRACE, "Checking method " + mi.Name + " from type " + t.FullName + " for background tags");
             foreach (BackgroundOperationCall boc in mi.GetCustomAttributes(typeof(BackgroundOperationCall), false))
                 _calls.Add(new sCall(t, boc, mi));
         }
     }
     Logger.LogMessage(DiagnosticsLevels.TRACE, "Background caller ready with " + _calls.Count.ToString() + " calls available");
 }