Chinaunix首页 | 论坛 | 博客
  • 博客访问: 272474
  • 博文数量: 42
  • 博客积分: 2718
  • 博客等级: 少校
  • 技术积分: 467
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-19 10:23
个人简介

青 田 酒 店 服 务 小 姐 价 格 186-6531-7773

文章分类

全部博文(42)

文章存档

2011年(36)

2010年(6)

分类: 数据库开发技术

2011-03-25 12:11:12

数据结构定义如下:
  1. #define TASK_NAME 128
  2. #define POL_NAME 128
  3. #define SAVE_PATH 128
  4. typedef struct
  5. {
  6.     unsigned int     pol_id;
  7.     char          pol_name[POL_NAME];
  8.     unsigned int    pol_type;//configfile scripts, crontab or file attribute
  9.     char        save_path[SAVE_PATH];
  10. }policy_t;

  11. typedef struct _dev_task_t
  12. {
  13.     unsigned int index;//id for table t_task_result
  14.     unsigned int host_id; //host id in t_dev
  15.     unsigned int host_addr; //host address
  16.     struct _dev_task_t * next;
  17. }dev_task_t;

  18. typedef struct _hosts_policy_t
  19. {
  20.     policy_t pol;
  21.     dev_task_t * dev_list;
  22.     struct _hosts_policy_t * next;
  23. }hosts_policy_t;

  24. typedef struct _task_t
  25. {
  26.     unsigned int task_id;
  27.     char task_name[TASK_NAME];
  28.     time_t     borthday;
  29.     hosts_policy_t * pol_list;
  30.     struct _task_t * next;
  31. }task_t;
用下图来表示:

初始化代码也比较复杂,从数据库中加载:
  1. int    DbMonitor::load_host_policy (task_t* pt)
  2. {
  3.     int nRet = 0;
  4.     task_t * pTask = pt;
  5.     unsigned int old_pol_id = 0;
  6.     //loop task list
  7.     while (pTask)
  8.     {
  9.         char szSQL[800] = {0};
  10.         pTask->pol_list = NULL;    
  11.         printf ("For task [%s]\n", pTask->task_name);
  12.         cm_log.LogMessage (CONFIG_SERVER_LOG, "[%s:%d] for task [%s]\n", __FILE__, __LINE__, pTask->task_name);
  13.         //load policy information for each task from table 't_task_result'
  14.         sprintf (szSQL,
  15.         "select t_task_result.Id, t_task_result.f_host_id, t_dev.dev_ip, t_task_result.f_pol_id, t_policy.name, t_policy.type, t_policy.savepath from t_task_result, t_dev, t_policy where t_task_result.f_task_id = %u and t_dev.Id = t_task_result.f_host_id and t_task_result.f_pol_id = t_policy.Id order by t_task_result.f_pol_id desc",
  16.             pTask->task_id);    
  17.         cm_log.LogMessage (CONFIG_SERVER_LOG, "[%s:%d]try to run sql[%s]\n", __FILE__, __LINE__, szSQL);

  18.         mysqlpp::Query query = m_pConnect->query(szSQL);
  19.         if (mysqlpp::StoreQueryResult res = query.store())
  20.         {
  21.             cm_log.LogMessage(CONFIG_SERVER_LOG,
  22.             "[%s:%d] success run sql[%s]\n", __FILE__, __LINE__, szSQL);
  23.             //get each item
  24.             mysqlpp::StoreQueryResult::const_iterator it;
  25.         
  26.             for (it = res.begin(); it != res.end(); ++it)
  27.             {
  28.                 mysqlpp::Row row = *it;
  29.                 dev_task_t * new_dev = NULL;
  30.                 
  31.                 unsigned int tmp_id = atol(((string)row[3]).c_str());

  32.                 //new policy comes
  33.                 if (old_pol_id != tmp_id)
  34.                 {
  35.                     hosts_policy_t * new_pol = new hosts_policy_t;
  36.                     old_pol_id = tmp_id;

  37.                     if (!new_pol)
  38.                     {

  39.                             cm_log.LogMessage(CONFIG_SERVER_LOG,
  40.                         "[%s:%d] malloc new hosts_policy_t for task [%s] failed\n",
  41.                             __FILE__, __LINE__, pTask->task_name);
  42.                             return -1;

  43.                     }
  44.                                                 
  45.                     memset (new_pol, 0, sizeof(hosts_policy_t));
  46.                     new_pol->next = NULL;
  47.                     new_pol->pol.pol_id = tmp_id;     
  48.                     new_pol->pol.pol_type = atol(((string)row[5]).c_str());     
  49.                     sprintf (new_pol->pol.pol_name, "%s", ((string)row[4]).c_str() );
  50.                     sprintf (new_pol->pol.save_path, "%s", ((string)row[6]).c_str() );
  51.                     new_pol->dev_list = NULL;
  52.                     if (!(pTask->pol_list))
  53.                     {
  54.                         pTask->pol_list = new_pol;
  55.                     }else
  56.                     {
  57.                         new_pol->next = pTask->pol_list;
  58.                         pTask->pol_list = new_pol;
  59.                     }
  60.                 }

  61.                 //then add new node of device
  62.                 new_dev = new dev_task_t;    
  63.                 if (!new_dev)
  64.                 {

  65.                     cm_log.LogMessage(CONFIG_SERVER_LOG,
  66.                     "[%s:%d] malloc new dev_task_t for task [%s]policy[%s] failed\n",
  67.                     __FILE__, __LINE__, pTask->task_name, pTask->pol_list->pol.pol_name);
  68.                     return -1;
  69.                 }
  70.                 memset (new_dev, 0, sizeof(dev_task_t));
  71.                 new_dev->next = NULL;
  72.                 
  73.                 new_dev->index = atol(((string)row[0]).c_str());
  74.                 new_dev->host_id = atol(((string)row[1]).c_str());
  75.                 new_dev->host_addr = atol(((string)row[2]).c_str());

  76.                 if (!pTask->pol_list->dev_list)
  77.                 {
  78.                     pTask->pol_list->dev_list = new_dev;
  79.                 }else
  80.                 {
  81.                     new_dev->next = pTask->pol_list->dev_list;
  82.                     pTask->pol_list->dev_list = new_dev;
  83.                 }
  84.                 nRet++;
  85.             }
  86.         }//end of result list
  87.         pTask = pTask->next;    
  88.     
  89.     }
  90.     return nRet;
  91. }

阅读(1870) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~