00001 /* Copyright (C) 2003 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 00018 #include <ndb_global.h> 00019 #include "../CpcClient.hpp" 00020 #include <Vector.hpp> 00021 00022 SimpleCpcClient g_client("localhost", 1234); 00023 Vector<SimpleCpcClient::Process> g_procs; 00024 00025 void define(); 00026 void start(SimpleCpcClient::Process & p); 00027 void stop(SimpleCpcClient::Process & p); 00028 void undefine(SimpleCpcClient::Process & p); 00029 void list(); 00030 SimpleCpcClient::Process* find(int id); 00031 00032 #define ABORT() {ndbout_c("ABORT"); while(true); abort();} 00033 00034 int name = 0; 00035 00036 int 00037 main(void){ 00038 00039 g_client.connect(); 00040 00041 srand(time(0)); 00042 for(int i = 0; i<1000; i++){ 00043 int sz = g_procs.size(); 00044 int test = rand() % 100; 00045 if(sz == 0 || test < 10){ 00046 define(); 00047 continue; 00048 } 00049 00050 list(); 00051 00052 int proc = rand() % g_procs.size(); 00053 SimpleCpcClient::Process & p = g_procs[proc]; 00054 if(p.m_status == "running" && test > 50){ 00055 ndbout_c("undefine %d: %s (running)", p.m_id, p.m_name.c_str()); 00056 undefine(p); 00057 g_procs.erase(proc); 00058 continue; 00059 } 00060 if(p.m_status == "running" && test <= 50){ 00061 ndbout_c("stop %d: %s(running)", p.m_id, p.m_name.c_str()); 00062 stop(p); 00063 continue; 00064 } 00065 if(p.m_status == "stopped" && test > 50){ 00066 ndbout_c("undefine %d: %s(stopped)", p.m_id, p.m_name.c_str()); 00067 undefine(p); 00068 g_procs.erase(proc); 00069 continue; 00070 } 00071 if(p.m_status == "stopped" && test <= 50){ 00072 ndbout_c("start %d %s(stopped)", p.m_id, p.m_name.c_str()); 00073 start(p); 00074 continue; 00075 } 00076 ndbout_c("Unknown: %s", p.m_status.c_str()); 00077 } 00078 } 00079 00080 void define(){ 00081 SimpleCpcClient::Process m_proc; 00082 m_proc.m_id = -1; 00083 m_proc.m_type = "temporary"; 00084 m_proc.m_owner = "atrt"; 00085 m_proc.m_group = "group"; 00086 //m_proc.m_cwd 00087 //m_proc.m_env 00088 //proc.m_proc.m_stdout = "log.out"; 00089 //proc.m_proc.m_stderr = "2>&1"; 00090 //proc.m_proc.m_runas = proc.m_host->m_user; 00091 m_proc.m_ulimit = "c:unlimited"; 00092 if((rand() & 15) >= 0){ 00093 m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "sleep"); 00094 m_proc.m_path.assign("/bin/sleep"); 00095 m_proc.m_args = "600"; 00096 } else { 00097 m_proc.m_name.assfmt("%d-%d-%s", getpid(), name++, "test.sh"); 00098 m_proc.m_path.assign("/home/jonas/run/cpcd/test.sh"); 00099 m_proc.m_args = "600"; 00100 } 00101 g_procs.push_back(m_proc); 00102 00103 Properties reply; 00104 if(g_client.define_process(g_procs.back(), reply) != 0){ 00105 ndbout_c("define %s -> ERR", m_proc.m_name.c_str()); 00106 reply.print(); 00107 ABORT(); 00108 } 00109 ndbout_c("define %s -> %d", m_proc.m_name.c_str(), m_proc.m_id); 00110 } 00111 00112 void start(SimpleCpcClient::Process & p){ 00113 Properties reply; 00114 if(g_client.start_process(p.m_id, reply) != 0){ 00115 reply.print(); 00116 ABORT(); 00117 } 00118 } 00119 00120 void stop(SimpleCpcClient::Process & p){ 00121 Properties reply; 00122 if(g_client.stop_process(p.m_id, reply) != 0){ 00123 reply.print(); 00124 ABORT(); 00125 } 00126 } 00127 00128 void undefine(SimpleCpcClient::Process & p){ 00129 Properties reply; 00130 if(g_client.undefine_process(p.m_id, reply) != 0){ 00131 reply.print(); 00132 ABORT(); 00133 } 00134 } 00135 00136 void list(){ 00137 Properties reply; 00138 Vector<SimpleCpcClient::Process> procs; 00139 if(g_client.list_processes(procs, reply) != 0){ 00140 reply.print(); 00141 ABORT(); 00142 } 00143 00144 for(Uint32 i = 0; i<procs.size(); i++){ 00145 SimpleCpcClient::Process * p = find(procs[i].m_id); 00146 if(p != 0){ 00147 p->m_status = procs[i].m_status; 00148 } 00149 } 00150 } 00151 SimpleCpcClient::Process* find(int id){ 00152 for(Uint32 i = 0; i<g_procs.size(); i++){ 00153 if(g_procs[i].m_id == id) 00154 return &g_procs[i]; 00155 } 00156 return 0; 00157 }
1.4.7

