Skip to content

Commit 61dbe9a

Browse files
authored
0.24.0
Fix U16 and U32 handling in MsgPack Add DSAbstractAction to simplify anonymous actions. Support secure broker connections in v1.
1 parent 2f742d1 commit 61dbe9a

File tree

8 files changed

+454
-285
lines changed

8 files changed

+454
-285
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'java'
22
apply plugin: 'maven'
33

44
group 'org.iot.dsa'
5-
version '0.23.0'
5+
version '0.24.0'
66

77
sourceCompatibility = 1.6
88
targetCompatibility = 1.6

dslink-core/src/main/java/com/acuity/iot/dsa/dslink/io/DSByteBuffer.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,39 @@ public DSByteBuffer putShort(short v, boolean bigEndian) {
392392
return put((byte) ((v >>> 0) & 0xFF), (byte) ((v >>> 8) & 0xFF));
393393
}
394394

395+
/**
396+
* Encodes the primitive into buffer.
397+
*
398+
* @param v The value to encode.
399+
* @param bigEndian Whether to encode in big or little endian byte ordering.
400+
*/
401+
public DSByteBuffer putU16(int v, boolean bigEndian) {
402+
if (bigEndian) {
403+
return put((byte) ((v >>> 8) & 0xFF),
404+
(byte) ((v >>> 0) & 0xFF));
405+
}
406+
return put((byte) ((v >>> 0) & 0xFF),
407+
(byte) ((v >>> 8) & 0xFF));
408+
}
409+
410+
/**
411+
* Encodes the primitive into the buffer.
412+
*
413+
* @param v The value to encode.
414+
* @param bigEndian Whether to encode in big or little endian byte ordering.
415+
*/
416+
public DSByteBuffer putU32(long v, boolean bigEndian) {
417+
if (bigEndian) {
418+
return put((byte) (v >>> 24),
419+
(byte) (v >>> 16),
420+
(byte) (v >>> 8),
421+
(byte) (v >>> 0));
422+
}
423+
return put((byte) (v >>> 0),
424+
(byte) (v >>> 8),
425+
(byte) (v >>> 16),
426+
(byte) (v >>> 24));
427+
}
395428
/**
396429
* Returns the next byte in the buffer, or -1 when nothing is available.
397430
*/

dslink-core/src/main/java/com/acuity/iot/dsa/dslink/io/msgpack/MsgpackReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,13 @@ private Token readNumber(byte b) throws IOException {
286286
case UINT8:
287287
return setNextValue((short) in.read() & 0xFF);
288288
case INT16:
289-
case UINT16:
290289
return setNextValue(DSBytes.readShort(in, true));
290+
case UINT16:
291+
return setNextValue(DSBytes.readU16(in, true));
291292
case INT32:
292-
case UINT32:
293293
return setNextValue(DSBytes.readInt(in, true));
294+
case UINT32:
295+
return setNextValue(DSBytes.readU32(in, true));
294296
default: //INT64
295297
return setNextValue(DSBytes.readLong(in, true));
296298
}

dslink-core/src/main/java/com/acuity/iot/dsa/dslink/io/msgpack/MsgpackWriter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,14 @@ protected void write(long arg) throws IOException {
199199
byteBuffer.put((byte) arg);
200200
} else {
201201
byteBuffer.put(UINT16);
202-
byteBuffer.putShort((short) arg);
202+
byteBuffer.putU16((int) arg, true);
203203
}
204204
} else {
205205
if (arg < (1L << 32)) {
206206
byteBuffer.put(UINT32);
207-
byteBuffer.putInt((int) arg);
207+
byteBuffer.putU32((int) arg, true);
208208
} else {
209-
byteBuffer.put(UINT64);
209+
byteBuffer.put(INT64);
210210
byteBuffer.putLong(arg);
211211
}
212212
}

dslink-core/src/main/java/com/acuity/iot/dsa/dslink/protocol/v1/DS1ConnectionInit.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.IOException;
55
import java.io.InputStream;
66
import java.net.HttpURLConnection;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.security.MessageDigest;
910
import org.iot.dsa.dslink.DSLink;
@@ -136,7 +137,11 @@ String makeWsUrl(String wsPath) {
136137
StringBuilder buf = new StringBuilder();
137138
try {
138139
URL url = new URL(brokerUri);
139-
buf.append("ws://");
140+
if (brokerUri.toLowerCase().startsWith("https:")) {
141+
buf.append("wss://");
142+
} else {
143+
buf.append("ws://");
144+
}
140145
buf.append(url.getHost());
141146
if (url.getPort() >= 0) {
142147
buf.append(':').append(url.getPort());

dslink-core/src/main/java/org/iot/dsa/node/DSBytes.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,88 @@ public static short readShort(InputStream in, boolean bigEndian) {
308308
return ret;
309309
}
310310

311+
/**
312+
* Reads the primitive from a byte array.
313+
*
314+
* @param buf Must be at least off + 2 in length.
315+
* @param off The offset into the buffer to start reading.
316+
* @param bigEndian Whether to decode in big or little endian byte ordering.
317+
*/
318+
public static int readU16(byte[] buf, int off, boolean bigEndian) {
319+
if (bigEndian) {
320+
return (((buf[off] & 0xFF) << 8) |
321+
((buf[++off] & 0xFF) << 0));
322+
}
323+
return (((buf[off] & 0xFF) << 0) |
324+
((buf[++off] & 0xFF) << 8));
325+
}
326+
327+
/**
328+
* Reads the primitive from a stream.
329+
*
330+
* @param in Must have at least 2 bytes to read.
331+
* @param bigEndian Whether to decode in big or little endian byte ordering.
332+
*/
333+
public static int readU16(InputStream in, boolean bigEndian) {
334+
int ret = 0;
335+
try {
336+
if (bigEndian) {
337+
return (((in.read() & 0xFF) << 8) |
338+
((in.read() & 0xFF) << 0));
339+
}
340+
ret = (((in.read() & 0xFF) << 0) |
341+
((in.read() & 0xFF) << 8));
342+
} catch (IOException x) {
343+
DSException.throwRuntime(x);
344+
}
345+
return ret;
346+
}
347+
348+
/**
349+
* Reads the primitive from a byte array.
350+
*
351+
* @param buf Must be at least off + 4 in length.
352+
* @param off The offset into the buffer to start reading.
353+
* @param bigEndian Whether to decode in big or little endian byte ordering.
354+
*/
355+
public static long readU32(byte[] buf, int off, boolean bigEndian) {
356+
if (bigEndian) {
357+
return (((long) (buf[++off] & 0xFF) << 24) |
358+
((buf[++off] & 0xFF) << 16) |
359+
((buf[++off] & 0xFF) << 8) |
360+
((buf[++off] & 0xFF) << 0));
361+
}
362+
return (((buf[off] & 0xFF) << 0) |
363+
((buf[++off] & 0xFF) << 8) |
364+
((buf[++off] & 0xFF) << 16) |
365+
((long) (buf[++off] & 0xFF) << 24));
366+
}
367+
368+
/**
369+
* Reads the primitive from a stream.
370+
*
371+
* @param in Must have 8 bytes to read.
372+
* @param bigEndian Whether to decode in big or little endian byte ordering.
373+
*/
374+
public static long readU32(InputStream in, boolean bigEndian) {
375+
long ret = 0;
376+
try {
377+
if (bigEndian) {
378+
return (((long) (in.read() & 0xFF) << 24) |
379+
((in.read() & 0xFF) << 16) |
380+
((in.read() & 0xFF) << 8) |
381+
((in.read() & 0xFF) << 0));
382+
}
383+
ret = (((in.read() & 0xFF) << 0) |
384+
((in.read() & 0xFF) << 8) |
385+
((in.read() & 0xFF) << 16) |
386+
((long) (in.read() & 0xFF) << 24));
387+
} catch (IOException x) {
388+
DSException.throwRuntime(x);
389+
}
390+
return ret;
391+
}
392+
311393
@Override
312394
public byte[] toBytes() {
313395
return value;

0 commit comments

Comments
 (0)