Debian Codegraph @

src/R_zmq_transfer.c

raw symbols SCIP
1 #include "R_zmq.h"
2 #include <stdint.h>
3
4 // 200 KiB
5 #define BUFLEN 204800
6
7 #define PROGRESS_BARLEN 30
8 #define PROGRESS_SCALE 1024
9 static const char *memnames[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"};
10
11 typedef struct file_t
12 {
13 char *filename;
14 FILE *file;
15 double filesize;
16 int verbose;
17 } file_t;
18
19
20
21 static inline int progress_init(const int verbose, double total)
22 {
23 int i;
24 int ind = 0;
25
26 if (!verbose) return -1;
27
28 while (total >= PROGRESS_SCALE)
29 {
30 total /= (double) PROGRESS_SCALE;
31 ind++;
32 }
33
34 Rprintf("[");
35 for (i=0; i<PROGRESS_BARLEN; i++)
36 Rprintf("-");
37
38 Rprintf("] 0 / %.3f %s", total, memnames[ind]);
39 return ind;
40 }
41
42
43
44 static inline void progress_update(const int verbose, const double current, const double total, const int ind)
45 {
46 int i;
47 int len = (int) PROGRESS_BARLEN*(current/total);
48 double divby = 1./pow(PROGRESS_SCALE, ind);
49
50 if (!verbose) return;
51
52 Rprintf("\r[");
53 for (i=0; i<len; i++)
54 Rprintf("#");
55 for (i=len+1; i<PROGRESS_BARLEN; i++)
56 Rprintf("-");
57
58 Rprintf("] %.2f / %.2f %s", current*divby, total*divby, memnames[ind]);
59 }
60
61
62
63 static inline void transfer_check(int info, char *kind, void *buf, FILE *infile)
64 {
65 if (info == -1)
66 {
67 free(buf);
68 fclose(infile);
69 int C_errno = zmq_errno();
70 error("could not %s data: %d strerror: %s\n", kind, C_errno, zmq_strerror(C_errno));
71 }
72 }
73
74
75
76 // ----------------------------------------------------------------------------
77 // Send
78 // ----------------------------------------------------------------------------
79
80 static inline void send_file(file_t *f, void *socket, void *buf, int flags, int type)
81 {
82 uint64_t total_size = 0;
83 size_t size;
84
85 int ind = progress_init(f->verbose, f->filesize);
86
87 do
88 {
89 if (type == ZMQ_REP)
90 zmq_recv(socket, buf, 1, flags);
91
92 size = fread(buf, 1, BUFLEN, f->file);
93 total_size += size;
94
95 if (size < BUFLEN && !feof(f->file))
96 {
97 free(buf);
98 fclose(f->file);
99 error("Error reading from file: %s", f->filename);
100 }
101
102 int info = zmq_send(socket, buf, size, flags);
103 transfer_check(info, "send", buf, f->file);
104 progress_update(f->verbose, (double) total_size, f->filesize, ind);
105
106 if (type == ZMQ_REQ)
107 zmq_recv(socket, buf, 1, flags);
108
109 } while (size == BUFLEN);
110 }
111
112
113
114 SEXP R_zmq_send_file(SEXP R_socket, SEXP R_filename, SEXP verbose,
115 SEXP filesize, SEXP R_flags, SEXP R_forcebin, SEXP type_)
116 {
117 SEXP ret;
118 int C_flags = INTEGER(R_flags)[0];
119 void *C_socket = R_ExternalPtrAddr(R_socket);
120 char *filename = CHARPT(R_filename, 0);
121 int type = INTEGER(type_)[0];
122
123 void *buf = malloc(BUFLEN);
124 if (buf == NULL)
125 error("Could not allocate temporary buffer");
126
127 char *mode = (INTEGER(R_forcebin)[0] == 0) ? "r" : "r+b";
128 FILE *infile = fopen(filename, mode);
129 if (infile == NULL)
130 {
131 free(buf);
132 error("Could not open file: %s", filename);
133 }
134
135 file_t f;
136 f.filename = filename;
137 f.file = infile;
138 f.filesize = REAL(filesize)[0];
139 f.verbose = INTEGER(verbose)[0];
140
141 send_file(&f, C_socket, buf, C_flags, type);
142
143 free(buf);
144 fclose(infile);
145 if (f.verbose)
146 Rprintf("\n");
147
148 PROTECT(ret = allocVector(INTSXP, 1));
149 INTEGER(ret)[0] = 0;
150 UNPROTECT(1);
151 return ret;
152 }
153
154
155
156
157 // ----------------------------------------------------------------------------
158 // Receive
159 // ----------------------------------------------------------------------------
160
161 static inline void recv_file(file_t *f, void *socket, void *buf, int flags, int type)
162 {
163 uint64_t total_size = 0;
164 size_t size;
165
166 int ind = progress_init(f->verbose, f->filesize);
167
168 do
169 {
170 if (type == ZMQ_REQ)
171 zmq_send(socket, buf, 1, flags);
172
173 int expected_size = zmq_recv(socket, buf, BUFLEN, flags);
174 transfer_check(expected_size, "receive", buf, f->file);
175 size = (size_t) expected_size;
176
177 if (size > BUFLEN) /* Truncated data. Error? */
178 size = BUFLEN;
179
180 total_size += size;
181
182 size = fwrite(buf, 1, size, f->file);
183
184 if (expected_size < 0 || size < (size_t)expected_size)
185 {
186 free(buf);
187 fclose(f->file);
188 error("Could not write to file: %s", f->filename);
189 }
190
191 if (type == ZMQ_REP)
192 zmq_send(socket, buf, 1, flags);
193
194 progress_update(f->verbose, (double) total_size, f->filesize, ind);
195
196 } while (size == BUFLEN);
197 }
198
199
200
201 SEXP R_zmq_recv_file(SEXP R_socket, SEXP R_filename, SEXP verbose,
202 SEXP filesize_, SEXP R_flags, SEXP R_forcebin, SEXP type_)
203 {
204 SEXP ret;
205 int C_flags = INTEGER(R_flags)[0];
206 void *C_socket = R_ExternalPtrAddr(R_socket);
207 char *filename = CHARPT(R_filename, 0);
208 int type = INTEGER(type_)[0];
209
210 void *buf = malloc(BUFLEN);
211 if (buf == NULL)
212 error("Could not allocate temporary buffer");
213
214 char *mode = (INTEGER(R_forcebin)[0] == 0) ? "w" : "w+b";
215 FILE *outfile = fopen(filename, mode);
216 if (outfile == NULL)
217 {
218 free(buf);
219 error("Could not open file: %s", filename);
220 }
221
222 file_t f;
223 f.filename = filename;
224 f.file = outfile;
225 f.filesize = REAL(filesize_)[0];
226 f.verbose = INTEGER(verbose)[0];
227
228 recv_file(&f, C_socket, buf, C_flags, type);
229
230 free(buf);
231 fclose(outfile);
232 if (f.verbose)
233 Rprintf("\n");
234
235 PROTECT(ret = allocVector(INTSXP, 1));
236 INTEGER(ret)[0] = 0;
237 UNPROTECT(1);
238 return ret;
239 }