myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myserver-commit] [2804] Code refactoring.


From: Giuseppe Scrivano
Subject: [myserver-commit] [2804] Code refactoring.
Date: Fri, 12 Sep 2008 23:38:39 +0000

Revision: 2804
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2804
Author:   gscrivano
Date:     2008-09-12 23:38:39 +0000 (Fri, 12 Sep 2008)

Log Message:
-----------
Code refactoring.  Extracted new methods from Server::start.

Modified Paths:
--------------
    trunk/myserver/include/server/server.h
    trunk/myserver/src/server/server.cpp

Modified: trunk/myserver/include/server/server.h
===================================================================
--- trunk/myserver/include/server/server.h      2008-09-12 23:36:00 UTC (rev 
2803)
+++ trunk/myserver/include/server/server.h      2008-09-12 23:38:39 UTC (rev 
2804)
@@ -172,6 +172,10 @@
        /*! Do not allow to create directly objects.  */
        Server();
 
+  void mainLoop();
+  void loadPlugins();
+  void displayBoot();
+
        CachedFileFactory cachedFiles;
 
        void *envString;
@@ -207,7 +211,7 @@
        string* path;
   string* externalPath;
        string* serverAdmin;
-       int initialize(int);
+       int initialize();
   int addThread(int staticThread = 0);
        ConnectionPtr addConnectionToList(Socket* s, MYSERVER_SOCKADDRIN 
*asock_in,
                                     char *ipAddr, char *localIpAddr,

Modified: trunk/myserver/src/server/server.cpp
===================================================================
--- trunk/myserver/src/server/server.cpp        2008-09-12 23:36:00 UTC (rev 
2803)
+++ trunk/myserver/src/server/server.cpp        2008-09-12 23:38:39 UTC (rev 
2804)
@@ -115,14 +115,8 @@
 void Server::start()
 {
   u_long i;
-  u_long configsCheck = 0;
-  u_long purgeThreadsCounter = 0;
-  time_t mainConfTime;
-  time_t hostsConfTime;
-  time_t mimeConf;
   string buffer;
   int err = 0;
-  int osVer = getOSVersion();
   int ret;
   ostringstream nCPU;
   string strCPU;
@@ -133,69 +127,8 @@
   INPUT_RECORD irInBuf[128];
 #endif
 
-#ifdef CLEAR_BOOT_SCREEN
+  displayBoot();
 
-  if(logManager.getType() == LogManager::TYPE_CONSOLE )
-  {
-#ifdef WIN32
-
-    /*
-     *Under the windows platform use the cls operating-system
-     *command to clear the screen.
-     */
-    _flushall();
-    system("cls");
-#endif
-#ifdef NOT_WIN
-
-  /*
-   *Under an UNIX environment, clearing the screen
-   *can be done in a similar method
-   */
-    sync();
-    system("clear");
-#endif
-  }
-
-#endif /* CLEAR_BOOT_SCREEN.  */
-
-  /*
-   *Print the MyServer signature only if the log writes to the console.
-   */
-  if(logManager.getType() == LogManager::TYPE_CONSOLE )
-  {
-    try
-    {
-      string softwareSignature;
-      softwareSignature.assign("************");
-      softwareSignature.append(MYSERVER_VERSION);
-      softwareSignature.append("************");
-
-      i = softwareSignature.length();
-      while(i--)
-        logManager.write("*");
-      logManager.writeln("");
-      logManager.write(softwareSignature.c_str());
-      logManager.write("\n");
-      i = softwareSignature.length();
-      while(i--)
-        logManager.write("*");
-      logManager.writeln("");
-    }
-    catch(exception& e)
-    {
-      ostringstream err;
-      err << "Error: " << e.what();
-      logManager.write(err.str().c_str());
-      logManager.write("\n");
-      return;
-    }
-    catch(...)
-    {
-      return;
-    };
-  }
-
   try
   {
     setcwdBuffer();
@@ -209,9 +142,8 @@
      */
     logWriteln("Initializing server configuration...");
     err = 0;
-    osVer = getOSVersion();
 
-    err = initialize(osVer);
+    err = initialize();
     if(err)
       return;
 
@@ -271,15 +203,6 @@
       logWriteln(msg.c_str());
     }
 
-    if(filtersFactory.insert("gzip", Gzip::factory))
-    {
-      ostringstream stream;
-      stream <<  languageParser.getValue("ERR_ERROR") << ": Gzip Filter";
-      logPreparePrintError();
-      logWriteln(stream.str().c_str());
-      logEndPrintError();
-    }
-
     /* Load the MIME types.  */
     logWriteln(languageParser.getValue("MSG_LOADMIME"));
     if(mimeManager)
@@ -325,40 +248,9 @@
 
     /* Load the home directories configuration.  */
     homeDir.load();
+    
+    loadPlugins();
 
-
-    {
-      Protocol *protocolsSet[] = {new HttpProtocol(),
-                                  new HttpsProtocol(),
-                                  new FtpProtocol(),
-                                  new ControlProtocol(),
-                                  0};
-
-      for (int j = 0; protocolsSet[j]; j++)
-      {
-        char protocolName[32];
-        Protocol *protocol = protocolsSet[j];
-        protocol->loadProtocol(&languageParser);
-        protocol->registerName(protocolName, 32);
-        
-        getProtocolsManager()->addProtocol(protocolName, protocol);
-      }
-    }
-
-    getPluginsManager()->addNamespace(&executors);
-    getPluginsManager()->addNamespace(&protocols);
-    getPluginsManager()->addNamespace(&filters);
-    getPluginsManager()->addNamespace(&genericPluginsManager);
-
-
-    {
-      string res("plugins");
-      getPluginsManager()->preLoad(this, &languageParser, res);
-      getPluginsManager()->load(this, &languageParser, res);
-      getPluginsManager()->postLoad(this, &languageParser);
-    }
-
-
     /* Load the virtual hosts configuration from the xml file.  */
     vhostList->loadXMLConfigurationFile(vhostConfigurationFile->c_str(),
                                         getMaxLogFileSize());
@@ -401,168 +293,292 @@
 
     if(logManager.getType() == LogManager::TYPE_CONSOLE)
       logWriteln(languageParser.getValue("MSG_BREAK"));
+ 
+    serverReady = 1;
 
-    mainConfTime = 
-      FilesUtility::getLastModTime(mainConfigurationFile->c_str());
-    hostsConfTime = 
-      FilesUtility::getLastModTime(vhostConfigurationFile->c_str());
-    mimeConf = 
-      FilesUtility::getLastModTime(mimeConfigurationFile->c_str());
+    /* Finally we can give control to the main loop.  */
+    mainLoop();
 
- 
-    serverReady = 1;
- 
-    /*
-     *Keep thread alive.
-     *When the endServer flag is set to True exit
-     *from the loop and terminate the server execution.
-     */
-    while(!endServer)
+  }
+  catch(bad_alloc &ba)
+  {
+    ostringstream s;
+    s << "Bad alloc: " << ba.what();
+    logWriteln(s.str().c_str());
+  }
+  catch(exception &e)
+  {
+    ostringstream s;
+    s << "Error: " << e.what();
+    logWriteln(s.str().c_str());
+  };
+  this->terminate();
+  finalCleanup();
+#ifdef WIN32
+  WSACleanup();
+#endif// WIN32
+}
+
+/*!
+ *Load the plugins.
+ */
+void Server::loadPlugins()
+{
+  if(filtersFactory.insert("gzip", Gzip::factory))
+  {
+    ostringstream stream;
+    stream <<  languageParser.getValue("ERR_ERROR") << ": Gzip Filter";
+    logPreparePrintError();
+    logWriteln(stream.str().c_str());
+    logEndPrintError();
+  }
+
+  Protocol *protocolsSet[] = {new HttpProtocol(),
+                              new HttpsProtocol(),
+                              new FtpProtocol(),
+                              new ControlProtocol(),
+                              0};
+  
+  for (int j = 0; protocolsSet[j]; j++)
+  {
+    char protocolName[32];
+    Protocol *protocol = protocolsSet[j];
+    protocol->loadProtocol(&languageParser);
+    protocol->registerName(protocolName, 32);
+    getProtocolsManager()->addProtocol(protocolName, protocol);
+  }
+  
+
+  getPluginsManager()->addNamespace(&executors);
+  getPluginsManager()->addNamespace(&protocols);
+  getPluginsManager()->addNamespace(&filters);
+  getPluginsManager()->addNamespace(&genericPluginsManager);
+  
+  getPluginsManager()->preLoad(this, &languageParser, *externalPath);
+  getPluginsManager()->load(this, &languageParser, *externalPath);
+  getPluginsManager()->postLoad(this, &languageParser);
+}
+
+/*!
+ *Server main loop.
+ */
+void Server::mainLoop()
+{
+  time_t mainConfTime;
+  time_t hostsConfTime;
+  time_t mimeConfTime;
+
+  u_long configsCheck = 0;
+  u_long purgeThreadsCounter = 0;
+
+  mainConfTime = 
+    FilesUtility::getLastModTime(mainConfigurationFile->c_str());
+  hostsConfTime = 
+    FilesUtility::getLastModTime(vhostConfigurationFile->c_str());
+  mimeConfTime = 
+    FilesUtility::getLastModTime(mimeConfigurationFile->c_str());
+  
+  /*
+   *Keep thread alive.
+   *When the endServer flag is set to True exit
+   *from the loop and terminate the server execution.
+   */
+  while(!endServer)
+  {
+    Thread::wait(100000);
+
+    /* Check threads.  */
+    if(purgeThreadsCounter++ >= 100)
     {
-      Thread::wait(100000);
+      purgeThreadsCounter = 0;
+      purgeThreads();
+    }
 
-      /* Check threads.  */
-      if(purgeThreadsCounter++ >= 100)
+    if(autoRebootEnabled)
+    {
+      configsCheck++;
+      /* Do not check for modified configuration files every cycle.  */
+      if(configsCheck > 10)
       {
-        purgeThreadsCounter = 0;
-        purgeThreads();
-      }
+        time_t mainConfTimeNow =
+          FilesUtility::getLastModTime(mainConfigurationFile->c_str());
+        time_t hostsConfTimeNow =
+          FilesUtility::getLastModTime(vhostConfigurationFile->c_str());
+        time_t mimeConfNow =
+          FilesUtility::getLastModTime(mimeConfigurationFile->c_str());
 
-      if(autoRebootEnabled)
-      {
-        configsCheck++;
-        /* Do not check for modified configuration files every cycle.  */
-        if(configsCheck > 10)
+        /* If a configuration file was modified reboot the server. */
+        if(((mainConfTimeNow!=-1) && (hostsConfTimeNow!=-1)  &&
+            (mimeConfNow!=-1)) || toReboot)
         {
-          time_t mainConfTimeNow =
-            FilesUtility::getLastModTime(mainConfigurationFile->c_str());
-          time_t hostsConfTimeNow =
-            FilesUtility::getLastModTime(vhostConfigurationFile->c_str());
-          time_t mimeConfNow =
-            FilesUtility::getLastModTime(mimeConfigurationFile->c_str());
-
-          /* If a configuration file was modified reboot the server. */
-          if(((mainConfTimeNow!=-1) && (hostsConfTimeNow!=-1)  &&
-              (mimeConfNow!=-1)) || toReboot)
+          if( (mainConfTimeNow  != mainConfTime) || toReboot)
           {
-            if( (mainConfTimeNow  != mainConfTime) || toReboot)
+            string msg("main-conf-changed");
+            notifyMulticast(msg, 0);
+            
+            reboot();
+            /* Store new mtime values.  */
+            mainConfTime = mainConfTimeNow;
+            mimeConfTime = mimeConfNow;
+          }
+          else if(mimeConfNow != mimeConfTime)
+          {
+            string msg("mime-conf-changed");
+            notifyMulticast(msg, 0);
+            
+            if(logManager.getType() == LogManager::TYPE_CONSOLE)
             {
-              string msg("main-conf-changed");
-              notifyMulticast(msg, 0);
-              
-              reboot();
-              /* Store new mtime values.  */
-              mainConfTime = mainConfTimeNow;
-              mimeConf = mimeConfNow;
+              char beep[]={static_cast<char>(0x7), '\0'};
+              logManager.write(beep);
             }
-            else if(mimeConfNow != mimeConf)
-            {
-              string msg("mime-conf-changed");
-              notifyMulticast(msg, 0);
 
-              if(logManager.getType() == LogManager::TYPE_CONSOLE)
-              {
-                char beep[]={static_cast<char>(0x7), '\0'};
-                logManager.write(beep);
-              }
+            logWriteln("Reloading MIMEtypes.xml");
 
-              logWriteln("Reloading MIMEtypes.xml");
-              
-              getMimeManager()->loadXML(getMIMEConfFile());
+            getMimeManager()->loadXML(getMIMEConfFile());
+            
+            logWriteln("Reloaded");
+            
+            mimeConfTime = mimeConfNow;
+          }
+          else if(hostsConfTimeNow != hostsConfTime)
+          {
+            VhostManager* oldvhost = vhostList;
+            string msg("vhosts-conf-changed");
+            notifyMulticast(msg, 0);
 
-              logWriteln("Reloaded");
-
-              mimeConf = mimeConfNow;
+            /* Do a beep if outputting to console.  */
+            if(logManager.getType() == LogManager::TYPE_CONSOLE)
+            {
+              char beep[]={static_cast<char>(0x7), '\0'};
+              logManager.write(beep);
             }
-            else if(hostsConfTimeNow != hostsConfTime)
-            {
-              VhostManager* oldvhost = vhostList;
-              string msg("vhosts-conf-changed");
-              notifyMulticast(msg, 0);
+            
+            logWriteln("Rebooting...");
+            
+            connectionsScheduler.release();
 
-              /* Do a beep if outputting to console.  */
-              if(logManager.getType() == LogManager::TYPE_CONSOLE)
-              {
-                char beep[]={static_cast<char>(0x7), '\0'};
-                logManager.write(beep);
-              }
+            Socket::stopBlockingOperations(true);
+              
+            listenThreads.beginFastReboot();
 
-              logWriteln("Rebooting...");
+            listenThreads.terminate();
+              
+            connectionsScheduler.terminateConnections();
+            clearAllConnections();
 
-              connectionsScheduler.release();
-
-              Socket::stopBlockingOperations(true);
-
-              listenThreads.beginFastReboot();
-
-              listenThreads.terminate();
               
-              connectionsScheduler.terminateConnections();
-              clearAllConnections();
+            Socket::stopBlockingOperations(false);
 
+            connectionsScheduler.restart();
+            listenThreads.initialize(&languageParser);
               
-              Socket::stopBlockingOperations(false);
+            vhostList = new VhostManager(&listenThreads);
+            
+            if(vhostList == 0)
+              continue;
+            
+            delete oldvhost;
 
-              connectionsScheduler.restart();
-              listenThreads.initialize(&languageParser);
+            /* Load the virtual hosts configuration from the xml file.  */
+            
if(vhostList->loadXMLConfigurationFile(vhostConfigurationFile->c_str(),
+                                                   getMaxLogFileSize()))
+            {
+              listenThreads.rollbackFastReboot();
+            }
+            else
+            {
+              listenThreads.commitFastReboot();
+            }
+            
+            hostsConfTime = hostsConfTimeNow;
+            logWriteln("Reloaded");
+          }
 
-              vhostList = new VhostManager(&listenThreads);
+          configsCheck = 0;
+        }
+        else
+        {
+          /*
+           *If there are problems in loading mtimes
+           *check again after a bit.
+           */
+          configsCheck = 7;
+        }
+      }
+    }//end  if(autoRebootEnabled)
+  }
+}
 
-              if(vhostList == 0)
-              {
-                continue;
-              }
+/*!
+ *Display the MyServer boot.
+ */
+void Server::displayBoot()
+{
+  u_long i;
 
-              delete oldvhost;
+#ifdef CLEAR_BOOT_SCREEN
 
-              /* Load the virtual hosts configuration from the xml file.  */
-              
if(vhostList->loadXMLConfigurationFile(vhostConfigurationFile->c_str(),
-                                                     getMaxLogFileSize()))
-              {
-                listenThreads.rollbackFastReboot();
+  if(logManager.getType() == LogManager::TYPE_CONSOLE )
+  {
+#ifdef WIN32
 
-              }
-              else
-              {
-                listenThreads.commitFastReboot();
-              }
+    /*
+     *Under the windows platform use the cls operating-system
+     *command to clear the screen.
+     */
+    _flushall();
+    system("cls");
+#endif
+#ifdef NOT_WIN
 
-              hostsConfTime = hostsConfTimeNow;
-              logWriteln("Reloaded");
+  /*
+   *Under an UNIX environment, clearing the screen
+   *can be done in a similar method
+   */
+    sync();
+    system("clear");
+#endif
+  }
 
-            }
+#endif /* CLEAR_BOOT_SCREEN.  */
 
-            configsCheck = 0;
-          }
-          else
-          {
-            /*
-             *If there are problems in loading mtimes
-             *check again after a bit.
-             */
-            configsCheck = 7;
-          }
-        }
-      }//end  if(autoRebootEnabled)
+  /*
+   *Print the MyServer signature only if the log writes to the console.
+   */
+  if(logManager.getType() == LogManager::TYPE_CONSOLE )
+  {
+    try
+    {
+      string softwareSignature;
+      softwareSignature.assign("************");
+      softwareSignature.append(MYSERVER_VERSION);
+      softwareSignature.append("************");
+
+      i = softwareSignature.length();
+      while(i--)
+        logManager.write("*");
+      logManager.writeln("");
+      logManager.write(softwareSignature.c_str());
+      logManager.write("\n");
+      i = softwareSignature.length();
+      while(i--)
+        logManager.write("*");
+      logManager.writeln("");
     }
+    catch(exception& e)
+    {
+      ostringstream err;
+      err << "Error: " << e.what();
+      logManager.write(err.str().c_str());
+      logManager.write("\n");
+      return;
+    }
+    catch(...)
+    {
+      return;
+    };
   }
-  catch(bad_alloc &ba)
-  {
-    ostringstream s;
-    s << "Bad alloc: " << ba.what();
-    logWriteln(s.str().c_str());
-  }
-  catch(exception &e)
-  {
-    ostringstream s;
-    s << "Error: " << e.what();
-    logWriteln(s.str().c_str());
-  };
-  this->terminate();
-  finalCleanup();
-#ifdef WIN32
-  WSACleanup();
-#endif// WIN32
+
 }
 
 /*!
@@ -838,7 +854,7 @@
  *The configuration file is a XML file.
  *Return nonzero on errors.
  */
-int Server::initialize(int /*!osVer*/)
+int Server::initialize()
 {
   char *data;
   int ret;
@@ -1668,7 +1684,7 @@
 #endif
 
 #ifdef WIN32
-    externalPath->assign("plugins/protocols");
+    externalPath->assign("plugins");
 #endif
 
 
@@ -1909,7 +1925,7 @@
 
   rebooting = 0;
 
-  ret = initialize(0);
+  ret = initialize();
 
   if(ret)
     return ret;






reply via email to

[Prev in Thread] Current Thread [Next in Thread]