Skip to content

Commit c62d2ab

Browse files
author
Aaron
committed
Fix empty node deserialization.
Add enabled to backup service.
1 parent db76db4 commit c62d2ab

File tree

6 files changed

+174
-130
lines changed

6 files changed

+174
-130
lines changed

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/sys/backup/SysBackupService.java

+84-69
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.iot.dsa.dslink.DSLink;
1616
import org.iot.dsa.io.NodeEncoder;
1717
import org.iot.dsa.io.json.JsonWriter;
18+
import org.iot.dsa.node.DSBool;
1819
import org.iot.dsa.node.DSIValue;
1920
import org.iot.dsa.node.DSInfo;
2021
import org.iot.dsa.node.DSLong;
@@ -25,45 +26,43 @@
2526
import org.iot.dsa.time.DSTime;
2627

2728
public class SysBackupService extends DSNode implements Runnable {
28-
29+
30+
static final String ENABLED = "Enabled";
2931
static final String INTERVAL = "Backup Interval";
30-
static final String MAXIMUM = "Max Number of Backups";
32+
static final String MAXIMUM = "Max Backups";
3133
static final String SAVE = "Save";
32-
34+
35+
private DSInfo enabled = getInfo(ENABLED);
3336
private DSInfo interval = getInfo(INTERVAL);
34-
private DSInfo maximum = getInfo(MAXIMUM);
35-
private DSInfo save = getInfo(SAVE);
36-
3737
private DSLink link;
38-
private Timer nextSave;
3938
private Object lock = new Object();
40-
41-
@Override
42-
protected void declareDefaults() {
43-
declareDefault(SAVE, DSAction.DEFAULT);
44-
declareDefault(INTERVAL, DSLong.valueOf(60));
45-
declareDefault(MAXIMUM, DSLong.valueOf(3));
39+
private DSInfo maximum = getInfo(MAXIMUM);
40+
private Timer nextSave;
41+
private DSInfo save = getInfo(SAVE);
42+
43+
public boolean isEnabled() {
44+
return enabled.getElement().toBoolean();
4645
}
47-
46+
4847
@Override
49-
protected void onStable() {
50-
File nodes = getLink().getConfig().getNodesFile();
51-
if (nodes.exists()) {
48+
public void onChildChanged(DSInfo info) {
49+
super.onChildChanged(info);
50+
if (info == interval) {
51+
DSIValue value = info.getValue();
5252
synchronized (lock) {
53-
scheduleNextSave();
53+
if (nextSave != null) {
54+
long newNextRun =
55+
(value.toElement().toLong() * 60000) + System.currentTimeMillis();
56+
long scheduledNextRun = nextSave.nextRun();
57+
if (newNextRun < scheduledNextRun) {
58+
nextSave.cancel();
59+
nextSave = DSRuntime.runAt(this, newNextRun);
60+
}
61+
}
5462
}
55-
} else {
56-
DSRuntime.run(this);
57-
}
58-
}
59-
60-
private DSLink getLink() {
61-
if (link == null) {
62-
link = (DSLink) getAncestor(DSLink.class);
6363
}
64-
return link;
6564
}
66-
65+
6766
@Override
6867
public ActionResult onInvoke(DSInfo action, ActionInvocation invocation) {
6968
if (action == save) {
@@ -73,30 +72,29 @@ public ActionResult onInvoke(DSInfo action, ActionInvocation invocation) {
7372
}
7473
return null;
7574
}
76-
75+
7776
@Override
78-
public void onChildChanged(DSInfo info) {
79-
super.onChildChanged(info);
80-
if (info == interval) {
81-
DSIValue value = info.getValue();
82-
synchronized (lock) {
83-
if (nextSave != null) {
84-
long newNextRun = (value.toElement().toLong() * 60000) + System.currentTimeMillis();
85-
long scheduledNextRun = nextSave.nextRun();
86-
if (newNextRun < scheduledNextRun) {
87-
nextSave.cancel();
88-
nextSave = DSRuntime.runAt(this, newNextRun);
89-
}
90-
}
91-
}
77+
public void onStopped() {
78+
if (nextSave != null) {
79+
nextSave.cancel();
80+
nextSave = null;
9281
}
82+
save();
9383
}
94-
84+
85+
@Override
86+
public void run() {
87+
synchronized (lock) {
88+
save();
89+
scheduleNextSave();
90+
}
91+
}
92+
9593
/**
9694
* Serializes the configuration database.
9795
*/
9896
public void save() {
99-
if (!getLink().isSaveEnabled()) {
97+
if (!isEnabled()) {
10098
return;
10199
}
102100
ZipOutputStream zos = null;
@@ -170,7 +168,47 @@ public void save() {
170168
error("Closing output", x);
171169
}
172170
}
173-
171+
172+
/**
173+
* Intended for use by DSLink subclasses, such as testing links.
174+
*/
175+
public void setEnabled(boolean arg) {
176+
put(enabled, DSBool.valueOf(arg));
177+
}
178+
179+
@Override
180+
protected void declareDefaults() {
181+
declareDefault(SAVE, DSAction.DEFAULT);
182+
declareDefault(ENABLED, DSBool.TRUE).setTransient(true);
183+
declareDefault(INTERVAL, DSLong.valueOf(60));
184+
declareDefault(MAXIMUM, DSLong.valueOf(3));
185+
}
186+
187+
@Override
188+
protected void onStable() {
189+
File nodes = getLink().getConfig().getNodesFile();
190+
if (nodes.exists()) {
191+
synchronized (lock) {
192+
scheduleNextSave();
193+
}
194+
} else {
195+
DSRuntime.run(this);
196+
}
197+
}
198+
199+
private DSLink getLink() {
200+
if (link == null) {
201+
link = (DSLink) getAncestor(DSLink.class);
202+
}
203+
return link;
204+
}
205+
206+
private void scheduleNextSave() {
207+
long saveInterval = interval.getElement().toLong();
208+
saveInterval *= 60000;
209+
nextSave = DSRuntime.runDelayed(this, saveInterval);
210+
}
211+
174212
/**
175213
* Called by save, no need to explicitly call.
176214
*/
@@ -213,28 +251,5 @@ public boolean accept(File dir, String name) {
213251
backups[i].delete();
214252
}
215253
}
216-
217-
private void scheduleNextSave() {
218-
long saveInterval = interval.getElement().toLong();
219-
saveInterval *= 60000;
220-
nextSave = DSRuntime.runDelayed(this, saveInterval);
221-
}
222-
223-
@Override
224-
public void run() {
225-
synchronized(lock) {
226-
save();
227-
scheduleNextSave();
228-
}
229-
}
230-
231-
@Override
232-
public void onStopped() {
233-
if (nextSave != null) {
234-
nextSave.cancel();
235-
nextSave = null;
236-
}
237-
save();
238-
}
239254

240255
}

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/test/V1TestLink.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public V1TestLink() {
2121
}
2222

2323
public V1TestLink(DSMainNode MainNode) {
24-
setSaveEnabled(false);
24+
getSys().getBackupService().setEnabled(false);
2525
setNodes(MainNode);
2626
DSLinkConfig cfg = new DSLinkConfig();
2727
cfg.setDslinkJson(new DSMap().put("configs", new DSMap()));

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/test/V2TestLink.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public V2TestLink() {
1818
}
1919

2020
public V2TestLink(DSMainNode MainNode) {
21-
setSaveEnabled(false);
21+
getSys().getBackupService().setEnabled(false);
2222
setNodes(MainNode);
2323
DSLinkConfig cfg = new DSLinkConfig();
2424
cfg.setDslinkJson(new DSMap().put("configs", new DSMap()));

dslink-v2/src/main/java/org/iot/dsa/dslink/DSLink.java

+47-55
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package org.iot.dsa.dslink;
22

3-
import org.iot.dsa.logging.DSLogHandler;
43
import java.io.File;
54
import java.net.URL;
65
import java.util.logging.Handler;
76
import java.util.logging.LogManager;
87
import java.util.logging.Logger;
98
import org.iot.dsa.io.NodeDecoder;
109
import org.iot.dsa.io.json.JsonReader;
10+
import org.iot.dsa.logging.DSLogHandler;
1111
import org.iot.dsa.node.DSInfo;
1212
import org.iot.dsa.node.DSNode;
1313
import org.iot.dsa.security.DSKeys;
@@ -44,7 +44,6 @@ public class DSLink extends DSNode implements Runnable {
4444
private DSInfo main = getInfo(MAIN);
4545
private String name;
4646
private Thread runThread;
47-
private boolean saveEnabled = true;
4847
private DSInfo sys = getInfo(SYS);
4948

5049
///////////////////////////////////////////////////////////////////////////
@@ -60,18 +59,9 @@ public DSLink() {
6059
}
6160

6261
///////////////////////////////////////////////////////////////////////////
63-
// Methods in alphabetical order
62+
// Public Methods
6463
///////////////////////////////////////////////////////////////////////////
6564

66-
/**
67-
* Adds the save action, overrides should call super if they want this action.
68-
*/
69-
@Override
70-
protected void declareDefaults() {
71-
declareDefault(MAIN, new DSNode());
72-
declareDefault(SYS, new DSSysNode()).setAdmin(true);
73-
}
74-
7565
public DSLinkConfig getConfig() {
7666
return config;
7767
}
@@ -111,22 +101,6 @@ public String getLinkName() {
111101
return name;
112102
}
113103

114-
@Override
115-
protected String getLogName() {
116-
String s = getLinkName();
117-
if (s.startsWith("dslink-java")) {
118-
if (s.startsWith("dslink-java-v2-")) {
119-
s = s.substring("dslink-java-v2-".length());
120-
} else if (s.startsWith("dslink-java-")) {
121-
s = s.substring("dslink-java-".length());
122-
}
123-
}
124-
if (s.isEmpty()) {
125-
return getClass().getSimpleName();
126-
}
127-
return s;
128-
}
129-
130104
public DSMainNode getMain() {
131105
return (DSMainNode) main.getNode();
132106
}
@@ -135,20 +109,6 @@ public DSSysNode getSys() {
135109
return (DSSysNode) sys.getNode();
136110
}
137111

138-
/**
139-
* Configures a link instance including creating the appropriate connection.
140-
*
141-
* @return This
142-
*/
143-
protected DSLink init(DSLinkConfig config) {
144-
this.config = config;
145-
DSLogHandler.setRootLevel(config.getLogLevel());
146-
name = config.getLinkName();
147-
keys = config.getKeys();
148-
getSys().init();
149-
return this;
150-
}
151-
152112
/**
153113
* Creates a link by first testing for an existing serialized database.
154114
*
@@ -266,11 +226,9 @@ public void run() {
266226
}
267227
}
268228

269-
@Override
270-
protected void onStopped() {
271-
synchronized (this) {
272-
notifyAll();
273-
}
229+
public DSLink setNodes(DSMainNode node) {
230+
put(main, node);
231+
return this;
274232
}
275233

276234
/**
@@ -291,20 +249,54 @@ public void shutdown() {
291249
}
292250
}
293251

294-
public DSLink setNodes(DSMainNode node) {
295-
put(main, node);
296-
return this;
252+
///////////////////////////////////////////////////////////////////////////
253+
// Protected Methods
254+
///////////////////////////////////////////////////////////////////////////
255+
256+
/**
257+
* Adds the save action, overrides should call super if they want this action.
258+
*/
259+
@Override
260+
protected void declareDefaults() {
261+
declareDefault(MAIN, new DSNode());
262+
declareDefault(SYS, new DSSysNode()).setAdmin(true);
263+
}
264+
265+
@Override
266+
protected String getLogName() {
267+
String s = getLinkName();
268+
if (s.startsWith("dslink-java")) {
269+
if (s.startsWith("dslink-java-v2-")) {
270+
s = s.substring("dslink-java-v2-".length());
271+
} else if (s.startsWith("dslink-java-")) {
272+
s = s.substring("dslink-java-".length());
273+
}
274+
}
275+
if (s.isEmpty()) {
276+
return getClass().getSimpleName();
277+
}
278+
return s;
297279
}
298280

299281
/**
300-
* This is a transient option intended for unit tests. True by default.
282+
* Configures a link instance including creating the appropriate connection.
283+
*
284+
* @return This
301285
*/
302-
protected DSLink setSaveEnabled(boolean enabled) {
303-
saveEnabled = enabled;
286+
protected DSLink init(DSLinkConfig config) {
287+
this.config = config;
288+
DSLogHandler.setRootLevel(config.getLogLevel());
289+
name = config.getLinkName();
290+
keys = config.getKeys();
291+
getSys().init();
304292
return this;
305293
}
306294

307-
public boolean isSaveEnabled() {
308-
return saveEnabled;
295+
@Override
296+
protected void onStopped() {
297+
synchronized (this) {
298+
notifyAll();
299+
}
309300
}
301+
310302
}

0 commit comments

Comments
 (0)