00001 using System; 00002 using System.Drawing; 00003 using System.Collections; 00004 using System.ComponentModel; 00005 using System.Windows.Forms; 00006 using System.Data; 00007 using System.IO; 00008 using NDB_CPC.socketcomm; 00009 using NDB_CPC.simpleparser; 00010 00011 00012 namespace NDB_CPC 00013 { 00017 public class Computer 00018 { 00019 public enum Status {Disconnected=1,Connected=2, Unknown=3} 00020 private string m_ip; 00021 private int m_cpcdPort; 00022 private string m_name; 00023 private Status m_status; 00024 private ArrayList m_processes; 00025 private SocketComm m_socket; 00026 public Computer(string name, int port) 00027 { 00028 m_name = name; 00029 m_status = Status.Disconnected; 00030 m_processes = new ArrayList(); 00031 m_cpcdPort=port; 00032 m_socket = new SocketComm(m_name,m_cpcdPort); 00033 } 00034 00035 public Computer(string name, string ip) 00036 { 00037 m_ip = ip; 00038 m_name = name; 00039 m_status = Status.Disconnected; 00040 m_processes = new ArrayList(); 00041 m_cpcdPort=1234; //default port 00042 m_socket = new SocketComm(m_ip,m_cpcdPort); 00043 } 00044 00045 public void connectToCpcd() 00046 { 00047 m_socket.doConnect(); 00048 } 00049 00050 private bool sendMessage(string str) 00051 { 00052 return m_socket.writeMessage(str); 00053 00054 } 00055 00056 public string getName() {return m_name;} 00057 public string getIp() {return m_ip;} 00058 public ArrayList getProcesses() 00059 { 00060 if(m_processes.Count>0) 00061 return m_processes; 00062 else 00063 return null; 00064 } 00065 public string getStatusString() 00066 { 00067 try 00068 { 00069 if(m_socket.isConnected()) 00070 return "Connected"; 00071 else 00072 return "Disconnected"; 00073 } 00074 catch(Exception e) 00075 { 00076 return "Unknown"; 00077 } 00078 } 00079 00080 00081 public bool isConnected() 00082 { 00083 if(m_socket.isConnected()) 00084 return true; 00085 return false; 00086 } 00087 00088 public Status getStatus() 00089 { 00090 try 00091 { 00092 if(m_socket.isConnected()) 00093 return Status.Connected; 00094 else 00095 return Status.Disconnected; 00096 } 00097 catch(Exception e) 00098 { 00099 return Status.Unknown; 00100 } 00101 } 00102 00103 public void setStatus(Status status) 00104 { 00105 m_status=status; 00106 } 00107 00108 public void addProcess(Process process) 00109 { 00110 m_processes.Add(process); 00111 } 00112 00113 public Process getProcessByName(string name) 00114 { 00115 foreach(Process process in m_processes) 00116 { 00117 if(process.getName().Equals(name)) 00118 return process; 00119 } 00120 return null; 00121 } 00122 00123 00124 public bool removeProcess(string name, string database) 00125 { 00126 foreach(Process p in m_processes) 00127 { 00128 if(p.getName().Equals(name) && p.getDatabase().Equals(database)) 00129 { 00130 m_processes.Remove(p); 00131 return true; 00132 } 00133 } 00134 return false; 00135 } 00136 00137 public void disconnect() 00138 { 00139 m_socket.disconnect(); 00140 } 00141 public Process getProcess(string id) 00142 { 00143 foreach(Process process in m_processes) 00144 { 00145 if(process.getId().Equals(id)) 00146 return process; 00147 } 00148 return null; 00149 } 00150 00151 public int listProcesses() 00152 { 00153 string list = "list processes\n\n"; 00154 00155 if(!sendMessage(list)) 00156 return -2; 00157 00158 SimpleCPCParser.parse(m_processes, this, m_socket); 00159 return 1; 00160 } 00161 00162 public int defineProcess(Process p) 00163 { 00164 string define = "define process \n"; 00165 define = define + "name:" + p.getName() + "\n"; 00166 define = define + "group:" + p.getDatabase() + "\n"; 00167 define = define + "env:" + "NDB_CONNECTSTRING="+p.getConnectString() ; 00168 if(p.getEnv().Equals("")) 00169 define = define + "\n"; 00170 else 00171 define = define + " " + p.getEnv() + "\n"; 00172 00173 //if(p.getPath().EndsWith("\\")) 00174 // define = define + "path:" + p.getPath()+ "ndb" + "\n"; 00175 //else 00176 define = define + "path:" + p.getPath() + "\n"; 00177 define = define + "args:" + p.getArgs() + "\n"; 00178 define = define + "type:" + "permanent" + "\n"; 00179 define = define + "cwd:" + p.getCwd() + "\n"; 00180 define = define + "owner:" + "ejohson" + "\n\n"; 00181 00182 if(!sendMessage(define)) 00183 return -2; 00184 00185 SimpleCPCParser.parse(p, m_socket); 00186 if(p.isDefined()) 00187 return 1; 00188 else 00189 return -1; 00190 00191 } 00192 00193 public int startProcess(Process p) 00194 { 00195 if(!p.isDefined()) 00196 { 00197 this.defineProcess(p); 00198 if(!p.isDefined()) 00199 return -4; //process misconfigured 00200 00201 } 00202 string start= "start process \n"; 00203 start = start + "id:" + p.getId() + "\n\n"; 00204 if(!sendMessage(start)) 00205 return -2; 00206 SimpleCPCParser.parse(p, m_socket); 00207 if(p.getStatus().Equals(Process.Status.Running)) 00208 return 1; 00209 else 00210 return -1; 00211 } 00212 00213 public int stopProcess(Process p) 00214 { 00215 if(!p.isDefined()) 00216 { 00217 return -4; //process not defined 00218 } 00219 string stop= "stop process \n"; 00220 stop = stop + "id:" + p.getId() + "\n\n"; 00221 if(!sendMessage(stop)) 00222 return -2; 00223 SimpleCPCParser.parse(p, m_socket); 00224 00225 if(p.getStatus().Equals(Process.Status.Stopped)) 00226 return 1; 00227 else 00228 return -1; 00229 } 00230 00231 public int undefineProcess(Process p) 00232 { 00233 if(!p.isDefined()) 00234 { 00235 return -4; //process not defined 00236 } 00237 string undefine= "undefine process \n"; 00238 undefine = undefine + "id:" + p.getId() + "\n\n"; 00239 if(!sendMessage(undefine)) 00240 return -2; 00241 SimpleCPCParser.parse(p, m_socket); 00242 if(!p.isDefined()) 00243 { 00244 return 1; 00245 00246 } 00247 return -1; 00248 } 00249 00250 public int getCpcdPort() 00251 { 00252 return this.m_cpcdPort; 00253 } 00254 00255 } 00256 }
1.4.7

