00001 00023 #ifndef APP_MANAGER_H_ 00024 #define APP_MANAGER_H_ 00025 00026 #include <iostream> 00027 #include <vector> 00028 #include <map> 00029 #include <boost/bind.hpp> 00030 #include <boost/thread.hpp> 00031 #include <boost/function.hpp> 00032 #include <boost/shared_ptr.hpp> 00033 #include <boost/noncopyable.hpp> 00034 #include <ros/ros.h> 00035 #include <pluginlib/class_list_macros.h> 00036 #include <pluginlib/class_loader.h> 00037 #include <pluginlib/class_list_macros.h> 00038 00039 #include "micros_swarm/application.h" 00040 #include "app_loader/AppLoad.h" 00041 #include "app_loader/AppUnload.h" 00042 #include "app_loader/RTDestroy.h" 00043 00044 namespace micros_swarm{ 00045 struct AppInstance{ 00046 std::string app_name_; 00047 std::string app_type_; 00048 boost::shared_ptr<Application> app_ptr_; 00049 bool running_; 00050 uint8_t status_; 00051 }; 00052 00053 struct Worker{ 00054 public: 00055 Worker(int id); 00056 ~Worker(); 00057 void addApp(AppInstance* app); 00058 void removeApp(const std::string& app_name); 00059 int getAppNum(); 00060 AppInstance* getAppInstance(const std::string& app_name); 00061 private: 00062 void workFunc(); 00063 int id_; 00064 volatile bool run_; 00065 boost::thread* thread_; 00066 std::vector<AppInstance*> apps_; 00067 }; 00068 00069 class AppManager 00070 { 00071 public: 00072 AppManager(); 00073 AppManager(int worker_num); 00074 ~AppManager(); 00075 void stop(); 00076 private: 00077 bool recordExist(const std::string& name); 00078 void addRecord(const std::string& name, int worker_id); 00079 void removeRecord(const std::string& name); 00080 int allocateWorker(); 00081 void insertOrUpdatePluginUseCount(const std::string& type); 00082 void decreasePluginUseCount(const std::string& type); 00083 int getPluginUseCount(const std::string& type); 00084 bool loadService(app_loader::AppLoad::Request &req, app_loader::AppLoad::Response &resp); 00085 bool unloadService(app_loader::AppUnload::Request &req, app_loader::AppUnload::Response &resp); 00086 pluginlib::ClassLoader<micros_swarm::Application> app_loader_; 00087 ros::ServiceServer app_load_srv_, app_unload_srv_; 00088 ros::ServiceClient client_; 00089 int worker_num_; 00090 std::vector<Worker*> worker_table_; 00091 std::vector<uint16_t> load_table_; 00092 std::map<std::string, int> apps_record_; 00093 std::map<std::string, int> plugin_use_count_; 00094 }; 00095 }; 00096 00097 #endif