Debian Codegraph @

R/nano.R

raw
1 # nanonext - Core - Nano Object and S3 Methods ---------------------------------
2
3 #' Create Nano Object
4 #'
5 #' Create a nano object, encapsulating a Socket, Dialers/Listeners and
6 #' associated methods.
7 #'
8 #' This function encapsulates a Socket, Dialer and/or Listener, and its
9 #' associated methods.
10 #'
11 #' The Socket may be accessed by `$socket`, and the Dialer or Listener by
12 #' `$dialer[[1]]` or `$listener[[1]]` respectively.
13 #'
14 #' The object's methods may be accessed by `$` e.g. `$send()` or `$recv()`.
15 #' These methods mirror their functional equivalents, with the same arguments
16 #' and defaults, apart from that the first argument of the functional equivalent
17 #' is mapped to the object's encapsulated socket (or context, if active) and
18 #' does not need to be supplied.
19 #'
20 #' More complex network topologies may be created by binding further dialers or
21 #' listeners using the object's `$dial()` and `$listen()` methods. The new
22 #' dialer/listener will be attached to the object e.g. if the object already has
23 #' a dialer, then at `$dialer[[2]]` etc.
24 #'
25 #' Note that `$dialer_opt()` and `$listener_opt()` methods will be available
26 #' once dialers/listeners are attached to the object. These methods get or apply
27 #' settings for all dialers or listeners equally. To get or apply settings for
28 #' individual dialers/listeners, access them directly via `$dialer[[2]]` or
29 #' `$listener[[2]]` etc.
30 #'
31 #' The methods `$opt()`, and also `$dialer_opt()` or `$listener_opt()` as may be
32 #' applicable, will get the requested option if a single argument `name` is
33 #' provided, and will set the value for the option if both arguments `name` and
34 #' `value` are provided.
35 #'
36 #' For Dialers or Listeners not automatically started, the `$dialer_start()` or
37 #' `$listener_start()` methods will be available. These act on the most recently
38 #' created Dialer or Listener respectively.
39 #'
40 #' For applicable protocols, new contexts may be created by using the
41 #' `$context_open()` method. This will attach a new context at `$context` as
42 #' well as a `$context_close()` method. While a context is active, all object
43 #' methods use the context rather than the socket. A new context may be created
44 #' by calling `$context_open()`, which will replace any existing context. It is
45 #' only necessary to use `$context_close()` to close the existing context and
46 #' revert to using the socket.
47 #'
48 #' @inheritParams socket
49 #'
50 #' @return A nano object of class 'nanoObject'.
51 #'
52 #' @examples
53 #' nano <- nano("bus", listen = "inproc://nanonext")
54 #' nano
55 #' nano$socket
56 #' nano$listener[[1]]
57 #'
58 #' nano$opt("send-timeout", 1500)
59 #' nano$opt("send-timeout")
60 #'
61 #' nano$listen(url = "inproc://nanonextgen")
62 #' nano$listener
63 #'
64 #' nano1 <- nano("bus", dial = "inproc://nanonext")
65 #' nano$send("example test", mode = "raw")
66 #' nano1$recv("character")
67 #'
68 #' nano$close()
69 #' nano1$close()
70 #'
71 #' @export
72 #'
73 nano <- function(
74 protocol = c("bus", "pair", "poly", "push", "pull", "pub", "sub", "req", "rep", "surveyor", "respondent"),
75 dial = NULL,
76 listen = NULL,
77 tls = NULL,
78 autostart = TRUE
79 ) {
80 nano <- `class<-`(new.env(hash = FALSE), "nanoObject")
81 socket <- socket(protocol)
82 sock2 <- NULL
83 makeActiveBinding(
84 "socket",
85 function(x) if (length(sock2)) sock2 else socket,
86 nano
87 )
88 is_poly <- attr(socket, "protocol") == "poly"
89
90 if (length(dial)) {
91 r <- dial(socket, url = dial, tls = tls, autostart = autostart)
92 if (r == 0L) {
93 nano[["dialer"]] <- attr(socket, "dialer")
94 nano[["dialer_opt"]] <- function(name, value)
95 if (missing(value))
96 lapply(.subset2(nano, "dialer"), opt, name = name) else
97 invisible(lapply(.subset2(nano, "dialer"), `opt<-`, name = name, value = value))
98 if (isFALSE(autostart)) nano[["dialer_start"]] <- function(async = TRUE) {
99 s <- start.nanoDialer(.subset2(nano, "dialer")[[1L]], async = async)
100 if (s == 0L) rm("dialer_start", envir = nano)
101 invisible(s)
102 }
103 }
104 }
105
106 if (length(listen)) {
107 r <- listen(socket, url = listen, tls = tls, autostart = autostart)
108 if (r == 0L) {
109 nano[["listener"]] <- attr(socket, "listener")
110 nano[["listener_opt"]] <- function(name, value)
111 if (missing(value))
112 lapply(.subset2(nano, "listener"), opt, name = name) else
113 invisible(lapply(.subset2(nano, "listener"), `opt<-`, name = name, value = value))
114 if (isFALSE(autostart)) nano[["listener_start"]] <- function() {
115 s <- start.nanoListener(.subset2(nano, "listener")[[1L]])
116 if (s == 0L) rm("listener_start", envir = nano)
117 invisible(s)
118 }
119 }
120 }
121
122 nano[["close"]] <- function() close(.subset2(nano, "socket"))
123
124 nano[["dial"]] <- function(url = "inproc://nanonext", tls = NULL, autostart = TRUE) {
125 r <- dial(socket, url = url, tls = tls, autostart = autostart)
126 if (r == 0L) {
127 nano[["dialer"]] <- attr(socket, "dialer")
128 nano[["dialer_opt"]] <- function(name, value)
129 if (missing(value))
130 lapply(.subset2(nano, "dialer"), opt, name = name) else
131 invisible(lapply(.subset2(nano, "dialer"), `opt<-`, name = name, value = value))
132 if (isFALSE(autostart)) nano[["dialer_start"]] <- function(async = TRUE) {
133 s <- start.nanoDialer((d <- .subset2(nano, "dialer"))[[length(d)]], async = async)
134 if (s == 0L) rm("dialer_start", envir = nano)
135 invisible(s)
136 }
137 }
138 invisible(r)
139 }
140
141 nano[["listen"]] <- function(url = "inproc://nanonext", tls = NULL, autostart = TRUE) {
142 r <- listen(socket, url = url, tls = tls, autostart = autostart)
143 if (r == 0L) {
144 nano[["listener"]] <- attr(socket, "listener")
145 nano[["listener_opt"]] <- function(name, value)
146 if (missing(value))
147 lapply(.subset2(nano, "listener"), opt, name = name) else
148 invisible(lapply(.subset2(nano, "listener"), `opt<-`, name = name, value = value))
149 if (isFALSE(autostart)) nano[["listener_start"]] <- function() {
150 s <- start.nanoListener((l <- .subset2(nano, "listener"))[[length(l)]])
151 if (s == 0L) rm("listener_start", envir = nano)
152 invisible(s)
153 }
154 }
155 invisible(r)
156 }
157
158 nano[["recv"]] <- function(mode = c("serial", "character", "complex", "double",
159 "integer", "logical", "numeric", "raw", "string"),
160 block = NULL)
161 recv(socket, mode = mode, block = block)
162
163 nano[["recv_aio"]] <- function(mode = c("serial", "character", "complex", "double",
164 "integer", "logical", "numeric", "raw", "string"),
165 timeout = NULL)
166 recv_aio(socket, mode = mode, timeout = timeout)
167
168 nano[["send"]] <- if (is_poly) {
169 function(data, mode = c("serial", "raw"), block = NULL, pipe = 0L)
170 send(socket, data = data, mode = mode, block = block, pipe = pipe)
171 } else {
172 function(data, mode = c("serial", "raw"), block = NULL)
173 send(socket, data = data, mode = mode, block = block)
174 }
175
176 nano[["send_aio"]] <- if (is_poly) {
177 function(data, mode = c("serial", "raw"), timeout = NULL, pipe = 0L)
178 send_aio(socket, data = data, mode = mode, timeout = timeout, pipe = pipe)
179 } else {
180 function(data, mode = c("serial", "raw"), timeout = NULL)
181 send_aio(socket, data = data, mode = mode, timeout = timeout)
182 }
183
184 nano[["opt"]] <- function(name, value)
185 if (missing(value)) opt(socket, name = name) else
186 invisible(`opt<-`(socket, name = name, value = value))
187
188 nano[["stat"]] <- function(name) stat(socket, name = name)
189
190 switch(attr(socket, "protocol"),
191 req = ,
192 rep = {
193 nano[["context_open"]] <- function() {
194 if (is.null(sock2)) sock2 <<- socket
195 nano[["context_close"]] <- function() if (length(sock2)) {
196 r <- close(socket)
197 socket <<- sock2
198 sock2 <<- NULL
199 rm(list = c("context", "context_close"), envir = nano)
200 r
201 }
202 socket <<- nano[["context"]] <- context(sock2)
203 }
204 },
205 sub = {
206 nano[["context_open"]] <- function() {
207 if (is.null(sock2)) sock2 <<- socket
208 nano[["context_close"]] <- function() if (length(sock2)) {
209 r <- close(socket)
210 socket <<- sock2
211 sock2 <<- NULL
212 rm(list = c("context", "context_close"), envir = nano)
213 r
214 }
215 socket <<- nano[["context"]] <- context(sock2)
216 }
217 nano[["subscribe"]] <- function(topic = NULL)
218 subscribe(socket, topic = topic)
219 nano[["unsubscribe"]] <- function(topic = NULL)
220 unsubscribe(socket, topic = topic)
221 },
222 surveyor = {
223 nano[["context_open"]] <- function() {
224 if (is.null(sock2)) sock2 <<- socket
225 nano[["context_close"]] <- function() if (length(sock2)) {
226 r <- close(socket)
227 socket <<- sock2
228 sock2 <<- NULL
229 rm(list = c("context", "context_close"), envir = nano)
230 r
231 }
232 socket <<- nano[["context"]] <- context(sock2)
233 }
234 nano[["survey_time"]] <- function(value = 1000L)
235 survey_time(socket, value = value)
236 },
237 respondent = {
238 nano[["context_open"]] <- function() {
239 if (is.null(sock2)) sock2 <<- socket
240 nano[["context_close"]] <- function() if (length(sock2)) {
241 r <- close(socket)
242 socket <<- sock2
243 sock2 <<- NULL
244 rm(list = c("context", "context_close"), envir = nano)
245 r
246 }
247 socket <<- nano[["context"]] <- context(sock2)
248 }
249 },
250 NULL)
251
252 nano
253 }
254
255 #' @export
256 #'
257 print.nanoObject <- function(x, ...) {
258 cat(
259 sprintf(
260 "< nano object >\n - socket id: %d\n - state: %s\n - protocol: %s\n",
261 attr(.subset2(x, "socket"), "id"),
262 attr(.subset2(x, "socket"), "state"),
263 attr(.subset2(x, "socket"), "protocol")
264 ),
265 file = stdout()
266 )
267 if (length(.subset2(x, "listener")))
268 cat(
269 " - listener:", as.character(lapply(.subset2(x, "listener"), attr, "url")),
270 sep = "\n ",
271 file = stdout()
272 )
273 if (length(.subset2(x, "dialer")))
274 cat(
275 " - dialer:", as.character(lapply(.subset2(x, "dialer"), attr, "url")),
276 sep = "\n ",
277 file = stdout()
278 )
279 invisible(x)
280 }
281
282 #' @export
283 #'
284 print.nanoSocket <- function(x, ...) {
285 cat(
286 sprintf(
287 "< nanoSocket >\n - id: %d\n - state: %s\n - protocol: %s\n",
288 attr(x, "id"),
289 attr(x, "state"),
290 attr(x, "protocol")
291 ),
292 file = stdout()
293 )
294 if (length(attr(x, "listener")))
295 cat(
296 " - listener:", as.character(lapply(attr(x, "listener"), attr, "url")),
297 sep = "\n ",
298 file = stdout()
299 )
300 if (length(attr(x, "dialer")))
301 cat(
302 " - dialer:", as.character(lapply(attr(x, "dialer"), attr, "url")),
303 sep = "\n ",
304 file = stdout()
305 )
306 invisible(x)
307 }
308
309 #' @export
310 #'
311 print.nanoContext <- function(x, ...) {
312 cat(
313 sprintf(
314 "< nanoContext >\n - id: %d\n - socket: %d\n - state: %s\n - protocol: %s\n",
315 attr(x, "id"),
316 attr(x, "socket"),
317 attr(x, "state"),
318 attr(x, "protocol")
319 ),
320 file = stdout()
321 )
322 invisible(x)
323 }
324
325 #' @export
326 #'
327 print.nanoDialer <- function(x, ...) {
328 cat(
329 sprintf(
330 "< nanoDialer >\n - id: %d\n - socket: %d\n - state: %s\n - url: %s\n",
331 attr(x, "id"),
332 attr(x, "socket"),
333 attr(x, "state"),
334 attr(x, "url")
335 ),
336 file = stdout()
337 )
338 invisible(x)
339 }
340
341 #' @export
342 #'
343 print.nanoListener <- function(x, ...) {
344 cat(
345 sprintf(
346 "< nanoListener >\n - id: %d\n - socket: %d\n - state: %s\n - url: %s\n",
347 attr(x, "id"),
348 attr(x, "socket"),
349 attr(x, "state"),
350 attr(x, "url")
351 ),
352 file = stdout()
353 )
354 invisible(x)
355 }
356
357 #' @export
358 #'
359 print.nanoStream <- function(x, ...) {
360 cat(
361 sprintf(
362 "< nanoStream >\n - mode: %s\n - state: %s\n - url: %s\n",
363 attr(x, "mode"),
364 attr(x, "state"),
365 attr(x, "url")
366 ),
367 file = stdout()
368 )
369 invisible(x)
370 }
371
372 #' @export
373 #'
374 print.nanoMonitor <- function(x, ...) {
375 cat(
376 sprintf("< nanoMonitor >\n - socket: %s\n", attr(x, "socket")),
377 file = stdout()
378 )
379 invisible(x)
380 }
381
382 #' @export
383 #'
384 print.recvAio <- function(x, ...) {
385 cat("< recvAio | $data >\n", file = stdout())
386 invisible(x)
387 }
388
389 #' @export
390 #'
391 print.sendAio <- function(x, ...) {
392 cat("< sendAio | $result >\n", file = stdout())
393 invisible(x)
394 }
395
396 #' @export
397 #'
398 print.ncurlAio <- function(x, ...) {
399 cat("< ncurlAio | $status $headers $data >\n", file = stdout())
400 invisible(x)
401 }
402
403 #' @export
404 #'
405 print.ncurlSession <- function(x, ...) {
406 cat(
407 sprintf(
408 "< ncurlSession > - %s\n",
409 if (is.null(attr(x, "state"))) "transact() to return data" else "not active"
410 ),
411 file = stdout()
412 )
413 invisible(x)
414 }
415
416 #' @export
417 #'
418 print.unresolvedValue <- function(x, ...) {
419 cat("'unresolved' logi NA\n", file = stdout())
420 invisible(x)
421 }
422
423 #' @export
424 #'
425 print.errorValue <- function(x, ...) {
426 cat(sprintf("'errorValue' int %s\n", nng_error(x)), file = stdout())
427 invisible(x)
428 }
429
430 #' @export
431 #'
432 print.conditionVariable <- function(x, ...) {
433 cat("< conditionVariable >\n", file = stdout())
434 invisible(x)
435 }
436
437 #' @export
438 #'
439 print.tlsConfig <- function(x, ...) {
440 cat(
441 sprintf("< TLS %s config | auth mode: %s >\n", attr(x, "spec"), attr(x, "mode")),
442 file = stdout()
443 )
444 invisible(x)
445 }
446
447 #' @export
448 #'
449 `[[.nano` <- function(x, i, exact = TRUE)
450 attr(x, i, exact = exact)
451
452 #' @export
453 #'
454 `[.nano` <- function(x, i, exact = TRUE)
455 attr(x, deparse(substitute(i)), exact = exact)
456
457 #' @export
458 #'
459 `$.nano` <- function(x, name)
460 attr(x, name, exact = TRUE)
461
462 #' @export
463 #'
464 `$<-.nano` <- function(x, name, value) x
465
466 #' @export
467 #'
468 `$<-.nanoObject` <- function(x, name, value) x
469
470 #' @export
471 #'
472 `[.recvAio` <- function(x, i) collect_aio_(x)
473
474 #' @export
475 #'
476 `$<-.recvAio` <- function(x, name, value) x
477
478 #' @export
479 #'
480 `[.sendAio` <- function(x, i) collect_aio_(x)
481
482 #' @export
483 #'
484 `$<-.sendAio` <- function(x, name, value) x
485
486 #' @exportS3Method utils::.DollarNames
487 #'
488 .DollarNames.nano <- function(x, pattern = "")
489 grep(pattern, names(attributes(x)), value = TRUE, fixed = TRUE)
490
491 #' @exportS3Method utils::.DollarNames
492 #'
493 .DollarNames.recvAio <- function(x, pattern = "")
494 if (startsWith("data", pattern)) "data" else character()
495
496 #' @exportS3Method utils::.DollarNames
497 #'
498 .DollarNames.sendAio <- function(x, pattern = "")
499 if (startsWith("result", pattern)) "result" else character()
500
501 #' @exportS3Method utils::.DollarNames
502 #'
503 .DollarNames.ncurlAio <- function(x, pattern = "")
504 grep(pattern, c("status", "headers", "data"), value = TRUE, fixed = TRUE)