diff options
| author | Paul Buetow <paul@buetow.org> | 2008-06-01 22:45:59 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-06-01 22:45:59 +0000 |
| commit | 293c73f50f87b3d73d3947a9f79430b23ec4ddba (patch) | |
| tree | 129ece28dcda9d17bb6e0fc417d8b25bb82dac4c /sources/core | |
| parent | 8086082daaed71ee9105c156e3a1e8e63caf1990 (diff) | |
better serialization
Diffstat (limited to 'sources/core')
| -rw-r--r-- | sources/core/VSProcess.java | 70 | ||||
| -rw-r--r-- | sources/core/VSTask.java | 76 | ||||
| -rw-r--r-- | sources/core/VSTaskManager.java | 88 |
3 files changed, 118 insertions, 116 deletions
diff --git a/sources/core/VSProcess.java b/sources/core/VSProcess.java index 023e017..bf4fa98 100644 --- a/sources/core/VSProcess.java +++ b/sources/core/VSProcess.java @@ -32,6 +32,7 @@ import events.*; import events.implementations.*; import prefs.*; import protocols.*; +import serialize.*; import simulator.*; import utils.*; @@ -41,7 +42,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSProcess extends VSPrefs implements Serializable { +public class VSProcess extends VSPrefs implements VSSerializable { /** The data serialization id. */ private static final long serialVersionUID = 1L; @@ -378,7 +379,7 @@ public class VSProcess extends VSPrefs implements Serializable { * returns a non-negative value. The random crash task uses the simulaion's * global time for its scheduling. */ - public void createRandomCrashTask() { + public synchronized void createRandomCrashTask() { if (!isCrashed) { VSTaskManager taskManager = simulatorCanvas.getTaskManager(); long crashTime = getARandomCrashTime(); @@ -715,7 +716,7 @@ public class VSProcess extends VSPrefs implements Serializable { /** * Increases the process' lamport time. */ - public void increaseLamportTime() { + public synchronized void increaseLamportTime() { setLamportTime(getLamportTime()+1); } @@ -724,7 +725,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @param time the lamport time to use as its update reference. */ - public void updateLamportTime(long time) { + public synchronized void updateLamportTime(long time) { final long lamportTime = getLamportTime() + 1; if (time > lamportTime) @@ -841,7 +842,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @param message the message to send. */ - public void sendMessage(VSMessage message) { + public synchronized void sendMessage(VSMessage message) { StringBuffer buffer = new StringBuffer(); buffer.append(prefs.getString("lang.message.sent")); buffer.append("; "); @@ -873,7 +874,7 @@ public class VSProcess extends VSPrefs implements Serializable { /* (non-Javadoc) * @see prefs.VSPrefs#toString() */ - public String toString() { + public synchronized String toString() { StringBuffer buffer = new StringBuffer(); buffer.append(prefs.getString("lang.process.id")); buffer.append(": "); @@ -898,7 +899,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @return the extended string representation */ - public String toStringFull() { + public synchronized String toStringFull() { StringBuffer buffer = new StringBuffer(); buffer.append(toString()); buffer.append("; paused: "); @@ -946,7 +947,7 @@ public class VSProcess extends VSPrefs implements Serializable { * * @param index the index the process has to get removed. */ - public void removedAProcessAtIndex(int index) { + public synchronized void removedAProcessAtIndex(int index) { if (index < processNum) --processNum; @@ -960,7 +961,7 @@ public class VSProcess extends VSPrefs implements Serializable { * Called by the simulator canvas if a process has been added to the * simulator. */ - public void addedAProcess() { + public synchronized void addedAProcess() { vectorTime.add(new Long(0)); for (VSVectorTime vectorTime : vectorTimeHistory) vectorTime.add(new Long(0)); @@ -991,7 +992,8 @@ public class VSProcess extends VSPrefs implements Serializable { * * @return the protocol object */ - public VSAbstractProtocol getProtocolObject(String protocolClassname) { + public synchronized VSAbstractProtocol getProtocolObject( + String protocolClassname) { VSAbstractProtocol protocol = null; if (!objectExists(protocolClassname)) { @@ -1010,42 +1012,42 @@ public class VSProcess extends VSPrefs implements Serializable { } /* (non-Javadoc) - * @see prefs.VSPrefs#writeObject() + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - super.writeObject(objectOutputStream); - objectOutputStream.writeObject(new Integer(processNum)); - objectOutputStream.writeObject(protocolsToReset); + super.serialize(serialize, objectOutputStream); + objectOutputStream.writeObject(new Integer(protocolsToReset.size())); + for (VSAbstractProtocol protocol : protocolsToReset) { + objectOutputStream.writeObject(protocol.getClassname()); + protocol.serialize(serialize, objectOutputStream); + } } /* (non-Javadoc) - * @see prefs.VSPrefs#readObject() + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - super.readObject(objectInputStream); + super.deserialize(serialize, objectInputStream); - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.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(); + int numProtocols = ((Integer) + objectInputStream.readObject()).intValue(); - for (VSAbstractProtocol protocol : protocolsToReset) - setObject(protocol.getClassname(), protocol); + for (int i = 0; i < numProtocols; ++i) { + String protocolClassname = (String) objectInputStream.readObject(); + VSAbstractProtocol protocol = getProtocolObject(protocolClassname); + protocol.deserialize(serialize, objectInputStream); + } - VSDeserializationHelper.setObject(processNum.intValue(), - "process", this); - init(prefs, processNum.intValue(), simulatorCanvas, logging); + serialize.setObject(processNum, "process", this); } } diff --git a/sources/core/VSTask.java b/sources/core/VSTask.java index d27a9a4..a903868 100644 --- a/sources/core/VSTask.java +++ b/sources/core/VSTask.java @@ -30,6 +30,7 @@ import events.implementations.*; import events.internal.*; import prefs.VSPrefs; import protocols.VSAbstractProtocol; +import serialize.*; import utils.*; /** @@ -41,7 +42,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSTask implements Comparable, Serializable { +public class VSTask implements Comparable, VSSerializable { /** The serial version uid */ private static final long serialVersionUID = 1L; @@ -95,6 +96,17 @@ public class VSTask implements Comparable, Serializable { } /** + * Instantiates a new task during a deserialization. + * + * @param serialize the serialize object + * @param process the object input stream + */ + public VSTask(VSSerialize serialize, ObjectInputStream objectInputStream) + throws IOException, ClassNotFoundException { + deserialize(serialize, objectInputStream); + } + + /** * Inits the task * * @param taskTime the task time @@ -338,51 +350,59 @@ public class VSTask implements Comparable, Serializable { return 0; } - /** - * Write object. - * - * @param objectOutputStream the object output stream - * - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - objectOutputStream.writeObject(event); objectOutputStream.writeObject(new Integer(process.getProcessNum())); + 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)); } - /** - * 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 + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.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(); + int processNum = ((Integer) objectInputStream.readObject()).intValue(); + VSProcess process = (VSProcess) + serialize.getObject(processNum, "process"); + + String eventClassname = (String) objectInputStream.readObject(); + int eventID = ((Integer) objectInputStream.readObject()).intValue(); + + VSAbstractEvent event = (VSAbstractEvent) + serialize.getObject(eventID, "event"); + + if (event == null) { + event = VSRegisteredEvents. + createEventInstanceByClassname(eventClassname, process); + + serialize.setObject(eventID, "event", event); + } + + int taskNum = ((Integer) objectInputStream.readObject()).intValue(); + long taskTime = ((Long) objectInputStream.readObject()).longValue(); Boolean isGlobalTimed = (Boolean) objectInputStream.readObject(); Boolean isProgrammed = (Boolean) objectInputStream.readObject(); - VSDeserializationHelper.setObject(taskNum.intValue(), "task", this); - - VSProcess process = (VSProcess) VSDeserializationHelper.getObject( - processNum.intValue(), "process"); + serialize.setObject(taskNum, "task", this); - init(taskTime.longValue(), process, event, - !isGlobalTimed.booleanValue()); + init(taskTime, process, event, !isGlobalTimed.booleanValue()); this.isProgrammed = isProgrammed.booleanValue(); } diff --git a/sources/core/VSTaskManager.java b/sources/core/VSTaskManager.java index a63db51..cbbde56 100644 --- a/sources/core/VSTaskManager.java +++ b/sources/core/VSTaskManager.java @@ -27,6 +27,7 @@ import java.io.*; import java.util.*; import prefs.*; +import serialize.*; import simulator.*; import utils.*; @@ -38,7 +39,7 @@ import utils.*; * * @author Paul C. Buetow */ -public class VSTaskManager implements Serializable { +public class VSTaskManager implements VSSerializable { /** The seriao version uid */ private static final long serialVersionUID = 1L; @@ -493,80 +494,59 @@ public class VSTaskManager implements Serializable { return descr + ")"; } - /** - * Write object. - * - * @param objectOutputStream the object output stream - * - * @throws IOException Signals that an I/O exception has occurred. + /* (non-Javadoc) + * @see serialize.VSSerializable#serialize(serialize.VSSerialize, + * java.io.ObjectOutputStream) */ - public synchronized void writeObject(ObjectOutputStream objectOutputStream) + public synchronized void serialize(VSSerialize serialize, + ObjectOutputStream objectOutputStream) throws IOException { - VSPriorityQueue<VSTask> temp = new VSPriorityQueue<VSTask>(); + + ArrayList<VSTask> tasks = new ArrayList<VSTask>(); for (VSTask task : fullfilledProgrammedTasks) - if (task.isGlobalTimed()) - temp.add(task); + tasks.add(task); - for (VSTask task : globalTasks) - temp.add(task); + for (VSTask task : this.globalTasks) + tasks.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_); + VSPriorityQueue<VSTask> localTasks = process.getTasks(); + ArrayList<VSTask> tasks_ = new ArrayList<VSTask>(); + for (VSTask task : localTasks) + tasks.add(task); } } - objectOutputStream.writeObject(temp); - objectOutputStream.writeObject(map); + objectOutputStream.writeObject(new Integer(tasks.size())); + for (VSTask task : tasks) + task.serialize(serialize, objectOutputStream); } - /** - * 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 + /* (non-Javadoc) + * @see serialize.VSSerializable#deserialize(serialize.VSSerialize, + * java.io.ObjectInputStream) */ @SuppressWarnings("unchecked") - public synchronized void readObject(ObjectInputStream objectInputStream) + public synchronized void deserialize(VSSerialize serialize, + ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { - if (VSDeserializationHelper.DEBUG) + if (VSSerialize.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"); + int numTasks = ((Integer) objectInputStream.readObject()).intValue(); + globalTasks.clear(); - ArrayList<VSTask> tasks = (ArrayList<VSTask>) map.get(processNum); - VSPriorityQueue<VSTask> tasks_ = process.getTasks(); - - for (VSTask task: tasks) - tasks_.add(task); - } + ArrayList<VSProcess> processes = simulatorCanvas.getProcesses(); + synchronized (processes) { + for (VSProcess process : processes) + process.getTasks().clear(); + } - init(prefs, simulatorCanvas); + for (int i = 0; i < numTasks; ++i) + addTask(new VSTask(serialize, objectInputStream)); } } |
