| 1 | #' All Wrapper Functions for rzmq |
| 2 | #' |
| 3 | #' Wrapper functions for backwards compatibility with rzmq. See vignette |
| 4 | #' for examples. |
| 5 | #' |
| 6 | #' @details |
| 7 | #' \code{send.socket()}/\code{receive.socket()} send/receive messages over |
| 8 | #' a socket. These are simple wrappers around \code{zmq.msg.send()} and |
| 9 | #' \code{zmq.msg.receive()}, respectively. |
| 10 | #' |
| 11 | #' \code{init.context()} creates a new ZeroMQ context. A useful wrapper |
| 12 | #' around \code{zmq.ctx.new()} which handles freeing memory for you, i.e. |
| 13 | #' \code{zmq.ctx.destroy()} will automatically be called for you. |
| 14 | #' |
| 15 | #' \code{init.socket()} creates a ZeroMQ socket; serves as a high-level |
| 16 | #' binding for \code{zmq.socket()}, including handling freeing memory |
| 17 | #' automatically. See also \code{ZMQ.ST()}. |
| 18 | #' |
| 19 | #' \code{bind.socket()}: see \code{zmq.bind()}. |
| 20 | #' |
| 21 | #' \code{connect.socket()}: see \code{zmq.connect()} |
| 22 | #' |
| 23 | #' @param socket |
| 24 | #' A ZMQ socket. |
| 25 | #' @param data |
| 26 | #' An R object. |
| 27 | #' @param send.more |
| 28 | #' Logical; will more messages be sent? |
| 29 | #' @param serialize,unserialize |
| 30 | #' Logical; determines if serialize/unserialize should be called |
| 31 | #' on the sent/received data. |
| 32 | #' @param serialversion |
| 33 | #' NULL or numeric; the workspace format version to use when serializing. |
| 34 | #' NULL specifies the current default version. The only other supported |
| 35 | #' values are 2 and 3. |
| 36 | #' @param dont.wait |
| 37 | #' Logical; determines if reception is blocking. |
| 38 | #' @param context |
| 39 | #' A ZMQ context. |
| 40 | #' @param socket.type |
| 41 | #' The type of ZMQ socket as a string, of the form "ZMQ_type". Valid 'type' |
| 42 | #' values are PAIR, PUB, SUB, REQ, REP, DEALER, PULL, PUSH, XPUB, XSUB, and |
| 43 | #' STERAM. |
| 44 | #' @param address |
| 45 | #' A valid address. See details. |
| 46 | #' |
| 47 | #' @author Wei-Chen Chen \email{wccsnow@@gmail.com}. |
| 48 | #' |
| 49 | #' @references ZeroMQ/4.1.0 API Reference: |
| 50 | #' \url{http://api.zeromq.org/4-1:_start} |
| 51 | #' |
| 52 | #' Programming with Big Data in R Website: \url{https://pbdr.org/} |
| 53 | #' |
| 54 | #' @keywords rzmq |
| 55 | #' @rdname xx_rzmq_wrapper |
| 56 | #' @name Wrapper Functions for rzmq |
| 57 | NULL |
| 58 | |
| 59 | |
| 60 | |
| 61 | #' @rdname xx_rzmq_wrapper |
| 62 | #' @export |
| 63 | send.socket <- function(socket, data, send.more = FALSE, serialize = TRUE, |
| 64 | serialversion = NULL){ |
| 65 | if(send.more){ |
| 66 | flags <- ZMQ.SR()$SNDMORE |
| 67 | } else{ |
| 68 | flags <- ZMQ.SR()$BLOCK |
| 69 | } |
| 70 | zmq.msg.send(data, socket, flags = flags, serialize = serialize, |
| 71 | serialversion = serialversion) |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | #' @rdname xx_rzmq_wrapper |
| 77 | #' @export |
| 78 | receive.socket <- function(socket, unserialize = TRUE, dont.wait = FALSE){ |
| 79 | if(dont.wait){ |
| 80 | flags <- ZMQ.SR()$DONTWAIT |
| 81 | } else{ |
| 82 | flags <- ZMQ.SR()$BLOCK |
| 83 | } |
| 84 | zmq.msg.recv(socket, flags = flags, unserialize = unserialize) |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | #' @rdname xx_rzmq_wrapper |
| 90 | #' @export |
| 91 | init.context <- function(){ |
| 92 | try.zmq.ctx.destroy <- function(ctx){ |
| 93 | invisible(suppressWarnings(zmq.ctx.destroy(ctx))) |
| 94 | } |
| 95 | |
| 96 | ctx <- zmq.ctx.new() |
| 97 | reg.finalizer(ctx, try.zmq.ctx.destroy, onexit = TRUE) |
| 98 | ctx |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | #' @rdname xx_rzmq_wrapper |
| 104 | #' @export |
| 105 | init.socket <- function(context, socket.type){ |
| 106 | try.zmqt.close <- function(socket){ |
| 107 | invisible(suppressWarnings(zmq.close(socket))) |
| 108 | } |
| 109 | |
| 110 | socket.type <- sub(".*_", "", socket.type) |
| 111 | id <- which(names(ZMQ.ST()) == socket.type) |
| 112 | if(length(id) != 1){ |
| 113 | stop("socket.type is not found.") |
| 114 | } else{ |
| 115 | socket.type <- ZMQ.ST()[[id]] |
| 116 | } |
| 117 | |
| 118 | socket <- zmq.socket(context, type = socket.type) |
| 119 | reg.finalizer(socket, try.zmqt.close, onexit = TRUE) |
| 120 | socket |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | #' @rdname xx_rzmq_wrapper |
| 126 | #' @export |
| 127 | bind.socket <- function(socket, address){ |
| 128 | zmq.bind(socket, address) |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | #' @rdname xx_rzmq_wrapper |
| 134 | #' @export |
| 135 | connect.socket <- function(socket, address){ |
| 136 | zmq.connect(socket, address) |
| 137 | } |
| 138 |