xorp

module_manager.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 
00021 #ifndef __RTRMGR_MODULE_MANAGER_HH__
00022 #define __RTRMGR_MODULE_MANAGER_HH__
00023 
00024 
00025 #include "libxorp/timer.hh"
00026 #include "libxorp/callback.hh"
00027 #include "generic_module_manager.hh"
00028 
00029 class EventLoop;
00030 class MasterConfigTree;
00031 class ModuleManager;
00032 class Rtrmgr;
00033 class RunCommand;
00034 
00038 class Module : public GenericModule {
00039 public:
00050     Module(ModuleManager& mmgr, const string& name, const string& path,
00051        const string& expath, bool verbose);
00052 
00056     virtual ~Module();
00057 
00063     void new_status(ModuleStatus new_status);
00064 
00070     string str() const;
00071 
00077     ModuleManager& module_manager() const { return _mmgr; }
00078 
00084     const string& expath() const { return _expath; }
00085 
00093     int execute(bool do_exec, bool is_verification,
00094         XorpCallback1<void, bool>::RefPtr cb);
00095 
00101     int restart();
00102 
00108     void terminate(XorpCallback0<void>::RefPtr cb);
00109 
00115     void terminate_with_prejudice(XorpCallback0<void>::RefPtr cb);
00116 
00128     void module_exited(bool success, bool is_signal_terminated,
00129                int term_signal, bool is_coredumped);
00130 
00136     void module_stopped(int stop_signal);
00137 
00138 private:
00139     void module_restart_cb(bool success);
00140 
00141     static const TimeVal SHUTDOWN_TIMEOUT_TIMEVAL;
00142     ModuleManager& _mmgr;   // The module manager to use
00143     string  _path;      // The path to the program
00144     string  _expath;    // The expanded absolute path to the program
00145     bool    _do_exec;   // false indicates we're running in test mode,
00146                 // when we may not actually start any processes
00147     bool    _verbose;   // Set to true if output is verbose
00148     XorpTimer   _shutdown_timer; // A timer used during shutdown
00149     XorpCallback0<void>::RefPtr _terminate_cb; // The cb when module terminated
00150 };
00151 
00152 class ModuleManager : public GenericModuleManager {
00153 public:
00154     class Process;
00155 
00166     ModuleManager(EventLoop& eventloop, Rtrmgr& rtrmgr,
00167           bool do_restart, bool verbose,
00168           const string& xorp_root_dir,
00169           const string& xorp_module_dir);
00170 
00174     virtual ~ModuleManager();
00175 
00185     bool new_module(const string& module_name, const string& path,
00186             string& error_msg);
00187 
00199     int start_module(const string& module_name, bool do_exec,
00200              bool is_verification,
00201              XorpCallback1<void, bool>::RefPtr cb);
00202 
00210     int kill_module(const string& module_name,
00211            XorpCallback0<void>::RefPtr cb);
00212 
00219     bool module_is_running(const string& module_name) const;
00220 
00227     bool module_has_started(const string& module_name) const;
00228 
00232     void shutdown();
00233 
00239     bool is_shutdown_completed() const;
00240 
00248     void module_status_changed(const string& module_name, 
00249                    Module::ModuleStatus old_status,
00250                    Module::ModuleStatus new_status);
00251 
00257     list<string> get_module_names() const;
00258 
00265     list<Module *> find_running_modules_by_path(const string& expath);
00266 
00274     int execute_process(const string& expath, string& error_msg);
00275 
00288     void process_exited(const string& expath, bool success,
00289             bool is_signal_terminated, int term_signal,
00290             bool is_coredumped);
00291 
00298     void process_stopped(const string& expath, int stop_signal);
00299 
00306     ModuleManager::Process* find_process_by_path(const string& expath);
00307 
00313     const string& xorp_root_dir() const { return _xorp_root_dir; }
00314 
00320     const string& xorp_module_dir() const { return _xorp_module_dir; }
00321 
00327     MasterConfigTree* master_config_tree() const { return _master_config_tree; }
00328 
00334     void set_master_config_tree(MasterConfigTree* v) { _master_config_tree = v; }
00335 
00341     bool do_restart() const { return _do_restart; }
00342 
00343     class Process {
00344     public:
00351     Process(ModuleManager& mmgr, const string& expath);
00352 
00356     ~Process();
00357 
00364     int startup(string& error_msg);
00365 
00369     void terminate();
00370 
00374     void terminate_with_prejudice();
00375 
00376     private:
00377     void stdout_cb(RunCommand* run_command, const string& output);
00378     void stderr_cb(RunCommand* run_command, const string& output);
00379     void done_cb(RunCommand* run_command, bool success,
00380              const string& error_msg);
00381     void stopped_cb(RunCommand* run_command, int stop_signal);
00382 
00383     ModuleManager&  _mmgr;      // The module manager to use
00384     string      _expath;    // The absolute expanded path
00385     RunCommand* _run_command;   // The result running command
00386     };
00387 
00388 private:
00389     void module_shutdown_cb(string module_name);
00390     int expand_execution_path(const string& path, string& expath,
00391                   string& error_msg);
00392 
00393     Rtrmgr&     _rtrmgr;    // The router manager to use
00394     MasterConfigTree*   _master_config_tree; // The master configuration tree
00395     map<string, Process *> _expath2process; // Map exec path to running process
00396     bool        _do_restart;    // Set to true to enable module restart
00397     bool        _verbose;   // Set to true if output is verbose
00398     string      _xorp_root_dir; // The root of the XORP tree
00399     string      _xorp_module_dir; // Module directory
00400 };
00401 
00402 #endif // __RTRMGR_MODULE_MANAGER_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations