diff options
| author | Paul Buetow <paul@buetow.org> | 2008-05-28 22:14:40 +0000 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2008-05-28 22:14:40 +0000 |
| commit | 1a16f73ec3bb0eae98b4d540984f9015abe4c249 (patch) | |
| tree | a721b20b6a667ad5944c8e94e8646b5a5d447ca9 /sources/protocols/implementations/VSOnePhaseCommitProtocol.java | |
| parent | ca09c67e85acb0ab6c85c87f0dd0c05b35262b12 (diff) | |
all protocol classnames now have a VS prefix
Diffstat (limited to 'sources/protocols/implementations/VSOnePhaseCommitProtocol.java')
| -rw-r--r-- | sources/protocols/implementations/VSOnePhaseCommitProtocol.java | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/sources/protocols/implementations/VSOnePhaseCommitProtocol.java b/sources/protocols/implementations/VSOnePhaseCommitProtocol.java new file mode 100644 index 0000000..985378e --- /dev/null +++ b/sources/protocols/implementations/VSOnePhaseCommitProtocol.java @@ -0,0 +1,143 @@ +/* + * VS is (c) 2008 by Paul C. Buetow + * vs@dev.buetow.org + */ +package protocols.implementations; + +import java.util.ArrayList; +import java.util.Vector; + +import protocols.VSAbstractProtocol; +import core.VSMessage; + +/** + * The Class VSOnePhaseCommitProtocol. + */ +public class VSOnePhaseCommitProtocol extends VSAbstractProtocol { + private static final long serialVersionUID = 1L; + + /* Client variables, coordinator */ + private ArrayList<Integer> pids; + + /* Server variables */ + private boolean ackSent; + + /** + * Instantiates a one phase commit protocol. + */ + public VSOnePhaseCommitProtocol() { + super(VSAbstractProtocol.HAS_ON_SERVER_START); + setClassname(getClass().toString()); + } + + /* (non-Javadoc) + * @see events.VSAbstractProtocol#onServerInit() + */ + public void onServerInit() { + /* Can be changed via GUI variables editor of each process */ + Vector<Integer> vec = new Vector<Integer>(); + vec.add(2); + vec.add(3); + + initVector("pids", vec, "PIDs beteilitger Prozesse"); + initLong("timeout", 2500, "Zeit bis erneuerter Anfrage", "ms"); + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#onServerReset() + */ + public void onServerReset() { + if (pids != null) { + pids.clear(); + pids.addAll(getVector("pids")); + } + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#onServerStart() + */ + public void onServerStart() { + if (pids == null) { + pids = new ArrayList<Integer>(); + pids.addAll(getVector("pids")); + } + + if (pids.size() != 0) { + long timeout = getLong("timeout") + process.getTime(); + scheduleAt(timeout); /* Will run onServerSchedule() at the specified local time */ + + VSMessage message = new VSMessage(); + message.setBoolean("wantAck", true); + sendMessage(message); + } + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#onServerRecv(core.VSMessage) + */ + public void onServerRecv(VSMessage recvMessage) { + if (pids.size() == 0) + return; + + if (recvMessage.getBoolean("isAck")) { + Integer pid = recvMessage.getIntegerObj("pid"); + if (pids.contains(pid)) + pids.remove(pid); + else + return; + + logg("ACK von Prozess " + pid + " erhalten!"); + + if (pids.size() == 0) + logg("ACKs von allen beteiligten Prozessen erhalten! Festgeschrieben!"); + } + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#onServerSchedule() + */ + public void onServerSchedule() { + onServerStart(); + } + + /* (non-Javadoc) + * @see events.VSAbstractProtocol#onClientInit() + */ + public void onClientInit() { + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#onClientReset() + */ + public void onClientReset() { + ackSent = false; + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#onClientRecv(core.VSMessage) + */ + public void onClientRecv(VSMessage recvMessage) { + if (ackSent) + return; + + VSMessage message = new VSMessage(); + message.setBoolean("isAck", true); + message.setInteger("pid", process.getProcessID()); + sendMessage(message); + ackSent = true; + logg("Festgeschrieben"); + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#onClientSchedule() + */ + public void onClientSchedule() { + } + + /* (non-Javadoc) + * @see protocols.VSAbstractProtocol#toString() + */ + public String toString() { + return super.toString(); + } +} |
