Debian Codegraph @

R/R_zmq_transfer.r

raw symbols SCIP
1 #' File Transfer Functions
2 #'
3 #' High level functions calling \code{zmq_send()} and \code{zmq_recv()}
4 #' to transfer a file in 200 KiB chunks.
5 #'
6 #' @details
7 #' If no socket is passed, then by default \code{zmq.sendfile()} binds a
8 #' \code{ZMQ_PUSH} socket, and \code{zmq.recvfile()} connects to this with a
9 #' \code{ZMQ_PULL} socket. On the other hand, a PUSH/PULL, REQ/REP, or REP/REQ
10 #' socket pairing may be passed. In that case, the socket should already be
11 #' connected to the desired endpoint. Be careful not to pass the wrong socket
12 #' combination (e.g., do not do REQ/REQ), as this can put the processes in an
13 #' un-recoverable state.
14 #'
15 #' @param port
16 #' A valid tcp port.
17 #' @param endpoint
18 #' A ZMQ socket endpoint.
19 #' @param filename
20 #' The name (as a string) of the in/out files. The in and out file names
21 #' can be different.
22 #' @param verbose
23 #' Logical; determines if a progress bar should be shown.
24 #' @param flags
25 #' A flag for the method used by \code{zmq_sendfile} and
26 #' \code{zmq_recvfile}
27 #' @param forcebin
28 #' Force to read/send/recv/write in binary form. Typically for a Windows
29 #' system, text (ASCII) and binary files are processed differently.
30 #' If \code{TRUE}, "r+b" and "w+b" will be enforced in the C code.
31 #' This option is mainly for Windows.
32 #' @param ctx
33 #' A ZMQ ctx. If \code{NULL} (default), the function will initial one at
34 #' the beginning and destroy it after finishing file transfer.
35 #' @param socket
36 #' A ZMQ socket based on \code{ctx}.
37 #' If \code{NULL} (default), the function will create one at the beginning
38 #' and close it after finishing file transfer.
39 #'
40 #'
41 #' @return \code{zmq.sendfile()} and \code{zmq.recvfile()} return
42 #' number of bytes (invisible) in the sent message if successful,
43 #' otherwise returns -1 (invisible) and sets \code{errno} to the error
44 #' value, see ZeroMQ manual for details.
45 #'
46 #' @author Drew Schmidt and Christian Heckendorf
47 #'
48 #' @references ZeroMQ/4.1.0 API Reference:
49 #' \url{http://api.zeromq.org/4-1:_start}
50 #'
51 #' Programming with Big Data in R Website: \url{https://pbdr.org/}
52 #'
53 #' @examples
54 #' \dontrun{
55 #' ### Run the sender and receiver code in separate R sessions.
56 #'
57 #' # Receiver
58 #' library(pbdZMQ, quietly = TRUE)
59 #' zmq.recvfile(55555, "localhost", "/tmp/outfile", verbose=TRUE)
60 #'
61 #' # Sender
62 #' library(pbdZMQ, quietly = TRUE)
63 #' zmq.sendfile(55555, "/tmp/infile", verbose=TRUE)
64 #' }
65 #'
66 #' @keywords programming
67 #' @seealso \code{\link{zmq.msg.send}()}, \code{\link{zmq.msg.recv}()}.
68 #' @rdname b1_sendrecvfile
69 #' @name File Transfer Functions
70 NULL
71
72
73
74 # -----------------------------------------------------------------------------
75 # Send
76 # -----------------------------------------------------------------------------
77
78 #' @rdname b1_sendrecvfile
79 #' @export
80 zmq.sendfile <- function(port, filename, verbose=FALSE,
81 flags = ZMQ.SR()$BLOCK, forcebin = FALSE, ctx = NULL, socket = NULL)
82 {
83 if (is.null(socket))
84 {
85 if (is.null(ctx))
86 {
87 ctx <- zmq.ctx.new()
88 ctx.destroy <- TRUE
89 }
90 else
91 ctx.destroy <- FALSE
92
93 socket <- zmq.socket(ctx, ZMQ.ST()$PUSH)
94 socket.close <- TRUE
95
96 endpoint <- address("*", port)
97 zmq.bind(socket, endpoint)
98 }
99 else
100 {
101 ctx.destroy <- FALSE
102 socket.close <- FALSE
103 }
104
105 type = attr(socket, "type")
106 if (is.null(type))
107 stop("unable to determine socket type")
108 else if (type != ZMQ.ST()$PUSH && type != ZMQ.ST()$REQ && type != ZMQ.ST()$REP)
109 stop("socket type must be one of PUSH, REQ, or REP (matching PULL, REP, and REQ respectively in zmq.recvfile())")
110
111 fi <- file.info(filename)
112 if (!is.na(fi$isdir) && !fi$isdir)
113 filesize <- as.double(fi$size)
114 else
115 stop(paste("File does not exist:", filename))
116
117 if (type == ZMQ.ST()$REP)
118 receive.socket(socket)
119 send.socket(socket, filesize)
120 if (type == ZMQ.ST()$REQ)
121 receive.socket(socket)
122
123 ret <- .Call("R_zmq_send_file", socket, filename, as.integer(verbose),
124 filesize, as.integer(flags), as.integer(forcebin), type, PACKAGE="pbdZMQ")
125
126 if (socket.close || ctx.destroy)
127 zmq.close(socket)
128 if (ctx.destroy)
129 zmq.ctx.destroy(ctx)
130
131 invisible(ret)
132 }
133
134
135
136 # -----------------------------------------------------------------------------
137 # Receive
138 # -----------------------------------------------------------------------------
139
140 #' @rdname b1_sendrecvfile
141 #' @export
142 zmq.recvfile <- function(port, endpoint, filename, verbose=FALSE,
143 flags = ZMQ.SR()$BLOCK, forcebin = FALSE, ctx = NULL, socket = NULL)
144 {
145 if (is.null(socket))
146 {
147 if (is.null(ctx))
148 {
149 ctx <- zmq.ctx.new()
150 ctx.destroy <- TRUE
151 }
152 else
153 ctx.destroy <- FALSE
154
155 socket <- zmq.socket(ctx, ZMQ.ST()$PULL)
156 socket.close <- TRUE
157
158 endpoint <- address(endpoint, port)
159 zmq.connect(socket, endpoint)
160 }
161 else
162 {
163 ctx.destroy <- FALSE
164 socket.close <- FALSE
165 }
166
167 type = attr(socket, "type")
168 if (is.null(type))
169 stop("unable to determine socket type")
170 else if (type != ZMQ.ST()$PULL && type != ZMQ.ST()$REP && type != ZMQ.ST()$REQ)
171 stop("socket type must be one of PULL, REP, or REQ (matching PUSH, REQ, and REP respectively in zmq.sendfile())")
172
173 if (type == ZMQ.ST()$REQ)
174 send.socket(socket, NULL)
175 filesize <- receive.socket(socket)
176 if (type == ZMQ.ST()$REP)
177 send.socket(socket, NULL)
178
179 ret <- .Call("R_zmq_recv_file", socket, filename, as.integer(verbose),
180 filesize, as.integer(flags), as.integer(forcebin), type, PACKAGE="pbdZMQ")
181
182 if (socket.close || ctx.destroy)
183 zmq.close(socket)
184 if (ctx.destroy)
185 zmq.ctx.destroy(ctx)
186
187 invisible(ret)
188 }