File: ULProcess.m

package info (click to toggle)
adun.app 0.81-13
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,600 kB
  • sloc: objc: 70,912; ansic: 6,662; yacc: 394; python: 75; cpp: 36; makefile: 33; xml: 15; awk: 3
file content (322 lines) | stat: -rwxr-xr-x 7,388 bytes parent folder | download | duplicates (2)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
   Project: UL

   Copyright (C) 2005 Michael Johnston & Jordi Villa-Freixa

   Author: Michael Johnston

   Created: 2005-05-23 14:11:56 +0200 by michael johnston

   This application is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This application is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/

#include "ULFramework/ULProcess.h"

@implementation ULProcess

+ (id) processWithInputData: (NSDictionary*) aDict 
	simulationTemplate: (id) simulationTemplate
	additionalArguments: (NSArray*) anArray
	host: (NSString*) aName;
{
	id process;

	process = [[ULProcess alloc]
			initWithInputData: aDict 
			simulationTemplate: simulationTemplate
			additionalArguments: anArray
			host: aName];
			
	return [process autorelease];
}

- (id) initWithInputData: (NSDictionary*) aDict 
	simulationTemplate: (id) simulationTemplate
	additionalArguments: (NSArray*) anArray
	host: (NSString*) aName
{
	NSEnumerator* dataEnum;
	NSMutableDictionary* dataDict = [NSMutableDictionary dictionary];;
	id data;

	if((self = [super init]))
	{
		dataSets = nil;
		simulationData = nil;
		terminationError = nil;
		processIdentifier = -1;
		isFinished = NO;
		inputDataSent = NO;
		templateSent = NO;
		
		simulationArgs = [NSMutableArray arrayWithObjects: @"-RunMode", @"Server",
				  @"-CreateLogFiles", @"YES",
				  @"-ConnectToAdServer", @"YES",
				  @"-RunInteractive", @"YES",
				  nil];
		[simulationArgs retain];
		
		if(anArray != nil)
			[simulationArgs addObjectsFromArray: anArray];
		
		[self setValue: @"N/A" forMetadataKey: @"Length"];
		[self setValue: aName forMetadataKey: @"Host"];
		[self setValue: @"N/A" forMetadataKey: @"Started"];
		[self setValue: @"Waiting" forMetadataKey: @"Status"];
		[self setValue: [NSNumber numberWithInt: processIdentifier]
			forMetadataKey: @"Process Identifier"];
		[self setValue: [simulationTemplate 
				    valueForKeyPath: @"metadata.simulationName"]
		   forMetadataKey: @"Name"];	
		[self setValue: simulationArgs
			forMetadataKey: @"Arguments"];	

		inputData = [aDict retain];
		dataEnum = [inputData objectEnumerator];
		while((data = [dataEnum nextObject]))
			[dataDict setObject: [data identification] 
				forKey: [data name]];
		
		[self setValue: dataDict
			forMetadataKey: @"InputData"];
		//FIXME: Template name?
		simTemplate = [simulationTemplate retain];
	}

	return self;
}


- (void) dealloc
{
	[simulationArgs release];
	[dataSets release];
	[inputData release];
	[simTemplate release];
	[simulationData release];
	[terminationError release];

	[super dealloc];
}

- (NSArray*) arguments
{
	return [[simulationArgs copy] autorelease];
}

- (void) addArguments: (NSArray*) anArray
{
	if(anArray != nil)
	{
		[simulationArgs addObjectsFromArray: anArray];
		[self setValue: simulationArgs
			forMetadataKey: @"Arguments"];	
	}
}

- (int) processIdentifier
{
	return [[self valueForMetadataKey: @"Process Identifier"] intValue];
}

- (NSString*) processHost
{
	return [self valueForMetadataKey: @"Host"];
}

- (NSString*) processStatus
{
	return [self valueForMetadataKey: @"Status"];
}

- (NSDate*) started
{
	return [self valueForMetadataKey: @"Started"];
}

- (id) length
{
	return [self valueForMetadataKey: @"Length"];
}

- (NSArray*) controllerResults
{
	return [[dataSets retain] autorelease];
}

- (id) simulationData
{
	return [[simulationData retain] autorelease];
}

- (NSError*) terminationError
{
	return [[terminationError retain] autorelease];
}

- (BOOL) hasSentProcessData
{
	return (templateSent && inputDataSent);
}

- (BOOL) isFinished
{
	return isFinished;
}

- (void) waitUntilFinished
{
	while(!isFinished)
		[[NSRunLoop currentRunLoop] 
		 runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 5]];
	
}

/*
 * ULClientInterface Methods
*/

- (void) setStarted: (NSDate*) value
{
	[self setValue: value
	forMetadataKey: @"Started"];
}

- (void) setProcessStatus: (NSString*) value
{
	[self setValue: value forMetadataKey: @"Status"];
}

- (void) setProcessIdentifier: (int) number
{
	[self setValue: [NSNumber numberWithInt: number] 
		forMetadataKey: @"Process Identifier"];
}

- (void) setControllerResults: (NSArray*) results
{
	NSDebugLLog(@"ULProcess", 
		@"Received controller data %@", results);

	[dataSets release];
	dataSets = [results retain];
}

- (void) setSimulationData: (AdSimulationData*) data
{
	NSString* dataDir;
	AdFileSystemSimulationStorage* storage;

	if(simulationData != nil)
		[simulationData release];
	
	simulationData = [data retain];

	//Retrieve the data ID and hence find where
	//the data is stored
	//FIXME: temp until program args are transfered here
	dataDir = [[[ULDatabaseInterface databaseInterface]
			primaryFileSystemBackend] simulationDir];

	//FIXME: Add error handling		
	storage = [AdFileSystemSimulationStorage 
			storageForSimulation: simulationData 
			inDirectory: dataDir 
			mode: AdSimulationStorageReadMode 
			error: NULL];
	[simulationData setDataStorage: storage];
}

- (bycopy NSDictionary*) simulationTemplate
{
	templateSent = YES;
	return [[simTemplate retain] autorelease];
}

- (bycopy NSDictionary*) inputData
{
	inputDataSent = YES;
	return [[inputData retain] autorelease];
}

- (void) processDidTerminate: (NSError*) error
{
	NSTimeInterval interval;
	NSString* length;
	NSMutableDictionary* userInfo;

	NSDebugLLog(@"ULProcess", @"Received termination message");
	
	isFinished = YES;
	[self setValue: @"Finished" forKey: @"processStatus"];

	interval = -1*[date timeIntervalSinceNow];
	length = ULConvertTimeIntervalToString(interval);
	[self setValue: length
		forMetadataKey: @"Length"];

	processIdentifier = -1;

	userInfo  = [NSMutableDictionary dictionary];
	[userInfo setObject: self forKey: @"ULTerminatedProcess"];
	terminationError = [error retain];
	if(error != nil)
		[userInfo setObject: error forKey: @"AdTerminationErrorKey"];

	NSDebugMLLog(@"ULProcess", @"Posting notification with dictionary %@", userInfo);

	[[NSNotificationCenter defaultCenter]
		postNotificationName: @"ULProcessDidFinishNotification"
		object: self
		userInfo: userInfo];
}



- (id) initWithCoder: (NSCoder*) decoder
{
	if((self = [super initWithCoder: decoder]))
	{
		if([decoder allowsKeyedCoding])
		{
			inputData = [decoder decodeObjectForKey: @"InputData"];
			simTemplate = [decoder decodeObjectForKey: @"Template"];
			simulationData = [decoder decodeObjectForKey: @"SimulationData"];
			[simulationData retain];
			[inputData retain];
			[simTemplate retain];
		}		
	}
	
	return self;
}	

- (void) encodeWithCoder: (NSCoder*) encoder
{
	[super encodeWithCoder: encoder];
	
	if([encoder allowsKeyedCoding])
	{
		[encoder encodeObject: inputData
			forKey: @"InputData"];
		[encoder encodeObject: simTemplate
			forKey: @"Template"];
		if(simulationData != nil)
			[encoder encodeObject: simulationData
				forKey: @"SimulationData"];
	}
}

@end