started protocol development

Sergey [2009-02-26 20:41:22]
started protocol development
Filename
centrixd/cxconfig.py
centrixd/cxcore.py
centrixd/cxlogger.py
centrixd/cxproto.py
centrixd/xmlserialize.py
diff --git a/centrixd/cxconfig.py b/centrixd/cxconfig.py
index a372133..e8aaf6e 100644
--- a/centrixd/cxconfig.py
+++ b/centrixd/cxconfig.py
@@ -3,13 +3,17 @@ from louie import dispatcher
 from cxutils import log

 class CXConfig(ConfigObj):
+    def __init__(self, infile=None, options=None):
+        ConfigObj.__init__(self, infile, options)
+        dispatcher.connect(self.reload,"do_reload")
+
     def _load(self,infile,configspec):
-	ConfigObj._load(self,infile,configspec)
-	dispatcher.send("reconfig")
+	   ConfigObj._load(self,infile,configspec)
+	   dispatcher.send("reconfig")

     def reload(self):
-	ConfigObj.reload(self)
-	dispatcher.send("reconfig")
+	   ConfigObj.reload(self)
+	   dispatcher.send("reconfig")

 try:
     config
diff --git a/centrixd/cxcore.py b/centrixd/cxcore.py
index 5e2768a..5ada62a 100644
--- a/centrixd/cxcore.py
+++ b/centrixd/cxcore.py
@@ -2,10 +2,10 @@
 # author: Sergey Shakshin
 # jid: rigid_mgn@jabber.ru

-
 from cxutils import log
 from cxlogger import CXLogger
 from cxconfig import config
+from sys import exit

 from louie import dispatcher
 from time import sleep
@@ -13,6 +13,8 @@ from time import sleep

 def on_shutdown():
     mCycle = False
+    sleep(3)
+    exit()


 dispatcher.connect(on_shutdown,"shutdown")
@@ -20,11 +22,13 @@ dispatcher.connect(on_shutdown,"shutdown")
 logger = CXLogger()
 logger.start()

+import cxproto
+
 mCycle = True
 try:
     while(mCycle):
         sleep(1)
-
+
 except KeyboardInterrupt:
     log("Ctrl-C pressed at console - exiting")
     print "\nCtrl-C pressed at console - exiting\n"
diff --git a/centrixd/cxlogger.py b/centrixd/cxlogger.py
index 7a27a1c..98c9e9a 100644
--- a/centrixd/cxlogger.py
+++ b/centrixd/cxlogger.py
@@ -19,7 +19,7 @@ class CXLogger(Thread):
         self.__pool.put(message)

     def on_reconfig(self):
-	syslog("configuration read complete")
+	       syslog("configuration read complete")

     def run(self):
         dispatcher.connect(self.on_shutdown,"shutdown")
diff --git a/centrixd/cxproto.py b/centrixd/cxproto.py
new file mode 100644
index 0000000..7c78f8a
--- /dev/null
+++ b/centrixd/cxproto.py
@@ -0,0 +1,35 @@
+from twisted.internet.protocol import Protocol, Factory
+from twisted.internet import reactor
+
+
+from louie import dispatcher
+from threading import Thread
+
+
+from cxutils import log
+from xmlserialize import serialize
+
+CXPORT = 9222
+
+class CXMessage(object):
+    pass
+
+class CXProto(Protocol):
+    status = None
+
+    def connectionMade(self):
+        print "BLA"
+        log("Client connected")
+        hello = CXMessage();
+        hello.type = "Hello"
+        hello.service = "Centrix"
+        hello.software = "centrixd"
+        self.transport.write(serialize(hello)+"\n")
+
+class CXFactory(Factory):
+    protocol = CXProto
+
+
+reactor.usingThreads = True
+reactor.listenTCP(CXPORT, CXFactory())
+reactor.run(installSignalHandlers=0)
\ No newline at end of file
diff --git a/centrixd/xmlserialize.py b/centrixd/xmlserialize.py
new file mode 100644
index 0000000..52619b5
--- /dev/null
+++ b/centrixd/xmlserialize.py
@@ -0,0 +1,103 @@
+import inspect
+from lxml import etree
+from StringIO import StringIO
+
+class Storable(object):
+    pass
+
+def serialize(ob):
+    if type(ob)==str:
+        return ob
+    if type(ob)==int:
+        return str(ob)
+    if type(ob)==list:
+        r = '<list>'
+        for item in ob:
+            r += '<item>'+serialize(item)+'</item>'
+        r += '</list>'
+        return r
+    if type(ob)==tuple:
+        r = '<tuple>'
+        for item in ob:
+            r += '<item>'+serialize(item)+'</item>'
+        r += '</tuple>'
+        return r
+    if type(ob)==dict:
+        r = '<dict>'
+        for k,v in ob.items():
+            r += "<pair key='%s'>%s</pair>" % (k,serialize(v))
+        r += '</dict>'
+        return r
+    r = '<object>'
+    for name in dir(ob):
+        if name.startswith('_'):
+            continue
+        val = getattr(ob,name)
+        if inspect.ismethod(val):
+            continue
+        r += "<attribute name='%s'>%s</attribute>" % (name,serialize(val))
+    r += '</object>'
+    return r
+
+
+def deserialize(text,cls=None):
+    def parse_text(t):
+        try:
+            return int(t)
+        except ValueError:
+            return t
+
+    def work(node):
+        if len(node) == 0:
+            return parse_text(node.text)
+        else:
+            if node.tag == 'list':
+                return list(map(work,node))
+            if node.tag == 'tuple':
+                return tuple(map(work,node))
+            if node.tag == 'dict':
+                d = dict()
+                for pair in node:
+                    d[pair.attrib['key']] = work(pair)
+                return d
+            if node.tag == 'object':
+                ob = Storable()
+                for attr in node:
+                    setattr(ob,attr.attrib['name'],work(attr))
+                return ob
+            if node.tag == 'attribute':
+                return work(node[0])
+            print "Unknown tag:", node.tag
+
+    xml = etree.parse(StringIO(text))
+    ob = work(xml.getroot())
+    if cls:
+        return convert(ob,cls)
+    else:
+        return ob
+
+def convert(ob,cls):
+    "Convert object (loaded from xml) to given class"
+    res = cls()
+    for name in dir(ob):
+        if name.startswith('_'):
+            continue
+        v = getattr(ob,name)
+        setattr(res,name,v)
+    return res
+
+if __name__ == '__main__':
+
+    a = Storable()
+    c = Storable()
+    c.z = 5
+    c.s = "string"
+    a.c = c
+    a.x = 12
+    a.lst = [1,2]
+    a.d = dict(a=5,b=7)
+    a.t = (7,9)
+    s = serialize(a)
+    b = deserialize(s)
+    print serialize(b)
+
ViewGit