myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [2999] refactoring and fixed dependences checking


From: Daniele Perrone
Subject: [myserver-commit] [2999] refactoring and fixed dependences checking
Date: Fri, 06 Feb 2009 20:17:03 +0000

Revision: 2999
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2999
Author:   dperrone
Date:     2009-02-06 20:17:01 +0000 (Fri, 06 Feb 2009)

Log Message:
-----------
refactoring and fixed dependences checking

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

Modified: trunk/myserver/include/plugin/plugin_info.h
===================================================================
--- trunk/myserver/include/plugin/plugin_info.h 2009-02-06 18:57:04 UTC (rev 
2998)
+++ trunk/myserver/include/plugin/plugin_info.h 2009-02-06 20:17:01 UTC (rev 
2999)
@@ -60,9 +60,16 @@
        int setMyServerMaxVersion(int v);
        
        HashMap<string, pair<int,int>* >::Iterator begin(){return 
dependences.begin();}
-       HashMap<string, pair<int,int>* > ::Iterator end(){return 
dependences.end();}
+       HashMap<string, pair<int,int>* >::Iterator end(){return 
dependences.end();}
        
+       void setPlugin(Plugin* plugin);
+       Plugin* getPlugin();
+       Plugin* removePlugin();
        
+       void setEnabled(bool enabled);
+       
+       pair<int,int>* getDependence(string name);
+       
        static int convertVersion(string* s);
        
        
@@ -76,6 +83,7 @@
     int version;
     int msMinVersion;
     int msMaxVersion;
+    Plugin* plugin;
     HashMap<string, pair<int,int>* > dependences;
     static Regex* regex;
 };

Modified: trunk/myserver/include/plugin/plugins_manager.h
===================================================================
--- trunk/myserver/include/plugin/plugins_manager.h     2009-02-06 18:57:04 UTC 
(rev 2998)
+++ trunk/myserver/include/plugin/plugins_manager.h     2009-02-06 20:17:01 UTC 
(rev 2999)
@@ -33,8 +33,8 @@
 class PluginsManager
 {
 public:
-       HashMap<string, Plugin*>::Iterator begin(){return plugins.begin();}
-       HashMap<string, Plugin*>::Iterator end(){return plugins.end();}
+       HashMap<string, PluginInfo*>::Iterator begin(){return 
pluginsInfos.begin();}
+       HashMap<string, PluginInfo*>::Iterator end(){return pluginsInfos.end();}
        
        Plugin* getPlugin(string& name);
        
@@ -55,12 +55,12 @@
        
 private:
        HashMap<string, PluginInfo*> pluginsInfos;
-       HashMap<string, Plugin*> plugins;
        int loadOptions(Server *server, XmlParser* languageFile);
-       int addPlugin(string& file, Server* server, 
+       void recursiveDependencesFallDown(Server* server, string name, 
HashMap<string,bool> remove,  HashMap<string,list<string>*> dependsOn);
+       Plugin* preLoadPlugin(string& file, Server* server, 
                 XmlParser* languageFile, bool global);
                 
-    int loadInfo(string& name, string& path);
+    PluginInfo* loadInfo(Server* server, string& name, string& path);
 };
 
 #endif

Modified: trunk/myserver/src/plugin/plugin_info.cpp
===================================================================
--- trunk/myserver/src/plugin/plugin_info.cpp   2009-02-06 18:57:04 UTC (rev 
2998)
+++ trunk/myserver/src/plugin/plugin_info.cpp   2009-02-06 20:17:01 UTC (rev 
2999)
@@ -37,6 +37,7 @@
   this->version = 0;
   this->msMinVersion = 0;
   this->msMaxVersion = 0;
+  this->plugin = NULL;
 }
 
 
@@ -47,7 +48,8 @@
 */     
 PluginInfo::~PluginInfo()
 {
-
+  if (this->plugin)
+       delete this->plugin;
 }
 
 /*!
@@ -136,6 +138,56 @@
 }
 
 /*!
+ * Returns the plugin object.
+*/
+Plugin* PluginInfo::getPlugin()
+{
+  return this->plugin;
+}
+
+/*!
+ * Sets the state of the plugin.
+*/
+void PluginInfo::setEnabled(bool enabled)
+{
+  this->enabled = enabled;     
+}
+
+/*!
+ * Returns the depandence, if any exists, with a plugin with the specified 
name.
+*/
+pair<int,int>* PluginInfo::getDependence(string name)
+{   
+       if(dependences.empty())
+         return NULL;
+       if (!this->dependences.containsKey(name))
+         return NULL;
+       pair<int,int>* p =  this->dependences.get(name);
+       if (p)
+         return p;
+       return NULL;
+}
+/*!
+ * Sets the plugin object.
+*/
+void PluginInfo::setPlugin(Plugin* plugin)
+{
+  //if(this->plugin)
+  //   delete this->plugin;
+  this->plugin = plugin;
+}
+
+/*!
+ * Removes the plugin object.
+*/
+Plugin* PluginInfo::removePlugin()
+{
+  if(this->plugin)
+       delete this->plugin;
+  this->plugin = NULL;
+}
+
+/*!
  * Converts a string in the format "a.b.c.d" in an int in the format abcd 
where each number takes 8 bit.
 */
 int PluginInfo::convertVersion(string* s)

Modified: trunk/myserver/src/plugin/plugins_manager.cpp
===================================================================
--- trunk/myserver/src/plugin/plugins_manager.cpp       2009-02-06 18:57:04 UTC 
(rev 2998)
+++ trunk/myserver/src/plugin/plugins_manager.cpp       2009-02-06 20:17:01 UTC 
(rev 2999)
@@ -46,7 +46,10 @@
  */
 Plugin* PluginsManager::getPlugin(string& name)
 {
-  return plugins.get(name);
+  PluginInfo* info = pluginsInfos.get(name);
+  if (info)
+       return info->getPlugin();
+  return NULL;
 }
   
 /*!
@@ -100,7 +103,7 @@
             if(!pluginName.length())
             {
               string error;
-              error.assign("Warning: invalid plugin name in PLUGIN block");
+              error.assign("PLUGINS MANAGER: Warning: invalid plugin name in 
PLUGIN block");
               server->logWriteln(error.c_str(), MYSERVER_LOG_MSG_ERROR);
               ret = -1;  
             }
@@ -170,15 +173,8 @@
       if(flib.name[0]=='.')
        continue;
 
-          /*!
-     *Do not consider file other than dynamic libraries.
-     */
-#ifdef WIN32
-      if(!strstr(flib.name,".dll") && !strstr(flib.name,"plugin.xml"))
-#endif
-#ifdef NOT_WIN
-      if(!strstr(flib.name,".so") && !strstr(flib.name,"plugin.xml"))
-#endif    
+          
+      if(!strstr(flib.name,"plugin.xml"))
         continue;
       completeFileName.assign(filename);
       
@@ -190,23 +186,38 @@
         completeFileName.append("/");
       completeFileName.append(flib.name);
 
-         if(strstr(flib.name,"plugin.xml"))
-         {
-               string pname(fdir.name);
-        ret |= loadInfo(pname, completeFileName);
+         string pname(fdir.name);
+      PluginInfo* pinfo = loadInfo(server,pname, completeFileName);
+      if(!pinfo)
+      {
+        ret|=1;
         continue;
-         }
+      }
+      string libname;
+      libname.assign(dirname);
+         
+      libname.append("/");
+      libname.append(pinfo->getName());
 #ifdef WIN32
-      name = name.substr(0, name.length() - 4);
+      libname.append(".dll");
 #endif
 #ifdef NOT_WIN
-      name = name.substr(0, name.length() - 3);
+      libname.append(".so");
 #endif
-      PluginInfo* pinfo = getPluginInfo(name);
-       
-       if(!pinfo || pinfo->isEnabled())
-         ret |= addPlugin(completeFileName, server, languageFile, pinfo && 
pinfo->isGlobal());
-          
+       if(pinfo->isEnabled())
+       {
+         Plugin* plugin = preLoadPlugin(libname, server, languageFile, 
pinfo->isGlobal());
+         if (!plugin)
+         {
+           ret|=1;
+           string logBuf;
+              logBuf.append("PLUGINS MANAGER: Error!! Unable to enable plugin 
");
+              logBuf.append(name);
+              server->logWriteln( logBuf.c_str() );
+         }else
+           pinfo->setPlugin(plugin);
+       }
+       addPluginInfo(pname,pinfo);
      }while(!flib.findnext());
    }while(!fdir.findnext());
   fdir.findclose();
@@ -227,18 +238,27 @@
  *\param name The plugin name.
  *\param path the plugin xml descriptor path.
  */
-int PluginsManager::loadInfo(string& name, string& path)
+PluginInfo* PluginsManager::loadInfo(Server* server, string& name, string& 
path)
 {
   PluginInfo* pinfo;
   
   pinfo = getPluginInfo(name);
   if (!pinfo)
     pinfo = new PluginInfo(name);
-    
+  else
+       if (pinfo->getVersion()!=0)
+       {
+         string logBuf;
+         logBuf.append("PLUGINS MANAGER: Error!!! a version of plugin ");
+         logBuf.append(name);
+         logBuf.append(" is already loading!");
+         server->logWriteln( logBuf.c_str() );
+         return NULL;
+       }  
   XmlParser xml;
   
   if (xml.open(path,true))
-    return 1;
+    return NULL;
   
   XmlXPathResult* xpathRes = xml.evaluateXpath("/PLUGIN");
   
@@ -247,7 +267,7 @@
   int size = (nodes) ? nodes->nodeNr : 0;
   
   if (size!=1)
-    return 1;
+    return NULL;
   
   const char* mSMinVersion = (const char*)xmlGetProp(nodes->nodeTab[0],(const 
xmlChar*)"min-version");
   
@@ -264,12 +284,12 @@
   
   
   if (size!=1)
-    return 1;
+    return NULL;
   const char* cname = (const char*)nodes->nodeTab[0]->content;
 
   
   if (strcmp(name.c_str(),cname))
-    return 1;
+    return NULL;
   
   delete xpathRes;
   
@@ -280,7 +300,7 @@
   
   
   if (size!=1)
-    return 1;
+    return NULL;
   const char* version = (const char*)nodes->nodeTab[0]->content;
   
   
@@ -309,11 +329,8 @@
   }
 
   delete xpathRes;
-  
-  addPluginInfo(name,pinfo);
-  
-  
-  return 0;
+
+  return pinfo;
 }
 
 /*!
@@ -324,7 +341,7 @@
  *messages.
  *\param global Specify if the library should be loaded globally.
  */
-int PluginsManager::addPlugin(string& file, Server* server, 
+Plugin* PluginsManager::preLoadPlugin(string& file, Server* server, 
                                        XmlParser* languageFile, bool global)
 {
   Plugin *plugin = createPluginObject();
@@ -335,7 +352,7 @@
   if(plugin->preLoad(file, server, languageFile, global))
   {
     delete plugin;
-    return 1;
+    return NULL;
   }
   namePtr = plugin->getName(0, 0);
 
@@ -344,14 +361,33 @@
   else
   {
     delete plugin;
-    return 1;
+    return NULL;
   }
-    
-  plugins.put(name, plugin);
+ 
+  return plugin;
+}
 
+void PluginsManager::recursiveDependencesFallDown(Server* server, string name, 
HashMap<string,bool> remove,  HashMap<string,list<string>*> dependsOn)
+{
+  remove.put(name,true);
+  list<string>* dependsList = dependsOn.get(name);
+  if (!dependsList || dependsList->empty())
+    return;
   
-  
-  return 0;
+  list<string>::iterator lit = dependsList->begin();
+
+  for (;lit!=dependsList->end();lit++)
+  {
+       
+       string logBuf;
+       logBuf.append("PLUGINS MANAGER: missing dependence: ");
+       logBuf.append(*lit);
+       logBuf.append(" --> ");
+       logBuf.append(name);
+       recursiveDependencesFallDown(server,*lit,remove,dependsOn);
+       server->logWriteln( logBuf.c_str() );
+  }
+       
 }
 
 /*!
@@ -367,15 +403,17 @@
        
   
   list<string*> toRemove;
-  HashMap<string, Plugin*>::Iterator it = plugins.begin();
-  while(it != plugins.end())
+  HashMap<string,list<string>*> dependsOn;
+  HashMap<string, PluginInfo*>::Iterator it = pluginsInfos.begin();
+  HashMap<string,bool> remove;
+  while(it != pluginsInfos.end())
   {
-       string logBuf;
        
+       
        string name(it.getKey());
        
        
-       PluginInfo* pinfo = getPluginInfo(name);
+       PluginInfo* pinfo = *it;
     
     
        HashMap<string, pair<int,int>* >::Iterator depIt = pinfo->begin();
@@ -389,55 +427,103 @@
        int msVersion = PluginInfo::convertVersion(&msversion);
        if (msVersion < pinfo->getMyServerMinVersion() || msVersion > 
pinfo->getMyServerMaxVersion())
        {
-         logBuf.append("myserver version not compatible --> ");
-         goodVersions = false;
+         string logBuf;
+         logBuf.append("PLUGINS MANAGER: ");
+         logBuf.append(name);
+         logBuf.append(" not compatible with this myserver version! ");
+         server->logWriteln( logBuf.c_str() );
+         toRemove.push_front(&name);
        }
        else
+         remove.put(name,false);
          for (;depIt != pinfo->end(); depIt++)
          {
                string dname = depIt.getKey();
                
-               PluginInfo* dep = getPluginInfo(dname);
+               list<string>* deps = dependsOn.get(dname);
+               if (!deps)
+               {
+                 deps = new list<string>();
+                 dependsOn.put(dname,deps);
+               }
                
-               if (!dep)
+               deps->push_front(name);
+         }
+         
+       
+    it++;
+  }
+
+  list<string*>::iterator tRIt = toRemove.begin();
+  for (;tRIt != toRemove.end(); tRIt++)
+    removePlugin(**tRIt);
+  toRemove.clear();
+  
+  
+  
+  HashMap<string,list<string>*>::Iterator dIt = dependsOn.begin();
+  for(;dIt!=dependsOn.end();dIt++)
+  {
+       string logBuf;
+       string dname = dIt.getKey();
+       
+       PluginInfo* pinfo = getPluginInfo(dname);
+       
+       if (!pinfo || pinfo->getVersion()==0)
+               remove.put(dname,true);
+               
+       list<string>* dependsList = (*dIt);
+       if (!dependsList)
+         continue;
+       if(dependsList->empty())
+         continue;
+       
+       bool rem = remove.get(dname);
+       if (rem)
+       {
+         recursiveDependencesFallDown(server,dname,remove,dependsOn);
+         continue;
+       }
+       
+       HashMap<string, pair<int,int>* >::Iterator lit = pinfo->begin();
+       for (;lit!=pinfo->end();lit++)
+       {
+               string depN = lit.getKey();
+               PluginInfo* dep = getPluginInfo(depN);
+               if (!dep || remove.get(depN))
                {
-                 logBuf.append("missing dependence: ");
+                 logBuf.append("PLUGINS MANAGER: missing dependence: ");
                  logBuf.append(dname);
                  logBuf.append(" --> ");
-                 logBuf.append(name);
-                 goodVersions = false;
+                 logBuf.append(depN);
+                 recursiveDependencesFallDown(server,dname,remove,dependsOn);
+                 server->logWriteln( logBuf.c_str() );
                  break;
                }
-                 
-               if (dep->getVersion() < (*depIt)->first || dep->getVersion() > 
(*depIt)->second)
+               
+
+               pair<int,int>* pdep = *lit;
+               if (dep->getVersion() < pdep->first || dep->getVersion() > 
pdep->second)
                {
-                 logBuf.append("plugin version not compatible: ");
+                 logBuf.append("PLUGINS MANAGER: plugin version not 
compatible: ");
                  logBuf.append(dname);
                  logBuf.append(" --> ");
-                 logBuf.append(name);
-                 goodVersions = false;
+                 logBuf.append(depN);
+                 recursiveDependencesFallDown(server,dname,remove,dependsOn);
+                 server->logWriteln( logBuf.c_str() );
                  break;
                }
-         }
-       
-       
-       if (goodVersions)
-       {
-         logBuf.assign(languageFile->getValue("MSG_LOADED"));
-         logBuf.append(" -plugin- ");
-      logBuf.append(name);
-      (*it)->load(resource, server, languageFile);
-       } 
-       else
-         toRemove.push_front(&name);
-       server->logWriteln( logBuf.c_str() );
-    it++;
+       }
   }
-
-  list<string*>::iterator tRIt = toRemove.begin();
-  for (;tRIt != toRemove.end(); tRIt++)
-    removePlugin(**tRIt);
   
+  
+  HashMap<string,bool>::Iterator rIt = remove.begin();
+  for(;rIt!=remove.end();rIt++)
+  {
+       string name(rIt.getKey());
+       if (*rIt)
+         removePlugin(name);
+  }
   return 0;
   
 }
@@ -450,10 +536,19 @@
  */
 int PluginsManager::postLoad(Server *server, XmlParser* languageFile)
 {
-  HashMap<string, Plugin*>::Iterator it = plugins.begin();
-  while(it != plugins.end())
+  HashMap<string, PluginInfo*>::Iterator it = pluginsInfos.begin();
+  while(it != pluginsInfos.end())
   {
-    (*it)->postLoad(server, languageFile);
+    Plugin* plugin = (*it)->getPlugin();
+    if (plugin)
+    {
+      plugin->postLoad(server, languageFile);
+      string logBuf;
+      logBuf.append("PLUGINS MANAGER: plugin ");
+         logBuf.append((*it)->getName());
+         logBuf.append(" loaded! ");
+         server->logWriteln( logBuf.c_str() );
+    }
     it++;
   }
   return 0;
@@ -467,23 +562,15 @@
  */
 int PluginsManager::unLoad(Server *server, XmlParser* languageFile)
 {
-  HashMap<string, Plugin*>::Iterator it = plugins.begin();
+
   HashMap<string, PluginInfo*>::Iterator poit = pluginsInfos.begin();
 
-  while(it != plugins.end())
-  {
-    (*it)->unLoad(languageFile);
-    delete *it;
-    it++;
-  }
-
   while(poit != pluginsInfos.end())
   {
     delete *poit;
     poit++;
   }
 
-  plugins.clear();
   pluginsInfos.clear();
   return 0;
 }
@@ -494,7 +581,9 @@
  */
 void PluginsManager::removePlugin(string& name)
 {
-  plugins.remove(name);
+  PluginInfo* info = pluginsInfos.remove(name);
+  
+  delete info;
 }
 
 /*!

Modified: trunk/myserver/src/server/server.cpp
===================================================================
--- trunk/myserver/src/server/server.cpp        2009-02-06 18:57:04 UTC (rev 
2998)
+++ trunk/myserver/src/server/server.cpp        2009-02-06 20:17:01 UTC (rev 
2999)
@@ -526,11 +526,7 @@
   }
 
 
-  //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);






reply via email to

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