diff options
| author | Paul Buetow <paul@buetow.org> | 2008-05-26 17:39:03 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-05-26 17:39:03 +0000 |
| commit | d04ee66ac7a02e7e226478bdc0eebdd97f060e14 (patch) | |
| tree | 641362ceaaf0f8814c3d732b5e2d26f7616979a7 | |
| parent | d0b6e434d76cfea9e3096f0d50706c9a286a4b9d (diff) | |
Vector type introduced @ VSPrefs.
| -rw-r--r-- | ROADMAP | 1 | ||||
| -rw-r--r-- | sources/exceptions/ParseIntegerVectorException.java | 4 | ||||
| -rw-r--r-- | sources/prefs/VSPrefs.java | 95 | ||||
| -rw-r--r-- | sources/prefs/editors/VSEditor.java | 99 | ||||
| -rw-r--r-- | sources/protocols/implementations/OnePhaseCommitProtocol.java | 12 | ||||
| -rw-r--r-- | sources/protocols/implementations/README (renamed from sources/protocols/README) | 4 | ||||
| -rw-r--r-- | sources/utils/VSTools.java | 49 |
7 files changed, 245 insertions, 19 deletions
@@ -7,6 +7,7 @@ Zwei-Phasen Commit Protokoll LATER: Periodische Tasks anlegen koennen Ganze simulationseinstellungen abspeichern/laden koennen + VSPrefs.integerVectorPrefs serialisierbar machen TaskManager + Tasks serialisierbar machen Tasks anhand der Tasknummern den richtigen Prozessen zuordnen Jedes Eventobjekte den Tasks und der Prozesse anhand der IDs zuordnen diff --git a/sources/exceptions/ParseIntegerVectorException.java b/sources/exceptions/ParseIntegerVectorException.java new file mode 100644 index 0000000..0c8f58a --- /dev/null +++ b/sources/exceptions/ParseIntegerVectorException.java @@ -0,0 +1,4 @@ +package exceptions; + +public class ParseIntegerVectorException extends Exception { +} diff --git a/sources/prefs/VSPrefs.java b/sources/prefs/VSPrefs.java index dd274b9..915d8e3 100644 --- a/sources/prefs/VSPrefs.java +++ b/sources/prefs/VSPrefs.java @@ -25,6 +25,9 @@ public abstract class VSPrefs implements Serializable { /** The Constant INTEGER_PREFIX. */ public static final String INTEGER_PREFIX = "Integer: "; + /** The Constant VECTOR_PREFIX. */ + public static final String VECTOR_PREFIX = "Vector: "; + /** The Constant LONG_PREFIX. */ public static final String LONG_PREFIX = "Long: "; @@ -40,6 +43,9 @@ public abstract class VSPrefs implements Serializable { /** The integer prefs. */ private HashMap<String,Integer> integerPrefs; + /** The integer vector prefs. */ + private HashMap<String,Vector<Integer>> vectorPrefs; + /** The long prefs. */ private HashMap<String,Long> longPrefs; @@ -160,6 +166,7 @@ public abstract class VSPrefs implements Serializable { descriptionPrefs = new HashMap<String,String>(); floatPrefs = new HashMap<String,Float>(); integerPrefs = new HashMap<String,Integer>(); + vectorPrefs = new HashMap<String,Vector<Integer>>(); longPrefs = new HashMap<String,Long>(); restrictions = new HashMap<String,VSPrefRestriction>(); stringPrefs = new HashMap<String,String>(); @@ -176,6 +183,7 @@ public abstract class VSPrefs implements Serializable { colorPrefs.clear(); floatPrefs.clear(); integerPrefs.clear(); + vectorPrefs.clear(); longPrefs.clear(); stringPrefs.clear(); booleanPrefs.clear(); @@ -707,6 +715,81 @@ public abstract class VSPrefs implements Serializable { setInteger(key, new Integer(val)); } + /* Integer vector methods */ + + /** + * Gets the integer key set. + * + * @return the integer key set + */ + public synchronized Set<String> getVectorKeySet() { + return vectorPrefs.keySet(); + } + + /** + * Gets the integer obj. + * + * @param key the key + * + * @return the integer obj + */ + public synchronized Vector<Integer> getVector(String key) { + Vector<Integer> val = vectorPrefs.get(key); + + if (val == null) { + System.err.println("Fatal: No such integer config value \"" + + key + "\""); + System.exit(1); + } + + return val; + } + + /** + * Inits the integer. + * + * @param key the key + * @param val the val + */ + public synchronized void initVector(String key, Vector<Integer> val) { + if (!vectorPrefs.containsKey(key)) + setVector(key, val); + } + + /** + * Inits the integer vector. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initVector(String key, Vector<Integer> val, String descr) { + initVector(key, val); + initDescription(VECTOR_PREFIX + key, descr); + } + + /** + * Inits the integer vector plus unit. + * + * @param key the key + * @param val the val + * @param descr the descr + */ + public void initVector(String key, Vector<Integer> val, String descr, String unit) { + initVector(key, val, descr); + initUnit(VECTOR_PREFIX + key, unit); + } + + /** + * Sets the integer vector. + * + * @param key the key + * @param val the val + */ + public synchronized void setVector(String key, Vector<Integer> val) { + vectorPrefs.put(key, val); + } + /* Long methods */ /** @@ -921,6 +1004,7 @@ public abstract class VSPrefs implements Serializable { objectOutputStream.writeObject(colorPrefs); objectOutputStream.writeObject(floatPrefs); objectOutputStream.writeObject(integerPrefs); + objectOutputStream.writeObject(vectorPrefs); objectOutputStream.writeObject(longPrefs); objectOutputStream.writeObject(stringPrefs); objectOutputStream.writeObject(units); @@ -941,6 +1025,7 @@ public abstract class VSPrefs implements Serializable { descriptionPrefs = new HashMap<String,String>(); floatPrefs = (HashMap<String,Float>) objectInputStream.readObject(); integerPrefs = (HashMap<String,Integer>) objectInputStream.readObject(); + vectorPrefs = (HashMap<String,Vector<Integer>>) objectInputStream.readObject(); longPrefs = (HashMap<String,Long>) objectInputStream.readObject(); restrictions = new HashMap<String,VSPrefRestriction>(); stringPrefs = (HashMap<String,String>) objectInputStream.readObject(); @@ -1071,6 +1156,13 @@ public abstract class VSPrefs implements Serializable { descr += key + "=" + getInteger(key) + "; "; } + set = getVectorKeySet(); + if (set.size() > 0) { + descr += VECTOR_PREFIX; + for (String key : set) + descr += key + "=" + getVector(key) + "; "; + } + set = getLongKeySet(); if (set.size() > 0) { descr += LONG_PREFIX; @@ -1129,6 +1221,9 @@ public abstract class VSPrefs implements Serializable { if (!integerPrefs.isEmpty()) return false; + if (!vectorPrefs.isEmpty()) + return false; + if (!longPrefs.isEmpty()) return false; diff --git a/sources/prefs/editors/VSEditor.java b/sources/prefs/editors/VSEditor.java index 13d1f02..4436825 100644 --- a/sources/prefs/editors/VSEditor.java +++ b/sources/prefs/editors/VSEditor.java @@ -31,6 +31,9 @@ public abstract class VSEditor implements ActionListener { /** The integer keys. */ private ArrayList<String> integerKeys; + /** The vector keys. */ + private ArrayList<String> vectorKeys; + /** The long keys. */ private ArrayList<String> longKeys; @@ -43,6 +46,9 @@ public abstract class VSEditor implements ActionListener { /** The integer fields. */ private HashMap<String,JComboBox> integerFields; + /** The vector fields. */ + private HashMap<String,JTextField> vectorFields; + /** The color fields. */ private HashMap<String,JTextField> colorFields; @@ -190,6 +196,7 @@ public abstract class VSEditor implements ActionListener { colorFields = new HashMap<String,JTextField>(); floatFields = new HashMap<String,JTextField>(); integerFields = new HashMap<String,JComboBox>(); + vectorFields = new HashMap<String,JTextField>(); longFields = new HashMap<String,JTextField>(); booleanFields = new HashMap<String,JCheckBox>(); stringFields = new HashMap<String,JTextField>(); @@ -197,6 +204,7 @@ public abstract class VSEditor implements ActionListener { colorKeys = filterKeys(prefsToEdit.getColorKeySet()); floatKeys = filterKeys(prefsToEdit.getFloatKeySet()); integerKeys = filterKeys(prefsToEdit.getIntegerKeySet()); + vectorKeys = filterKeys(prefsToEdit.getVectorKeySet()); longKeys = filterKeys(prefsToEdit.getLongKeySet()); booleanKeys = filterKeys(prefsToEdit.getBooleanKeySet()); stringKeys = filterKeys(prefsToEdit.getStringKeySet()); @@ -332,13 +340,45 @@ public abstract class VSEditor implements ActionListener { } /** - * Creates the boolean component. + * Creates the vector component. * * @param fullKey the full key * @param key the key * @param prefsToEdit the prefs to edit + */ + protected VSTupel<String,Component,JTextField> createVectorComponent(String fullKey, String key, VSPrefs prefsToEdit) { + String descr = prefsToEdit.getDescription(fullKey); + String label = descr == null ? fullKey : descr; + Vector<Integer> vec = prefsToEdit.getVector(key); + + JTextField valField = new JTextField(); + valField.setBorder(null); + + StringBuffer buffer = new StringBuffer(); + buffer.append("["); + + synchronized (vec) { + for (Integer integer : vec) { + buffer.append(integer + ","); + } + } + + try { + valField.setText(buffer.toString().substring(0, buffer.length()-1)+"]"); + } catch (StringIndexOutOfBoundsException e) { + valField.setText("[]"); + } + + return new VSTupel<String,Component,JTextField>(label, + createUnitPanel(prefsToEdit, valField, fullKey), valField); + } + + /** + * Creates the boolean component. * - * @return the lang.process.removetupel< string, component, j check box> + * @param fullKey the full key + * @param key the key + * @param prefsToEdit the prefs to edit */ protected VSTupel<String,Component,JCheckBox> createBooleanComponent(String fullKey, String key, VSPrefs prefsToEdit) { final String activated = prefs.getString("lang.activated"); @@ -482,12 +522,22 @@ public abstract class VSEditor implements ActionListener { for (String key : integerKeys) { String fullKey = VSPrefs.INTEGER_PREFIX + key; - VSTupel<String,Component,JComboBox> tupel = createIntegerComponent(fullKey, key, prefsToEdit); + VSTupel<String,Component,JComboBox> tupel = + createIntegerComponent(fullKey, key, prefsToEdit); labels.put(fullKey, tupel.getA()); components.put(fullKey, tupel.getB()); integerFields.put(key, tupel.getC()); } + for (String key : vectorKeys) { + String fullKey = VSPrefs.VECTOR_PREFIX + key; + VSTupel<String,Component,JTextField> tupel = + createVectorComponent(fullKey, key, prefsToEdit); + labels.put(fullKey, tupel.getA()); + components.put(fullKey, tupel.getB()); + vectorFields.put(key, tupel.getC()); + } + for (String key : booleanKeys) { String fullKey = VSPrefs.BOOLEAN_PREFIX + key; VSTupel<String,Component,JCheckBox> tupel = createBooleanComponent(fullKey, key, prefsToEdit); @@ -612,12 +662,14 @@ public abstract class VSEditor implements ActionListener { ArrayList<String> fullKeys = new ArrayList<String>(); Set<String> integerKeys = prefsToAdd.getIntegerKeySet(); + Set<String> vectorKeys = prefsToAdd.getVectorKeySet(); Set<String> floatKeys = prefsToAdd.getFloatKeySet(); Set<String> longKeys = prefsToAdd.getLongKeySet(); Set<String> booleanKeys = prefsToAdd.getBooleanKeySet(); Set<String> stringKeys = prefsToAdd.getStringKeySet(); for (String key : integerKeys) fullKeys.add(VSPrefs.INTEGER_PREFIX + key); + for (String key : vectorKeys) fullKeys.add(VSPrefs.VECTOR_PREFIX + key); for (String key : floatKeys) fullKeys.add(VSPrefs.FLOAT_PREFIX + key); for (String key : longKeys) fullKeys.add(VSPrefs.LONG_PREFIX + key); for (String key : booleanKeys) fullKeys.add(VSPrefs.BOOLEAN_PREFIX + key); @@ -634,6 +686,13 @@ public abstract class VSEditor implements ActionListener { this.integerFields.put(prefsKey+key, tupel.getC()); addVariable(prefsKey, tupel.getA(), tupel.getB(), prefsToAdd); + } else if (fullKey.startsWith(VSPrefs.VECTOR_PREFIX)) { + VSTupel<String,Component,JTextField> tupel = + createVectorComponent(fullKey, key, prefsToAdd); + this.vectorKeys.add(prefsKey+key); + this.vectorFields.put(prefsKey+key, tupel.getC()); + addVariable(prefsKey, tupel.getA(), tupel.getB(), prefsToAdd); + } else if (fullKey.startsWith(VSPrefs.BOOLEAN_PREFIX)) { VSTupel<String,Component,JCheckBox> tupel = createBooleanComponent(fullKey, key, prefsToAdd); @@ -702,7 +761,7 @@ public abstract class VSEditor implements ActionListener { /** * Reset edit panel. */ - protected void resetEditPanel() { + protected void resetPrefs() { for (String key : integerKeys) { JComboBox valComboBox = integerFields.get(key); valComboBox.setSelectedIndex(0); @@ -714,6 +773,12 @@ public abstract class VSEditor implements ActionListener { valField.setSelected(prefsToEditMap.get(keys[1]).getBoolean(keys[0])); } + for (String key : vectorKeys) { + String keys[] = getKeys(key); + JTextField valField = vectorFields.get(key); + valField.setText(""+prefsToEditMap.get(keys[1]).getVector(keys[0])); + } + for (String key : floatKeys) { String keys[] = getKeys(key); JTextField valField = floatFields.get(key); @@ -765,14 +830,26 @@ public abstract class VSEditor implements ActionListener { for (String key : integerKeys) { String keys[] = getKeys(key); - //String fullKey = VSPrefs.INTEGER_PREFIX + keys[0]; JComboBox valComboBox = integerFields.get(key); prefsToEditMap.get(keys[1]).setInteger(keys[0], (Integer) valComboBox.getSelectedItem()); } + for (String key : vectorKeys) { + String keys[] = getKeys(key); + JTextField valField = vectorFields.get(key); + + try { + String val = valField.getText(); + Vector<Integer> vec = utils.VSTools.parseIntegerVector(val); + prefsToEditMap.get(keys[1]).setVector(keys[0], vec); + } catch (exceptions.ParseIntegerVectorException e) { + } + + valField.setText(""+prefsToEditMap.get(keys[1]).getVector(keys[0])); + } + for (String key : booleanKeys) { String keys[] = getKeys(key); - //String fullKey = VSPrefs.BOOLEAN_PREFIX + keys[0]; JCheckBox valField = booleanFields.get(key); prefsToEditMap.get(keys[1]).setBoolean(keys[0], valField.isSelected()); } @@ -782,12 +859,11 @@ public abstract class VSEditor implements ActionListener { JTextField valField = floatFields.get(key); try { - //String fullKey = VSPrefs.FLOAT_PREFIX + keys[0]; Float val = Float.valueOf(valField.getText()); prefsToEditMap.get(keys[1]).setFloat(keys[0], val); } catch (NumberFormatException e) { - valField.setText("0.0"); + valField.setText(""+prefsToEditMap.get(keys[1]).getFloat(keys[0])); } } @@ -796,25 +872,22 @@ public abstract class VSEditor implements ActionListener { JTextField valField = longFields.get(key); try { - //String fullKey = VSPrefs.LONG_PREFIX + keys[0]; Long val = Long.valueOf(valField.getText()); prefsToEditMap.get(keys[1]).setLong(keys[0], val); } catch (NumberFormatException e) { - valField.setText("0"); + valField.setText(""+prefsToEditMap.get(keys[1]).getLong(keys[0])); } } for (String key : colorKeys) { String keys[] = getKeys(key); - //String fullKey = VSPrefs.COLOR_PREFIX + keys[0]; JTextField valField = colorFields.get(key); prefsToEditMap.get(keys[1]).setColor(keys[0], valField.getBackground()); } for (String key : stringKeys) { String keys[] = getKeys(key); - //String fullKey = VSPrefs.STRING_PREFIX + keys[0]; JTextField valField = stringFields.get(key); prefsToEditMap.get(keys[1]).setString(keys[0], valField.getText()); } @@ -846,7 +919,7 @@ public abstract class VSEditor implements ActionListener { savePrefs(); } else if (actionCommand.equals(prefs.getString("lang.reset"))) { - resetEditPanel(); + resetPrefs(); } } diff --git a/sources/protocols/implementations/OnePhaseCommitProtocol.java b/sources/protocols/implementations/OnePhaseCommitProtocol.java index cb720f3..cb984d3 100644 --- a/sources/protocols/implementations/OnePhaseCommitProtocol.java +++ b/sources/protocols/implementations/OnePhaseCommitProtocol.java @@ -4,7 +4,7 @@ */ package protocols.implementations; -import java.util.ArrayList; +import java.util.Vector; import protocols.VSProtocol; import core.VSMessage; @@ -16,7 +16,6 @@ public class OnePhaseCommitProtocol extends VSProtocol { private static final long serialVersionUID = 1L; /* Client variables, coordinator */ - private ArrayList<Integer> peerPids; /* Server variables, peers */ private boolean ackSent; @@ -26,7 +25,14 @@ public class OnePhaseCommitProtocol extends VSProtocol { */ public OnePhaseCommitProtocol() { setClassname(getClass().toString()); - initInteger("numProcesses", 0, "Anzahl beteilitger Prozesse"); + + /* Can be changed via GUI variables editor of each process */ + Vector<Integer> vec = new Vector<Integer>(); + vec.add(2); + vec.add(3); + vec.add(4); + + initVector("pids", vec, "PIDs beteilitger Prozesse"); } /* (non-Javadoc) diff --git a/sources/protocols/README b/sources/protocols/implementations/README index 3bf4f6d..8126ce0 100644 --- a/sources/protocols/README +++ b/sources/protocols/implementations/README @@ -4,7 +4,7 @@ How to add a new protocol: 2. Edit YourOwnNiceProtocol.java and replace the classname! -3. Edit the initialize method of RegisteredProtocols.java and add the classname -of your protocol. E.g.: "org.buetow.vs.protocols.YourOwnNiceProtocol" +3. Edit the initialize method of events.RegisteredEvents.java and add the classname +of your protocol. E.g.: "protocols.implementations.YourOwnNiceProtocol" diff --git a/sources/utils/VSTools.java b/sources/utils/VSTools.java index 3423fe5..ca65fe6 100644 --- a/sources/utils/VSTools.java +++ b/sources/utils/VSTools.java @@ -4,8 +4,8 @@ */ package utils; +import java.util.Vector; -// TODO: Auto-generated Javadoc /** * The Class VSTools. */ @@ -45,4 +45,51 @@ public final class VSTools { return 0; } + + /** + * Gets the integer vector represented by a string comma separated. + * + * @param string the string + * + * @return the parsed vector + */ + public static Vector<Integer> parseIntegerVector(String string) + throws exceptions.ParseIntegerVectorException { + System.out.println("parse " + string); + Vector<Integer> vec = new Vector<Integer>(); + + int index = string.indexOf('['); + if (index == -1) + throw new exceptions.ParseIntegerVectorException(); + + string = string.substring(index+1); + + index = string.indexOf(']'); + if (index == -1) + throw new exceptions.ParseIntegerVectorException(); + + string = string.substring(0, index); + + try { + while ( (index = string.indexOf(',')) != -1 ) { + String substring = string.substring(0, index); + + /* Remove leading whitespaces */ + while (substring.charAt(0) == ' ') + substring = substring.substring(1); + + vec.add(Integer.parseInt(substring)); + string = string.substring(index+1); + } + /* Remove leading whitespaces */ + while (string.charAt(0) == ' ') + string = string.substring(1); + vec.add(Integer.parseInt(string)); + + } catch (StringIndexOutOfBoundsException e) { + } catch (NumberFormatException e) { + } + + return vec; + } } |
