| 1 | #' Poll Functions |
| 2 | #' |
| 3 | #' Poll functions |
| 4 | #' |
| 5 | #' \code{zmq.poll()} initials ZMQ poll items given ZMQ \code{socket}'s |
| 6 | #' and ZMQ poll \code{type}'s. Both \code{socket} and \code{type} are |
| 7 | #' in vectors of the same length, while \code{socket} contains socket pointers |
| 8 | #' and \code{type} contains types of poll. |
| 9 | #' See \code{\link{ZMQ.PO}()} for the possible values of |
| 10 | #' \code{type}. ZMQ defines several poll types and utilize |
| 11 | #' them to poll multiple sockets. |
| 12 | #' |
| 13 | #' \code{zmq.poll.free()} frees ZMQ poll structure memory internally. |
| 14 | #' |
| 15 | #' \code{zmq.poll.length()} obtains total numbers of ZMQ poll items. |
| 16 | #' |
| 17 | #' \code{zmq.poll.get.revents()} obtains revent types from ZMQ poll item by |
| 18 | #' the input index. |
| 19 | #' |
| 20 | #' @param socket |
| 21 | #' a vector of ZMQ sockets |
| 22 | #' @param type |
| 23 | #' a vector of socket types corresponding to \code{socket} argument |
| 24 | #' @param timeout |
| 25 | #' timeout for poll, see ZeroMQ manual for details |
| 26 | #' @param index |
| 27 | #' an index of ZMQ poll items to obtain revents |
| 28 | #' @param MC |
| 29 | #' a message control, see \code{\link{ZMQ.MC}()} for details |
| 30 | #' |
| 31 | #' @return \code{zmq.poll()} returns a ZMQ code and an errno, |
| 32 | #' see ZeroMQ manual for details, no error/warning/interrupt in this |
| 33 | #' \code{R} function, but some error/warning/interrupt may catch by |
| 34 | #' the \code{C} function \code{zmq_poll()}. |
| 35 | #' @return \code{zmq.poll.length()} returns the total number of poll items |
| 36 | #' @return \code{zmq.poll.get.revents()} returns the revent type |
| 37 | #' |
| 38 | #' @author Wei-Chen Chen \email{wccsnow@@gmail.com}. |
| 39 | #' |
| 40 | #' @references ZeroMQ/4.1.0 API Reference: |
| 41 | #' \url{http://api.zeromq.org/4-1:_start} |
| 42 | #' |
| 43 | #' Programming with Big Data in R Website: \url{https://pbdr.org/} |
| 44 | #' |
| 45 | #' @examples |
| 46 | #' \dontrun{ |
| 47 | #' ### Using poll pattern. |
| 48 | #' ### See demo/mspoller.r for details. |
| 49 | #' |
| 50 | #' ### Run next in background or the other window. |
| 51 | #' SHELL> Rscript wuserver.r & |
| 52 | #' SHELL> Rscript taskvent.r & |
| 53 | #' SHELL> Rscript mspoller.r |
| 54 | #' |
| 55 | #' ### The mspoller.r has next. |
| 56 | #' library(pbdZMQ, quietly = TRUE) |
| 57 | #' |
| 58 | #' ### Initial. |
| 59 | #' context <- zmq.ctx.new() |
| 60 | #' receiver <- zmq.socket(context, ZMQ.ST()$PULL) |
| 61 | #' zmq.connect(receiver, "tcp://localhost:5557") |
| 62 | #' subscriber <- zmq.socket(context, ZMQ.ST()$SUB) |
| 63 | #' zmq.connect(subscriber, "tcp://localhost:5556") |
| 64 | #' zmq.setsockopt(subscriber, ZMQ.SO()$SUBSCRIBE, "20993") |
| 65 | #' |
| 66 | #' ### Process messages from both sockets. |
| 67 | #' cat("Press Ctrl+C or Esc to stop mspoller.\n") |
| 68 | #' i.rec <- 0 |
| 69 | #' i.sub <- 0 |
| 70 | #' while(TRUE){ |
| 71 | #' ### Set poller. |
| 72 | #' zmq.poll(c(receiver, subscriber), |
| 73 | #' c(ZMQ.PO()$POLLIN, ZMQ.PO()$POLLIN)) |
| 74 | #' |
| 75 | #' ### Check receiver. |
| 76 | #' if(bitwAnd(zmq.poll.get.revents(1), ZMQ.PO()$POLLIN)){ |
| 77 | #' ret <- zmq.recv(receiver) |
| 78 | #' if(ret$len != -1){ |
| 79 | #' cat("task ventilator:", ret$buf, "at", i.rec, "\n") |
| 80 | #' i.rec <- i.rec + 1 |
| 81 | #' } |
| 82 | #' } |
| 83 | #' |
| 84 | #' ### Check subscriber. |
| 85 | #' if(bitwAnd(zmq.poll.get.revents(2), ZMQ.PO()$POLLIN)){ |
| 86 | #' ret <- zmq.recv(subscriber) |
| 87 | #' if(ret$len != -1){ |
| 88 | #' cat("weather update:", ret$buf, "at", i.sub, "\n") |
| 89 | #' i.sub <- i.sub + 1 |
| 90 | #' } |
| 91 | #' } |
| 92 | #' |
| 93 | #' if(i.rec >= 5 & i.sub >= 5){ |
| 94 | #' break |
| 95 | #' } |
| 96 | #' |
| 97 | #' Sys.sleep(runif(1, 0.5, 1)) |
| 98 | #' } |
| 99 | #' |
| 100 | #' ### Finish. |
| 101 | #' zmq.poll.free() |
| 102 | #' zmq.close(receiver) |
| 103 | #' zmq.close(subscriber) |
| 104 | #' zmq.ctx.destroy(context) |
| 105 | #' } |
| 106 | #' |
| 107 | #' @keywords programming |
| 108 | #' @seealso \code{\link{zmq.recv}()}, \code{\link{zmq.send}()}. |
| 109 | #' @rdname b3_poll |
| 110 | #' @name Poll Functions |
| 111 | NULL |
| 112 | |
| 113 | |
| 114 | |
| 115 | #' @rdname b3_poll |
| 116 | #' @export |
| 117 | zmq.poll <- function(socket, type, timeout = -1L, MC = ZMQ.MC()){ |
| 118 | if(length(socket) != length(type)){ |
| 119 | stop("socket and type are of different length.") |
| 120 | } |
| 121 | |
| 122 | type <- as.integer(type) |
| 123 | if(!all(type %in% 1:7)){ |
| 124 | stop("type should be integers in 1 to 7.") |
| 125 | } |
| 126 | |
| 127 | zmq.poll.free() |
| 128 | |
| 129 | ret <- .Call("R_zmq_poll", socket, type, as.integer(timeout), |
| 130 | as.logical(MC$check.eintr), |
| 131 | PACKAGE = "pbdZMQ") |
| 132 | return(invisible(ret)) |
| 133 | } |
| 134 | |
| 135 | |
| 136 | #' @rdname b3_poll |
| 137 | #' @export |
| 138 | zmq.poll.free <- function(){ |
| 139 | ret <- .Call("R_zmq_poll_free", PACKAGE = "pbdZMQ") |
| 140 | invisible(ret) |
| 141 | } |
| 142 | |
| 143 | #' @rdname b3_poll |
| 144 | #' @export |
| 145 | zmq.poll.length <- function(){ |
| 146 | ret <- .Call("R_zmq_poll_length", PACKAGE = "pbdZMQ") |
| 147 | invisible(ret) |
| 148 | } |
| 149 | |
| 150 | #' @rdname b3_poll |
| 151 | #' @export |
| 152 | zmq.poll.get.revents <- function(index = 1L){ |
| 153 | if(index < 1){ |
| 154 | stop("index is a positive interger.") |
| 155 | } |
| 156 | ret <- .Call("R_zmq_poll_get_revents", as.integer(index - 1)[1], |
| 157 | PACKAGE = "pbdZMQ") |
| 158 | invisible(ret) |
| 159 | } |