| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/R_zmq_poll.r |
| 3 |
\name{Poll Functions} |
| 4 |
\alias{Poll Functions} |
| 5 |
\alias{zmq.poll} |
| 6 |
\alias{zmq.poll.free} |
| 7 |
\alias{zmq.poll.length} |
| 8 |
\alias{zmq.poll.get.revents} |
| 9 |
\title{Poll Functions} |
| 10 |
\usage{ |
| 11 |
zmq.poll(socket, type, timeout = -1L, MC = ZMQ.MC()) |
| 12 |
|
| 13 |
zmq.poll.free() |
| 14 |
|
| 15 |
zmq.poll.length() |
| 16 |
|
| 17 |
zmq.poll.get.revents(index = 1L) |
| 18 |
} |
| 19 |
\arguments{ |
| 20 |
\item{socket}{a vector of ZMQ sockets} |
| 21 |
|
| 22 |
\item{type}{a vector of socket types corresponding to \code{socket} argument} |
| 23 |
|
| 24 |
\item{timeout}{timeout for poll, see ZeroMQ manual for details} |
| 25 |
|
| 26 |
\item{MC}{a message control, see \code{\link{ZMQ.MC}()} for details} |
| 27 |
|
| 28 |
\item{index}{an index of ZMQ poll items to obtain revents} |
| 29 |
} |
| 30 |
\value{ |
| 31 |
\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 |
|
| 36 |
\code{zmq.poll.length()} returns the total number of poll items |
| 37 |
|
| 38 |
\code{zmq.poll.get.revents()} returns the revent type |
| 39 |
} |
| 40 |
\description{ |
| 41 |
Poll functions |
| 42 |
} |
| 43 |
\details{ |
| 44 |
\code{zmq.poll()} initials ZMQ poll items given ZMQ \code{socket}'s |
| 45 |
and ZMQ poll \code{type}'s. Both \code{socket} and \code{type} are |
| 46 |
in vectors of the same length, while \code{socket} contains socket pointers |
| 47 |
and \code{type} contains types of poll. |
| 48 |
See \code{\link{ZMQ.PO}()} for the possible values of |
| 49 |
\code{type}. ZMQ defines several poll types and utilize |
| 50 |
them to poll multiple sockets. |
| 51 |
|
| 52 |
\code{zmq.poll.free()} frees ZMQ poll structure memory internally. |
| 53 |
|
| 54 |
\code{zmq.poll.length()} obtains total numbers of ZMQ poll items. |
| 55 |
|
| 56 |
\code{zmq.poll.get.revents()} obtains revent types from ZMQ poll item by |
| 57 |
the input index. |
| 58 |
} |
| 59 |
\examples{ |
| 60 |
\dontrun{ |
| 61 |
### Using poll pattern. |
| 62 |
### See demo/mspoller.r for details. |
| 63 |
|
| 64 |
### Run next in background or the other window. |
| 65 |
SHELL> Rscript wuserver.r & |
| 66 |
SHELL> Rscript taskvent.r & |
| 67 |
SHELL> Rscript mspoller.r |
| 68 |
|
| 69 |
### The mspoller.r has next. |
| 70 |
library(pbdZMQ, quietly = TRUE) |
| 71 |
|
| 72 |
### Initial. |
| 73 |
context <- zmq.ctx.new() |
| 74 |
receiver <- zmq.socket(context, ZMQ.ST()$PULL) |
| 75 |
zmq.connect(receiver, "tcp://localhost:5557") |
| 76 |
subscriber <- zmq.socket(context, ZMQ.ST()$SUB) |
| 77 |
zmq.connect(subscriber, "tcp://localhost:5556") |
| 78 |
zmq.setsockopt(subscriber, ZMQ.SO()$SUBSCRIBE, "20993") |
| 79 |
|
| 80 |
### Process messages from both sockets. |
| 81 |
cat("Press Ctrl+C or Esc to stop mspoller.\n") |
| 82 |
i.rec <- 0 |
| 83 |
i.sub <- 0 |
| 84 |
while(TRUE){ |
| 85 |
### Set poller. |
| 86 |
zmq.poll(c(receiver, subscriber), |
| 87 |
c(ZMQ.PO()$POLLIN, ZMQ.PO()$POLLIN)) |
| 88 |
|
| 89 |
### Check receiver. |
| 90 |
if(bitwAnd(zmq.poll.get.revents(1), ZMQ.PO()$POLLIN)){ |
| 91 |
ret <- zmq.recv(receiver) |
| 92 |
if(ret$len != -1){ |
| 93 |
cat("task ventilator:", ret$buf, "at", i.rec, "\n") |
| 94 |
i.rec <- i.rec + 1 |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
### Check subscriber. |
| 99 |
if(bitwAnd(zmq.poll.get.revents(2), ZMQ.PO()$POLLIN)){ |
| 100 |
ret <- zmq.recv(subscriber) |
| 101 |
if(ret$len != -1){ |
| 102 |
cat("weather update:", ret$buf, "at", i.sub, "\n") |
| 103 |
i.sub <- i.sub + 1 |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if(i.rec >= 5 & i.sub >= 5){ |
| 108 |
break |
| 109 |
} |
| 110 |
|
| 111 |
Sys.sleep(runif(1, 0.5, 1)) |
| 112 |
} |
| 113 |
|
| 114 |
### Finish. |
| 115 |
zmq.poll.free() |
| 116 |
zmq.close(receiver) |
| 117 |
zmq.close(subscriber) |
| 118 |
zmq.ctx.destroy(context) |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
\references{ |
| 123 |
ZeroMQ/4.1.0 API Reference: |
| 124 |
\url{https://libzmq.readthedocs.io/en/zeromq4-1/} |
| 125 |
|
| 126 |
Programming with Big Data in R Website: \url{https://pbdr.org/} |
| 127 |
} |
| 128 |
\seealso{ |
| 129 |
\code{\link{zmq.recv}()}, \code{\link{zmq.send}()}. |
| 130 |
} |
| 131 |
\author{ |
| 132 |
Wei-Chen Chen \email{[email protected]}. |
| 133 |
} |
| 134 |
\keyword{programming} |