Skip to content

Commit 27ecfb2

Browse files
committed
store called results in Aio
1 parent 5dc478f commit 27ecfb2

File tree

8 files changed

+87
-49
lines changed

8 files changed

+87
-49
lines changed

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ S3method(close,nanoContext)
88
S3method(close,nanoDialer)
99
S3method(close,nanoListener)
1010
S3method(close,nanoSocket)
11-
S3method(print,nano)
1211
S3method(print,nanoContext)
1312
S3method(print,nanoDialer)
1413
S3method(print,nanoListener)
1514
S3method(print,nanoObject)
1615
S3method(print,nanoSocket)
16+
S3method(print,recvAio)
17+
S3method(print,sendAio)
1718
S3method(setopt,nanoContext)
1819
S3method(setopt,nanoDialer)
1920
S3method(setopt,nanoListener)

R/context.R

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,15 @@ ctx_send <- function(context, data, mode = c("serial", "raw"), timeout, echo = T
104104
#' @inheritParams recv
105105
#' @inheritParams send_aio
106106
#'
107-
#' @return Named list of 2 elements: 'raw' containing a list of received raw
108-
#' vectors and 'data' containing a list of converted R objects, or else a
109-
#' list of converted R objects if keep.raw is set to FALSE.
107+
#' @return Named list of 2 elements: 'raw' containing the received raw vector
108+
#' and 'data' containing the converted R object, or else the converted R
109+
#' object if 'keep.raw' is set to FALSE.
110110
#'
111-
#' Note: a list of lists is always returned even when n = 1. To access the
112-
#' first raw element, for example, use \code{$raw[[1]]} and the first data
113-
#' element use \code{$data[[1]]}.
114-
#'
115-
#' @details Async recv will block while awaiting all 'n' messages to arrive. Set
116-
#' a timeout to ensure that the function returns under all scenarios.
111+
#' @details Async recv will block while awaiting the receive operation to complete.
112+
#' Set a timeout to ensure that the function returns under all scenarios.
117113
#'
118114
#' In case of an error in unserialisation or data conversion, the function
119-
#' will still return a list of received raw vectors to allow the data to be
120-
#' recovered.
115+
#' will still return the received raw vector to allow the data to be recovered.
121116
#'
122117
#' @examples
123118
#' req <- socket("req", listen = "inproc://nanonext")

R/nano.R

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,31 @@ print.nanoListener <- function(x, ...) {
201201

202202
#' @export
203203
#'
204-
print.nano <- function(x, ...) {
204+
print.recvAio <- function(x, ...) {
205205

206-
klass <- attr(x, "class")[1L]
207-
switch(klass,
208-
recvAio = cat("< recvAio > - use aio_call() to retrieve message\n"),
209-
sendAio = cat("< sendAio > - use aio_call() to retrieve result\n"))
206+
cat("< recvAio >\n")
207+
is.null(attr(x, "raw")) && is.null(attr(x, "data")) && {
208+
cat(": use aio_call() to retrieve message\n")
209+
return(invisible(x))
210+
}
211+
if (!is.null(attr(x, "raw")))
212+
cat(" - $raw for raw message\n")
213+
if (!is.null(attr(x, "data")))
214+
cat(" - $data for message data\n")
215+
invisible(x)
216+
217+
}
218+
219+
#' @export
220+
#'
221+
print.sendAio <- function(x, ...) {
222+
223+
cat("< sendAio >\n")
224+
if (is.null(attr(x, "result"))) {
225+
cat(": use aio_call() to retrieve result\n")
226+
} else {
227+
cat(" - $result for send result\n")
228+
}
210229
invisible(x)
211230

212231
}

R/sendrecv.R

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ send <- function(socket,
7272
#' aio_call(aio)
7373
#'
7474
#' aio <- send_aio(pub, "message 1", mode = "raw", timeout = 100)
75-
#' aio
7675
#' aio_call(aio)
7776
#'
7877
#' close(pub)
@@ -182,14 +181,18 @@ recv <- function(socket,
182181
#' res <- recv_aio(s2, timeout = 100, keep.raw = FALSE)
183182
#' res
184183
#' aio_call(res)
184+
#' res
185185
#'
186186
#' send_aio(s1, c(1.1, 2.2, 3.3), mode = "raw", timeout = 100)
187187
#' res <- recv_aio(s2, mode = "double", timeout = 100)
188188
#' aio_call(res)
189+
#' res
189190
#'
190191
#' send_aio(s1, "message 1", mode = "raw", timeout = 100)
191192
#' res <- recv_aio(s2, mode = "character", timeout = 100)
192193
#' aio_call(res)
194+
#' res$raw
195+
#' res$data
193196
#'
194197
#' close(s1)
195198
#' close(s2)
@@ -208,10 +211,9 @@ recv_aio <- function(socket,
208211
if (is.integer(aio)) {
209212
message(aio, " : ", nng_error(aio))
210213
} else {
211-
attr(aio, "mode") <- mode
212-
attr(aio, "keep.raw") <- missing(keep.raw) || isTRUE(keep.raw)
214+
attr(aio, "callparams") <- list(mode, missing(keep.raw) || isTRUE(keep.raw))
213215
}
214-
invisible(aio)
216+
aio
215217

216218
}
217219

@@ -228,17 +230,24 @@ recv_aio <- function(socket,
228230
#' the converted R object, or else just the converted R object depending on
229231
#' the parameters set.
230232
#'
233+
#' @details For a 'recvAio', in case of an error in unserialisation or data
234+
#' conversion, the received raw vector will be saved in $raw to allow the
235+
#' data to be recovered.
236+
#'
231237
#' @examples
232238
#' s1 <- socket("pair", listen = "inproc://nanonext")
233239
#' s2 <- socket("pair", dial = "inproc://nanonext")
234240
#'
235-
#' status <- send_aio(s1, data.frame(a = 1, b = 2), timeout = 100)
236-
#' status
237-
#' aio_call(status)
238-
#'
239-
#' res <- recv_aio(s2, timeout = 100, keep.raw = FALSE)
241+
#' res <- send_aio(s1, data.frame(a = 1, b = 2), timeout = 100)
240242
#' res
241243
#' aio_call(res)
244+
#' res
245+
#' res$result
246+
#'
247+
#' res <- recv_aio(s2, timeout = 100)
248+
#' res
249+
#' aio_call(res)$data
250+
#' res
242251
#'
243252
#' close(s1)
244253
#' close(s2)
@@ -248,28 +257,36 @@ recv_aio <- function(socket,
248257
aio_call <- function(aio) {
249258

250259
if (inherits(aio, "recvAio")) {
260+
261+
mode <- attr(aio, "callparams")[[1L]]
262+
keep.raw <- attr(aio, "callparams")[[2L]]
251263
res <- .Call(rnng_aio_get_msg, aio)
264+
if (keep.raw) attr(aio, "raw") <- res
252265
is.integer(res) && {
253266
message(res, " : ", nng_error(res))
254-
return(invisible(res))
267+
return(invisible(aio))
255268
}
256-
on.exit(expr = return(res))
257-
mode <- attr(aio, "mode")
269+
on.exit(expr = {
270+
attr(aio, "raw") <- res
271+
return(invisible(aio))
272+
})
258273
data <- switch(mode,
259274
serial = unserialize(connection = res),
260275
character = (r <- readBin(con = res, what = mode, n = length(res)))[r != ""],
261276
raw = res,
262277
readBin(con = res, what = mode, n = length(res)))
278+
attr(aio, "data") <- data
263279
on.exit(expr = NULL)
264-
if (attr(aio, "keep.raw")) list(raw = res, data = data) else data
280+
invisible(aio)
265281

266282
} else if (inherits(aio, "sendAio")) {
283+
267284
res <- .Call(rnng_aio_result, aio)
285+
attr(aio, "result") <- res
268286
if (res) {
269287
message(res, " : ", nng_error(res))
270-
return(invisible(res))
271288
}
272-
res
289+
invisible(aio)
273290

274291
} else {
275292
stop("this function may only be used on an Aio")

man/aio_call.Rd

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

man/ctx_recv.Rd

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

man/recv_aio.Rd

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

man/send_aio.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)