Skip to content

Commit fbbed80

Browse files
committed
rationalise options code and add stream options
1 parent fc0ff18 commit fbbed80

File tree

20 files changed

+242
-478
lines changed

20 files changed

+242
-478
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ S3method(setopt,nanoContext)
4040
S3method(setopt,nanoDialer)
4141
S3method(setopt,nanoListener)
4242
S3method(setopt,nanoSocket)
43+
S3method(setopt,nanoStream)
4344
S3method(start,nanoDialer)
4445
S3method(start,nanoListener)
4546
export(.mirai_scm)

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* New `stream()` interface exposes low-level byte stream functionality in the NNG library, intended for communicating with non-NNG endpoints, including but not limited to websocket servers.
66
* `ncurl()` adds an 'async' option to perform HTTP requests asynchronously, returning immediately with a 'recvAio'. Also adds explicit arguments for HTTP method, headers (which takes a named list or character vector) and request data.
77
* New `messenger()` function implements a multi-threaded console-based messaging system using NNG's scalability protocols.
8-
* New `nano_init()` function intended to be called immediately after package load to set how to handle warnings, with a default of immediate printing of warnings (settings automatically reverted upon unload).
8+
* New `nano_init()` function intended to be called immediately after package load to set global options.
99

1010
#### Updates
1111

R/aio.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ send_aio <- function(con, data, mode = c("serial", "raw"), timeout) UseMethod("s
4949
send_aio.nanoSocket <- function(con, data, mode = c("serial", "raw"), timeout) {
5050

5151
mode <- match.arg2(mode, c("serial", "raw"))
52-
if (missing(timeout)) timeout <- -2L
5352
force(data)
5453
data <- encode(data = data, mode = mode)
54+
if (missing(timeout)) timeout <- -2L
5555
aio <- .Call(rnng_send_aio, con, data, timeout)
5656
is.integer(aio) && return(invisible(aio))
5757

@@ -79,9 +79,9 @@ send_aio.nanoSocket <- function(con, data, mode = c("serial", "raw"), timeout) {
7979
send_aio.nanoContext <- function(con, data, mode = c("serial", "raw"), timeout) {
8080

8181
mode <- match.arg2(mode, c("serial", "raw"))
82-
if (missing(timeout)) timeout <- -2L
8382
force(data)
8483
data <- encode(data = data, mode = mode)
84+
if (missing(timeout)) timeout <- -2L
8585
aio <- .Call(rnng_ctx_send_aio, con, data, timeout)
8686
is.integer(aio) && return(invisible(aio))
8787

@@ -208,11 +208,11 @@ recv_aio.nanoSocket <- function(con,
208208

209209
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
210210
"integer", "logical", "numeric", "raw"))
211-
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
212211
if (missing(timeout)) timeout <- -2L
213212
aio <- .Call(rnng_recv_aio, con, timeout)
214213
is.integer(aio) && return(invisible(aio))
215214

215+
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
216216
env <- new.env(hash = FALSE)
217217
data <- raw <- NULL
218218
unresolv <- TRUE
@@ -285,11 +285,11 @@ recv_aio.nanoContext <- function(con,
285285

286286
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
287287
"integer", "logical", "numeric", "raw"))
288-
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
289288
if (missing(timeout)) timeout <- -2L
290289
aio <- .Call(rnng_ctx_recv_aio, con, timeout)
291290
is.integer(aio) && return(invisible(aio))
292291

292+
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
293293
env <- new.env(hash = FALSE)
294294
data <- raw <- NULL
295295
unresolv <- TRUE
@@ -363,11 +363,11 @@ recv_aio.nanoStream <- function(con,
363363

364364
mode <- match.arg2(mode, c("character", "complex", "double", "integer",
365365
"logical", "numeric", "raw")) + 1L
366-
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
367366
if (missing(timeout)) timeout <- -2L
368367
aio <- .Call(rnng_stream_recv_aio, con, n, timeout)
369368
is.integer(aio) && return(invisible(aio))
370369

370+
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
371371
env <- new.env(hash = FALSE)
372372
data <- raw <- NULL
373373
unresolv <- TRUE

R/context.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,16 @@ request <- function(context,
188188
send_mode <- match.arg2(send_mode, c("serial", "raw"))
189189
recv_mode <- match.arg2(recv_mode, c("serial", "character", "complex", "double",
190190
"integer", "logical", "numeric", "raw"))
191-
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
192-
if (missing(timeout)) timeout <- -2L
193191
force(data)
194192
data <- encode(data = data, mode = send_mode)
195193
res <- .Call(rnng_ctx_send_aio, context, data, -2L)
196194
is.integer(res) && return(invisible(res))
197195

196+
if (missing(timeout)) timeout <- -2L
198197
aio <- .Call(rnng_ctx_recv_aio, context, timeout)
199198
is.integer(aio) && return(invisible(aio))
200199

200+
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
201201
env <- new.env(hash = FALSE)
202202
data <- raw <- NULL
203203
unresolv <- TRUE

R/listdial.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ dial <- function(socket,
7373
url = "inproc://nanonext",
7474
autostart = TRUE) {
7575

76-
is.character(url) || stop("'url' should be a character string")
77-
7876
if (missing(autostart) || isTRUE(autostart)) {
7977
res <- .Call(rnng_dial, socket, url)
8078
is.integer(res) && return(invisible(res))
@@ -183,8 +181,6 @@ listen <- function(socket,
183181
url = "inproc://nanonext",
184182
autostart = TRUE) {
185183

186-
is.character(url) || stop("'url' should be a character string")
187-
188184
if (missing(autostart) || isTRUE(autostart)) {
189185
res <- .Call(rnng_listen, socket, url)
190186
is.integer(res) && return(invisible(res))

R/messenger.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#'
2828
messenger <- function(url) {
2929

30-
is.character(url) || stop("the url must be supplied as a character string")
3130
sock <- .Call(rnng_messenger, url)
3231
is.integer(sock) && return(invisible(sock))
3332
on.exit(expr = {

R/opts.R

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# nanonext - Options Configuration ---------------------------------------------
22

3-
#' Set Option on Socket, Context, Dialer or Listener
3+
#' Set Option on Socket, Context, Dialer, Listener or Stream
44
#'
5-
#' Set \link{opts} on a Socket, Context, Dialer or Listener.
5+
#' Set \link{opts} on a Socket, Context, Dialer, Listener or Stream.
66
#'
7-
#' @param object a Socket, Context, Listener or Dialer.
7+
#' @param object a Socket, Context, Listener, Dialer or Stream.
88
#' @param type [default 'bool'] type of option - either 'bool', 'int', 'ms'
99
#' (duration), 'size', 'string' or 'uint64'.
1010
#' @param opt name of option, e.g. 'reconnect-time-min', as a character string.
@@ -29,7 +29,6 @@ setopt <- function(object,
2929
opt,
3030
value) UseMethod("setopt")
3131

32-
#'
3332
#' @examples
3433
#' s <- socket("pair")
3534
#' setopt(s, "ms", "recv-timeout", 2000)
@@ -44,20 +43,32 @@ setopt.nanoSocket <- function(object,
4443
opt,
4544
value) {
4645

47-
type <- match.arg(type)
48-
xc <- switch(type,
49-
bool = .Call(rnng_socket_set_bool, object, opt, value),
50-
int = .Call(rnng_socket_set_int, object, opt, value),
51-
ms = .Call(rnng_socket_set_ms, object, opt, value),
52-
size = .Call(rnng_socket_set_size, object, opt, value),
53-
string = .Call(rnng_socket_set_string, object, opt, value),
54-
uint64 = .Call(rnng_socket_set_uint64, object, opt, value))
55-
56-
invisible(xc)
46+
type <- match.arg2(type, c("bool", "int", "ms", "size", "string", "uint64"))
47+
invisible(.Call(rnng_socket_set, object, type, opt, value))
5748

5849
}
5950

51+
#' @examples
52+
#' s <- socket("req")
53+
#' ctx <- context(s)
54+
#' setopt(ctx, "ms", "send-timeout", 2000)
55+
#' close(ctx)
56+
#' close(s)
57+
#'
58+
#' @rdname setopt
59+
#' @method setopt nanoContext
60+
#' @export
6061
#'
62+
setopt.nanoContext <- function(object,
63+
type = c("bool", "int", "ms", "size", "string", "uint64"),
64+
opt,
65+
value) {
66+
67+
type <- match.arg2(type, c("bool", "int", "ms", "size", "string", "uint64"))
68+
invisible(.Call(rnng_ctx_set, object, type, opt, value))
69+
70+
}
71+
6172
#' @examples
6273
#' s <- socket("pair", dial = "inproc://nanonext", autostart = FALSE)
6374
#' setopt(s$dialer[[1]], "ms", "reconnect-time-min", 2000)
@@ -73,20 +84,11 @@ setopt.nanoDialer <- function(object,
7384
opt,
7485
value) {
7586

76-
type <- match.arg(type)
77-
xc <- switch(type,
78-
bool = .Call(rnng_dialer_set_bool, object, opt, value),
79-
int = .Call(rnng_dialer_set_int, object, opt, value),
80-
ms = .Call(rnng_dialer_set_ms, object, opt, value),
81-
size = .Call(rnng_dialer_set_size, object, opt, value),
82-
string = .Call(rnng_dialer_set_string, object, opt, value),
83-
uint64 = .Call(rnng_dialer_set_uint64, object, opt, value))
84-
85-
invisible(xc)
87+
type <- match.arg2(type, c("bool", "int", "ms", "size", "string", "uint64"))
88+
invisible(.Call(rnng_dialer_set, object, type, opt, value))
8689

8790
}
8891

89-
#'
9092
#' @examples
9193
#' s <- socket("pair", listen = "inproc://nanonext", autostart = FALSE)
9294
#' setopt(s$listener[[1]], "size", "recv-size-max", 1024)
@@ -102,46 +104,22 @@ setopt.nanoListener <- function(object,
102104
opt,
103105
value) {
104106

105-
type <- match.arg(type)
106-
xc <- switch(type,
107-
bool = .Call(rnng_listener_set_bool, object, opt, value),
108-
int = .Call(rnng_listener_set_int, object, opt, value),
109-
ms = .Call(rnng_listener_set_ms, object, opt, value),
110-
size = .Call(rnng_listener_set_size, object, opt, value),
111-
string = .Call(rnng_listener_set_string, object, opt, value),
112-
uint64 = .Call(rnng_listener_set_uint64, object, opt, value))
113-
114-
invisible(xc)
107+
type <- match.arg2(type, c("bool", "int", "ms", "size", "string", "uint64"))
108+
invisible(.Call(rnng_listener_set, object, type, opt, value))
115109

116110
}
117111

118-
#'
119-
#' @examples
120-
#' s <- socket("req")
121-
#' ctx <- context(s)
122-
#' setopt(ctx, "ms", "send-timeout", 2000)
123-
#' close(ctx)
124-
#' close(s)
125-
#'
126112
#' @rdname setopt
127-
#' @method setopt nanoContext
113+
#' @method setopt nanoStream
128114
#' @export
129115
#'
130-
setopt.nanoContext <- function(object,
131-
type = c("bool", "int", "ms", "size", "string", "uint64"),
132-
opt,
133-
value) {
134-
135-
type <- match.arg(type)
136-
xc <- switch(type,
137-
bool = .Call(rnng_ctx_set_bool, object, opt, value),
138-
int = .Call(rnng_ctx_set_int, object, opt, value),
139-
ms = .Call(rnng_ctx_set_ms, object, opt, value),
140-
size = .Call(rnng_ctx_set_size, object, opt, value),
141-
string = .Call(rnng_ctx_set_string, object, opt, value),
142-
uint64 = .Call(rnng_ctx_set_uint64, object, opt, value))
116+
setopt.nanoStream <- function(object,
117+
type = c("bool", "int", "ms", "size", "string", "uint64"),
118+
opt,
119+
value) {
143120

144-
invisible(xc)
121+
type <- match.arg2(type, c("bool", "int", "ms", "size", "string", "uint64"))
122+
invisible(.Call(rnng_stream_set, object, type, opt, value))
145123

146124
}
147125

R/protocol.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ subscribe <- function(socket, topic = NULL) {
4242
loginfo(evt = "subscribe", pkey = "sock", pval = attr(socket, "id"),
4343
skey = "topic", sval = if (is.null(topic)) "ALL" else topic)
4444
}
45-
invisible(.Call(rnng_socket_set_string, socket, "sub:subscribe" , topic))
45+
invisible(.Call(rnng_socket_set, socket, 5L, "sub:subscribe", topic))
4646

4747
}
4848

@@ -92,7 +92,7 @@ unsubscribe <- function(socket, topic = NULL) {
9292
loginfo(evt = "unsubscribe", pkey = "sock", pval = attr(socket, "id"),
9393
skey = "topic", sval = if (is.null(topic)) "ALL" else topic)
9494
}
95-
invisible(.Call(rnng_socket_set_string, socket, "sub:unsubscribe" , topic))
95+
invisible(.Call(rnng_socket_set, socket, 5L, "sub:unsubscribe", topic))
9696

9797
}
9898

@@ -144,7 +144,7 @@ survey_time <- function(socket, time) {
144144
loginfo(evt = "survey", pkey = "sock", pval = attr(socket, "id"),
145145
skey = "set time", sval = as.character(time))
146146
}
147-
invisible(.Call(rnng_socket_set_ms, socket, "surveyor:survey-time", time))
147+
invisible(.Call(rnng_socket_set, socket, 3L, "surveyor:survey-time", time))
148148

149149
}
150150

R/sendrecv.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ send.nanoContext <- function(con,
9494
echo = TRUE) {
9595

9696
mode <- match.arg2(mode, c("serial", "raw"))
97-
if (missing(block) || isTRUE(block)) block <- -2L
9897
force(data)
9998
data <- encode(data = data, mode = mode)
99+
if (missing(block) || isTRUE(block)) block <- -2L
100100
res <- .Call(rnng_ctx_send, con, data, block)
101101
is.integer(res) && return(invisible(res))
102102
if (missing(echo) || isTRUE(echo)) res else invisible(0L)
@@ -142,7 +142,7 @@ send.nanoStream <- function(con,
142142
#' @param keep.raw [default TRUE] logical flag whether to keep the received raw
143143
#' vector (useful for verification e.g. via hashing). If FALSE, will return
144144
#' the converted data only.
145-
#' @param n <Streams> [default 10000] the maximum number of bytes to receive.
145+
#' @param n <Streams> [default 10000L] the maximum number of bytes to receive.
146146
#' Can be an over-estimate, but note that a buffer of this size is reserved.
147147
#' @param ... currently unused.
148148
#'
@@ -269,7 +269,7 @@ recv.nanoStream <- function(con,
269269
"logical", "numeric", "raw"),
270270
block = TRUE,
271271
keep.raw = TRUE,
272-
n = 10000,
272+
n = 10000L,
273273
...) {
274274

275275
mode <- match.arg2(mode, c("character", "complex", "double", "integer",

R/socket.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ socket <- function(protocol = c("pair", "bus", "req", "rep", "push", "pull",
6666
protocol <- match.arg2(protocol, c("pair", "bus", "req", "rep", "push", "pull",
6767
"pub", "sub", "surveyor", "respondent"))
6868
res <- .Call(rnng_protocol_open, protocol)
69-
is.integer(res) && return(invisible(res))
69+
is.integer(res) && return(res)
7070
if (.logging.) {
7171
loginfo(evt = "sock open", pkey = "id", pval = attr(res, "id"),
7272
skey = "protocol", sval = attr(res, "protocol"))

0 commit comments

Comments
 (0)