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