diff options
Diffstat (limited to 'sources/core')
| -rw-r--r-- | sources/core/VSProcess.java | 60 | ||||
| -rw-r--r-- | sources/core/VSTask.java | 71 | ||||
| -rw-r--r-- | sources/core/VSTaskManager.java | 99 |
3 files changed, 223 insertions, 7 deletions
diff --git a/sources/core/VSProcess.java b/sources/core/VSProcess.java index 31bbab6..023e017 100644 --- a/sources/core/VSProcess.java +++ b/sources/core/VSProcess.java @@ -24,6 +24,7 @@ package core; import java.awt.*; +import java.io.*; import java.util.*; import core.time.*; @@ -40,7 +41,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSProcess extends VSPrefs { +public class VSProcess extends VSPrefs implements Serializable { /** The data serialization id. */ private static final long serialVersionUID = 1L; @@ -198,7 +199,22 @@ public class VSProcess extends VSPrefs { */ public VSProcess(VSPrefs prefs, int processNum, VSSimulatorCanvas simulatorCanvas, VSLogging logging) { - this.protocolsToReset = new ArrayList<VSAbstractProtocol>(); + init(prefs, processNum, simulatorCanvas, logging); + } + + /** + * Inits a the process. + * + * @param prefs the simulator's default prefs + * @param processNum the process num + * @param simulatorCanvas the simulator canvas + * @param logging the logging object + */ + private void init(VSPrefs prefs, int processNum, + VSSimulatorCanvas simulatorCanvas, VSLogging logging) { + /* May be not null if called from deserialization */ + if (protocolsToReset == null) + this.protocolsToReset = new ArrayList<VSAbstractProtocol>(); this.processNum = processNum; this.prefs = prefs; this.simulatorCanvas = simulatorCanvas; @@ -992,4 +1008,44 @@ public class VSProcess extends VSPrefs { return protocol; } + + /* (non-Javadoc) + * @see prefs.VSPrefs#writeObject() + */ + public synchronized void writeObject(ObjectOutputStream objectOutputStream) + throws IOException { + super.writeObject(objectOutputStream); + objectOutputStream.writeObject(new Integer(processNum)); + objectOutputStream.writeObject(protocolsToReset); + } + + /* (non-Javadoc) + * @see prefs.VSPrefs#readObject() + */ + @SuppressWarnings("unchecked") + public synchronized void readObject(ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + super.readObject(objectInputStream); + + if (VSDeserializationHelper.DEBUG) + System.out.println("Deserializing: VSProcess"); + + VSLogging logging = (VSLogging) + VSDeserializationHelper.getObject("logging"); + VSSimulatorCanvas simulatorCanvas = (VSSimulatorCanvas) + VSDeserializationHelper.getObject( + "simulatorCanvas"); + VSPrefs prefs = (VSPrefs) VSDeserializationHelper.getObject("prefs"); + + Integer processNum = (Integer) objectInputStream.readObject(); + this.protocolsToReset = (ArrayList<VSAbstractProtocol>) + objectInputStream.readObject(); + + for (VSAbstractProtocol protocol : protocolsToReset) + setObject(protocol.getClassname(), protocol); + + VSDeserializationHelper.setObject(processNum.intValue(), + "process", this); + init(prefs, processNum.intValue(), simulatorCanvas, logging); + } } diff --git a/sources/core/VSTask.java b/sources/core/VSTask.java index d1e122e..d27a9a4 100644 --- a/sources/core/VSTask.java +++ b/sources/core/VSTask.java @@ -23,11 +23,14 @@ package core; +import java.io.*; + import events.*; import events.implementations.*; import events.internal.*; import prefs.VSPrefs; import protocols.VSAbstractProtocol; +import utils.*; /** * The class VSTask, an object of this class represents a task to do or done. @@ -38,7 +41,7 @@ import protocols.VSAbstractProtocol; * * @author Paul C. Buetow */ -public class VSTask implements Comparable { +public class VSTask implements Comparable, Serializable { /** The serial version uid */ private static final long serialVersionUID = 1L; @@ -88,9 +91,24 @@ public class VSTask implements Comparable { */ public VSTask(long taskTime, VSProcess process, VSAbstractEvent event, boolean isLocal) { + init(taskTime, process, event, isLocal); + } + + /** + * 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, VSProcess process, VSAbstractEvent event, + boolean isLocal) { this.process = process; this.taskTime = taskTime > 0 ? taskTime : 0; - this.event = event; + /* May be not null if called from deserialization */ + if (event == null) + this.event = event; this.prefs = process.getPrefs(); this.isGlobalTimed = !isLocal; this.taskNum = ++taskCounter; @@ -319,5 +337,54 @@ public class VSTask implements Comparable { return 0; } + + /** + * Write object. + * + * @param objectOutputStream the object output stream + * + * @throws IOException Signals that an I/O exception has occurred. + */ + public synchronized void writeObject(ObjectOutputStream objectOutputStream) + throws IOException { + objectOutputStream.writeObject(event); + objectOutputStream.writeObject(new Integer(process.getProcessNum())); + objectOutputStream.writeObject(new Integer(taskNum)); + objectOutputStream.writeObject(new Long(taskTime)); + objectOutputStream.writeObject(new Boolean(isGlobalTimed)); + objectOutputStream.writeObject(new Boolean(isProgrammed)); + } + + /** + * Read object. + * + * @param objectInputStream the object input stream + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ClassNotFoundException the class not found exception + */ + @SuppressWarnings("unchecked") + public synchronized void readObject(ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + if (VSDeserializationHelper.DEBUG) + System.out.println("Deserializing: VSTask"); + + this.event = (VSAbstractEvent) objectInputStream.readObject(); + Integer processNum = (Integer) objectInputStream.readObject(); + Integer taskNum = (Integer) objectInputStream.readObject(); + Long taskTime = (Long) objectInputStream.readObject(); + Boolean isGlobalTimed = (Boolean) objectInputStream.readObject(); + Boolean isProgrammed = (Boolean) objectInputStream.readObject(); + + VSDeserializationHelper.setObject(taskNum.intValue(), "task", this); + + VSProcess process = (VSProcess) VSDeserializationHelper.getObject( + processNum.intValue(), "process"); + + init(taskTime.longValue(), process, event, + !isGlobalTimed.booleanValue()); + + this.isProgrammed = isProgrammed.booleanValue(); + } } diff --git a/sources/core/VSTaskManager.java b/sources/core/VSTaskManager.java index 620d115..a63db51 100644 --- a/sources/core/VSTaskManager.java +++ b/sources/core/VSTaskManager.java @@ -23,6 +23,7 @@ package core; +import java.io.*; import java.util.*; import prefs.*; @@ -37,7 +38,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSTaskManager { +public class VSTaskManager implements Serializable { /** The seriao version uid */ private static final long serialVersionUID = 1L; @@ -60,14 +61,29 @@ public class VSTaskManager { private VSPrefs prefs; /** - * Instantiates a new lang.process.removetask manager. + * Instantiates a new task manager object. * * @param prefs the simulator's default prefs + * @param simulatorCanvas the simulator canvas */ public VSTaskManager(VSPrefs prefs, VSSimulatorCanvas simulatorCanvas) { + init(prefs, simulatorCanvas); + } + + /** + * Inits the task manager. + * + * @param prefs the simulator's default prefs + * @param simulatorCanvas the simulator canvas + */ + private void init(VSPrefs prefs, VSSimulatorCanvas simulatorCanvas) { this.prefs = prefs; this.simulatorCanvas = simulatorCanvas; - this.globalTasks = new PriorityQueue<VSTask>(); + + /* May be not null if called from deserialization */ + if (globalTasks == null) + this.globalTasks = new PriorityQueue<VSTask>(); + this.fullfilledProgrammedTasks = new LinkedList<VSTask>(); } @@ -476,4 +492,81 @@ public class VSTaskManager { return descr + ")"; } + + /** + * Write object. + * + * @param objectOutputStream the object output stream + * + * @throws IOException Signals that an I/O exception has occurred. + */ + public synchronized void writeObject(ObjectOutputStream objectOutputStream) + throws IOException { + VSPriorityQueue<VSTask> temp = new VSPriorityQueue<VSTask>(); + + for (VSTask task : fullfilledProgrammedTasks) + if (task.isGlobalTimed()) + temp.add(task); + + for (VSTask task : globalTasks) + temp.add(task); + + ArrayList<VSProcess> processes = simulatorCanvas.getProcesses(); + HashMap<Integer, ArrayList<VSTask>> map = + new HashMap<Integer, ArrayList<VSTask>>(); + + synchronized (processes) { + for (VSProcess process : processes) { + VSPriorityQueue<VSTask> tasks = process.getTasks(); + ArrayList<VSTask> tasks_ = new ArrayList<VSTask>(); + for (VSTask task : tasks) + tasks_.add(task); + map.put(new Integer(process.getProcessNum()), tasks_); + } + } + + objectOutputStream.writeObject(temp); + objectOutputStream.writeObject(map); + } + + /** + * Read object. + * + * @param objectInputStream the object input stream + * + * @throws IOException Signals that an I/O exception has occurred. + * @throws ClassNotFoundException the class not found exception + */ + @SuppressWarnings("unchecked") + public synchronized void readObject(ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + if (VSDeserializationHelper.DEBUG) + System.out.println("Deserializing: VSTaskManager"); + + VSPrefs prefs = (VSPrefs) VSDeserializationHelper.getObject("prefs"); + VSSimulatorCanvas simulatorCanvas = (VSSimulatorCanvas) + VSDeserializationHelper.getObject( + "prefs"); + this.globalTasks = (PriorityQueue<VSTask>) + objectInputStream.readObject(); + + HashMap<Integer, ArrayList<VSTask>> map = + (HashMap<Integer, ArrayList<VSTask>>) + objectInputStream.readObject(); + + Set<Integer> set = map.keySet(); + for (Integer processNum : set) { + VSProcess process = (VSProcess) VSDeserializationHelper.getObject( + processNum.intValue(), + "process"); + + ArrayList<VSTask> tasks = (ArrayList<VSTask>) map.get(processNum); + VSPriorityQueue<VSTask> tasks_ = process.getTasks(); + + for (VSTask task: tasks) + tasks_.add(task); + } + + init(prefs, simulatorCanvas); + } } |
