Skip to content

Commit

Permalink
Merge pull request #39 from shikokuchuo/serial
Browse files Browse the repository at this point in the history
Modularize serialization
  • Loading branch information
shikokuchuo authored Jul 23, 2024
2 parents 35165ea + d66c454 commit 34c2633
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 156 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: nanonext
Type: Package
Title: NNG (Nanomsg Next Gen) Lightweight Messaging Library
Version: 1.1.1.9005
Version: 1.1.1.9006
Description: R binding for NNG (Nanomsg Next Gen), a successor to ZeroMQ. NNG is
a socket library implementing 'Scalability Protocols', a reliable,
high-performance standard for common communications patterns including
Expand Down
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ S3method(utils::.DollarNames,recvAio)
S3method(utils::.DollarNames,sendAio)
export("%~>%")
export("opt<-")
export(.advance)
export(.context)
export(.rng_adv)
export(.mark)
export(.unresolved)
export(call_aio)
export(call_aio_)
Expand Down Expand Up @@ -84,6 +85,7 @@ export(reply)
export(request)
export(send)
export(send_aio)
export(serial_config)
export(set_promise_context)
export(socket)
export(stat)
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# nanonext 1.1.1.9005 (development)
# nanonext 1.1.1.9006 (development)

#### New Features

* Adds `serial_config()` for configuring individual sockets with custom serialization and unserialization functions for reference objects.
* Adds `collect_pipe()` for obtaining the underlying pipe from a 'recvAio'. This affords more granular control of connections, with the ability to close individual pipes.
* Adds the 'poly' protocol for one-to-one of many socket connections (NNG's pair v1 polyamorous mode).

#### Updates

* Send mode 'next' is folded into the default 'serial', with custom serialization functions applying automatically if they have been registered.
* The session-wide `next_config()` is now deprecated and defunct, in favour of the new `serial_config()`.
* Removes hard dependency on `stats` and `utils` base packages.
* Requires R >= 3.6.

Expand Down
88 changes: 57 additions & 31 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -264,45 +264,71 @@ status_code <- function(x) .Call(rnng_status_code, x)

#' Configure Custom Serialization
#'
#' Registers functions for custom serialization and unserialization of
#' non-system reference objects, allowing these to be sent and received
#' between different R sessions.
#'
#' @param refhook \strong{either} a list or pairlist of two functions: the
#' signature for the first must accept a reference object inheriting from
#' \sQuote{class} (or a list of such objects) and return a raw vector, and
#' the second must accept a raw vector and return reference objects (or a
#' list of such objects), \cr \strong{or else} NULL to reset.
#' @param class [default ""] a character string representing the class of object
#' that these serialization function will be applied to, e.g.
#' \sQuote{ArrowTabular} or \sQuote{torch_tensor}.
#' @param vec [default FALSE] the serialization functions accept and return
#' reference object individually e.g. \code{arrow::write_to_raw} and
#' \code{arrow::read_ipc_stream}. If TRUE, the serialization functions are
#' vectorized and accept and return a list of reference objects, e.g.
#' \code{torch::torch_serialize} and \code{torch::torch_load}.
#' This function is defunct. Please refer to \link{serial_config} instead.
#'
#' @param refhook not used.
#' @param class [default ""] not used.
#' @param vec [default FALSE] not used.
#' @param mark [default FALSE] (for advanced use only) logical value, whether to
#' mark serialized data with a special bit.
#'
#' @return A pairlist comprising the currently-registered \sQuote{refhook}
#' functions.
#' @return NULL.
#'
#' @details Calling this function without any arguments returns the pairlist of
#' currently-registered \sQuote{refhook} functions (and resets \sQuote{mark}
#' to FALSE).
#' @export
#'
next_config <- function(refhook = list(), class = "", vec = FALSE, mark = FALSE)
.Call(rnng_next_config, refhook, class, vec, mark)

#' Configure Custom Serialization
#'
#' Registers functions on a Socket for custom serialization and unserialization
#' of non-system reference objects, allowing these to be sent and received
#' between different R sessions. Registered functions apply to all send and
#' receive operations in mode \sQuote{serial} performed over the Socket,
#' including those using a Context.
#'
#' @inheritParams context
#' @param class character string of the class of object custom serialization
#' functions are applied to, e.g. \sQuote{ArrowTabular} or
#' \sQuote{torch_tensor}, or else NULL to reset.
#' @param sfunc a function that accepts a reference object inheriting from
#' \sQuote{class} (or a list of such objects) and returns a raw vector.
#' @param ufunc a function that accepts a raw vector and returns a reference
#' object (or list of such objects).
#' @param vec [default FALSE] whether or not the serialization functions are
#' vectorized and accept and return a list of reference objects, e.g.
#' \code{torch::torch_serialize} and \code{torch::torch_load}, or if FALSE
#' return reference object individually e.g. \code{arrow::write_to_raw} and
#' \code{arrow::read_ipc_stream}.
#'
#' @return A pairlist comprising the currently-registered configuration.
#'
#' @examples
#' g <- next_config(refhook = list(function(x) serialize(x, NULL), unserialize))
#' next_config()
#' next_config(g, mark = TRUE)
#' s <- socket()
#' serial_config(s, "test_cls", function(x) serialize(x, NULL), unserialize)
#' close(s)
#'
#' @export
#'
serial_config <- function(socket, class, sfunc = NULL, ufunc = NULL, vec = FALSE)
.Call(rnng_serial_config, socket, class, sfunc, ufunc, vec)

#' Set Serialization Marker
#'
#' Internal package function.
#'
#' next_config(NULL)
#' next_config()
#' @param x logical value.
#'
#' @return The logical value 'x' supplied.
#'
#' @examples
#' .mark()
#' .mark(FALSE)
#'
#' @keywords internal
#' @export
#'
next_config <- function(refhook = list(), class = "", vec = FALSE, mark = FALSE)
.Call(rnng_next_config, refhook, class, vec, mark)
.mark <- function(x = TRUE) .Call(rnng_set_marker, x)

#' Advances the RNG State
#'
Expand All @@ -312,13 +338,13 @@ next_config <- function(refhook = list(), class = "", vec = FALSE, mark = FALSE)
#'
#' @examples
#' .Random.seed
#' invisible(.rng_adv())
#' invisible(.advance())
#' .Random.seed
#'
#' @keywords internal
#' @export
#'
.rng_adv <- function() .Call(rnng_advance_rng_state)
.advance <- function() .Call(rnng_advance_rng_state)

#' Internal Package Function
#'
Expand Down
2 changes: 2 additions & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
url: https://shikokuchuo.net/nanonext/
development:
mode: auto
template:
bootstrap: 5
bslib:
Expand Down
8 changes: 4 additions & 4 deletions man/dot-rng_adv.Rd → man/dot-advance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions man/dot-mark.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 5 additions & 32 deletions man/next_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions man/serial_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/aio.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ SEXP rnng_aio_get_msg(SEXP env) {
sz = nng_msg_len(msg);
}

PROTECT(out = nano_decode(buf, sz, raio->mode));
PROTECT(out = nano_decode(buf, sz, raio->mode, ATTRIB(aio)));
Rf_defineVar(nano_ValueSymbol, out, env);
Rf_defineVar(nano_AioSymbol, R_NilValue, env);

Expand Down Expand Up @@ -444,11 +444,11 @@ SEXP rnng_send_aio(SEXP con, SEXP data, SEXP mode, SEXP timeout, SEXP clo) {

switch (nano_encodes(mode)) {
case 1:
nano_serialize(&buf, data); break;
nano_serialize(&buf, data, NANO_PROT(con)); break;
case 2:
nano_encode(&buf, data); break;
default:
nano_serialize(&buf, data); break;
nano_serialize(&buf, data, NANO_PROT(con)); break;
}

nng_msg *msg;
Expand Down Expand Up @@ -554,6 +554,7 @@ SEXP rnng_recv_aio(SEXP con, SEXP mode, SEXP timeout, SEXP cvar, SEXP bytes, SEX

PROTECT(aio = R_MakeExternalPtr(raio, nano_AioSymbol, R_NilValue));
R_RegisterCFinalizerEx(aio, raio_finalizer, TRUE);
SET_ATTRIB(aio, NANO_PROT(con));

} else if (ptrtag == nano_StreamSymbol) {

Expand Down
Loading

0 comments on commit 34c2633

Please sign in to comment.