| 1 |
# nanonext - Core - Sockets ---------------------------------------------------- |
| 2 |
|
| 3 |
#' Open Socket |
| 4 |
#' |
| 5 |
#' Open a Socket implementing `protocol`, and optionally dial (establish an |
| 6 |
#' outgoing connection) or listen (accept an incoming connection) at an address. |
| 7 |
#' |
| 8 |
#' NNG presents a socket view of networking. The sockets are constructed using |
| 9 |
#' protocol-specific functions, as a given socket implements precisely one |
| 10 |
#' protocol. |
| 11 |
#' |
| 12 |
#' Each socket may be used to send and receive messages (if the protocol |
| 13 |
#' supports it, and implements the appropriate protocol semantics). For example, |
| 14 |
#' sub sockets automatically filter incoming messages to discard those for |
| 15 |
#' topics that have not been subscribed. |
| 16 |
#' |
| 17 |
#' This function (optionally) binds a single Dialer and/or Listener to a Socket. |
| 18 |
#' More complex network topologies may be created by binding further Dialers / |
| 19 |
#' Listeners to the Socket as required using [dial()] and [listen()]. |
| 20 |
#' |
| 21 |
#' New contexts may also be created using [context()] if the protocol supports |
| 22 |
#' it. |
| 23 |
#' |
| 24 |
#' @param protocol \[default 'bus'\] choose protocol - `"bus"`, `"pair"`, |
| 25 |
#' `"poly"`, `"push"`, `"pull"`, `"pub"`, `"sub"`, `"req"`, `"rep"`, |
| 26 |
#' `"surveyor"`, or `"respondent"` - see [protocols]. |
| 27 |
#' @param dial (optional) a URL to dial, specifying the transport and address as |
| 28 |
#' a character string e.g. 'inproc://anyvalue' or 'tcp://127.0.0.1:5555' (see |
| 29 |
#' [transports]). |
| 30 |
#' @param listen (optional) a URL to listen at, specifying the transport and |
| 31 |
#' address as a character string e.g. 'inproc://anyvalue' or |
| 32 |
#' 'tcp://127.0.0.1:5555' (see [transports]). |
| 33 |
#' @param autostart \[default TRUE\] whether to start the dialer/listener. Set |
| 34 |
#' to FALSE if setting configuration options on the dialer/listener as it is |
| 35 |
#' not generally possible to change these once started. For dialers only: set |
| 36 |
#' to NA to start synchronously - this is less resilient if a connection is |
| 37 |
#' not immediately possible, but avoids subtle errors from attempting to use |
| 38 |
#' the socket before an asynchronous dial has completed. |
| 39 |
#' @param raw \[default FALSE\] whether to open raw mode sockets. Note: not for |
| 40 |
#' general use - do not enable unless you have a specific need (refer to NNG |
| 41 |
#' documentation). |
| 42 |
#' @inheritParams dial |
| 43 |
#' |
| 44 |
#' @return A Socket (object of class 'nanoSocket' and 'nano'). |
| 45 |
#' |
| 46 |
#' @section Protocols: |
| 47 |
#' |
| 48 |
#' The following Scalability Protocols (communication patterns) are implemented: |
| 49 |
#' \itemize{ |
| 50 |
#' \item Bus (mesh networks) - protocol: 'bus' |
| 51 |
#' \item Pair (two-way radio) - protocol: 'pair' |
| 52 |
#' \item Poly (one-to-one of many) - protocol: 'poly' |
| 53 |
#' \item Pipeline (one-way pipe) - protocol: 'push', 'pull' |
| 54 |
#' \item Publisher/Subscriber (topics & broadcast) - protocol: 'pub', 'sub' |
| 55 |
#' \item Request/Reply (RPC) - protocol: 'req', 'rep' |
| 56 |
#' \item Survey (voting & service discovery) - protocol: 'surveyor', |
| 57 |
#' 'respondent' |
| 58 |
#' } |
| 59 |
#' |
| 60 |
#' Please see [protocols] for further documentation. |
| 61 |
#' |
| 62 |
#' @section Transports: |
| 63 |
#' |
| 64 |
#' The following communications transports may be used: |
| 65 |
#' |
| 66 |
#' \itemize{ |
| 67 |
#' \item Inproc (in-process) - url: 'inproc://' |
| 68 |
#' \item IPC (inter-process communications) - url: 'ipc://' (or 'abstract://' |
| 69 |
#' on Linux) |
| 70 |
#' \item TCP and TLS over TCP - url: 'tcp://' and 'tls+tcp://' |
| 71 |
#' \item WebSocket and TLS over WebSocket - url: 'ws://' and 'wss://' |
| 72 |
#' } |
| 73 |
#' |
| 74 |
#' Please see [transports] for further documentation. |
| 75 |
#' |
| 76 |
#' @examples |
| 77 |
#' s <- socket(protocol = "req", listen = "inproc://nanosocket") |
| 78 |
#' s |
| 79 |
#' s1 <- socket(protocol = "rep", dial = "inproc://nanosocket") |
| 80 |
#' s1 |
| 81 |
#' |
| 82 |
#' send(s, "hello world!") |
| 83 |
#' recv(s1) |
| 84 |
#' |
| 85 |
#' close(s1) |
| 86 |
#' close(s) |
| 87 |
#' |
| 88 |
#' @export |
| 89 |
#' |
| 90 |
socket <- function( |
| 91 |
protocol = c("bus", "pair", "poly", "push", "pull", "pub", "sub", "req", "rep", "surveyor", "respondent"), |
| 92 |
dial = NULL, |
| 93 |
listen = NULL, |
| 94 |
tls = NULL, |
| 95 |
autostart = TRUE, |
| 96 |
raw = FALSE |
| 97 |
) |
| 98 |
.Call(rnng_protocol_open, protocol, dial, listen, tls, autostart, raw) |
| 99 |
|
| 100 |
#' Close Connection |
| 101 |
#' |
| 102 |
#' Close Connection on a Socket, Context, Dialer, Listener, Stream, Pipe, or |
| 103 |
#' ncurl Session. |
| 104 |
#' |
| 105 |
#' Closing an object explicitly frees its resources. An object can also be |
| 106 |
#' removed directly in which case its resources are freed when the object is |
| 107 |
#' garbage collected. |
| 108 |
#' |
| 109 |
#' Closing a Socket associated with a Context also closes the Context. |
| 110 |
#' |
| 111 |
#' Dialers and Listeners are implicitly closed when the Socket they are |
| 112 |
#' associated with is closed. |
| 113 |
#' |
| 114 |
#' Closing a Socket or a Context: messages that have been submitted for sending |
| 115 |
#' may be flushed or delivered, depending upon the transport. Closing the Socket |
| 116 |
#' while data is in transmission will likely lead to loss of that data. There is |
| 117 |
#' no automatic linger or flush to ensure that the Socket send buffers have |
| 118 |
#' completely transmitted. |
| 119 |
#' |
| 120 |
#' Closing a Stream: if any send or receive operations are pending, they will be |
| 121 |
#' terminated and any new operations will fail after the connection is closed. |
| 122 |
#' |
| 123 |
#' Closing an 'ncurlSession' closes the http(s) connection. |
| 124 |
#' |
| 125 |
#' @param con a Socket, Context, Dialer, Listener, Stream, or 'ncurlSession'. |
| 126 |
#' @param ... not used. |
| 127 |
#' |
| 128 |
#' @return Invisibly, an integer exit code (zero on success). |
| 129 |
#' |
| 130 |
#' @seealso [reap()] |
| 131 |
#' |
| 132 |
#' @name close |
| 133 |
#' @rdname close |
| 134 |
#' |
| 135 |
NULL |
| 136 |
|
| 137 |
#' @rdname close |
| 138 |
#' @method close nanoSocket |
| 139 |
#' @export |
| 140 |
#' |
| 141 |
close.nanoSocket <- function(con, ...) invisible(.Call(rnng_close, con)) |
| 142 |
|
| 143 |
#' Reap |
| 144 |
#' |
| 145 |
#' An alternative to `close` for Sockets, Contexts, Listeners, and Dialers |
| 146 |
#' avoiding S3 method dispatch. |
| 147 |
#' |
| 148 |
#' May be used on unclassed external pointers e.g. those created by |
| 149 |
#' [.context()]. Returns silently and does not warn or error, nor does it update |
| 150 |
#' the state of object attributes. |
| 151 |
#' |
| 152 |
#' @param con a Socket, Context, Listener or Dialer. |
| 153 |
#' |
| 154 |
#' @return An integer exit code (zero on success). |
| 155 |
#' |
| 156 |
#' @seealso [close()] |
| 157 |
#' |
| 158 |
#' @examples |
| 159 |
#' s <- socket("req") |
| 160 |
#' listen(s) |
| 161 |
#' dial(s) |
| 162 |
#' ctx <- .context(s) |
| 163 |
#' |
| 164 |
#' reap(ctx) |
| 165 |
#' reap(s[["dialer"]][[1]]) |
| 166 |
#' reap(s[["listener"]][[1]]) |
| 167 |
#' reap(s) |
| 168 |
#' reap(s) |
| 169 |
#' |
| 170 |
#' @export |
| 171 |
#' |
| 172 |
reap <- function(con) .Call(rnng_reap, con) |
| 173 |
|
| 174 |
#' Monitor a Socket for Pipe Changes |
| 175 |
#' |
| 176 |
#' This function monitors pipe additions and removals from a socket. |
| 177 |
#' |
| 178 |
#' @param sock a Socket. |
| 179 |
#' @param cv a 'conditionVariable'. |
| 180 |
#' |
| 181 |
#' @return For `monitor`: a Monitor (object of class 'nanoMonitor'). \cr |
| 182 |
#' For `read_monitor`: an integer vector of pipe IDs (positive if added, |
| 183 |
#' negative if removed), or else NULL if there were no changes since the |
| 184 |
#' previous read. |
| 185 |
#' |
| 186 |
#' @examples |
| 187 |
#' cv <- cv() |
| 188 |
#' s <- socket("poly") |
| 189 |
#' s1 <- socket("poly") |
| 190 |
#' |
| 191 |
#' m <- monitor(s, cv) |
| 192 |
#' m |
| 193 |
#' |
| 194 |
#' listen(s) |
| 195 |
#' dial(s1) |
| 196 |
#' |
| 197 |
#' cv_value(cv) |
| 198 |
#' read_monitor(m) |
| 199 |
#' |
| 200 |
#' close(s) |
| 201 |
#' close(s1) |
| 202 |
#' |
| 203 |
#' read_monitor(m) |
| 204 |
#' |
| 205 |
#' @export |
| 206 |
#' |
| 207 |
monitor <- function(sock, cv) .Call(rnng_monitor_create, sock, cv) |
| 208 |
|
| 209 |
#' @param x a Monitor. |
| 210 |
#' |
| 211 |
#' @rdname monitor |
| 212 |
#' @export |
| 213 |
#' |
| 214 |
read_monitor <- function(x) .Call(rnng_monitor_read, x) |