Browse Source

more command handling for CCSession stuff
added CCSession to Boss module
removed hard-coded boss stuff from cmd-ctrld and bindctl
removed a few unnecessary subscriptions from default channels


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/parkinglot@576 e5f2f494-b856-4b98-b285-d166d9295462

Jelte Jansen 15 years ago
parent
commit
33ae332cb5

+ 73 - 61
src/bin/bind10/bind10.py

@@ -93,20 +93,28 @@ class BoB:
         if self.verbose:
             print("[XX] handling new config:")
             print(new_config)
+        # TODO
 
     def command_handler(self, command):
+        # a command is of the form [ "command", { "arg1": arg1, "arg2": arg2 } ]
         if self.verbose:
             print("[XX] Boss got command:")
             print(command)
         answer = None
-        if "command" in command:
-            cmd = command["command"]
+        if type(command) != list or len(command) == 0:
+            answer = { "result": [ 1, "bad command" ] }
+        else:
+            cmd = command[0]
             if cmd == "shutdown":
-                self.shutdown()
+                print("[XX] got shutdown command")
+                self.runnable = False
+                answer = { "result": [ 0 ] }
+            elif cmd == "print_message":
+                if len(command) > 1 and type(command[1]) == dict and "message" in command[1]:
+                    print(command[1]["message"])
+                answer = { "result": [ 0 ] }
             else:
-                answer = { "result": [ 1, "unknown command: " + cmd ] }
-        else:
-            answer = { "result": [ 1, "bad command" ] }
+                answer = { "result": [ 1, "Unknown command" ] }
         return answer
     
     def startup(self):
@@ -140,7 +148,7 @@ class BoB:
                 self.cc_session = ISC.CC.Session(self.c_channel_port)
             except ISC.CC.session.SessionError:
                 time.sleep(0.1)
-        self.cc_session.group_subscribe("Boss", "boss")
+        #self.cc_session.group_subscribe("Boss", "boss")
 
         # start the configuration manager
         if self.verbose:
@@ -161,7 +169,7 @@ class BoB:
         time.sleep(1)
         if self.verbose:
             print("[XX] starting ccsession")
-        self.ccs = isc.config.CCSession("bob", "bob.spec", self.config_handler, self.command_handler)
+        self.ccs = isc.config.CCSession("Boss", "bob.spec", self.config_handler, self.command_handler)
         if self.verbose:
             print("[XX] ccsession started")
 
@@ -280,56 +288,60 @@ class BoB:
             else:
                 sys.stdout.write("Unknown child pid %d exited.\n" % pid)
 
-    def recv_and_process_cc_msg(self):
-        """Receive and process the next message on the c-channel,
-        if any."""
-        msg, envelope = self.cc_session.group_recvmsg(False)
-        print(msg)
-        if msg is None:
-            return
-        if not ((type(msg) is dict) and (type(envelope) is dict)):
-            if self.verbose:
-                sys.stdout.write("Non-dictionary message\n")
-            return
-        if not "command" in msg:
-            if self.verbose:
-                if "msg" in envelope:
-                    del envelope['msg']
-                sys.stdout.write("Unknown message received\n")
-                sys.stdout.write(pprint.pformat(envelope) + "\n")
-                sys.stdout.write(pprint.pformat(msg) + "\n")
-            return
-        cmd = msg['command']
-        if not (type(cmd) is list):
-            if self.verbose:
-                sys.stdout.write("Non-list command\n")
-            return
-
+    # 'old' command style, uncommented for now
+    # move the handling below move to command_handler please
+    #def recv_and_process_cc_msg(self):
+        #"""Receive and process the next message on the c-channel,
+        #if any."""
+        #self.ccs.checkCommand()
+        #msg, envelope = self.cc_session.group_recvmsg(False)
+        #print(msg)
+        #if msg is None:
+        #    return
+        #if not ((type(msg) is dict) and (type(envelope) is dict)):
+        #    if self.verbose:
+        #        sys.stdout.write("Non-dictionary message\n")
+        #    return
+        #if not "command" in msg:
+        #    if self.verbose:
+        #        if "msg" in envelope:
+        #            del envelope['msg']
+        #        sys.stdout.write("Unknown message received\n")
+        #        sys.stdout.write(pprint.pformat(envelope) + "\n")
+        #        sys.stdout.write(pprint.pformat(msg) + "\n")
+        #    return
+
+        #cmd = msg['command']
+        #if not (type(cmd) is list):
+        #    if self.verbose:
+        #        sys.stdout.write("Non-list command\n")
+        #    return
+        #
         # done checking and extracting... time to execute the command
-        if cmd[0] == "shutdown":
-            if self.verbose:
-                sys.stdout.write("shutdown command received\n")
-            self.runnable = False
-            # XXX: reply here?
-        elif cmd[0] == "getProcessList":
-            if self.verbose:
-                sys.stdout.write("getProcessList command received\n")
-            live_processes = [ ]
-            for proc_info in processes:
-                live_processes.append({ "name": proc_info.name, 
-                                        "args": proc_info.args, 
-                                        "pid": proc_info.pid, })
-            dead_processes = [ ]
-            for proc_info in dead_processes:
-                dead_processes.append({ "name": proc_info.name, 
-                                        "args": proc_info.args, })
-            cc.group_reply(envelope, { "response": cmd,
-                                       "sent": msg["sent"],
-                                       "live_processes": live_processes,
-                                       "dead_processes": dead_processes, })
-        else:
-            if self.verbose:
-                sys.stdout.write("Unknown command %s\n" % str(cmd))
+        #if cmd[0] == "shutdown":
+        #    if self.verbose:
+        #        sys.stdout.write("shutdown command received\n")
+        #    self.runnable = False
+        #    # XXX: reply here?
+        #elif cmd[0] == "getProcessList":
+        #    if self.verbose:
+        #        sys.stdout.write("getProcessList command received\n")
+        #    live_processes = [ ]
+        #    for proc_info in processes:
+        #        live_processes.append({ "name": proc_info.name, 
+        #                                "args": proc_info.args, 
+        #                                "pid": proc_info.pid, })
+        #    dead_processes = [ ]
+        #    for proc_info in dead_processes:
+        #        dead_processes.append({ "name": proc_info.name, 
+        #                                "args": proc_info.args, })
+        #    cc.group_reply(envelope, { "response": cmd,
+        #                               "sent": msg["sent"],
+        #                               "live_processes": live_processes,
+        #                               "dead_processes": dead_processes, })
+        #else:
+        #    if self.verbose:
+        #        sys.stdout.write("Unknown command %s\n" % str(cmd))
 
     def restart_processes(self):
         """Restart any dead processes."""
@@ -427,7 +439,7 @@ def main():
     # In our main loop, we check for dead processes or messages 
     # on the c-channel.
     wakeup_fd = wakeup_pipe[0]
-    cc_fd = boss_of_bind.cc_session._socket.fileno()
+    ccs_fd = boss_of_bind.ccs.getSocket().fileno()
     while boss_of_bind.runnable:
         # XXX: get time for next restart for timeout
 
@@ -435,7 +447,7 @@ def main():
         # even if they are resumable, so we have to catch
         # the exception
         try:
-            (rlist, wlist, xlist) = select.select([wakeup_fd, cc_fd], [], [])
+            (rlist, wlist, xlist) = select.select([wakeup_fd, ccs_fd], [], [])
         except select.error as err:
             if err.args[0] == errno.EINTR:
                 (rlist, wlist, xlist) = ([], [], [])
@@ -444,8 +456,8 @@ def main():
                 break
 
         for fd in rlist + xlist:
-            if fd == cc_fd:
-                boss_of_bind.recv_and_process_cc_msg()
+            if fd == ccs_fd:
+                boss_of_bind.ccs.checkCommand()
             elif fd == wakeup_fd:
                 os.read(wakeup_fd, 32)
 

+ 0 - 11
src/bin/bindctl/bindctl.py

@@ -49,22 +49,11 @@ def prepare_config_commands(tool):
     module.add_command(cmd)
 
     tool.add_module_info(module)
-    
-def prepare_boss_command(tool):
-    # Prepare the command 'shutdown' for Boss, this is one 'hardcode' exception.
-    shutdown_cmd = CommandInfo(name = 'shutdown', desc = "stop one module",
-                               need_inst_param = False)
-    boss_module = ModuleInfo(name = "Boss", desc = "boss of bind10")
-    boss_module.add_command(shutdown_cmd)               
-    tool.add_module_info(boss_module)
-
-
 
 if __name__ == '__main__':
     try:
         tool = BindCmdInterpreter("localhost:8080")
         prepare_config_commands(tool)
-        prepare_boss_command(tool)
         tool.run()
     except Exception as e:
         print(e)

+ 1 - 6
src/bin/cmd-ctrld/cmd-ctrld.py

@@ -108,7 +108,7 @@ class CommandControl():
     def __init__(self):
         self.cc = ISC.CC.Session()
         self.cc.group_subscribe('Cmd-Ctrld')
-        self.cc.group_subscribe('Boss', 'Cmd-Ctrld')
+        #self.cc.group_subscribe('Boss', 'Cmd-Ctrld')
         self.command_spec = self.get_cmd_specification()
         self.config_spec = self.get_data_specification()
         self.config_data = self.get_config_data()
@@ -149,11 +149,6 @@ class CommandControl():
         msg = {'command' : content}
         print('cmd-ctrld send command \'%s\' to %s' %(command_name, module_name))
         try:
-            # Do special for Boss shutdown. TODO, avoid specicialism
-            if module_name == 'Boss' and command_name == 'shutdown':
-                self.cc.group_sendmsg(msg, module_name, 'boss')
-                return {}
-
             self.cc.group_sendmsg(msg, module_name)
             answer, env = self.cc.group_recvmsg(False)
             if answer and 'result' in answer.keys() and type(answer['result']) == list:

+ 1 - 1
src/bin/parkinglot/main.cc

@@ -70,7 +70,7 @@ my_command_handler(isc::data::ElementPtr command)
 {
     isc::data::ElementPtr answer = isc::data::Element::createFromString("{ \"result\": [0] }");
 
-    cout << "[XX] Handle command: " << endl << command->str() << endl;
+    cout << "[XX] Parkinglot handle command: " << endl << command->str() << endl;
     if (command->get(0)->stringValue() == "print_message") 
     {
         cout << command->get(1)->get("message") << endl;

+ 2 - 2
src/lib/config/cpp/ccsession.cc

@@ -90,8 +90,8 @@ CommandSession::CommandSession(std::string module_name,
 
     session_.establish();
     session_.subscribe(module_name, "*");
-    session_.subscribe("Boss", "*");
-    session_.subscribe("statistics", "*");
+    //session_.subscribe("Boss", "*");
+    //session_.subscribe("statistics", "*");
     read_data_definition(spec_file_name);
     sleep(1);
     // send the data specification

+ 6 - 0
src/lib/config/python/bind-cfgd.py

@@ -91,6 +91,9 @@ class ConfigManager:
             cmd = msg["command"]
             try:
                 if cmd[0] == "get_commands":
+                    print("[XX] bind-cfgd got get_commands");
+                    print("[XX] sending:")
+                    print(self.commands)
                     answer["result"] = [ 0, self.commands ]
                 elif cmd[0] == "get_data_spec":
                     if len(cmd) > 1 and cmd[1]['module_name'] != '':
@@ -163,6 +166,9 @@ class ConfigManager:
                 raise ie
         elif "data_specification" in msg:
             # todo: validate? (no direct access to spec as
+            # todo: use DataDefinition class?
+            print("[XX] bind-cfgd got specification:")
+            print(msg["data_specification"])
             spec = msg["data_specification"]
             if "config_data" in spec:
                 self.set_config(spec["module_name"], spec["config_data"])

+ 10 - 3
src/lib/config/python/isc/config/CCSession.py

@@ -41,9 +41,16 @@ class CCSession:
         self.__sendSpec()
         self.__getFullConfig()
 
-    #do we need getSocket()?
-
-    def checkCommand():
+    def getSocket(self):
+        """Returns the socket from the command channel session"""
+        return self._session._socket
+    
+    def getSession(self):
+        """Returns the command-channel session that is used, so the
+           application can use it directly"""
+        return self._session
+    
+    def checkCommand(self):
         """Check whether there is a command on the channel.
            Call the command callback function if so"""
         msg, env = self._session.group_recvmsg(False)