test_cpcd.cpp
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:4k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. #include <ndb_global.h>
  14. #include "../CpcClient.hpp"
  15. #include <Vector.hpp>
  16. SimpleCpcClient g_client("localhost", 1234);
  17. Vector<SimpleCpcClient::Process> g_procs;
  18. void define();
  19. void start(SimpleCpcClient::Process & p);
  20. void stop(SimpleCpcClient::Process & p);
  21. void undefine(SimpleCpcClient::Process & p);
  22. void list();
  23. SimpleCpcClient::Process* find(int id);
  24. #define ABORT() {ndbout_c("ABORT"); while(true); abort();}
  25. int name = 0;
  26. int
  27. main(void){
  28.   g_client.connect();
  29.   srand(time(0));
  30.   for(int i = 0; i<1000; i++){
  31.     int sz = g_procs.size();
  32.     int test = rand() % 100;
  33.     if(sz == 0 || test < 10){
  34.       define();
  35.       continue;
  36.     }
  37.     list();
  38.     int proc = rand() % g_procs.size();
  39.     SimpleCpcClient::Process & p = g_procs[proc];
  40.     if(p.m_status == "running" && test > 50){
  41.       ndbout_c("undefine %d: %s (running)", p.m_id, p.m_name.c_str());
  42.       undefine(p);
  43.       g_procs.erase(proc);
  44.       continue;
  45.     }
  46.     if(p.m_status == "running" && test <= 50){
  47.       ndbout_c("stop %d: %s(running)", p.m_id, p.m_name.c_str());
  48.       stop(p);
  49.       continue;
  50.     }
  51.     if(p.m_status == "stopped" && test > 50){
  52.       ndbout_c("undefine %d: %s(stopped)", p.m_id, p.m_name.c_str());
  53.       undefine(p);
  54.       g_procs.erase(proc);
  55.       continue;
  56.     }
  57.     if(p.m_status == "stopped" && test <= 50){
  58.       ndbout_c("start %d %s(stopped)", p.m_id, p.m_name.c_str());
  59.       start(p);
  60.       continue;
  61.     }
  62.     ndbout_c("Unknown: %s", p.m_status.c_str());
  63.   }
  64. }
  65. void define(){
  66.   SimpleCpcClient::Process m_proc;
  67.   m_proc.m_id = -1;
  68.   m_proc.m_type = "temporary";
  69.   m_proc.m_owner = "atrt";  
  70.   m_proc.m_group = "group";    
  71.   //m_proc.m_cwd
  72.   //m_proc.m_env
  73.   //proc.m_proc.m_stdout = "log.out";
  74.   //proc.m_proc.m_stderr = "2>&1";
  75.   //proc.m_proc.m_runas = proc.m_host->m_user;
  76.   m_proc.m_ulimit = "c:unlimited";
  77.   if((rand() & 15) >= 0){
  78.     m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "sleep");
  79.     m_proc.m_path.assign("/bin/sleep");
  80.     m_proc.m_args = "600";
  81.   } else {
  82.     m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "test.sh");
  83.     m_proc.m_path.assign("/home/jonas/run/cpcd/test.sh");
  84.     m_proc.m_args = "600";
  85.   }
  86.   g_procs.push_back(m_proc);
  87.   
  88.   Properties reply;
  89.   if(g_client.define_process(g_procs.back(), reply) != 0){
  90.     ndbout_c("define %s -> ERR", m_proc.m_name.c_str());
  91.     reply.print();
  92.     ABORT();
  93.   }
  94.   ndbout_c("define %s -> %d", m_proc.m_name.c_str(), m_proc.m_id);
  95. }
  96. void start(SimpleCpcClient::Process & p){
  97.   Properties reply;
  98.   if(g_client.start_process(p.m_id, reply) != 0){
  99.     reply.print();
  100.     ABORT();
  101.   }
  102. }
  103. void stop(SimpleCpcClient::Process & p){
  104.   Properties reply;
  105.   if(g_client.stop_process(p.m_id, reply) != 0){
  106.     reply.print();
  107.     ABORT();
  108.   }
  109. }
  110. void undefine(SimpleCpcClient::Process & p){
  111.   Properties reply;
  112.   if(g_client.undefine_process(p.m_id, reply) != 0){
  113.     reply.print();
  114.     ABORT();  
  115.   }
  116. }
  117. void list(){
  118.   Properties reply;
  119.   Vector<SimpleCpcClient::Process> procs;
  120.   if(g_client.list_processes(procs, reply) != 0){
  121.     reply.print();
  122.     ABORT();
  123.   }
  124.   for(Uint32 i = 0; i<procs.size(); i++){
  125.     SimpleCpcClient::Process * p = find(procs[i].m_id);
  126.     if(p != 0){
  127.       p->m_status = procs[i].m_status;
  128.     }
  129.   }
  130. }
  131. SimpleCpcClient::Process* find(int id){
  132.   for(Uint32 i = 0; i<g_procs.size(); i++){
  133.     if(g_procs[i].m_id == id)
  134.       return &g_procs[i];
  135.   }
  136.   return 0;
  137. }