| 1 |
# nanonext - Utilities --------------------------------------------------------- |
| 2 |
|
| 3 |
#' NNG Library Version |
| 4 |
#' |
| 5 |
#' Returns the versions of the 'libnng' and 'libmbedtls' libraries used by the |
| 6 |
#' package. |
| 7 |
#' |
| 8 |
#' @return A character vector of length 2. |
| 9 |
#' |
| 10 |
#' @examples |
| 11 |
#' nng_version() |
| 12 |
#' |
| 13 |
#' @export |
| 14 |
#' |
| 15 |
nng_version <- function() .Call(rnng_version) |
| 16 |
|
| 17 |
#' Translate Error Codes |
| 18 |
#' |
| 19 |
#' Translate integer exit codes generated by the NNG library. All package |
| 20 |
#' functions return an integer exit code on error rather than the expected |
| 21 |
#' return value. These are classed 'errorValue' and may be checked by |
| 22 |
#' [is_error_value()]. |
| 23 |
#' |
| 24 |
#' @param xc integer exit code to translate. |
| 25 |
#' |
| 26 |
#' @return A character string comprising the error code and error message |
| 27 |
#' separated by `'|'`. |
| 28 |
#' |
| 29 |
#' @examples |
| 30 |
#' nng_error(1L) |
| 31 |
#' |
| 32 |
#' @export |
| 33 |
#' |
| 34 |
nng_error <- function(xc) .Call(rnng_strerror, xc) |
| 35 |
|
| 36 |
#' Clock Utility |
| 37 |
#' |
| 38 |
#' Provides the number of elapsed milliseconds since an arbitrary reference time |
| 39 |
#' in the past. The reference time will be the same for a given session, but may |
| 40 |
#' differ between sessions. |
| 41 |
#' |
| 42 |
#' A convenience function for building concurrent applications. The resolution |
| 43 |
#' of the clock depends on the underlying system timing facilities and may not |
| 44 |
#' be particularly fine-grained. This utility should however be faster than |
| 45 |
#' using `Sys.time()`. |
| 46 |
#' |
| 47 |
#' @return A double. |
| 48 |
#' |
| 49 |
#' @examples |
| 50 |
#' time <- mclock(); msleep(100); mclock() - time |
| 51 |
#' |
| 52 |
#' @export |
| 53 |
#' |
| 54 |
mclock <- function() .Call(rnng_clock) |
| 55 |
|
| 56 |
#' Sleep Utility |
| 57 |
#' |
| 58 |
#' Sleep function. May block for longer than requested, with the actual wait |
| 59 |
#' time determined by the capabilities of the underlying system. |
| 60 |
#' |
| 61 |
#' Non-integer values for `time` are coerced to integer. Negative, logical and |
| 62 |
#' other non-numeric values are ignored, causing the function to return |
| 63 |
#' immediately. |
| 64 |
#' |
| 65 |
#' Note that unlike `Sys.sleep()`, this function is not user-interruptible by |
| 66 |
#' sending SIGINT e.g. with ctrl + c. |
| 67 |
#' |
| 68 |
#' @param time integer number of milliseconds to block the caller. |
| 69 |
#' |
| 70 |
#' @return Invisible NULL. |
| 71 |
#' |
| 72 |
#' @examples |
| 73 |
#' time <- mclock(); msleep(100); mclock() - time |
| 74 |
#' |
| 75 |
#' @export |
| 76 |
#' |
| 77 |
msleep <- function(time) invisible(.Call(rnng_sleep, time)) |
| 78 |
|
| 79 |
#' Random Data Generation |
| 80 |
#' |
| 81 |
#' Strictly not for use in statistical analysis. Non-reproducible and with |
| 82 |
#' unknown statistical properties. Provides an alternative source of randomness |
| 83 |
#' from the Mbed TLS library for purposes such as cryptographic key generation. |
| 84 |
#' Mbed TLS uses a block-cipher in counter mode operation, as defined in |
| 85 |
#' NIST SP800-90A: *Recommendation for Random Number Generation Using |
| 86 |
#' Deterministic Random Bit Generators*. The implementation uses AES-256 as the |
| 87 |
#' underlying block cipher, with a derivation function, and an entropy collector |
| 88 |
#' combining entropy from multiple sources including at least one strong entropy |
| 89 |
#' source. |
| 90 |
#' |
| 91 |
#' @param n \[default 1L\] integer random bytes to generate (from 0 to 1024), |
| 92 |
#' coerced to integer if required. If a vector, the first element is taken. |
| 93 |
#' @param convert \[default TRUE\] logical `FALSE` to return a raw vector, or |
| 94 |
#' `TRUE` to return the hex representation of the bytes as a character string. |
| 95 |
#' |
| 96 |
#' @return A length `n` raw vector, or length one vector of `2n` random |
| 97 |
#' characters, depending on the value of `convert` supplied. |
| 98 |
#' |
| 99 |
#' @note Results obtained are independent of and do not alter the state of R's |
| 100 |
#' own pseudo-random number generators. |
| 101 |
#' |
| 102 |
#' @examples |
| 103 |
#' random() |
| 104 |
#' random(8L) |
| 105 |
#' random(n = 8L, convert = FALSE) |
| 106 |
#' |
| 107 |
#' @export |
| 108 |
#' |
| 109 |
random <- function(n = 1L, convert = TRUE) .Call(rnng_random, n, convert) |
| 110 |
|
| 111 |
#' Parse URL |
| 112 |
#' |
| 113 |
#' Parses a character string containing an RFC 3986 compliant URL as per NNG. |
| 114 |
#' |
| 115 |
#' @param url character string containing a URL. |
| 116 |
#' |
| 117 |
#' @return A named character vector of length 7, comprising: |
| 118 |
#' \itemize{ |
| 119 |
#' \item `scheme` - the URL scheme, such as "http" or "inproc" (always lower |
| 120 |
#' case). |
| 121 |
#' \item `userinfo` - the username and password (if supplied in the URL |
| 122 |
#' string). |
| 123 |
#' \item `hostname` - the name of the host. |
| 124 |
#' \item `port` - the port (if not specified, the default port if defined by |
| 125 |
#' the scheme). |
| 126 |
#' \item `path` - the path, typically used with HTTP or WebSocket. |
| 127 |
#' \item `query` - the query info (typically following ? in the URL). |
| 128 |
#' \item `fragment` - used for specifying an anchor, the part after # in a |
| 129 |
#' URL. |
| 130 |
#' } |
| 131 |
#' Values that cannot be determined are represented by an empty string `""`. |
| 132 |
#' |
| 133 |
#' @examples |
| 134 |
#' parse_url("https://user:[email protected]:8080/type/path?q=info#intro") |
| 135 |
#' parse_url("tcp://192.168.0.2:5555") |
| 136 |
#' |
| 137 |
#' @export |
| 138 |
#' |
| 139 |
parse_url <- function(url) .Call(rnng_url_parse, url) |
| 140 |
|
| 141 |
#' Validators |
| 142 |
#' |
| 143 |
#' Validator functions for object types created by \pkg{nanonext}. |
| 144 |
#' |
| 145 |
#' Is the object an Aio (inheriting from class 'sendAio' or 'recvAio'). |
| 146 |
#' |
| 147 |
#' Is the object an object inheriting from class 'nano' i.e. a nanoSocket, |
| 148 |
#' nanoContext, nanoStream, nanoListener, nanoDialer, nanoMonitor or nano |
| 149 |
#' Object. |
| 150 |
#' |
| 151 |
#' Is the object an ncurlSession (object of class 'ncurlSession'). |
| 152 |
#' |
| 153 |
#' Is the object a Condition Variable (object of class 'conditionVariable'). |
| 154 |
#' |
| 155 |
#' @param x an object. |
| 156 |
#' |
| 157 |
#' @return Logical value TRUE or FALSE. |
| 158 |
#' |
| 159 |
#' @examples |
| 160 |
#' nc <- call_aio(ncurl_aio("https://postman-echo.com/get", timeout = 1000L)) |
| 161 |
#' is_aio(nc) |
| 162 |
#' |
| 163 |
#' @export |
| 164 |
#' |
| 165 |
is_aio <- function(x) inherits(x, c("recvAio", "sendAio")) |
| 166 |
|
| 167 |
#' @examples |
| 168 |
#' s <- socket() |
| 169 |
#' is_nano(s) |
| 170 |
#' n <- nano() |
| 171 |
#' is_nano(n) |
| 172 |
#' close(s) |
| 173 |
#' n$close() |
| 174 |
#' |
| 175 |
#' @rdname is_aio |
| 176 |
#' @export |
| 177 |
#' |
| 178 |
is_nano <- function(x) inherits(x, c("nano", "nanoObject")) |
| 179 |
|
| 180 |
#' @examples |
| 181 |
#' s <- ncurl_session("https://postman-echo.com/get", timeout = 1000L) |
| 182 |
#' is_ncurl_session(s) |
| 183 |
#' if (is_ncurl_session(s)) close(s) |
| 184 |
#' |
| 185 |
#' @rdname is_aio |
| 186 |
#' @export |
| 187 |
#' |
| 188 |
is_ncurl_session <- function(x) inherits(x, "ncurlSession") |
| 189 |
|
| 190 |
#' Error Validators |
| 191 |
#' |
| 192 |
#' Validator functions for error value types created by \pkg{nanonext}. |
| 193 |
#' |
| 194 |
#' Is the object an error value generated by the package. All non-success |
| 195 |
#' integer return values are classed 'errorValue' to be distinguishable from |
| 196 |
#' integer message values. Includes error values returned after a timeout etc. |
| 197 |
#' |
| 198 |
#' Is the object a nul byte. |
| 199 |
#' |
| 200 |
#' @param x an object. |
| 201 |
#' |
| 202 |
#' @return Logical value TRUE or FALSE. |
| 203 |
#' |
| 204 |
#' @examples |
| 205 |
#' s <- socket() |
| 206 |
#' r <- recv_aio(s, timeout = 10) |
| 207 |
#' call_aio(r)$data |
| 208 |
#' close(s) |
| 209 |
#' r$data == 5L |
| 210 |
#' is_error_value(r$data) |
| 211 |
#' is_error_value(5L) |
| 212 |
#' |
| 213 |
#' @export |
| 214 |
#' |
| 215 |
is_error_value <- function(x) .Call(rnng_is_error_value, x) |
| 216 |
|
| 217 |
#' @examples |
| 218 |
#' is_nul_byte(as.raw(0L)) |
| 219 |
#' is_nul_byte(raw(length = 1L)) |
| 220 |
#' is_nul_byte(writeBin("", con = raw())) |
| 221 |
#' is_nul_byte(0L) |
| 222 |
#' is_nul_byte(NULL) |
| 223 |
#' is_nul_byte(NA) |
| 224 |
#' |
| 225 |
#' @rdname is_error_value |
| 226 |
#' @export |
| 227 |
#' |
| 228 |
is_nul_byte <- function(x) .Call(rnng_is_nul_byte, x) |
| 229 |
|
| 230 |
#' Translate HTTP Status Codes |
| 231 |
#' |
| 232 |
#' Provides an explanation for HTTP response status codes (in the range 100 to |
| 233 |
#' 599). If the status code is not defined as per RFC 9110, |
| 234 |
#' `"Unknown HTTP Status"` is returned - this may be a custom code used by the |
| 235 |
#' server. |
| 236 |
#' |
| 237 |
#' @param x numeric HTTP status code to translate. |
| 238 |
#' |
| 239 |
#' @return A character vector comprising the status code and explanation |
| 240 |
#' separated by `'|'`. |
| 241 |
#' |
| 242 |
#' @examples |
| 243 |
#' status_code(200) |
| 244 |
#' status_code(404) |
| 245 |
#' |
| 246 |
#' @export |
| 247 |
#' |
| 248 |
status_code <- function(x) .Call(rnng_status_code, x) |
| 249 |
|
| 250 |
#' Create Serialization Configuration |
| 251 |
#' |
| 252 |
#' Returns a serialization configuration, which may be set on a Socket for |
| 253 |
#' custom serialization and unserialization of non-system reference objects, |
| 254 |
#' allowing these to be sent and received between different R sessions. Once |
| 255 |
#' set, the functions apply to all send and receive operations performed in mode |
| 256 |
#' 'serial' over the Socket, or Context created from the Socket. |
| 257 |
#' |
| 258 |
#' This feature utilises the 'refhook' system of R native serialization. |
| 259 |
#' |
| 260 |
#' @param class a character string (or vector) of the class of object custom |
| 261 |
#' serialization functions are applied to, e.g. `'ArrowTabular'` or |
| 262 |
#' `c('torch_tensor', 'ArrowTabular')`. |
| 263 |
#' @param sfunc a function (or list of functions) that accepts a reference |
| 264 |
#' object inheriting from `class` and returns a raw vector. |
| 265 |
#' @param ufunc a function (or list of functions) that accepts a raw vector and |
| 266 |
#' returns a reference object. |
| 267 |
#' |
| 268 |
#' @return A list comprising the configuration. This should be set on a Socket |
| 269 |
#' using [opt<-()] with option name `"serial"`. |
| 270 |
#' |
| 271 |
#' @examples |
| 272 |
#' cfg <- serial_config("test_cls", function(x) serialize(x, NULL), unserialize) |
| 273 |
#' cfg |
| 274 |
#' |
| 275 |
#' cfg <- serial_config( |
| 276 |
#' c("class_one", "class_two"), |
| 277 |
#' list(function(x) serialize(x, NULL), function(x) serialize(x, NULL)), |
| 278 |
#' list(unserialize, unserialize) |
| 279 |
#' ) |
| 280 |
#' cfg |
| 281 |
#' |
| 282 |
#' s <- socket() |
| 283 |
#' opt(s, "serial") <- cfg |
| 284 |
#' |
| 285 |
#' # provide an empty list to remove registered functions |
| 286 |
#' opt(s, "serial") <- list() |
| 287 |
#' |
| 288 |
#' close(s) |
| 289 |
#' |
| 290 |
#' @export |
| 291 |
#' |
| 292 |
serial_config <- function(class, sfunc, ufunc) |
| 293 |
.Call(rnng_serial_config, class, sfunc, ufunc) |
| 294 |
|
| 295 |
#' Write to Stdout |
| 296 |
#' |
| 297 |
#' Performs a non-buffered write to `stdout` using the C function `writev()` or |
| 298 |
#' equivalent. Avoids interleaved output when writing concurrently from multiple |
| 299 |
#' processes. |
| 300 |
#' |
| 301 |
#' This function writes to the C-level `stdout` of the process and hence cannot |
| 302 |
#' be re-directed by [sink()]. |
| 303 |
#' |
| 304 |
#' A newline character is automatically appended to `x`, hence there is no need |
| 305 |
#' to include this within the input string. |
| 306 |
#' |
| 307 |
#' @param x character string. |
| 308 |
#' |
| 309 |
#' @return Invisible NULL. As a side effect, `x` is output to `stdout`. |
| 310 |
#' |
| 311 |
#' @examples |
| 312 |
#' write_stdout("") |
| 313 |
#' |
| 314 |
#' @export |
| 315 |
#' |
| 316 |
write_stdout <- function(x) invisible(.Call(rnng_write_stdout, x)) |
| 317 |
|
| 318 |
#' Read stdin |
| 319 |
#' |
| 320 |
#' Reads `stdin` from a background thread, allowing the stream to be accessed as |
| 321 |
#' messages from an NNG 'inproc' socket. As the read is blocking, it can only be |
| 322 |
#' used in non-interactive sessions. Closing `stdin` causes the background |
| 323 |
#' thread to exit and the socket connection to end. |
| 324 |
#' |
| 325 |
#' A 'pull' protocol socket is returned, and hence can only be used with receive |
| 326 |
#' functions. |
| 327 |
#' |
| 328 |
#' @return a Socket. |
| 329 |
#' |
| 330 |
#' @export |
| 331 |
#' |
| 332 |
read_stdin <- function() .Call(rnng_read_stdin, interactive()) |
| 333 |
|
| 334 |
#' IP Address |
| 335 |
#' |
| 336 |
#' Returns a character string comprising the local network IPv4 address, or |
| 337 |
#' vector if there are multiple addresses from multiple network adapters, or |
| 338 |
#' an empty character string if unavailable. |
| 339 |
#' |
| 340 |
#' The IP addresses will be named by interface (adapter friendly name on |
| 341 |
#' Windows) e.g. 'eth0' or 'en0'. |
| 342 |
#' |
| 343 |
#' @return A named character string. |
| 344 |
#' |
| 345 |
#' @examples |
| 346 |
#' ip_addr() |
| 347 |
#' |
| 348 |
#' @export |
| 349 |
#' |
| 350 |
ip_addr <- function() .Call(rnng_ip_addr) |
| 351 |
|
| 352 |
#' Advances the RNG State |
| 353 |
#' |
| 354 |
#' Internal package function. |
| 355 |
#' |
| 356 |
#' @return NULL. |
| 357 |
#' |
| 358 |
#' @keywords internal |
| 359 |
#' @export |
| 360 |
#' |
| 361 |
.advance <- function() .Call(rnng_advance_rng_state) |
| 362 |
|
| 363 |
#' Set Serialization Marker |
| 364 |
#' |
| 365 |
#' Internal package function. |
| 366 |
#' |
| 367 |
#' @param bool logical value. |
| 368 |
#' |
| 369 |
#' @return The logical `bool` supplied. |
| 370 |
#' |
| 371 |
#' @keywords internal |
| 372 |
#' @export |
| 373 |
#' |
| 374 |
.mark <- function(bool = TRUE) .Call(rnng_marker_set, bool) |
| 375 |
|
| 376 |
|
| 377 |
#' Internal Package Function |
| 378 |
#' |
| 379 |
#' Only present for cleaning up after running examples and tests. Do not attempt |
| 380 |
#' to run the examples. |
| 381 |
#' |
| 382 |
#' @examples |
| 383 |
#' if (Sys.info()[["sysname"]] == "Linux") { |
| 384 |
#' rm(list = ls()) |
| 385 |
#' invisible(gc()) |
| 386 |
#' .Call(nanonext:::rnng_fini_priors) |
| 387 |
#' Sys.sleep(1L) |
| 388 |
#' .Call(nanonext:::rnng_fini) |
| 389 |
#' } |
| 390 |
#' |
| 391 |
#' @keywords internal |
| 392 |
#' |
| 393 |
zzz <- function() {} |