/storage/ndb/src/cw/cpcc-win32/csharp/simpleparser/SimpleCPCParser.cs

https://github.com/suhailsherif/MySQL-5.1- · C# · 375 lines · 296 code · 44 blank · 35 comment · 45 complexity · 6551cb117ce81938175c3c62c74662ff MD5 · raw file

  1. /* Copyright (C) 2004 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; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. using System;
  13. using System.Collections;
  14. using System.IO;
  15. using System.Windows.Forms;
  16. using NDB_CPC;
  17. using NDB_CPC.socketcomm;
  18. namespace NDB_CPC.simpleparser
  19. {
  20. /// <summary>
  21. /// Summary description for SimpleCPCParser.
  22. /// </summary>
  23. public class SimpleCPCParser
  24. {
  25. public SimpleCPCParser()
  26. {
  27. //
  28. // TODO: Add constructor logic here
  29. //
  30. }
  31. public static void parse(Process p, SocketComm comm)
  32. {
  33. string line=comm.readLine();//reader.ReadLine();
  34. while(line.Equals(""))
  35. {
  36. line=comm.readLine();
  37. }
  38. if(line.Equals("define process"))
  39. {
  40. defineProcess(p, comm);
  41. line="";
  42. return;
  43. }
  44. if(line.Equals("start process"))
  45. {
  46. startProcess(p,comm);
  47. line="";
  48. return;
  49. }
  50. if(line.Equals("stop process"))
  51. {
  52. stopProcess(p,comm);
  53. line="";
  54. return;
  55. }
  56. if(line.Equals("undefine process"))
  57. {
  58. undefineProcess(p,comm);
  59. line="";
  60. return;
  61. }
  62. }
  63. public static void parse(ArrayList processes, Computer c, SocketComm comm)
  64. {
  65. string line=comm.readLine();//reader.ReadLine();
  66. while(line.Equals(""))
  67. {
  68. line=comm.readLine();
  69. }
  70. if(line.Equals("start processes"))
  71. {
  72. listProcesses(processes, c, comm);
  73. line="";
  74. return;
  75. }
  76. }
  77. private static void defineProcess(Process p, SocketComm comm)
  78. {
  79. string line=comm.readLine();//reader.ReadLine();
  80. while(!line.Equals(""))
  81. {
  82. if(line.StartsWith("status:"))
  83. {
  84. line=line.Remove(0,7);
  85. line=line.Trim();
  86. if(line.Equals("1"))
  87. {
  88. p.setDefined(true);
  89. p.setStatus(Process.Status.Stopped);
  90. }
  91. else
  92. p.setDefined(false);
  93. }
  94. if(line.StartsWith("id:"))
  95. {
  96. line=line.Remove(0,3);
  97. line=line.Trim();
  98. p.setId(line);
  99. }
  100. line=comm.readLine();
  101. }
  102. }
  103. private static void startProcess(Process p, SocketComm comm)
  104. {
  105. string line=comm.readLine();//reader.ReadLine();
  106. while(!line.Equals(""))
  107. {
  108. if(line.StartsWith("status:"))
  109. {
  110. line=line.Remove(0,7);
  111. line=line.Trim();
  112. if(line.Equals("1"))
  113. p.setStatus(NDB_CPC.Process.Status.Running);
  114. else
  115. p.setStatus(NDB_CPC.Process.Status.Unknown);
  116. }
  117. if(line.StartsWith("id:"))
  118. {
  119. line=line.Remove(0,3);
  120. line=line.Trim();
  121. if(p.getId().Equals(line))
  122. {
  123. ;
  124. }
  125. else
  126. {
  127. //damn something is wrong
  128. p.setStatus(NDB_CPC.Process.Status.Unknown);
  129. }
  130. }
  131. line=comm.readLine();
  132. }
  133. }
  134. private static void undefineProcess(Process p, SocketComm comm)
  135. {
  136. string line=comm.readLine();//reader.ReadLine();
  137. while(!line.Equals(""))
  138. {
  139. if(line.StartsWith("status:"))
  140. {
  141. line=line.Remove(0,7);
  142. line=line.Trim();
  143. if(line.Equals("1"))
  144. p.setDefined(false);
  145. else
  146. p.setDefined(true);
  147. }
  148. if(line.StartsWith("id:"))
  149. {
  150. line=line.Remove(0,3);
  151. line=line.Trim();
  152. }
  153. line=comm.readLine();
  154. }
  155. }
  156. private static void stopProcess(Process p, SocketComm comm)
  157. {
  158. string line=comm.readLine();//reader.ReadLine();
  159. while(!line.Equals(""))
  160. {
  161. if(line.StartsWith("status:"))
  162. {
  163. line=line.Remove(0,7);
  164. line=line.Trim();
  165. if(line.Equals("1"))
  166. p.setStatus(NDB_CPC.Process.Status.Stopped);
  167. else
  168. p.setStatus(NDB_CPC.Process.Status.Unknown);
  169. }
  170. if(line.StartsWith("id:"))
  171. {
  172. line=line.Remove(0,3);
  173. line=line.Trim();
  174. if(p.getId().Equals(line))
  175. {
  176. ;
  177. }
  178. else
  179. {
  180. //damn something is wrong
  181. p.setStatus(NDB_CPC.Process.Status.Unknown);
  182. }
  183. }
  184. line=comm.readLine();
  185. }
  186. }
  187. private static void listProcesses(ArrayList processes, Computer c, SocketComm comm)
  188. {
  189. bool processExist = false;
  190. string line=comm.readLine();//reader.ReadLine();
  191. while(!line.Equals("end processes"))
  192. {
  193. if(line.Equals("process"))
  194. {
  195. line=comm.readLine();
  196. Process p = new Process();
  197. while(!line.Equals(""))
  198. {
  199. if(line.StartsWith("id:"))
  200. {
  201. string pid;
  202. line=line.Remove(0,3);
  203. pid=line.Trim();
  204. /*check if process already exist*/
  205. processExist=findProcess(processes,pid);
  206. if(!processExist)
  207. {
  208. p.setId(pid);
  209. }
  210. }
  211. if(line.StartsWith("name:"))
  212. {
  213. line=line.Remove(0,5);
  214. line=line.Trim();
  215. /*check if process already exist*/
  216. if(!processExist)
  217. {
  218. p.setName(line);
  219. }
  220. }
  221. if(line.StartsWith("path:"))
  222. {
  223. line=line.Remove(0,5);
  224. line=line.Trim();
  225. /*check if process already exist*/
  226. if(!processExist)
  227. {
  228. p.setPath(line);
  229. }
  230. }
  231. if(line.StartsWith("args:"))
  232. {
  233. line=line.Remove(0,5);
  234. line=line.Trim();
  235. /*check if process already exist*/
  236. if(!processExist)
  237. {
  238. p.setArgs(line);
  239. }
  240. }
  241. if(line.StartsWith("type:"))
  242. {
  243. line=line.Remove(0,5);
  244. line=line.Trim();
  245. /*check if process already exist*/
  246. if(!processExist)
  247. {
  248. }
  249. }
  250. if(line.StartsWith("cwd:"))
  251. {
  252. line=line.Remove(0,4);
  253. line=line.Trim();
  254. /*check if process already exist*/
  255. if(!processExist)
  256. {
  257. p.setCwd(line);
  258. }
  259. }
  260. if(line.StartsWith("env:"))
  261. {
  262. line=line.Remove(0,4);
  263. line=line.Trim();
  264. /*check if process already exist*/
  265. if(!processExist)
  266. {
  267. p.setEnv(line);
  268. }
  269. }
  270. if(line.StartsWith("owner:"))
  271. {
  272. line=line.Remove(0,6);
  273. line=line.Trim();
  274. /*check if process already exist*/
  275. if(!processExist)
  276. {
  277. p.setOwner(line);
  278. }
  279. }
  280. if(line.StartsWith("group:"))
  281. {
  282. line=line.Remove(0,6);
  283. line=line.Trim();
  284. /*check if process already exist*/
  285. if(!processExist)
  286. {
  287. p.setDatabase(line);
  288. }
  289. }
  290. if(line.StartsWith("status:"))
  291. {
  292. line=line.Remove(0,7);
  293. line=line.Trim();
  294. /*check if process already exist*/
  295. //if(!processExist)
  296. //{
  297. if(line.Equals("0"))
  298. p.setStatus(Process.Status.Stopped);
  299. if(line.Equals("1"))
  300. p.setStatus(Process.Status.Running);
  301. if(line.Equals("2"))
  302. p.setStatus(Process.Status.Unknown);
  303. //}
  304. }
  305. line=comm.readLine();
  306. }
  307. if(!processExist)
  308. {
  309. p.setComputer(c);
  310. p.setDefined(true);
  311. processes.Add(p);
  312. }
  313. processExist=false;
  314. }
  315. line=comm.readLine();
  316. }
  317. }
  318. private static bool findProcess(ArrayList processes, string pid)
  319. {
  320. foreach (Process p in processes)
  321. {
  322. if(p.getId().Equals(pid))
  323. return true;
  324. }
  325. return false;
  326. }
  327. }
  328. }