| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/context.R |
| 3 |
\name{request} |
| 4 |
\alias{request} |
| 5 |
\title{Request over Context (RPC Client for Req/Rep Protocol)} |
| 6 |
\usage{ |
| 7 |
request( |
| 8 |
context, |
| 9 |
data, |
| 10 |
send_mode = c("serial", "raw"), |
| 11 |
recv_mode = c("serial", "character", "complex", "double", "integer", "logical", |
| 12 |
"numeric", "raw", "string"), |
| 13 |
timeout = NULL, |
| 14 |
cv = NULL, |
| 15 |
id = NULL |
| 16 |
) |
| 17 |
} |
| 18 |
\arguments{ |
| 19 |
\item{context}{a Context.} |
| 20 |
|
| 21 |
\item{data}{an object (if \code{send_mode = "raw"}, a vector).} |
| 22 |
|
| 23 |
\item{send_mode}{[default 'serial'] character value or integer equivalent - |
| 24 |
either \code{"serial"} (1L) to send serialised R objects, or \code{"raw"} (2L) to |
| 25 |
send atomic vectors of any type as a raw byte vector.} |
| 26 |
|
| 27 |
\item{recv_mode}{[default 'serial'] character value or integer equivalent - |
| 28 |
one of \code{"serial"} (1L), \code{"character"} (2L), \code{"complex"} (3L), \code{"double"} |
| 29 |
(4L), \code{"integer"} (5L), \code{"logical"} (6L), \code{"numeric"} (7L), \code{"raw"} (8L), |
| 30 |
or \code{"string"} (9L). The default \code{"serial"} means a serialised R object; for |
| 31 |
the other modes, received bytes are converted into the respective mode. |
| 32 |
\code{"string"} is a faster option for length one character vectors.} |
| 33 |
|
| 34 |
\item{timeout}{[default NULL] integer value in milliseconds or NULL, which |
| 35 |
applies a socket-specific default, usually the same as no timeout.} |
| 36 |
|
| 37 |
\item{cv}{(optional) a 'conditionVariable' to signal when the async receive |
| 38 |
is complete, or NULL.} |
| 39 |
|
| 40 |
\item{id}{NULL. For package internal use only.} |
| 41 |
} |
| 42 |
\value{ |
| 43 |
A 'recvAio' (object of class 'mirai' and 'recvAio') (invisibly). |
| 44 |
} |
| 45 |
\description{ |
| 46 |
Implements a caller/client for the req node of the req/rep protocol. Sends |
| 47 |
data to the rep node (executor/server) and returns an Aio, which can be |
| 48 |
called for the value when required. |
| 49 |
} |
| 50 |
\details{ |
| 51 |
Sending the request and receiving the result are both performed async, hence |
| 52 |
the function will return immediately with a 'recvAio' object. Access the |
| 53 |
return value at \verb{$data}. |
| 54 |
|
| 55 |
This is designed so that the process on the server can run concurrently |
| 56 |
without blocking the client. |
| 57 |
|
| 58 |
Optionally use \code{\link[=call_aio]{call_aio()}} on the 'recvAio' to call (and wait for) the |
| 59 |
result. |
| 60 |
|
| 61 |
If an error occured in the server process, a nul byte \code{00} will be received. |
| 62 |
This allows an error to be easily distinguished from a NULL return value. |
| 63 |
\code{\link[=is_nul_byte]{is_nul_byte()}} can be used to test for a nul byte. |
| 64 |
|
| 65 |
It is recommended to use a new context for each request to ensure consistent |
| 66 |
state tracking. The integer context ID is appended as the attribute \code{id} to |
| 67 |
the returned 'recvAio'. |
| 68 |
} |
| 69 |
\section{Send Modes}{ |
| 70 |
|
| 71 |
|
| 72 |
The default mode \code{"serial"} sends serialised R objects to ensure perfect |
| 73 |
reproducibility within R. When receiving, the corresponding mode \code{"serial"} |
| 74 |
should be used. Custom serialization and unserialization functions for |
| 75 |
reference objects may be enabled by the function \code{\link[=serial_config]{serial_config()}}. |
| 76 |
|
| 77 |
Mode \code{"raw"} sends atomic vectors of any type as a raw byte vector, and must |
| 78 |
be used when interfacing with external applications or raw system sockets, |
| 79 |
where R serialization is not in use. When receiving, the mode corresponding |
| 80 |
to the vector sent should be used. |
| 81 |
} |
| 82 |
|
| 83 |
\section{Signalling}{ |
| 84 |
|
| 85 |
|
| 86 |
By supplying a 'conditionVariable', when the receive is complete, the |
| 87 |
'conditionVariable' is signalled by incrementing its value by 1. This |
| 88 |
happens asynchronously and independently of the R execution thread. |
| 89 |
} |
| 90 |
|
| 91 |
\examples{ |
| 92 |
\dontrun{ |
| 93 |
|
| 94 |
# works if req and rep are running in parallel in different processes |
| 95 |
|
| 96 |
req <- socket("req", listen = "tcp://127.0.0.1:6546") |
| 97 |
rep <- socket("rep", dial = "tcp://127.0.0.1:6546") |
| 98 |
|
| 99 |
reply(.context(rep), execute = function(x) x + 1, timeout = 50) |
| 100 |
aio <- request(.context(req), data = 2022) |
| 101 |
aio$data |
| 102 |
|
| 103 |
close(req) |
| 104 |
close(rep) |
| 105 |
|
| 106 |
# Signalling a condition variable |
| 107 |
|
| 108 |
req <- socket("req", listen = "tcp://127.0.0.1:6546") |
| 109 |
ctxq <- context(req) |
| 110 |
cv <- cv() |
| 111 |
aio <- request(ctxq, data = 2022, cv = cv) |
| 112 |
until(cv, 10L) |
| 113 |
close(req) |
| 114 |
|
| 115 |
# The following should be run in another process |
| 116 |
rep <- socket("rep", dial = "tcp://127.0.0.1:6546") |
| 117 |
ctxp <- context(rep) |
| 118 |
reply(ctxp, execute = function(x) x + 1) |
| 119 |
close(rep) |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
} |