Debian Codegraph @

R/R_zmq_transfers.r

raw symbols SCIP
1 #' Transfer Functions for Files or Directories
2 #'
3 #' High level functions calling \code{zmq.sendfile()} and \code{zmq.recvfile()}
4 #' to zip, transfer, and unzip small files or directories contains small files.
5 #'
6 #' @details
7 #' \code{zmq.senddir()} calls \code{zmq.senddir()}, and
8 #' \code{zmq.recvdir()} calls \code{zmq.recvdir()}.
9 #'
10 #' @param port
11 #' A valid tcp port to be passed to \code{zmq.sendfile()} and
12 #' \code{zmq.recvfile()}.
13 #' @param endpoint
14 #' A ZMQ socket endpoint to be passed to \code{zmq.sendfile()} and
15 #' \code{zmq.recvfile()}.
16 #' @param infiles
17 #' The name (as a string) vector of the in files to be zipped and to be
18 #' sent away.
19 #' @param outfile
20 #' The name (as a string) of the out file to be saved on the disk.
21 #' If \code{outfile = NULL} and \code{exdir = NULL}, a tempfile will be
22 #' used and the tempfile nanme will be returned.
23 #' @param exdir
24 #' The name (as a string) of the out directory to save the unzip files
25 #' unzipped from the received \code{outfile}.
26 #' @param verbose
27 #' Logical; determines if a progress bar should be shown.
28 #' @param flags
29 #' A flag for the method used by \code{zmq_sendfile} and
30 #' \code{zmq_recvfile}
31 #' @param ctx
32 #' A ZMQ ctx. If \code{NULL} (default), the function will initial one at
33 #' the beginning and destroy it after finishing file transfer.
34 #' @param socket
35 #' A ZMQ socket based on \code{ctx}.
36 #' If \code{NULL} (default), the function will create one at the beginning
37 #' and close it after finishing file transfer.
38 #'
39 #'
40 #' @return \code{zmq.senddir()} and \code{zmq.recvdir()} return
41 #' number of bytes (invisible) in the sent message if successful,
42 #' otherwise returns -1 (invisible) and sets \code{errno} to the error
43 #' value, see ZeroMQ manual for details.
44 #' In addition, \code{zmq.recvdir()} returns a zipped file name in a list.
45 #'
46 #' @author Wei-Chen Chen
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.recvdir(55555, "localhost", outfile = "./backup_2019.zip",
60 #' verbose = TRUE)
61 #' ### or unzip to exdir
62 #' # zmq.recvdir(55555, "localhost", exdir = "./backup_2019", verbose = TRUE)
63 #'
64 #' ### Sender
65 #' library(pbdZMQ, quietly = TRUE)
66 #' zmq.senddir(55555, c("./pbdZMQ/R", "./pbdZMQ/src"), verbose = TRUE)
67 #' }
68 #'
69 #' @keywords programming
70 #' @seealso \code{\link{zmq.sendfile}()}, \code{\link{zmq.recvfile}()}.
71 #' @rdname b2_sendrecvdir
72 #' @name Transfer Functions for Files or Directories
73 #' @importFrom utils unzip zip
74 #'
75 NULL
76
77
78
79 #' @rdname b2_sendrecvdir
80 #' @export
81 zmq.senddir <- function(port, infiles, verbose = FALSE,
82 flags = ZMQ.SR()$BLOCK,
83 ctx = NULL, socket = NULL)
84 {
85 if (is.null(socket))
86 {
87 if (is.null(ctx))
88 {
89 ctx <- zmq.ctx.new()
90 ctx.destroy <- TRUE
91 }
92 else
93 ctx.destroy <- FALSE
94
95 socket <- zmq.socket(ctx, ZMQ.ST()$PUSH)
96 socket.close <- TRUE
97
98 endpoint <- address("*", port)
99 zmq.bind(socket, endpoint)
100 }
101 else
102 {
103 ctx.destroy <- FALSE
104 socket.close <- FALSE
105 }
106
107 type = attr(socket, "type")
108 if (is.null(type))
109 stop("unable to determine socket type")
110 else if (type != ZMQ.ST()$PUSH && type != ZMQ.ST()$REQ && type != ZMQ.ST()$REP)
111 stop("socket type must be one of PUSH, REQ, or REP (matching PULL, REP, and REQ respectively in zmq.recvfile())")
112
113 if (!verbose)
114 extras <- "-q"
115 else
116 extras <- ""
117
118 tmp.fn <- tempfile(fileext = ".zip")
119 zip(tmp.fn, infiles, extras = extras)
120 ret <- zmq.sendfile(port, tmp.fn, verbose = verbose, flags = flags,
121 forcebin = TRUE, ctx = ctx, socket = socket)
122
123 if (socket.close || ctx.destroy)
124 zmq.close(socket)
125
126 if (ctx.destroy)
127 zmq.ctx.destroy(ctx)
128
129 invisible(list(ret = ret, infile = tmp.fn))
130 }
131
132
133 #' @rdname b2_sendrecvdir
134 #' @export
135 zmq.recvdir <- function(port, endpoint, outfile = NULL, exdir = NULL,
136 verbose = FALSE, flags = ZMQ.SR()$BLOCK,
137 ctx = NULL, socket = NULL)
138 {
139 if (is.null(socket))
140 {
141 if (is.null(ctx))
142 {
143 ctx <- zmq.ctx.new()
144 ctx.destroy <- TRUE
145 }
146 else
147 ctx.destroy <- FALSE
148
149 socket <- zmq.socket(ctx, ZMQ.ST()$PULL)
150 socket.close <- TRUE
151
152 endpoint <- address(endpoint, port)
153 zmq.connect(socket, endpoint)
154 }
155 else
156 {
157 ctx.destroy <- FALSE
158 socket.close <- FALSE
159 }
160
161 type = attr(socket, "type")
162 if (is.null(type))
163 stop("unable to determine socket type")
164 else if (type != ZMQ.ST()$PULL && type != ZMQ.ST()$REP && type != ZMQ.ST()$REQ)
165 stop("socket type must be one of PULL, REP, or REQ (matching PUSH, REQ, and REP respectively in zmq.sendfile())")
166
167 if (is.null(outfile))
168 outfile <- tempfile()
169
170 ret <- zmq.recvfile(port, endpoint, outfile, verbose = verbose,
171 flags = flags, forcebin = TRUE,
172 ctx = ctx, socket = socket)
173
174 if (!is.null(exdir))
175 unzip(outfile, exdir = exdir)
176
177 if (socket.close || ctx.destroy)
178 zmq.close(socket)
179
180 if (ctx.destroy)
181 zmq.ctx.destroy(ctx)
182
183 invisible(list(ret = ret, outfile = outfile))
184 }