Beispiel #1
0
class ZConfigServer(ZReplier):

    def __init__(self, bind, zfile):
        super(ZConfigServer, self).__init__(bind)

        self.zfile = zfile

    def create_publisher(self):
        BIND_KEY = "dutils.zconfig.publisher_port"
        DEFAULT_BIND = "tcp://127.0.0.1:5558"
        if BIND_KEY in self.db:
            PUBLISHER_BIND = self.db[BIND_KEY]
        else:
            PUBLISHER_BIND = DEFAULT_BIND
            self.db[BIND_KEY] = DEFAULT_BIND
            self.db.sync()
        self.publisher = ZPublisher(PUBLISHER_BIND)
        print "ZConfigServer publishing on %s." % PUBLISHER_BIND

    def thread_init(self):
        super(ZConfigServer, self).thread_init()
        self.db = bsddb.hashopen(self.zfile)
        print "Loaded %s with %s records." % (self.zfile, len(self.db))
        self.create_publisher()

    def reply(self, message):
        if message.startswith("write"):
            cmd, key, val = message.split(":", 2)
            self.log("write: %s" % key)
            self.increment_stats_counter("write")
            self.db[key] = val
            self.db.sync()
            self.publisher.publish("%s:%s" % (key, val))
            return "written, thanks"
        elif message.startswith("del"):
            key = message.split(":", 1)[1]
            self.log("del: %s" % key)
            self.increment_stats_counter("del")
            if key in self.db: 
                del self.db[key]
                self.db.sync()
                return "deleted"
            return "not found"
        elif message.startswith("read"):
            key = message.split(":", 1)[1]
            self.log("read: %s" % key)
            self.increment_stats_counter("read")
            data = "NA"
            if key in self.db: data = self.db[key]
            return data
        elif message == "dump":
            from django.utils import simplejson
            self.log("dump")
            self.increment_stats_counter("dump")
            return simplejson.dumps(dict(self.db))
        return super(ZConfigServer, self).reply(message)

    def thread_quit(self):
        super(ZConfigServer, self).thread_quit()
        self.publisher.shutdown()
Beispiel #2
0
 def create_publisher(self):
     BIND_KEY = "dutils.zconfig.publisher_port"
     DEFAULT_BIND = "tcp://127.0.0.1:5558"
     if BIND_KEY in self.db:
         PUBLISHER_BIND = self.db[BIND_KEY]
     else:
         PUBLISHER_BIND = DEFAULT_BIND
         self.db[BIND_KEY] = DEFAULT_BIND
         self.db.sync()
     self.publisher = ZPublisher(PUBLISHER_BIND)
     print "ZConfigServer publishing on %s." % PUBLISHER_BIND