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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
/*
* Copyright (c) 2008 Paul C. Buetow, vs@dev.buetow.org
*
* This program 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 program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* All icons of the icons/ folder are under a Creative Commons
* Attribution-Noncommercial-Share Alike License a CC-by-nc-sa.
*
* The icon's homepage is http://code.google.com/p/ultimate-gnome/
*/
package core;
import java.io.*;
import events.*;
import events.implementations.*;
import events.internal.*;
import exceptions.*;
import prefs.VSPrefs;
import protocols.VSAbstractProtocol;
import serialize.*;
//import utils.*;
/**
* The class VSTask, an object of this class represents a task to do or done.
* All tasks are managed by the task manager. There are local and global timed
* tasks. Local timed tasks are being fullfilled if the process' local time is
* reached. Global timed tasks are being fullfilled if the simulator's time is
* reached.
*
* @author Paul C. Buetow
*/
public class VSTask implements Comparable, VSSerializable {
/** The serial version uid */
private static final long serialVersionUID = 1L;
/** The Constant LOCAL. Used for the constructor if it's a local timed
* task.
*/
public final static boolean LOCAL = true;
/** The Constant GLOBAL. Used for the constructor if it's a global timed
* task.
*/
public final static boolean GLOBAL = false;
/** The task time. */
private long taskTime;
/** The event which takes plase if the task time matches. */
private VSAbstractEvent event;
/** The process to run the task at. */
private VSInternalProcess process;
/** The simulator's default prefs. */
private VSPrefs prefs;
/** The task is programmed. The task will be still in the task manager
* after reset.
*/
private boolean isProgrammed;
/** The task is global timed. If set to true, its local timed. */
private boolean isGlobalTimed;
/** The task counter. Needed for the unique task numbers. */
private static int taskCounter;
/** The task number. */
private int taskNum;
/**
* Instantiates a new task.
*
* @param taskTime the task time
* @param process the process
* @param event the event
* @param isLocal the taks is local timed
*/
public VSTask(long taskTime, VSInternalProcess process,
VSAbstractEvent event,
boolean isLocal) {
init(taskTime, process, event, isLocal);
}
/**
* Instantiates a new task, it's a copy constructor.
*
* @param task the task to copy
*/
public VSTask(VSTask task) {
VSAbstractEvent event = task.getEvent();
try {
// Use the copy of the event object
event = event.getCopy();
} catch (VSEventNotCopyableException e) {
// Use the original event object
}
init(task.getTaskTime(),
task.getProcess(),
event,
!task.isGlobalTimed());
}
/**
* Instantiates a new task during a deserialization.
*
* @param serialize the serialize object
* @param objectInputStream The input stream
*/
public VSTask(VSSerialize serialize, ObjectInputStream objectInputStream)
throws IOException, ClassNotFoundException {
deserialize(serialize, objectInputStream);
}
/**
* Inits the task
*
* @param taskTime the task time
* @param process the process
* @param event the event
* @param isLocal the taks is local timed
*/
private void init(long taskTime, VSInternalProcess process,
VSAbstractEvent event, boolean isLocal) {
this.process = process;
this.taskTime = taskTime > 0 ? taskTime : 0;
/* May be not null if called from deserialization */
if (this.event == null)
this.event = event;
this.prefs = process.getPrefs();
this.isGlobalTimed = !isLocal;
this.taskNum = ++taskCounter;
}
/**
* Gets the task num.
*
* @return the task num
*/
public int getTaskNum() {
return taskNum;
}
/**
* Sets if the task is programmed.
*
* @param isProgrammed true, if the task is programmed
*/
public void isProgrammed(boolean isProgrammed) {
this.isProgrammed = isProgrammed;
}
/**
* Checks if the task is programmed.
*
* @return true, if the task is programmed
*/
public boolean isProgrammed() {
return isProgrammed;
}
/**
* Checks if the task is using an "internal event".
*
* @return true, if the task is using an internal event
*/
public boolean hasInternalEvent() {
return event instanceof VSAbstractInternalEvent;
}
/**
* Checks if the task should not get serialized.
*
* @return true, if the task should not get serialized
*/
public boolean hasNotSerializableEvent() {
return event instanceof VSNotSerializable;
}
/**
* Checks if the task is a message receive event.
*
* @return true, if it is a message receive event
*/
public boolean hasMessageReceiveEvent() {
return event instanceof VSMessageReceiveEvent;
}
/**
* Checks if the task is a process recover event.
*
* @return true, if it is a process recover event
*/
public boolean hasProcessRecoverEvent() {
return event instanceof VSProcessRecoverEvent;
}
/**
* Checks if the task belongs to the specified protocol object.
*
* @param protocol the protocol object to check against.
*
* @return true, if it's a task using the protocol object.
*/
public boolean isProtocol(VSAbstractProtocol protocol) {
if (event instanceof VSAbstractProtocol)
return ((VSAbstractProtocol) event).equals(protocol);
return false;
}
/**
* Checks if the task's time is over.
*
* @return true, if it's over
*/
public boolean timeOver() {
if (isGlobalTimed)
return taskTime < process.getGlobalTime();
return taskTime < process.getTime();
}
/**
* Checks if the task equals to another task.
*
* @param task the task to compare to
*
* @return true, if equal (the task nums equal)
*/
public boolean equals(VSTask task) {
return taskNum == task.getTaskNum();
}
/**
* Checks if the task belongs to the specified process.
*
* @param process the process to check against
*
* @return true, if the task is using the process
*/
public boolean isProcess(VSInternalProcess process) {
return this.process.equals(process);
}
/**
* Checks if the task is global timed.
*
* @return true, if the taks is global timed
*/
public boolean isGlobalTimed() {
return isGlobalTimed;
}
/**
* Gets the process.
*
* @return the process of the event
*/
public VSInternalProcess getProcess() {
return process;
}
/**
* Runs the task.
*/
public void run() {
if (event.getProcess() == null)
event.init(process);
if (!(event instanceof VSMessageReceiveEvent)
&& !(event instanceof VSAbstractProtocol))
process.increaseVectorAndLamportTimeIfAll();
event.onStart();
}
/**
* Gets the task time.
*
* @return the task time
*/
public long getTaskTime() {
return taskTime;
}
/**
* Sets the task time.
*
* @param taskTime the task time
*/
public void setTaskTime(long taskTime) {
this.taskTime = taskTime;
}
/**
* Sets the process.
*
* @param process the process
*/
public void setProcess(VSInternalProcess process) {
/* Only do it if the process differs */
if (!this.process.equals(process)) {
this.process = process;
try {
// Use the copy of the event object
event = event.getCopy(process);
} catch (VSEventNotCopyableException e) {
if (event instanceof VSAbstractProtocol) {
String eventShortname = event.getShortname();
event = process.getProtocolObject(event.getClassname());
event.setShortname(eventShortname);
} else {
System.out.println(e);
}
}
}
}
/**
* Gets the event.
*
* @return the event
*/
public VSAbstractEvent getEvent() {
return event;
}
/**
* Logg a message.
*
* @param message the message to log
*/
/*
private void log(String message) {
process.log(message);
}
*/
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append(prefs.getString("lang.task"));
buffer.append(" ");
buffer.append(getTaskTime());
buffer.append(event.toString());
buffer.append("; PID: ");
buffer.append(process.getProcessID());
/*
if (isProgrammed()) {
buffer.append("; Programmed");
}
*/
return buffer.toString();
}
/* (non-Javadoc)
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Object object) {
if (object instanceof VSTask) {
final VSTask task = (VSTask) object;
if (taskTime < task.getTaskTime())
return -1;
else if (taskTime > task.getTaskTime())
return 1;
VSAbstractEvent event2 = task.getEvent();
/* If it's a recovering, it should get handled very first */
boolean a = event instanceof VSProcessRecoverEvent;
boolean b = event2 instanceof VSProcessRecoverEvent;
if (a && b)
return 0;
if (a)
return -1;
if (b)
return 1;
/* If it's a crash, it should get handled second first */
a = event instanceof VSProcessCrashEvent;
b = event2 instanceof VSProcessCrashEvent;
if (a && b)
return 0;
if (a)
return -1;
if (b)
return 1;
/* If it's a VSProtocolEvent, it should get handled third */
a = event instanceof VSProtocolEvent;
b = event2 instanceof VSProtocolEvent;
if (a && b)
return 0;
if (a)
return -1;
if (b)
return 1;
String shortname = event.getShortname();
String shortname2 = event2.getShortname();
/* One of those may be null if an VSAbstractEvent object has not
been initialized yet */
if (shortname == null || shortname2 == null)
return 0;
return shortname.compareTo(shortname2);
}
return 0;
}
/* (non-Javadoc)
* @see serialize.VSSerializable#serialize(serialize.VSSerialize,
* java.io.ObjectOutputStream)
*/
public synchronized void serialize(VSSerialize serialize,
ObjectOutputStream objectOutputStream)
throws IOException {
/** For later backwards compatibility, to add more stuff */
objectOutputStream.writeObject(new Boolean(false));
objectOutputStream.writeObject(new Integer(process.getProcessNum()));
if (event.getClassname() == null)
event.init(process);
if (VSSerialize.DEBUG)
System.out.println("Serializing: " + event.getClassname());
objectOutputStream.writeObject(event.getClassname());
objectOutputStream.writeObject(new Integer(event.getID()));
event.serialize(serialize, objectOutputStream);
objectOutputStream.writeObject(new Integer(taskNum));
objectOutputStream.writeObject(new Long(taskTime));
objectOutputStream.writeObject(new Boolean(isGlobalTimed));
objectOutputStream.writeObject(new Boolean(isProgrammed));
/** For later backwards compatibility, to add more stuff */
objectOutputStream.writeObject(new Boolean(false));
}
/* (non-Javadoc)
* @see serialize.VSSerializable#deserialize(serialize.VSSerialize,
* java.io.ObjectInputStream)
*/
@SuppressWarnings("unchecked")
public synchronized void deserialize(VSSerialize serialize,
ObjectInputStream objectInputStream)
throws IOException, ClassNotFoundException {
if (VSSerialize.DEBUG)
System.out.println("Deserializing: VSTask");
/** For later backwards compatibility, to add more stuff */
objectInputStream.readObject();
int processNum = ((Integer) objectInputStream.readObject()).intValue();
VSInternalProcess process = (VSInternalProcess)
serialize.getObject(processNum, "process");
String eventClassname = (String) objectInputStream.readObject();
int eventID = ((Integer) objectInputStream.readObject()).intValue();
VSAbstractEvent event = null;
if (serialize.objectExists(eventID, "event")) {
event = (VSAbstractEvent) serialize.getObject(eventID, "event");
} else {
event = VSRegisteredEvents.
createEventInstanceByClassname(eventClassname, process);
serialize.setObject(eventID, "event", event);
}
event.deserialize(serialize, objectInputStream);
int taskNum = ((Integer) objectInputStream.readObject()).intValue();
long taskTime = ((Long) objectInputStream.readObject()).longValue();
Boolean isGlobalTimed = (Boolean) objectInputStream.readObject();
Boolean isProgrammed = (Boolean) objectInputStream.readObject();
serialize.setObject(taskNum, "task", this);
init(taskTime, process, event, !isGlobalTimed.booleanValue());
this.isProgrammed = isProgrammed.booleanValue();
/** For later backwards compatibility, to add more stuff */
objectInputStream.readObject();
}
}
|