From a0e03b33c8739f3e22250dbdbc7aa65efa51335a Mon Sep 17 00:00:00 2001 From: portnov Date: Tue, 3 Mar 2009 00:52:35 +0500 Subject: [PATCH] Protocol implementation --- centrixd/cxcore.py | 35 +++++++++++++++++++---------------- centrixd/cxproto.py | 42 ++++++++++++++++++++++++++++++++---------- centrixd/data.py | 6 ++++++ client_test.py | 4 ++-- 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/centrixd/cxcore.py b/centrixd/cxcore.py index 5ada62a..82a1921 100644 --- a/centrixd/cxcore.py +++ b/centrixd/cxcore.py @@ -2,6 +2,8 @@ # author: Sergey Shakshin # jid: rigid_mgn@jabber.ru +import signal + from cxutils import log from cxlogger import CXLogger from cxconfig import config @@ -9,27 +11,28 @@ from sys import exit from louie import dispatcher from time import sleep - -def on_shutdown(): - mCycle = False - sleep(3) - exit() +def on_sigint(signum, frame): + print "Got SIGINT" + log("Ctrl-C pressed at console - exiting") + print "\nCtrl-C pressed at console - exiting\n" + dispatcher.send("shutdown") - -dispatcher.connect(on_shutdown,"shutdown") +signal.signal(signal.SIGINT, on_sigint) logger = CXLogger() logger.start() -import cxproto +mode = config['Centrix']['Mode'] +master = (mode == 'Master') +log("Entering %s mode" % mode) -mCycle = True -try: - while(mCycle): - sleep(1) +if not master: + if 'Master' in config['Centrix']: + smarthost = config['Centrix'] + log("Will use %s as master" % smarthost) + else: + log("Master not found.") + exit() -except KeyboardInterrupt: - log("Ctrl-C pressed at console - exiting") - print "\nCtrl-C pressed at console - exiting\n" - dispatcher.send("shutdown") +import cxproto diff --git a/centrixd/cxproto.py b/centrixd/cxproto.py index 9006636..a3b57f6 100644 --- a/centrixd/cxproto.py +++ b/centrixd/cxproto.py @@ -1,27 +1,49 @@ from twisted.internet.protocol import Protocol, Factory +from twisted.protocols.basic import LineReceiver from twisted.internet import reactor - from louie import dispatcher from threading import Thread from cxutils import log -from xmlserialize import serialize -from data import CXMessage +from xmlserialize import serialize, deserialize +from data import CXMessage, Error, OK from cxconfig import config +def shutdown(): + global reactor +# zeroconf.stop() + reactor.stop() + +dispatcher.connect(shutdown, "shutdown") + CXPORT = config['Network'].get('Port', 9222) -class CXProto(Protocol): +class CXProto(LineReceiver): status = None def connectionMade(self): - print "BLA" - log("Client connected") - hello = CXMessage("Hello") - hello.service = "Centrix" - hello.software = "centrixd" - self.transport.write(serialize(hello)+"\n") + print "Connection" + + def lineReceived(self,line): + msg = CXMessage.parse(line) + if msg.type in self.callbacks: + answer = self.callbacks[msg.type](self,msg) + else: + answer = Error(reason="Unknown message type") + self.sendLine(serialize(answer)) + + def new_client(self,msg): + print "New client:", msg + return OK() + + def send_configs(self,msg): + print "Searching for config for machine %s, template %s" % (msg.machine, msg.template) +# FIXME: this is a dumb stub + return OK() + + callbacks = dict(new_client = new_client, + get_my_configs = send_configs) class CXFactory(Factory): protocol = CXProto diff --git a/centrixd/data.py b/centrixd/data.py index db3cbec..096cb8c 100644 --- a/centrixd/data.py +++ b/centrixd/data.py @@ -34,3 +34,9 @@ class CXMessage(object): @classmethod def parse(cls, text): return xml.deserialize(text,cls=cls) + +def Error(**kwargs): + return CXMessage("Error",**kwargs) + +def OK(): + return CXMessage("OK") diff --git a/client_test.py b/client_test.py index 993328e..8c862a6 100755 --- a/client_test.py +++ b/client_test.py @@ -11,7 +11,7 @@ class Centrixc(LineReceiver): self.sendLine(data.CXMessage(msg_type,**kwargs).to_xml()) def connectionMade(self): - self.sendMessage('register_machine') + self.sendMessage('new_client') def lineReceived(self,line): print data.CXMessage.parse(line) @@ -30,6 +30,6 @@ class CClientFactory(ClientFactory): reactor.stop() factory = CClientFactory() -reactor.connectTCP('localhost',2009,factory) +reactor.connectTCP('localhost',9222,factory) reactor.run() -- 1.7.2.3