Skip to content

Commit 4757310

Browse files
committed
implement pub/sub for all vector types
1 parent 95792cb commit 4757310

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
#### Updates
99

10+
* `subscribe()` / `unsubscribe()` now accept a topic in any of the standard types (not just character), allowing pub/sub to be used when sending integer, double, logical, complex, or raw vectors.
1011
* For receives, if an error occurs in unserialisation or data conversion (e.g. mode was incorrectly specified), the received raw vector is now available at both `$raw` and `$data` if `keep.raw = TRUE`.
1112
* Setting 'NANONEXT_TLS=1' now allows the downloaded NNG library to be built against a system mbedtls installation.
13+
* Setting 'NANONEXT_ARM' is no longer required on platforms such as Raspberry Pi - the package configure script should now detect platforms that require the libatomic linker flag to be set automatically.
1214
* Deprecated `send_ctx()`, `recv_ctx()` and logging are removed.
1315
* All-round internal performance optimisations.
1416

R/protocol.R

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
#' topic to subscribe to.
77
#'
88
#' @param socket a Socket using the sub protocol.
9-
#' @param topic [default NULL] a topic (given as a character string). The default
10-
#' NULL subscribes to all topics.
9+
#' @param topic [default NULL] a topic (vector or NULL). The default NULL
10+
#' subscribes to all topics.
1111
#'
1212
#' @return Invisibly, an integer exit code (zero on success).
1313
#'
1414
#' @details To use pub/sub the publisher must:
1515
#' \itemize{
1616
#' \item{specify \code{mode = 'raw'} when sending to allow the topics to be
1717
#' recognised by the receiving party.}
18-
#' \item{send a vector that separates the topic from the rest of the message
19-
#' e.g. \code{send(socket, c("topic", "message"), mode = "raw")} - this
20-
#' ensures that topic ends with the required nul byte for it to be
21-
#' recognised.}
18+
#' \item{when sending a character vector, the topic must be separated from
19+
#' the rest of the message using e.g.
20+
#' \code{send(socket, c("topic", "message"), mode = "raw")}}
2221
#' }
2322
#'
2423
#' @examples
@@ -38,7 +37,7 @@
3837
#'
3938
subscribe <- function(socket, topic = NULL) {
4039

41-
invisible(.Call(rnng_socket_set, socket, 5L, "sub:subscribe", topic))
40+
invisible(.Call(rnng_socket_set, socket, 0L, "sub:subscribe", topic))
4241

4342
}
4443

@@ -48,8 +47,8 @@ subscribe <- function(socket, topic = NULL) {
4847
#' a topic from the subscription list.
4948
#'
5049
#' @param socket a Socket using the sub protocol.
51-
#' @param topic [default NULL] a topic (given as a character string). The default
52-
#' NULL unsubscribes from all topics (if all topics were previously subscribed).
50+
#' @param topic [default NULL] a topic (vector or NULL). The default NULL
51+
#' unsubscribes from all topics (if all topics were previously subscribed).
5352
#'
5453
#' @return Invisibly, an integer exit code (zero on success).
5554
#'
@@ -60,10 +59,9 @@ subscribe <- function(socket, topic = NULL) {
6059
#' \itemize{
6160
#' \item{specify \code{mode = 'raw'} when sending to allow the topics to be
6261
#' recognised by the receiving party.}
63-
#' \item{send a vector that separates the topic from the rest of the message
64-
#' e.g. \code{send(socket, c("topic", "message"), mode = "raw")} - this
65-
#' ensures that topic ends with the required nul byte for it to be
66-
#' recognised.}
62+
#' \item{when sending a character vector, the topic must be separated from
63+
#' the rest of the message using e.g.
64+
#' \code{send(socket, c("topic", "message"), mode = "raw")}}
6765
#' }
6866
#'
6967
#' @examples
@@ -84,7 +82,7 @@ subscribe <- function(socket, topic = NULL) {
8482
#'
8583
unsubscribe <- function(socket, topic = NULL) {
8684

87-
invisible(.Call(rnng_socket_set, socket, 5L, "sub:unsubscribe", topic))
85+
invisible(.Call(rnng_socket_set, socket, 0L, "sub:unsubscribe", topic))
8886

8987
}
9088

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Send message from ‘nano1’:
105105

106106
``` r
107107
nano1$send("hello world!")
108-
#> [1] 58 0a 00 00 00 03 00 04 02 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
108+
#> [1] 58 0a 00 00 00 03 00 04 01 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
109109
#> [26] 00 10 00 00 00 01 00 04 00 09 00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64
110110
#> [51] 21
111111
```
@@ -115,7 +115,7 @@ Receive message using ‘nano2’:
115115
``` r
116116
nano2$recv()
117117
#> $raw
118-
#> [1] 58 0a 00 00 00 03 00 04 02 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
118+
#> [1] 58 0a 00 00 00 03 00 04 01 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
119119
#> [26] 00 10 00 00 00 01 00 04 00 09 00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64
120120
#> [51] 21
121121
#>
@@ -146,7 +146,7 @@ Send message from ‘socket1’:
146146

147147
``` r
148148
send(socket1, "hello world!")
149-
#> [1] 58 0a 00 00 00 03 00 04 02 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
149+
#> [1] 58 0a 00 00 00 03 00 04 01 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
150150
#> [26] 00 10 00 00 00 01 00 04 00 09 00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64
151151
#> [51] 21
152152
```
@@ -156,7 +156,7 @@ Receive message using ‘socket2’:
156156
``` r
157157
recv(socket2)
158158
#> $raw
159-
#> [1] 58 0a 00 00 00 03 00 04 02 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
159+
#> [1] 58 0a 00 00 00 03 00 04 01 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
160160
#> [26] 00 10 00 00 00 01 00 04 00 09 00 00 00 0c 68 65 6c 6c 6f 20 77 6f 72 6c 64
161161
#> [51] 21
162162
#>
@@ -285,7 +285,7 @@ msg$data
285285
#> a b
286286
#> 1 1 2
287287
msg$raw
288-
#> [1] 58 0a 00 00 00 03 00 04 02 00 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
288+
#> [1] 58 0a 00 00 00 03 00 04 01 02 00 03 05 00 00 00 00 05 55 54 46 2d 38 00 00
289289
#> [26] 03 13 00 00 00 02 00 00 00 0e 00 00 00 01 3f f0 00 00 00 00 00 00 00 00 00
290290
#> [51] 0e 00 00 00 01 40 00 00 00 00 00 00 00 00 00 04 02 00 00 00 01 00 04 00 09
291291
#> [76] 00 00 00 05 6e 61 6d 65 73 00 00 00 10 00 00 00 02 00 04 00 09 00 00 00 01
@@ -388,7 +388,7 @@ aio
388388
#> < recvAio >
389389
#> - $data for message data
390390
aio$data |> str()
391-
#> num [1:100000000] 0.5354 0.1424 0.3993 0.0906 1.6356 ...
391+
#> num [1:100000000] -0.901 0.2851 -0.0205 0.1886 1.3892 ...
392392
```
393393

394394
As `call_aio()` is blocking and will wait for completion, an alternative
@@ -528,11 +528,11 @@ ncurl("http://httpbin.org/headers")
528528
#> [1] 7b 0a 20 20 22 68 65 61 64 65 72 73 22 3a 20 7b 0a 20 20 20 20 22 48 6f 73
529529
#> [26] 74 22 3a 20 22 68 74 74 70 62 69 6e 2e 6f 72 67 22 2c 20 0a 20 20 20 20 22
530530
#> [51] 58 2d 41 6d 7a 6e 2d 54 72 61 63 65 2d 49 64 22 3a 20 22 52 6f 6f 74 3d 31
531-
#> [76] 2d 36 32 36 64 62 31 35 33 2d 32 39 39 31 38 39 63 35 33 61 30 30 35 61 32
532-
#> [101] 38 32 33 65 31 34 38 62 37 22 0a 20 20 7d 0a 7d 0a
531+
#> [76] 2d 36 32 37 33 61 65 38 65 2d 37 33 31 61 36 62 35 30 32 30 65 63 36 30 31
532+
#> [101] 32 35 32 62 64 66 32 39 38 22 0a 20 20 7d 0a 7d 0a
533533
#>
534534
#> $data
535-
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626db153-299189c53a005a2823e148b7\"\n }\n}\n"
535+
#> [1] "{\n \"headers\": {\n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-6273ae8e-731a6b5020ec601252bdf298\"\n }\n}\n"
536536
```
537537

538538
For advanced use, supports additional HTTP methods such as POST or PUT.
@@ -548,7 +548,7 @@ res
548548
#> - $raw for raw message
549549

550550
call_aio(res)$data
551-
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-626db153-400a5a4e7880ba63075877ec\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"78.145.225.121\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
551+
#> [1] "{\n \"args\": {}, \n \"data\": \"{\\\"key\\\": \\\"value\\\"}\", \n \"files\": {}, \n \"form\": {}, \n \"headers\": {\n \"Authorization\": \"Bearer APIKEY\", \n \"Content-Length\": \"16\", \n \"Content-Type\": \"application/json\", \n \"Host\": \"httpbin.org\", \n \"X-Amzn-Trace-Id\": \"Root=1-6273ae8f-5a31583a2f9f02b8619845b7\"\n }, \n \"json\": {\n \"key\": \"value\"\n }, \n \"origin\": \"79.173.189.204\", \n \"url\": \"http://httpbin.org/post\"\n}\n"
552552
```
553553

554554
In this respect, it may be used as a performant and lightweight method

man/subscribe.Rd

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/unsubscribe.Rd

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/opts.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ SEXP rnng_socket_set(SEXP socket, SEXP type, SEXP opt, SEXP value) {
1515
int xc;
1616

1717
switch (typ) {
18+
case 0:
19+
if (value == R_NilValue) {
20+
xc = nng_socket_set(*sock, op, NULL, 0);
21+
} else {
22+
SEXP enc = nano_encode(value);
23+
xc = nng_socket_set(*sock, op, RAW(enc), Rf_xlength(enc));
24+
}
25+
break;
1826
case 1:
1927
xc = nng_socket_set_bool(*sock, op, (bool) Rf_asInteger(value));
2028
break;

0 commit comments

Comments
 (0)