| 1 | #' Socket Functions |
| 2 | #' |
| 3 | #' Socket functions |
| 4 | #' |
| 5 | #' \code{zmq.socket()} initials a ZMQ socket given a ZMQ context \code{ctx} and |
| 6 | #' a socket \code{type}. See \code{\link{ZMQ.ST}()} for the possible values of |
| 7 | #' \code{type}. ZMQ defines several patterns for the socket type and utilize |
| 8 | #' them to communicate in different ways including request-reply, |
| 9 | #' publish-subscribe, pipeline, exclusive pair, and naive patterns. |
| 10 | #' |
| 11 | #' \code{zmq.close()} destroys the ZMQ socket. |
| 12 | #' |
| 13 | #' \code{zmq.bind()} binds the socket to a local endpoint and then accepts |
| 14 | #' incoming connections on that endpoint. See \code{endpoint} next for details. |
| 15 | #' |
| 16 | #' \code{zmq.connect()} connects the socket to a remote endpoint and then |
| 17 | #' accepts outgoing connections on that endpoint. See \code{endpoint} next for |
| 18 | #' details. |
| 19 | #' |
| 20 | #' \code{endpoint} is a string consisting of a transport :// followed by an |
| 21 | #' address. The transport specifies the underlying protocol to use. The address |
| 22 | #' specifies the transport-specific address to bind to. pbdZMQ/ZMQ provides |
| 23 | #' the following transports: \tabular{ll}{ Transport \tab Usage \cr \code{tcp} |
| 24 | #' \tab unicast transport using TCP \cr \code{ipc} \tab local inter-process |
| 25 | #' communication transport \cr \code{inproc} \tab local in-process |
| 26 | #' (inter-thread) communication transport \cr \code{pgm,epgm} \tab reliable |
| 27 | #' multicast transport using PGM } *** warning: \code{epgm} is not turned on by |
| 28 | #' default in the pbdZMQ's internal ZeroMQ library. \cr *** warning: \code{ipc} |
| 29 | #' is not supported in Windows system. |
| 30 | #' |
| 31 | #' \code{zmq.setsockopt()} is to set/change socket options. |
| 32 | #' |
| 33 | #' \code{zmq.getsockopt()} is to get socket options and returns |
| 34 | #' \code{option.value}. |
| 35 | #' |
| 36 | #' @param ctx |
| 37 | #' a ZMQ context |
| 38 | #' @param type |
| 39 | #' a socket type |
| 40 | #' @param socket |
| 41 | #' a ZMQ socket |
| 42 | #' @param endpoint |
| 43 | #' a ZMQ socket endpoint |
| 44 | #' @param option.name |
| 45 | #' an option name to the socket |
| 46 | #' @param option.value |
| 47 | #' an option value to the option name |
| 48 | #' @param MC |
| 49 | #' a message control, see \code{\link{ZMQ.MC}()} for details |
| 50 | #' |
| 51 | #' @return \code{zmq.socket()} returns an R external pointer (\code{socket}) |
| 52 | #' generated by ZMQ C API pointing to a socket if successful, otherwise returns |
| 53 | #' an R \code{NULL} and sets \code{errno} to the error value, see ZeroMQ manual |
| 54 | #' for details. |
| 55 | #' |
| 56 | #' \code{zmq.close()} destroys the socket reference/pointer (\code{socket}) and |
| 57 | #' returns 0 if successful, otherwise returns -1 and sets \code{errno} to the |
| 58 | #' error value, see ZeroMQ manual for details. |
| 59 | #' |
| 60 | #' \code{zmq.bind()} binds the socket to specific \code{endpoint} and returns 0 |
| 61 | #' if successful, otherwise returns -1 and sets \code{errno} to the error |
| 62 | #' value, see ZeroMQ manual for details. |
| 63 | #' |
| 64 | #' \code{zmq.connect()} connects the socket to specific \code{endpoint} and |
| 65 | #' returns 0 if successful, otherwise returns -1 and sets \code{errno} to the |
| 66 | #' error value, see ZeroMQ manual for details. |
| 67 | #' |
| 68 | #' \code{zmq.setsockopt()} sets/changes the socket option and returns 0 if |
| 69 | #' successful, otherwise returns -1 and sets \code{errno} to the error value, |
| 70 | #' see ZeroMQ manual for details. |
| 71 | #' |
| 72 | #' \code{zmq.getsockopt()} returns the value of socket option, |
| 73 | #' see ZeroMQ manual for details. |
| 74 | #' |
| 75 | #' @author Wei-Chen Chen \email{wccsnow@@gmail.com}. |
| 76 | #' |
| 77 | #' @references ZeroMQ/4.1.0 API Reference: |
| 78 | #' \url{http://api.zeromq.org/4-1:_start} |
| 79 | #' |
| 80 | #' Programming with Big Data in R Website: \url{https://pbdr.org/} |
| 81 | #' |
| 82 | #' @examples |
| 83 | #' \dontrun{ |
| 84 | #' ### Using request-reply pattern. |
| 85 | #' |
| 86 | #' ### At the server, run next in background or the other windows. |
| 87 | #' library(pbdZMQ, quietly = TRUE) |
| 88 | #' |
| 89 | #' context <- zmq.ctx.new() |
| 90 | #' responder <- zmq.socket(context, ZMQ.ST()$REP) |
| 91 | #' zmq.bind(responder, "tcp://*:5555") |
| 92 | #' zmq.close(responder) |
| 93 | #' zmq.ctx.destroy(context) |
| 94 | #' |
| 95 | #' |
| 96 | #' ### At a client, run next in foreground. |
| 97 | #' library(pbdZMQ, quietly = TRUE) |
| 98 | #' |
| 99 | #' context <- zmq.ctx.new() |
| 100 | #' requester <- zmq.socket(context, ZMQ.ST()$REQ) |
| 101 | #' zmq.connect(requester, "tcp://localhost:5555") |
| 102 | #' zmq.close(requester) |
| 103 | #' zmq.ctx.destroy(context) |
| 104 | #' } |
| 105 | #' |
| 106 | #' @keywords programming |
| 107 | #' @seealso \code{\link{zmq.ctx.new}()}, \code{\link{zmq.ctx.destroy}()}. |
| 108 | #' @rdname a1_socket |
| 109 | #' @name Socket Functions |
| 110 | NULL |
| 111 | |
| 112 | |
| 113 | |
| 114 | #' @rdname a1_socket |
| 115 | #' @export |
| 116 | zmq.socket <- function(ctx, type = ZMQ.ST()$REP){ |
| 117 | ret <- .Call("R_zmq_socket", ctx, type, PACKAGE = "pbdZMQ") |
| 118 | attr(ret, "type") = type |
| 119 | ### Users are responsible to take care free and gc. |
| 120 | # reg.finalizer(ret, zmq.close, TRUE) |
| 121 | ret |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | #' @rdname a1_socket |
| 127 | #' @export |
| 128 | zmq.close <- function(socket){ |
| 129 | ret <- .Call("R_zmq_close", socket, PACKAGE = "pbdZMQ") |
| 130 | invisible(ret) |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | #' @rdname a1_socket |
| 136 | #' @export |
| 137 | zmq.bind <- function(socket, endpoint, MC = ZMQ.MC()){ |
| 138 | ret <- .Call("R_zmq_bind", socket, endpoint, PACKAGE = "pbdZMQ") |
| 139 | |
| 140 | if(ret == -1){ |
| 141 | if(MC$stop.at.error){ |
| 142 | stop(paste("zmq.bind fails, ", endpoint, sep = "")) |
| 143 | return(invisible(ret)) |
| 144 | } |
| 145 | if(MC$warning.at.error){ |
| 146 | warning(paste("zmq.bind fails, ", endpoint, sep = "")) |
| 147 | return(invisible(ret)) |
| 148 | } |
| 149 | } else{ |
| 150 | return(invisible(ret)) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | #' @rdname a1_socket |
| 157 | #' @export |
| 158 | zmq.connect <- function(socket, endpoint, MC = ZMQ.MC()){ |
| 159 | ret <- .Call("R_zmq_connect", socket, endpoint, PACKAGE = "pbdZMQ") |
| 160 | |
| 161 | if(ret == -1){ |
| 162 | if(MC$stop.at.error){ |
| 163 | stop(paste("zmq.connect fails, ", endpoint, sep = "")) |
| 164 | return(invisible(ret)) |
| 165 | } |
| 166 | if(MC$warning.at.error){ |
| 167 | warning(paste("zmq.connect fails, ", endpoint, sep = "")) |
| 168 | return(invisible(ret)) |
| 169 | } |
| 170 | } else{ |
| 171 | return(invisible(ret)) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | #' @rdname a1_socket |
| 178 | #' @export |
| 179 | zmq.disconnect <- function(socket, endpoint, MC = ZMQ.MC()){ |
| 180 | ret <- .Call("R_zmq_disconnect", socket, endpoint, PACKAGE = "pbdZMQ") |
| 181 | |
| 182 | if(ret == -1){ |
| 183 | if(MC$stop.at.error){ |
| 184 | stop(paste("zmq.disconnect fails, ", endpoint, sep = "")) |
| 185 | return(invisible(ret)) |
| 186 | } |
| 187 | if(MC$warning.at.error){ |
| 188 | warning(paste("zmq.disconnect fails, ", endpoint, sep = "")) |
| 189 | return(invisible(ret)) |
| 190 | } |
| 191 | } else{ |
| 192 | return(invisible(ret)) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | #' @rdname a1_socket |
| 199 | #' @export |
| 200 | zmq.setsockopt <- function(socket, option.name, option.value, MC = ZMQ.MC()){ |
| 201 | if(is.character(option.value)){ |
| 202 | option.type <- 0L |
| 203 | } else if(is.integer(option.value)){ |
| 204 | option.type <- 1L |
| 205 | } else{ |
| 206 | stop("Type of option.value is not implemented") |
| 207 | } |
| 208 | |
| 209 | ret <- .Call("R_zmq_setsockopt", socket, option.name, option.value, |
| 210 | option.type, PACKAGE = "pbdZMQ") |
| 211 | |
| 212 | if(ret == -1){ |
| 213 | if(MC$stop.at.error){ |
| 214 | stop(paste("zmq.setsockopt fails, ", option.value, sep = "")) |
| 215 | return(invisible(ret)) |
| 216 | } |
| 217 | if(MC$warning.at.error){ |
| 218 | warning(paste("zmq.setsockopt fails, ", option.value, sep = "")) |
| 219 | return(invisible(ret)) |
| 220 | } |
| 221 | } else{ |
| 222 | return(invisible(ret)) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | #' @rdname a1_socket |
| 227 | #' @export |
| 228 | zmq.getsockopt <- function(socket, option.name, option.value, MC = ZMQ.MC()){ |
| 229 | if(is.character(option.value)){ |
| 230 | option.type <- 0L |
| 231 | } else if(is.integer(option.value)){ |
| 232 | option.type <- 1L |
| 233 | } else{ |
| 234 | stop("Type of option.value is not implemented") |
| 235 | } |
| 236 | |
| 237 | ret <- .Call("R_zmq_getsockopt", socket, option.name, option.value, |
| 238 | option.type, PACKAGE = "pbdZMQ") |
| 239 | |
| 240 | if(ret == -1){ |
| 241 | if(MC$stop.at.error){ |
| 242 | stop(paste("zmq.getsockopt fails, ", option.value, sep = "")) |
| 243 | return(invisible(ret)) |
| 244 | } |
| 245 | if(MC$warning.at.error){ |
| 246 | warning(paste("zmq.getsockopt fails, ", option.value, sep = "")) |
| 247 | return(invisible(ret)) |
| 248 | } |
| 249 | } else{ |
| 250 | return(invisible(option.value)) |
| 251 | } |
| 252 | } |