From d04ee66ac7a02e7e226478bdc0eebdd97f060e14 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Mon, 26 May 2008 17:39:03 +0000 Subject: Vector type introduced @ VSPrefs. --- .../exceptions/ParseIntegerVectorException.java | 4 + sources/prefs/VSPrefs.java | 95 +++++++++++++++++++++ sources/prefs/editors/VSEditor.java | 99 +++++++++++++++++++--- sources/protocols/README | 10 --- .../implementations/OnePhaseCommitProtocol.java | 12 ++- sources/protocols/implementations/README | 10 +++ sources/utils/VSTools.java | 49 ++++++++++- 7 files changed, 252 insertions(+), 27 deletions(-) create mode 100644 sources/exceptions/ParseIntegerVectorException.java delete mode 100644 sources/protocols/README create mode 100644 sources/protocols/implementations/README (limited to 'sources') 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 integerPrefs; + /** The integer vector prefs. */ + private HashMap> vectorPrefs; + /** The long prefs. */ private HashMap longPrefs; @@ -160,6 +166,7 @@ public abstract class VSPrefs implements Serializable { descriptionPrefs = new HashMap(); floatPrefs = new HashMap(); integerPrefs = new HashMap(); + vectorPrefs = new HashMap>(); longPrefs = new HashMap(); restrictions = new HashMap(); stringPrefs = new HashMap(); @@ -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 getVectorKeySet() { + return vectorPrefs.keySet(); + } + + /** + * Gets the integer obj. + * + * @param key the key + * + * @return the integer obj + */ + public synchronized Vector getVector(String key) { + Vector 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 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 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 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 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(); floatPrefs = (HashMap) objectInputStream.readObject(); integerPrefs = (HashMap) objectInputStream.readObject(); + vectorPrefs = (HashMap>) objectInputStream.readObject(); longPrefs = (HashMap) objectInputStream.readObject(); restrictions = new HashMap(); stringPrefs = (HashMap) 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 integerKeys; + /** The vector keys. */ + private ArrayList vectorKeys; + /** The long keys. */ private ArrayList longKeys; @@ -43,6 +46,9 @@ public abstract class VSEditor implements ActionListener { /** The integer fields. */ private HashMap integerFields; + /** The vector fields. */ + private HashMap vectorFields; + /** The color fields. */ private HashMap colorFields; @@ -190,6 +196,7 @@ public abstract class VSEditor implements ActionListener { colorFields = new HashMap(); floatFields = new HashMap(); integerFields = new HashMap(); + vectorFields = new HashMap(); longFields = new HashMap(); booleanFields = new HashMap(); stringFields = new HashMap(); @@ -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 createVectorComponent(String fullKey, String key, VSPrefs prefsToEdit) { + String descr = prefsToEdit.getDescription(fullKey); + String label = descr == null ? fullKey : descr; + Vector 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(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 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 tupel = createIntegerComponent(fullKey, key, prefsToEdit); + VSTupel 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 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 tupel = createBooleanComponent(fullKey, key, prefsToEdit); @@ -612,12 +662,14 @@ public abstract class VSEditor implements ActionListener { ArrayList fullKeys = new ArrayList(); Set integerKeys = prefsToAdd.getIntegerKeySet(); + Set vectorKeys = prefsToAdd.getVectorKeySet(); Set floatKeys = prefsToAdd.getFloatKeySet(); Set longKeys = prefsToAdd.getLongKeySet(); Set booleanKeys = prefsToAdd.getBooleanKeySet(); Set 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 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 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 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/README b/sources/protocols/README deleted file mode 100644 index 3bf4f6d..0000000 --- a/sources/protocols/README +++ /dev/null @@ -1,10 +0,0 @@ -How to add a new protocol: - -1. Copy the file DummyProtocol.java into YourOwnNiceProtocol.java - -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" - - 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 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 vec = new Vector(); + vec.add(2); + vec.add(3); + vec.add(4); + + initVector("pids", vec, "PIDs beteilitger Prozesse"); } /* (non-Javadoc) diff --git a/sources/protocols/implementations/README b/sources/protocols/implementations/README new file mode 100644 index 0000000..8126ce0 --- /dev/null +++ b/sources/protocols/implementations/README @@ -0,0 +1,10 @@ +How to add a new protocol: + +1. Copy the file DummyProtocol.java into YourOwnNiceProtocol.java + +2. Edit YourOwnNiceProtocol.java and replace the classname! + +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 parseIntegerVector(String string) + throws exceptions.ParseIntegerVectorException { + System.out.println("parse " + string); + Vector vec = new Vector(); + + 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; + } } -- cgit v1.2.3