Browse Source

store config in (binary) database on exit, read on start. database file currently hardcoded as /tmp/parkinglot.db

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/f2f200910@170 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen 15 years ago
parent
commit
1e70a81488
1 changed files with 23 additions and 16 deletions
  1. 23 16
      src/lib/bind-cfgd/python/bind-cfgd.py

+ 23 - 16
src/lib/bind-cfgd/python/bind-cfgd.py

@@ -1,5 +1,6 @@
 import ISC
-
+import pickle
+    
 class ConfigData:
     def __init__(self):
         self.zones = {}
@@ -32,14 +33,16 @@ class ConfigManager:
     def read_config(self, filename):
         print("Reading config")
         try:
-            file = open(filename, 'r');
+            file = open(filename, 'rb');
             self.config = pickle.load(file)
         except IOError as ioe:
             print("No config file found, starting with empty config")
+        except EOFError as eofe:
+            print("Config file empty, starting with empty config")
 
     def write_config(self, filename):
         print("Writing config")
-        file = open(filename, 'w');
+        file = open(filename, 'wb');
         pickle.dump(self.config, file)
 
     def handle_msg(self, msg):
@@ -68,30 +71,34 @@ class ConfigManager:
         return answer
         
     def run(self):
-        while (True):
+        msg = 1
+        while (msg):
             msg, env = self.cc.group_recvmsg(False)
-            print("received message: ")
-            print(msg)
-            answer = self.handle_msg(msg);
-            print("sending answer: ")
-            print(answer)
-            self.cc.group_reply(env, answer)
-            print("answer sent")
-            pass
-
+            if msg:
+                print("received message: ")
+                print(msg)
+                answer = self.handle_msg(msg);
+                print("sending answer: ")
+                print(answer)
+                self.cc.group_reply(env, answer)
+                print("answer sent")
+            
+                
 if __name__ == "__main__":
     print("Hello, BIND10 world!")
+    db_file = "/tmp/parkinglot.db"
     try:
         cm = ConfigManager()
-        cm.read_config("/tmp/a")
+        cm.read_config(db_file)
         # do loading here if necessary
         cm.notify_boss()
         cm.run()
+        cm.write_config(db_file)
     except ISC.CC.SessionError as se:
         print("Error creating config manager, "
               "is the command channel daemon running?")
     except KeyboardInterrupt as kie:
         print("Got ctrl-c, save config and exit")
-        cm.write_config("/tmp/a")
-
+        cm.write_config(db_file)
+