| 1 | #' Message Functions |
| 2 | #' |
| 3 | #' Message functions |
| 4 | #' |
| 5 | #' \code{zmq.msg.send()} sends an R message. |
| 6 | #' |
| 7 | #' \code{zmq.msg.recv()} receives an R message. |
| 8 | #' |
| 9 | #' @param rmsg |
| 10 | #' an R message |
| 11 | #' @param socket |
| 12 | #' a ZMQ socket |
| 13 | #' @param flags |
| 14 | #' a flag for method of send and receive |
| 15 | #' @param serialize |
| 16 | #' if serialize the \code{rmsg} |
| 17 | #' @param unserialize |
| 18 | #' if unserialize the received R message |
| 19 | #' @param serialversion |
| 20 | #' NULL or numeric; the workspace format version to use when serializing. |
| 21 | #' NULL specifies the current default version. The only other supported |
| 22 | #' values are 2 and 3 |
| 23 | #' |
| 24 | #' @return \code{zmq.msg.send()} returns 0 if successful, otherwise returns -1 |
| 25 | #' and sets \code{errno} to \code{EFAULT}. |
| 26 | #' |
| 27 | #' \code{zmq.msg.recv()} returns the message if successful, otherwise returns |
| 28 | #' -1 and sets \code{errno} to \code{EFAULT}. |
| 29 | #' |
| 30 | #' @author Wei-Chen Chen \email{wccsnow@@gmail.com}. |
| 31 | #' |
| 32 | #' @references ZeroMQ/4.1.0 API Reference: |
| 33 | #' \url{http://api.zeromq.org/4-1:_start} |
| 34 | #' |
| 35 | #' Programming with Big Data in R Website: \url{https://pbdr.org/} |
| 36 | #' |
| 37 | #' @examples |
| 38 | #' \dontrun{ |
| 39 | #' ### Using request-reply pattern. |
| 40 | #' |
| 41 | #' ### At the server, run next in background or the other window. |
| 42 | #' library(pbdZMQ, quietly = TRUE) |
| 43 | #' |
| 44 | #' context <- zmq.ctx.new() |
| 45 | #' responder <- zmq.socket(context, ZMQ.ST()$REP) |
| 46 | #' zmq.bind(responder, "tcp://*:5555") |
| 47 | #' buf <- zmq.msg.recv(responder) |
| 48 | #' set.seed(1234) |
| 49 | #' ret <- rnorm(5) |
| 50 | #' print(ret) |
| 51 | #' zmq.msg.send(ret, responder) |
| 52 | #' zmq.close(responder) |
| 53 | #' zmq.ctx.destroy(context) |
| 54 | #' |
| 55 | #' |
| 56 | #' ### At a client, run next in foreground. |
| 57 | #' library(pbdZMQ, quietly = TRUE) |
| 58 | #' |
| 59 | #' context <- zmq.ctx.new() |
| 60 | #' requester <- zmq.socket(context, ZMQ.ST()$REQ) |
| 61 | #' zmq.connect(requester, "tcp://localhost:5555") |
| 62 | #' zmq.msg.send(NULL, requester) |
| 63 | #' ret <- zmq.msg.recv(requester) |
| 64 | #' print(ret) |
| 65 | #' zmq.close(requester) |
| 66 | #' zmq.ctx.destroy(context) |
| 67 | #' } |
| 68 | #' |
| 69 | #' @keywords programming |
| 70 | #' @seealso \code{\link{zmq.send}()}, \code{\link{zmq.recv}()}. |
| 71 | #' @rdname a2_message |
| 72 | #' @name Message Function |
| 73 | NULL |
| 74 | |
| 75 | |
| 76 | |
| 77 | zmq.msg.init <- function(){ |
| 78 | .Call("R_zmq_msg_init", PACKAGE = "pbdZMQ") |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | zmq.msg.close <- function(msg.t){ |
| 84 | .Call("R_zmq_msg_close", msg.t, PACKAGE = "pbdZMQ") |
| 85 | } |
| 86 | |
| 87 | |
| 88 | |
| 89 | #' @rdname a2_message |
| 90 | #' @export |
| 91 | zmq.msg.send <- function(rmsg, socket, flags = ZMQ.SR()$BLOCK, |
| 92 | serialize = TRUE, serialversion = NULL){ |
| 93 | if(serialize){ |
| 94 | rmsg <- serialize(rmsg, NULL, version = serialversion) |
| 95 | } |
| 96 | ret <- .Call("R_zmq_msg_send", rmsg, socket, as.integer(flags), PACKAGE = "pbdZMQ") |
| 97 | invisible(ret) |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | #' @rdname a2_message |
| 103 | #' @export |
| 104 | zmq.msg.recv <- function(socket, flags = ZMQ.SR()$BLOCK, |
| 105 | unserialize = TRUE){ |
| 106 | rmsg <- .Call("R_zmq_msg_recv", socket, as.integer(flags), PACKAGE = "pbdZMQ") |
| 107 | if(unserialize && is.raw(rmsg)){ |
| 108 | rmsg <- unserialize(rmsg) |
| 109 | } |
| 110 | rmsg |
| 111 | } |
| 112 |