Skip to content

Commit bf3132b

Browse files
committed
implement match.arg2()
1 parent 65f3c21 commit bf3132b

File tree

19 files changed

+151
-113
lines changed

19 files changed

+151
-113
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Unified `send()` and `recv()` functions, and their asynchronous counterparts `send_aio()` and `recv_aio()`, are now S3 generics and can be used across Sockets, Contexts and Streams.
1414
* Revised 'block' argument for `send()` and `recv()` now allows an integer value for setting a timeout.
1515
* `send_ctx()` and `recv_ctx()` are deprecated and will be removed in a future package version - the methods for `send()` and `recv()` should be used instead.
16-
* To allow for more flexible practices, logging is now deprecated and being phased out. Logging can still be set via 'NANONEXT_LOG' prior to package load but `logging()` can no longer be used. Logging will be removed entirely in a future package version.
16+
* To allow for more flexible practices, logging is now deprecated and being phased out. Logging can still be set via 'NANONEXT_LOG' prior to package load but `logging()` can no longer be used. Logging will be removed entirely in the next package version.
1717
* Returned NNG error codes are now all classed as 'errorValue' across the package.
1818

1919
# nanonext 0.3.0

R/aio.R

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

51-
mode <- match.arg(mode)
51+
mode <- match.arg2(mode, c("serial", "raw"))
5252
if (missing(timeout)) timeout <- -2L
5353
force(data)
5454
data <- encode(data = data, mode = mode)
@@ -78,7 +78,7 @@ send_aio.nanoSocket <- function(con, data, mode = c("serial", "raw"), timeout) {
7878
#'
7979
send_aio.nanoContext <- function(con, data, mode = c("serial", "raw"), timeout) {
8080

81-
mode <- match.arg(mode)
81+
mode <- match.arg2(mode, c("serial", "raw"))
8282
if (missing(timeout)) timeout <- -2L
8383
force(data)
8484
data <- encode(data = data, mode = mode)
@@ -109,7 +109,7 @@ send_aio.nanoContext <- function(con, data, mode = c("serial", "raw"), timeout)
109109
send_aio.nanoStream <- function(con, data, mode = "raw", timeout) {
110110

111111
force(data)
112-
data <- encode(data = data, mode = "raw")
112+
data <- encode(data = data, mode = 2L)
113113
if (missing(timeout)) timeout <- -2L
114114
aio <- .Call(rnng_stream_send_aio, con, data, timeout)
115115
is.integer(aio) && return(invisible(aio))
@@ -206,7 +206,8 @@ recv_aio.nanoSocket <- function(con,
206206
keep.raw = TRUE,
207207
...) {
208208

209-
mode <- match.arg(mode)
209+
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
210+
"integer", "logical", "numeric", "raw"))
210211
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
211212
if (missing(timeout)) timeout <- -2L
212213
aio <- .Call(rnng_recv_aio, con, timeout)
@@ -282,7 +283,8 @@ recv_aio.nanoContext <- function(con,
282283
keep.raw = TRUE,
283284
...) {
284285

285-
mode <- match.arg(mode)
286+
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
287+
"integer", "logical", "numeric", "raw"))
286288
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
287289
if (missing(timeout)) timeout <- -2L
288290
aio <- .Call(rnng_ctx_recv_aio, con, timeout)
@@ -359,7 +361,8 @@ recv_aio.nanoStream <- function(con,
359361
n = 10000,
360362
...) {
361363

362-
mode <- match.arg(mode)
364+
mode <- match.arg2(mode, c("character", "complex", "double", "integer",
365+
"logical", "numeric", "raw")) + 1L
363366
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
364367
if (missing(timeout)) timeout <- -2L
365368
aio <- .Call(rnng_stream_recv_aio, con, n, timeout)

R/context.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ reply <- function(context,
114114
timeout,
115115
...) {
116116

117-
recv_mode <- match.arg(recv_mode)
118-
send_mode <- match.arg(send_mode)
117+
recv_mode <- match.arg2(recv_mode, c("serial", "character", "complex", "double",
118+
"integer", "logical", "numeric", "raw"))
119+
send_mode <- match.arg2(send_mode, c("serial", "raw"))
119120
if (missing(timeout)) timeout <- -2L
120121
res <- .Call(rnng_ctx_recv, context, timeout)
121122
is.integer(res) && return(invisible(res))
@@ -184,8 +185,9 @@ request <- function(context,
184185
timeout,
185186
keep.raw = TRUE) {
186187

187-
send_mode <- match.arg(send_mode)
188-
recv_mode <- match.arg(recv_mode)
188+
send_mode <- match.arg2(send_mode, c("serial", "raw"))
189+
recv_mode <- match.arg2(recv_mode, c("serial", "character", "complex", "double",
190+
"integer", "logical", "numeric", "raw"))
189191
keep.raw <- missing(keep.raw) || isTRUE(keep.raw)
190192
if (missing(timeout)) timeout <- -2L
191193
force(data)
@@ -278,7 +280,7 @@ request <- function(context,
278280
#'
279281
send_ctx <- function(context, data, mode = c("serial", "raw"), timeout, echo = TRUE) {
280282

281-
mode <- match.arg(mode)
283+
mode <- match.arg2(mode, c("serial", "raw"))
282284
if (missing(timeout)) timeout <- -2L
283285
force(data)
284286
data <- encode(data = data, mode = mode)
@@ -321,7 +323,8 @@ recv_ctx <- function(context,
321323
timeout,
322324
keep.raw = TRUE) {
323325

324-
mode <- match.arg(mode)
326+
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
327+
"integer", "logical", "numeric", "raw"))
325328
if (missing(timeout)) timeout <- -2L
326329
res <- .Call(rnng_ctx_recv, context, timeout)
327330
is.integer(res) && return(invisible(res))

R/docs.R

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,6 @@
3737
#' block; instead if the message cannot be delivered for any reason it is
3838
#' discarded.
3939
#'
40-
#' @section Pipeline (one-way pipe):
41-
#'
42-
#' In the pipeline pattern, pushers distribute messages to pullers, hence
43-
#' useful for solving producer/consumer problems.
44-
#'
45-
#' If multiple peers are connected, the pattern attempts to distribute fairly.
46-
#' Each message sent by a pusher will be sent to one of its peer pullers,
47-
#' chosen in a round-robin fashion. This property makes this pattern useful
48-
#' in load-balancing scenarios.
49-
#'
50-
#' [protocol, push] The push protocol is one half of a pipeline pattern. The
51-
#' other side is the pull protocol.
52-
#'
53-
#' [protocol, pull] The pull protocol is one half of a pipeline pattern. The
54-
#' other half is the push protocol.
55-
#'
5640
#' @section Request/Reply (I ask, you answer):
5741
#'
5842
#' In a request/reply pattern, a requester sends a message to one replier,
@@ -70,6 +54,22 @@
7054
#' This socket may be used to receive messages (requests), and then to send
7155
#' replies. Generally a reply can only be sent after receiving a request.
7256
#'
57+
#' @section Pipeline (one-way pipe):
58+
#'
59+
#' In the pipeline pattern, pushers distribute messages to pullers, hence
60+
#' useful for solving producer/consumer problems.
61+
#'
62+
#' If multiple peers are connected, the pattern attempts to distribute fairly.
63+
#' Each message sent by a pusher will be sent to one of its peer pullers,
64+
#' chosen in a round-robin fashion. This property makes this pattern useful
65+
#' in load-balancing scenarios.
66+
#'
67+
#' [protocol, push] The push protocol is one half of a pipeline pattern. The
68+
#' other side is the pull protocol.
69+
#'
70+
#' [protocol, pull] The pull protocol is one half of a pipeline pattern. The
71+
#' other half is the push protocol.
72+
#'
7373
#' @section Publisher/Subscriber (topics & broadcast):
7474
#'
7575
#' In a publisher/subscriber pattern, a publisher sends data, which is

R/messenger.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#'
2020
#' \code{:q} is the command to quit.
2121
#'
22-
#' NOTE: This is a proof of concept meant for use within friendly internal
22+
#' NOTE: This is a proof of concept intended for use within internal
2323
#' networks. Currently no measures are taken to verify the identity of
24-
#' endpoints, hence should not be used to transmit sensitive information.
24+
#' endpoints, and hence should not be used to transmit sensitive information.
2525
#'
2626
#' @export
2727
#'

R/nano.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#'
5252
#' @export
5353
#'
54-
nano <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
54+
nano <- function(protocol = c("pair", "bus", "req", "rep", "push", "pull",
5555
"pub", "sub", "surveyor", "respondent"),
5656
dial = NULL,
5757
listen = NULL,

R/nanonext-package.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
#' @section Usage notes:
1313
#'
1414
#' Call \code{\link{nano_init}} after package load to set global options.
15-
#' The default by calling \code{nano_init()} with no arguments will cause
16-
#' generated warnings to print immediately as they occur.
15+
#' Using defaults will cause warnings to print immediately as they occur.
1716
#'
1817
#' \{nanonext\} offers 2 equivalent interfaces: an object-oriented interface,
1918
#' and a functional interface.

R/sendrecv.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ send.nanoSocket <- function(con,
7474
block = FALSE,
7575
echo = TRUE) {
7676

77-
mode <- match.arg(mode)
77+
mode <- match.arg2(mode, c("serial", "raw"))
7878
force(data)
7979
data <- encode(data = data, mode = mode)
8080
res <- .Call(rnng_send, con, data, block)
@@ -93,7 +93,7 @@ send.nanoContext <- function(con,
9393
block = TRUE,
9494
echo = TRUE) {
9595

96-
mode <- match.arg(mode)
96+
mode <- match.arg2(mode, c("serial", "raw"))
9797
if (missing(block) || isTRUE(block)) block <- -2L
9898
force(data)
9999
data <- encode(data = data, mode = mode)
@@ -114,7 +114,7 @@ send.nanoStream <- function(con,
114114
echo = TRUE) {
115115

116116
force(data)
117-
data <- encode(data = data, mode = "raw")
117+
data <- encode(data = data, mode = 2L)
118118
if (missing(block) || isTRUE(block)) block <- -2L
119119
res <- .Call(rnng_stream_send, con, data, block)
120120
is.integer(res) && return(invisible(res))
@@ -225,7 +225,8 @@ recv.nanoSocket <- function(con,
225225
keep.raw = TRUE,
226226
...) {
227227

228-
mode <- match.arg(mode)
228+
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
229+
"integer", "logical", "numeric", "raw"))
229230
res <- .Call(rnng_recv, con, block)
230231
is.integer(res) && return(invisible(res))
231232
on.exit(expr = return(res))
@@ -246,7 +247,8 @@ recv.nanoContext <- function(con,
246247
keep.raw = TRUE,
247248
...) {
248249

249-
mode <- match.arg(mode)
250+
mode <- match.arg2(mode, c("serial", "character", "complex", "double",
251+
"integer", "logical", "numeric", "raw"))
250252
if (missing(block) || isTRUE(block)) block <- -2L
251253
res <- .Call(rnng_ctx_recv, con, block)
252254
is.integer(res) && return(invisible(res))
@@ -270,7 +272,8 @@ recv.nanoStream <- function(con,
270272
n = 10000,
271273
...) {
272274

273-
mode <- match.arg(mode)
275+
mode <- match.arg2(mode, c("character", "complex", "double", "integer",
276+
"logical", "numeric", "raw")) + 1L
274277
if (missing(block) || isTRUE(block)) block <- -2L
275278
res <- .Call(rnng_stream_recv, con, n, block)
276279
is.integer(res) && return(invisible(res))

R/socket.R

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#' outgoing connection) or listen (accept an incoming connection) at an
77
#' address.
88
#'
9-
#' @param protocol [default 'pair'] choose protocol - 'pair', 'bus', 'push',
10-
#' 'pull', 'req', 'rep', 'pub', 'sub', 'surveyor', or 'respondent' - see
9+
#' @param protocol [default 'pair'] choose protocol - 'pair', 'bus', 'req',
10+
#' 'rep', 'push', 'pull', 'pub', 'sub', 'surveyor', or 'respondent' - see
1111
#' \link{protocols}.
1212
#' @param dial (optional) a URL to dial, specifying the transport and address as
1313
#' a character string e.g. 'inproc://anyvalue' or 'tcp://127.0.0.1:5555'
@@ -57,25 +57,22 @@
5757
#'
5858
#' @export
5959
#'
60-
socket <- function(protocol = c("pair", "bus", "push", "pull", "req", "rep",
60+
socket <- function(protocol = c("pair", "bus", "req", "rep", "push", "pull",
6161
"pub", "sub", "surveyor", "respondent"),
6262
dial = NULL,
6363
listen = NULL,
6464
autostart = TRUE) {
6565

66-
protocol <- match.arg(protocol)
66+
protocol <- match.arg2(protocol, c("pair", "bus", "req", "rep", "push", "pull",
67+
"pub", "sub", "surveyor", "respondent"))
6768
res <- .Call(rnng_protocol_open, protocol)
6869
is.integer(res) && return(invisible(res))
6970
if (.logging.) {
7071
loginfo(evt = "sock open", pkey = "id", pval = attr(res, "id"),
7172
skey = "protocol", sval = attr(res, "protocol"))
7273
}
73-
if (!missing(dial)) {
74-
dial(res, url = dial, autostart = autostart)
75-
}
76-
if (!missing(listen)) {
77-
listen(res, url = listen, autostart = autostart)
78-
}
74+
if (!missing(dial)) dial(res, url = dial, autostart = autostart)
75+
if (!missing(listen)) listen(res, url = listen, autostart = autostart)
7976
res
8077

8178
}

R/utils.R

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ is_nul_byte <- function(x) identical(x, as.raw(0L))
132132
#'
133133
is_error_value <- function(x) inherits(x, "errorValue")
134134

135-
#' Nanonext Initialise
135+
#' nanonext Initialise
136136
#'
137137
#' Initialise global options - intended to be called immediately after package load.
138138
#'
@@ -176,10 +176,8 @@ is_error_value <- function(x) inherits(x, "errorValue")
176176
#'
177177
nano_init <- function(warn = c("immediate", "deferred", "error", "none")) {
178178

179-
cand <- c("immediate", "deferred", "error", "none")
180-
warn <- switch(pmatch(warn[1L], cand, nomatch = NULL),
181-
1L, 0L, 2L, -1L)
182-
is.null(warn) && stop("'warn' should be one of ", paste(cand, collapse = ", "))
179+
warn <- match.arg2(warn, c("immediate", "deferred", "error", "none"))
180+
warn <- switch(warn, 1L, 0L, 2L, -1L)
183181
if (is.null(getOption("nanonext.original.warn"))) options(nanonext.original.warn = getOption("warn"))
184182
options(warn = warn)
185183
invisible(warn)
@@ -227,16 +225,27 @@ logging <- function(level = c("keep", "check", "error", "info")) {
227225

228226
encode <- function(data, mode) {
229227
switch(mode,
230-
serial = serialize(object = data, connection = NULL),
231-
raw = if (is.raw(data)) data else writeBin(object = data, con = raw()))
228+
serialize(object = data, connection = NULL),
229+
if (is.raw(data)) data else writeBin(object = data, con = raw()))
232230
}
233231

234232
decode <- function(con, mode) {
235233
switch(mode,
236-
serial = unserialize(connection = con),
237-
character = (r <- readBin(con = con, what = mode, n = length(con)))[r != ""],
238-
raw = con,
239-
readBin(con = con, what = mode, n = length(con)))
234+
unserialize(connection = con),
235+
(r <- readBin(con = con, what = "character", n = length(con)))[r != ""],
236+
readBin(con = con, what = "complex", n = length(con)),
237+
readBin(con = con, what = "double", n = length(con)),
238+
readBin(con = con, what = "integer", n = length(con)),
239+
readBin(con = con, what = "logical", n = length(con)),
240+
readBin(con = con, what = "numeric", n = length(con)),
241+
con)
242+
}
243+
244+
match.arg2 <- function(choice, choices) {
245+
arg <- deparse(substitute(choice))
246+
index <- pmatch(choice[1L], choices, nomatch = 0L)
247+
index || stop(sprintf("'%s' should be one of %s", arg, paste(choices, collapse = ", ")))
248+
index
240249
}
241250

242251
loginfo <- function(evt, pkey, pval, skey, sval) {

0 commit comments

Comments
 (0)