|
From: <br...@us...> - 2016-07-14 17:21:26
|
Revision: 4180
http://sourceforge.net/p/openlcb/svn/4180
Author: bracz
Date: 2016-07-14 17:21:23 +0000 (Thu, 14 Jul 2016)
Log Message:
-----------
Adds the concept of "default value" to the VersionedValue object. Makes sure that getValue() always returns non-null, but it is possible to figure out if the object is still at the default value.
Changes BitEventPC to producer "unknown" if the value is still at the default, and send off an actual event at the first set() call.
Modified Paths:
--------------
trunk/prototypes/java/src/org/openlcb/implementations/BitProducerConsumer.java
trunk/prototypes/java/src/org/openlcb/implementations/VersionedValue.java
trunk/prototypes/java/test/org/openlcb/implementations/BitProducerConsumerTest.java
Modified: trunk/prototypes/java/src/org/openlcb/implementations/BitProducerConsumer.java
===================================================================
--- trunk/prototypes/java/src/org/openlcb/implementations/BitProducerConsumer.java 2016-07-14 17:21:05 UTC (rev 4179)
+++ trunk/prototypes/java/src/org/openlcb/implementations/BitProducerConsumer.java 2016-07-14 17:21:23 UTC (rev 4180)
@@ -22,13 +22,26 @@
private final EventID eventOn;
private final EventID eventOff;
private final OlcbInterface iface;
- private VersionedValue<Boolean> value = null;
- private VersionedValueListener<Boolean> valueListener = null;
+ private final VersionedValue<Boolean> value;
+ private final VersionedValueListener<Boolean> valueListener;
- public BitProducerConsumer(OlcbInterface iface, EventID eventOn, EventID eventOff) {
+ public BitProducerConsumer(OlcbInterface iface, EventID eventOn, EventID eventOff, boolean
+ defaultValue) {
this.iface = iface;
this.eventOn = eventOn;
this.eventOff = eventOff;
+ value = new VersionedValue<>(defaultValue);
+ valueListener = new VersionedValueListener<Boolean>(value) {
+ @Override
+ public void update(Boolean newValue) {
+ Message msg = new ProducerConsumerEventReportMessage(BitProducerConsumer.this.iface
+ .getNodeId(),
+ newValue ? BitProducerConsumer.this.eventOn :
+ BitProducerConsumer.this.eventOff);
+ BitProducerConsumer.this.iface.getOutputConnection().put(msg, BitProducerConsumer
+ .this);
+ }
+ };
iface.registerMessageListener(this);
iface.getOutputConnection().registerStartNotification(new ConnectionListener() {
@Override
@@ -38,19 +51,22 @@
});
}
- public synchronized VersionedValue<Boolean> getValue(boolean defaultValue) {
- if(value == null) {
- setValueFromNetwork(defaultValue);
- }
+ public VersionedValue<Boolean> getValue() {
return value;
}
- public VersionedValue<Boolean> getValue() {
- return value;
+ /**
+ * @return true if we have not received any network state yet, thus the value is still at the
+ * default value passed in.
+ */
+ public boolean isValueAtDefault() {
+ return (value.getVersion() == value.DEFAULT_VERSION);
}
private EventState getOnEventState() {
- if (value == null) return EventState.Unknown;
+ if (isValueAtDefault()) {
+ return EventState.Unknown;
+ }
if (value.getLatestData()) return EventState.Valid;
return EventState.Invalid;
}
@@ -161,20 +177,6 @@
}
private void setValueFromNetwork(boolean isOn) {
- synchronized (this) {
- if (value == null) {
- value = new VersionedValue<>(isOn);
- valueListener = new VersionedValueListener<Boolean>(value) {
- @Override
- public void update(Boolean newValue) {
- Message msg = new ProducerConsumerEventReportMessage(iface.getNodeId(),
- newValue ? eventOn : eventOff);
- iface.getOutputConnection().put(msg, BitProducerConsumer.this);
- }
- };
- return;
- }
- }
valueListener.setFromOwner(isOn);
}
}
Modified: trunk/prototypes/java/src/org/openlcb/implementations/VersionedValue.java
===================================================================
--- trunk/prototypes/java/src/org/openlcb/implementations/VersionedValue.java 2016-07-14 17:21:05 UTC (rev 4179)
+++ trunk/prototypes/java/src/org/openlcb/implementations/VersionedValue.java 2016-07-14 17:21:23 UTC (rev 4180)
@@ -8,10 +8,11 @@
int version;
int nextVersion;
java.beans.PropertyChangeSupport pcs = new java.beans.PropertyChangeSupport(this);
+ public static int DEFAULT_VERSION = 1;
public VersionedValue(T t) {
- version = 1;
- nextVersion = 2;
+ version = DEFAULT_VERSION;
+ nextVersion = DEFAULT_VERSION + 1;
data = t;
}
@@ -35,14 +36,18 @@
boolean updated = false;
synchronized (this) {
if (atVersion <= version) return false;
+ int oldVersion = version;
version = atVersion;
if (nextVersion <= atVersion) {
nextVersion = atVersion + 1;
}
- if (data.equals(t)) {
+ if (data.equals(t) && oldVersion != DEFAULT_VERSION) {
return true;
}
old = data;
+ if (oldVersion == DEFAULT_VERSION) {
+ old = null;
+ }
data = t;
}
firePropertyChange("updated", old, t);
Modified: trunk/prototypes/java/test/org/openlcb/implementations/BitProducerConsumerTest.java
===================================================================
--- trunk/prototypes/java/test/org/openlcb/implementations/BitProducerConsumerTest.java 2016-07-14 17:21:05 UTC (rev 4179)
+++ trunk/prototypes/java/test/org/openlcb/implementations/BitProducerConsumerTest.java 2016-07-14 17:21:23 UTC (rev 4180)
@@ -82,9 +82,10 @@
}
public void helperInputSetClear(String frameOn, String frameOff) {
- assertNull(pc.getValue());
+ assertTrue(pc.isValueAtDefault());
+ assertFalse(pc.getValue().getLatestData());
sendFrame(frameOn);
- assertNotNull(pc.getValue());
+ assertFalse(pc.isValueAtDefault());
assertTrue(pc.getValue().getLatestData());
sendFrame(frameOff);
assertFalse(pc.getValue().getLatestData());
@@ -161,15 +162,21 @@
}
public void testGenerateEvents() throws Exception {
- VersionedValue<Boolean> v = pc.getValue(false);
+ VersionedValue<Boolean> v = pc.getValue();
sendFrameAndExpectResult( //
":X19914444N0504030201000708;",
- ":X19545333N0504030201000708;");
+ ":X19547333N0504030201000708;");
expectNoFrames();
v.set(false);
+ expectFrame(":X195B4333N0504030201000709;");
+
expectNoFrames();
+ sendFrameAndExpectResult( //
+ ":X19914444N0504030201000708;",
+ ":X19545333N0504030201000708;");
+ expectNoFrames();
v.set(true);
expectFrame(":X195B4333N0504030201000708;");
@@ -193,7 +200,7 @@
public void setUp() {
iface.fakeOutputConnection.history.clear();
aliasMap.insert(0x444, new NodeID(new byte[]{1,2,3,1,2,3}));
- pc = new BitProducerConsumer(iface, onEvent, offEvent);
+ pc = new BitProducerConsumer(iface, onEvent, offEvent, false);
expectFrame(":X19547333N0504030201000708;");
expectFrame(":X19547333N0504030201000709;");
expectFrame(":X194C7333N0504030201000708;");
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|