Skip to content

Commit 045a99f

Browse files
authored
0.67.0
- New locked flag and many dynamic objects are now locked. - Decode renamed defaults as generic nodes. - Value node reports cov to parent. - Refactor the time package.
1 parent 1995989 commit 045a99f

File tree

23 files changed

+1672
-1055
lines changed

23 files changed

+1672
-1055
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ Please read the [developer guide](https://github.com/iot-dsa-v2/sdk-dslink-java-
2020

2121
**/dslink-v2**
2222

23-
- For links that wish to use a custom websocket library, such as in a servlet
24-
container that already provides one.
23+
- Link implementation. Links that wish to use a custom websocket library should declare
24+
a dependency on this, such as in a servlet container that already provides one. Otherwise,
25+
use dslink-v2-websocket.
2526

2627
**/dslink-v2-api**
2728

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ subprojects {
55
apply plugin: 'maven'
66

77
group 'org.iot-dsa'
8-
version '0.66.1'
8+
version '0.67.0'
99

1010
targetCompatibility = JavaVersion.VERSION_1_8
1111
sourceCompatibility = JavaVersion.VERSION_1_8

dslink-v2-api/src/main/java/org/iot/dsa/DSRuntime.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.iot.dsa;
22

3-
import org.iot.dsa.time.DSTime;
3+
import org.iot.dsa.time.Time;
44

55
/**
66
* DSA thread pool and timers.
@@ -61,8 +61,8 @@ public static Timer run(Runnable arg, long start, long intervalMillis) {
6161
* @return For inspecting and cancel execution.
6262
*/
6363
public static Timer runAfterDelay(Runnable arg, long delayMillis, long intervalMillis) {
64-
long intervalNanos = intervalMillis * DSTime.NANOS_IN_MS;
65-
long delayNanos = delayMillis * DSTime.NANOS_IN_MS;
64+
long intervalNanos = intervalMillis * Time.NANOS_IN_MS;
65+
long delayNanos = delayMillis * Time.NANOS_IN_MS;
6666
long startNanos = System.nanoTime() + delayNanos;
6767
Timer f = new Timer(arg, startNanos, intervalNanos);
6868
synchronized (DSRuntime.class) {
@@ -102,7 +102,7 @@ public static Timer runAt(Runnable arg, long at) {
102102
* @return For inspecting and cancel execution.
103103
*/
104104
public static Timer runDelayed(Runnable arg, long delayMillis) {
105-
long delayNanos = delayMillis * DSTime.NANOS_IN_MS;
105+
long delayNanos = delayMillis * Time.NANOS_IN_MS;
106106
long startNanos = System.nanoTime() + delayNanos;
107107
Timer f = new Timer(arg, startNanos, -1);
108108
synchronized (DSRuntime.class) {
@@ -179,6 +179,14 @@ private static void executeTimers() {
179179
}
180180
}
181181

182+
private static long nanoTimeToSystemTimeMillis(long nanoTime) {
183+
long nowNanos = System.nanoTime();
184+
long nowMillis = System.currentTimeMillis();
185+
long nanosTillTime = nanoTime - nowNanos;
186+
long millisTillTime = nanosTillTime / Time.NANOS_IN_MS;
187+
return nowMillis + millisTillTime;
188+
}
189+
182190
private static void shutdown() {
183191
synchronized (DSRuntime.class) {
184192
alive = false;
@@ -206,7 +214,7 @@ public void run() {
206214
while (alive) {
207215
executeTimers();
208216
synchronized (DSRuntime.class) {
209-
delta = (nextCycle - System.nanoTime()) / DSTime.NANOS_IN_MS;
217+
delta = (nextCycle - System.nanoTime()) / Time.NANOS_IN_MS;
210218
if (delta > 0) {
211219
try {
212220
DSRuntime.class.wait(delta);
@@ -274,7 +282,7 @@ public void cancel() {
274282
* The interval between runs, zero or less for no interval.
275283
*/
276284
public long getInterval() {
277-
return interval / DSTime.NANOS_IN_MS;
285+
return interval / Time.NANOS_IN_MS;
278286
}
279287

280288
/**
@@ -306,7 +314,7 @@ public boolean isRunning() {
306314
* The lastRun run or -1 if it hasn't run yet.
307315
*/
308316
public long lastRun() {
309-
return hasRun ? DSTime.nanoTimeToSystemTimeMillis(lastRun) : -1;
317+
return hasRun ? nanoTimeToSystemTimeMillis(lastRun) : -1;
310318
}
311319

312320
/**
@@ -315,7 +323,7 @@ public long lastRun() {
315323
* @return 0 or less when finished.
316324
*/
317325
public long nextRun() {
318-
return done ? cancelled ? 0 : -1 : DSTime.nanoTimeToSystemTimeMillis(nextRun);
326+
return done ? cancelled ? 0 : -1 : nanoTimeToSystemTimeMillis(nextRun);
319327
}
320328

321329
/**
@@ -350,7 +358,7 @@ public Timer setSkipMissedIntervals(boolean skipMissed) {
350358

351359
public String toString() {
352360
StringBuilder buf = new StringBuilder();
353-
DSTime.encode(nextRun(), false, buf);
361+
Time.encode(nextRun(), false, buf);
354362
buf.append(" - ").append(runnable.toString());
355363
return buf.toString();
356364
}

dslink-v2-api/src/main/java/org/iot/dsa/DSThreadPool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.LinkedList;
44
import java.util.logging.Level;
55
import java.util.logging.Logger;
6-
import org.iot.dsa.time.DSTime;
6+
import org.iot.dsa.time.Time;
77

88
/**
99
* A simple thread pool with the option for an unbounded maximum number of threads.
@@ -193,13 +193,13 @@ public void run() {
193193
while ((r == null) && alive) {
194194
r = dequeue();
195195
if (r == null) {
196-
if (((System.currentTimeMillis() - start) > DSTime.MILLIS_MINUTE) &&
196+
if (((System.currentTimeMillis() - start) > Time.MILLIS_MINUTE) &&
197197
(numThreads > min)) {
198198
idleThreads--;
199199
return;
200200
} else {
201201
try {
202-
DSThreadPool.this.wait(DSTime.MILLIS_FIVE_SECONDS);
202+
DSThreadPool.this.wait(Time.MILLIS_FIVE_SECONDS);
203203
} catch (Exception ignore) {
204204
}
205205
}

dslink-v2-api/src/main/java/org/iot/dsa/dslink/DSLinkConnection.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.iot.dsa.dslink;
22

33
import org.iot.dsa.conn.DSConnection;
4+
import org.iot.dsa.node.DSInfo;
45

56
/**
67
* Abstract representation of a DSA connection. Subclasses are responsible for providing
@@ -102,18 +103,21 @@ protected void doDisconnect() {
102103
@Override
103104
protected void onDisconnected() {
104105
super.onDisconnected();
106+
DSInfo info = getInfo(TRANSPORT);
107+
if (info != null) {
108+
remove(info.setLocked(false));
109+
}
105110
transport = null;
106-
remove(TRANSPORT);
107111
}
108112

109113
protected void setSession(DSISession session) {
110114
this.session = session;
111-
put(SESSION, session).setTransient(true);
115+
put(SESSION, session).setTransient(true).setLocked(true);
112116
}
113117

114118
protected void setTransport(DSITransport transport) {
115119
this.transport = transport;
116-
put(TRANSPORT, transport).setTransient(true);
120+
put(TRANSPORT, transport).setTransient(true).setLocked(true);
117121
}
118122

119123
}

dslink-v2-api/src/main/java/org/iot/dsa/io/NodeDecoder.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,14 @@ private void readChild(DSNode parent) {
162162
obj = info.get();
163163
}
164164
if (obj == null) { //dynamic, or declareDefaults was modified
165-
in.next();
166-
obj = in.getElement();
167-
info = parent.put(name, obj);
165+
if (in.next() == Token.BEGIN_MAP) {
166+
obj = new DSNode();
167+
info = parent.put(name, obj);
168+
readChildren((DSNode)obj);
169+
} else {
170+
obj = in.getElement();
171+
info = parent.put(name, obj);
172+
}
168173
} else if (obj instanceof DSNode) {
169174
readChildren((DSNode) obj);
170175
} else {
@@ -178,7 +183,7 @@ private void readChild(DSNode parent) {
178183
}
179184
}
180185
}
181-
if (obj == null) { //Node with no children
186+
if (obj == null) { //Node with no children, there was no "v" key.
182187
if (name == null) {
183188
throw new IllegalStateException("Missing name");
184189
}
@@ -191,6 +196,8 @@ private void readChild(DSNode parent) {
191196
}
192197
} else if (info != null) {
193198
obj = info.get();
199+
} else {
200+
obj = new DSNode();
194201
}
195202
}
196203
if (info != null) {

dslink-v2-api/src/main/java/org/iot/dsa/logging/DSLogHandler.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.logging.Level;
1111
import java.util.logging.LogRecord;
1212
import java.util.logging.Logger;
13-
import org.iot.dsa.time.DSTime;
13+
import org.iot.dsa.time.Time;
1414

1515
/**
1616
* All instances of this handler asynchronously print log records to System.out.
@@ -246,8 +246,8 @@ static void write(Handler handler, LogRecord record, StringBuilder builder) {
246246
}
247247
builder.append('[');
248248
// timestamp
249-
Calendar calendar = DSTime.getCalendar(record.getMillis());
250-
DSTime.encodeForLogs(calendar, builder);
249+
Calendar calendar = Time.getCalendar(record.getMillis());
250+
Time.encodeForLogs(calendar, builder);
251251
builder.append(']');
252252
builder.append(' ');
253253
// severity
@@ -284,7 +284,7 @@ static void write(Handler handler, LogRecord record, StringBuilder builder) {
284284
pw.close();
285285
builder.append(sw.toString());
286286
}
287-
DSTime.recycle(calendar);
287+
Time.recycle(calendar);
288288
}
289289

290290
/* Old V2 Format, save for now...
@@ -298,10 +298,10 @@ static void write(Handler handler, LogRecord record, StringBuilder buf) {
298298
// severity
299299
buf.append('[').append(toString(record.getLevel())).append(' ');
300300
// timestamp
301-
Calendar calendar = DSTime.getCalendar(record.getMillis());
301+
Calendar calendar = Time.getCalendar(record.getMillis());
302302
calendar.setTimeInMillis(record.getMillis());
303-
DSTime.encodeForLogs(calendar, buf);
304-
DSTime.recycle(calendar);
303+
Time.encodeForLogs(calendar, buf);
304+
Time.recycle(calendar);
305305
buf.append(']');
306306
// log name
307307
String name = record.getLoggerName();
@@ -365,7 +365,7 @@ public void run() {
365365
}
366366
if (state == STATE_OPEN) {
367367
try {
368-
queue.wait(DSTime.MILLIS_SECOND);
368+
queue.wait(Time.MILLIS_SECOND);
369369
} catch (Exception ignore) {
370370
}
371371
} else {

dslink-v2-api/src/main/java/org/iot/dsa/node/DSInfo.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class DSInfo implements ApiObject, GroupListener {
3434
static final int READONLY = 3;
3535
static final int DECLARED = 4;
3636
static final int DEFAULT_ON_COPY = 5;
37+
static final int LOCKED = 6;
3738

3839
///////////////////////////////////////////////////////////////////////////
3940
// Fields
@@ -319,6 +320,20 @@ public boolean isDefaultOnCopy() {
319320
return getFlag(DEFAULT_ON_COPY);
320321
}
321322

323+
/**
324+
* True if declared or locked.
325+
*/
326+
public boolean isFrozen() {
327+
return isDeclared() || isLocked();
328+
}
329+
330+
/**
331+
* True if the info cannot be removed or renamed. Use for non-default nodes.
332+
*/
333+
public boolean isLocked() {
334+
return getFlag(LOCKED);
335+
}
336+
322337
/**
323338
* Whether or not the object is a DSNode.
324339
*/
@@ -453,6 +468,15 @@ public DSInfo setDefaultOnCopy(boolean defaultOnCopy) {
453468
return this;
454469
}
455470

471+
/**
472+
* False by default, set to true if the info cannot be removed or renamed.
473+
* Use for non-default nodes.
474+
*/
475+
public DSInfo setLocked(boolean locked) {
476+
setFlag(LOCKED, locked);
477+
return this;
478+
}
479+
456480
public DSInfo setMetadata(DSMap map) {
457481
map.setParent(this);
458482
metadata = map;
@@ -542,13 +566,6 @@ boolean isProxy() {
542566
return false;
543567
}
544568

545-
/**
546-
* Whether or not this info represents a declared default.
547-
*/
548-
boolean isRemovable() {
549-
return !getFlag(DECLARED);
550-
}
551-
552569
DSInfo setFlag(int position, boolean on) {
553570
//modified?
554571
flags = DSUtil.setBit(flags, position, on);

dslink-v2-api/src/main/java/org/iot/dsa/node/DSInfoProxy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ public boolean equalsDefaultType() {
8585

8686
@Override
8787
public boolean equalsDefaultValue() {
88+
if (isNode()) {
89+
return getNode().equivalent(defaultInfo.object);
90+
}
8891
return DSUtil.equal(object, defaultInfo.object);
8992
}
9093

0 commit comments

Comments
 (0)