summaryrefslogtreecommitdiff
path: root/storage/ndb/src/cw/cpcc-win32/csharp/Computer.cs
blob: 9763fac562212e25170bc5981eff94d7301e0954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using NDB_CPC.socketcomm;
using NDB_CPC.simpleparser;


namespace NDB_CPC
{
	/// <summary>
	/// Summary description for Computer.
	/// </summary>
	public class Computer
	{
		public enum	Status {Disconnected=1,Connected=2, Unknown=3}
		private string m_ip;
		private int m_cpcdPort;
		private string m_name;
		private Status m_status;
		private ArrayList m_processes;
		private SocketComm m_socket;
		public Computer(string name, int port)
		{
			m_name = name;
			m_status = Status.Disconnected;
			m_processes = new ArrayList();
			m_cpcdPort=port;
			m_socket = new SocketComm(m_name,m_cpcdPort);
		}

		public Computer(string name, string ip)
		{
			m_ip = ip;
			m_name = name;
			m_status = Status.Disconnected;
			m_processes = new ArrayList();
			m_cpcdPort=1234; //default port
			m_socket = new SocketComm(m_ip,m_cpcdPort);
		}

		public void connectToCpcd()
		{
			m_socket.doConnect();
		}

		private bool sendMessage(string str)
		{	
			return m_socket.writeMessage(str);
			
		}
		
		public string getName() {return m_name;}
		public string getIp() {return m_ip;}
		public ArrayList getProcesses() 
		{ 
			if(m_processes.Count>0)
				return m_processes;	
			else
				return null;
		}
		public string getStatusString() 
		{
			try 
			{
				if(m_socket.isConnected())
					return "Connected";
				else
					return "Disconnected";
			}
			catch(Exception e)
			{
				return "Unknown";
			}
		}

		
		public bool isConnected() 
		{
			if(m_socket.isConnected())
				return true;
			return false;		
		}

		public Status getStatus() 
		{
			try 
			{
				if(m_socket.isConnected())
					return Status.Connected;
				else
					return Status.Disconnected;
			}
			catch(Exception e)
			{
				return Status.Unknown;
			}
		}

		public void setStatus(Status status) 
		{
			m_status=status;
		}

		public void addProcess(Process process) 
		{
			m_processes.Add(process);
		}

		public Process getProcessByName(string name) 
		{
			foreach(Process process in m_processes)
			{
				if(process.getName().Equals(name))
					return process;
			}
			return null;
		}


		public bool removeProcess(string name, string database)
		{
			foreach(Process p in m_processes)
			{
				if(p.getName().Equals(name) && p.getDatabase().Equals(database))
				{
					m_processes.Remove(p);
					return true;
				}
			}
			return false;
		}

		public void disconnect()
		{	
			m_socket.disconnect();
		}
		public Process getProcess(string id) 
		{
			foreach(Process process in m_processes)
			{
				if(process.getId().Equals(id))
					return process;
			}
			return null;
		}

		public int listProcesses()
		{
			string list = "list processes\n\n";

			if(!sendMessage(list))
				return -2;
			
			SimpleCPCParser.parse(m_processes, this, m_socket);
			return 1;
		}

		public int defineProcess(Process p)
		{
			string define = "define process \n";
			define = define + "name:" + p.getName() + "\n";
			define = define + "group:" + p.getDatabase() + "\n";
			define = define + "env:" + "NDB_CONNECTSTRING="+p.getConnectString() ;
			if(p.getEnv().Equals(""))
				define = define + "\n";
			else
				define = define + " " + p.getEnv() + "\n";
				
			//if(p.getPath().EndsWith("\\")) 
			//	define = define + "path:" + p.getPath()+ "ndb" + "\n";
			//else
			define = define + "path:" + p.getPath() + "\n";
			define = define + "args:" + p.getArgs() + "\n";
			define = define + "type:" + "permanent" + "\n";
			define = define + "cwd:" + p.getCwd() + "\n";
			define = define + "owner:" + "ejohson" + "\n\n";
			
			if(!sendMessage(define))
				return -2;
			
			SimpleCPCParser.parse(p, m_socket);
			if(p.isDefined())
				return 1;
			else 
				return -1;
		
		}

		public int startProcess(Process p)
		{
			if(!p.isDefined())
			{
				this.defineProcess(p);
				if(!p.isDefined())
					return -4; //process misconfigured

			}
			string start= "start process \n";
			start = start + "id:" + p.getId() + "\n\n";
			if(!sendMessage(start))
				return -2;
			SimpleCPCParser.parse(p, m_socket);
			if(p.getStatus().Equals(Process.Status.Running))
				return 1;
			else 
				return -1;
		}

		public int stopProcess(Process p)
		{
			if(!p.isDefined())
			{
				return -4; //process not defined
			}
			string stop= "stop process \n";
			stop = stop + "id:" + p.getId() + "\n\n";
			if(!sendMessage(stop))
				return -2;
			SimpleCPCParser.parse(p, m_socket);
	
			if(p.getStatus().Equals(Process.Status.Stopped))
				return 1;
			else 
				return -1;
		}

		public int undefineProcess(Process p)
		{
			if(!p.isDefined())
			{
				return -4; //process not defined
			}
			string undefine= "undefine process \n";
			undefine = undefine + "id:" + p.getId() + "\n\n";
			if(!sendMessage(undefine))
				return -2;
			SimpleCPCParser.parse(p, m_socket);
			if(!p.isDefined()) 
			{
				return 1;

			}
			return -1;
		}

		public int getCpcdPort()
		{
			return this.m_cpcdPort;
		}

	}
}