From 7576d78247cc476acbbf8385b6ab79e080afeab3 Mon Sep 17 00:00:00 2001
From: Anael Beutot <anael.beutot@smartjog.com>
Date: Mon, 7 May 2012 11:58:06 +0200
Subject: [PATCH] Plugin handling updated.

---
 ccnode/node.py    |  5 +++--
 ccnode/plugins.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 2 deletions(-)
 create mode 100644 ccnode/plugins.py

diff --git a/ccnode/node.py b/ccnode/node.py
index 2299333..a60c788 100644
--- a/ccnode/node.py
+++ b/ccnode/node.py
@@ -100,13 +100,14 @@ class AuthHandler(object):
         if response == u'host':
             logger.debug('Role host affected')
             from ccnode.host import Handler as HostHandler
-            self.loop.role = HostHandler()
+            self.loop.role = HostHandler(loop=self.loop.loop)
         elif response == u'hv':
             logger.debug('Role hypervisor affected')
             from ccnode.hypervisor import Handler as HypervisorHandler
             self.loop.role = HypervisorHandler(
                 proxy=self.loop.proxy,
                 hypervisor_name=self.loop.config.server_user,
+                loop=self.loop.loop,
             )
         else:
             logger.error('Failed authentication, role returned: %s', response)
@@ -222,7 +223,7 @@ class MainLoop(object):
         # register handler
         self.rpc_handler.update(plugin.rpc_handler)
 
-        plugin.start(self.loop)
+        plugin.start()
 
     def unregister_plugin(self, plugin):
         try:
diff --git a/ccnode/plugins.py b/ccnode/plugins.py
new file mode 100644
index 0000000..acbe038
--- /dev/null
+++ b/ccnode/plugins.py
@@ -0,0 +1,53 @@
+"""Plugins helpers for Cloud Control node."""
+
+from itertools import chain, imap
+
+
+class Base(object):
+    """Example skeleton plugin for cc-node.
+
+    If you want to create your own plugin for the `cc-node` you may create an
+    object that would quack just like this one or just inherit from this class.
+
+    """
+    def __init__(self, *args, **kwargs):
+        """
+        :param loop: pyev loop instance
+        """
+        self.loop = kwargs.pop('loop')
+
+        # plugins may define tags (see :mod:`ccnode.tags`)
+        self.tag_db = dict(
+            __main__=dict(),
+            # subobjects tags go here
+        )
+
+        # plugins may define handler functions that would be called by the
+        # server
+        self.rpc_handler = dict()
+
+        # tag_db and rpc_handler can be implemented as properties if more logic
+        # is needed
+
+    def __hash__(self):
+        """This method is used when registering a plugin in the main loop.
+
+        By default, only one instance is allowed. Subclasses can overide this
+        method to change this behaviour.
+
+        """
+        return hash(self.__class__.__name__)
+
+    def start(self):
+        """Used to start pyev watchers."""
+        # start tags
+        for tag in chain.from_iterable(
+            imap(lambda d: d.itervalues(), self.tag_db.itervalues()),
+        ):
+            tag.start(self.loop)
+
+    def stop(self):
+        """Cleanup for plugins, can be used to clean pyev watchers."""
+
+        self.loop = None
+        # TODO dependencies
-- 
GitLab