/
next.R
81 lines (76 loc) · 2.78 KB
/
next.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright (C) 2023-2024 Hibiki AI Limited <info@hibiki-ai.com>
#
# This file is part of mirai.
#
# mirai is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# mirai is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# mirai. If not, see <https://www.gnu.org/licenses/>.
# mirai ------------------------------------------------------------------------
#' Next >> Developer Interface
#'
#' \code{nextstream} retrieves the currently stored L'Ecuyer-CMRG RNG stream
#' for the specified compute profile and advances it to the next stream.
#'
#' @inheritParams mirai
#'
#' @return For \code{nextstream}: a length 7 integer vector, as given by
#' \code{.Random.seed} when the L'Ecuyer-CMRG RNG is in use (may be passed
#' directly to the \sQuote{rs} argument of \code{\link{daemon}}), or else
#' NULL if a stream has not yet been created.
#'
#' @details These functions are exported for use by packages extending
#' \pkg{mirai} with alternative launchers of \code{\link{daemon}} processes.
#'
#' For \code{nextstream}: This function should be called for its return
#' value when required. The function also has the side effect of
#' automatically advancing the stream stored within the compute profile.
#' This ensures that the next recursive stream is returned when the function
#' is called again.
#'
#' @examples
#' if (interactive()) {
#' # Only run examples in interactive R sessions
#'
#' daemons(1L)
#' nextstream()
#' nextstream()
#'
#' nextget("pid")
#' nextget("urls")
#'
#' daemons(0)
#'
#' }
#'
#' @export
#'
nextstream <- function(.compute = "default") next_stream(..[[.compute]])
#' Next >> Developer Interface
#'
#' \code{nextget} retrieves the specified item from the specified compute
#' profile.
#'
#' @param x character value of item to retrieve. One of \sQuote{pid} (dispatcher
#' process ID), \sQuote{urls} (URLs dispatcher is listening at) or
#' \sQuote{tls} (the stored client TLS configuration for use by daemons).
#'
#' @return For \code{nextget}: the requested item, or else NULL if not present.
#'
#' @rdname nextstream
#' @export
#'
nextget <- function(x, .compute = "default") ..[[.compute]][[x]]
# internals --------------------------------------------------------------------
next_stream <- function(envir) {
stream <- envir[["stream"]]
if (length(stream)) `[[<-`(envir, "stream", parallel::nextRNGStream(stream))
stream
}