Commits

Anonymous committed b276c63

zmq-as-plugin test

Comments (0)

Files changed (3)

pyred/plugins/plugin.py

 import os, glob
 from pyred.core.coreobject import CoreObject as cobj
+from pyred.core.errors import *
 
 class AbstractPlugin(cobj):
     def __init__(self):
         super(AbstractPlugin, self).__init__()
     
+    def additionalSettings(self, settings=None):
+        pass
+    
     @staticmethod
     def registerPlugin():
         raise NotImplementedError
                         cls = getattr(module_obj,item)
                         if issubclass(cls,AbstractPlugin):
                             pluginInfo = cls.registerPlugin()
-                            self.pluginReg[pluginInfo.name] = filename
+                            self.pluginReg[pluginInfo.name] = pluginInfo
                             self.log.debug("plugin found: %s" % pluginInfo.name)
                             #TODO: add better registration step
                     except NotImplementedError:
             print k
                 
     def getPlugIn(self, name=None):
-        pass
-
+        if name == None or name == '':
+            return None
+        else:
+            pr=self.pluginReg
+            plugInfo=None
+            try:
+                plugInfo=pr[name]
+            except IndexError:
+                self.log.error("No plugin with name %s was found" % name)
+                raise ResourceNotAvaiable
+            if plugInfo != None and callable(plugInfo.mainClass):
+                return plugInfo.mainClass.__call__()
 
 class PluginInfo(cobj):
     def __init__(self):
         self.name=''
         self.ver=''
         self.type=''
-        self.dec=''
+        self.dec=''
+        self.mainClass=None

pyred/plugins/zmq_plug.py

 
 
 class ZmqBus(AbstractPlugin, AbstractBus):
-    def __init__(self, settings):
+    def __init__(self):
         super(ZmqBus, self).__init__()
         self.ctx=None
         self.messageList=[]
-        self.addr=settings['addr']
-        self.port=settings['port']
         
-    def registerPlugIn(self):
+    def additionalSettings(self, settings=None):
+        if settings != None:
+            self.proto=settings['proto']
+            self.addr=settings['addr']
+            self.port=settings['port']
+    
+    @staticmethod   
+    def registerPlugin():
         info=PluginInfo()
         info.name="zmq_bus"
         info.ver='0.1'
         info.type='bus'
         info.desc="main bus implementation based on zmq library"
+        info.mainClass=ZmqBus
         return info
  
 
     def start(self):
         ctx=self.ctx
-        ctx=zmq.Context
+        ctx=zmq.Context()
         s = ctx.socket(zmq.SUB)
         addrString=":".join([self.addr,self.port])
+        p="".join([self.proto,":"])
+        addrString="//".join([p,addrString])
         s.bind(addrString)
+        self.log.info("Ready to serving at %s" % addrString)
         while True:
             msg = s.recv()
             self.messageList.append(msg)

pyred/test/zmqTest.py

+import logging
+import sys
+
+l=logging.getLogger('defaultLog')
+l.setLevel(logging.DEBUG)
+main_handler = logging.StreamHandler(sys.stdout)
+l.addHandler(main_handler)
+
+import pyred
+
+l.info("query-ing for zmq plug")
+settings={}
+settings['proto']='tcp'
+settings['addr']='127.0.0.1'
+settings['port']='7777'
+
+zmq = pyred.plugins.manager.getPlugIn("zmq_bus")
+zmq.additionalSettings(settings)
+zmq.start()
+zmq.stop()