10 * remote procedure in a loop |
10 * remote procedure in a loop |
11 */ |
11 */ |
12 int get_file(char *host, char *name) |
12 int get_file(char *host, char *name) |
13 { |
13 { |
14 CLIENT *clnt; |
14 CLIENT *clnt; |
15 int total_bytes = 0, write_bytes; |
15 int bytes = 0, write_items; |
16 readfile_res *result; |
16 readfile_res *result; |
17 request req; |
17 request req; |
18 FILE *file; |
18 FILE *file; |
19 |
19 |
20 /* |
20 /* |
21 * Initialize the request with the file name |
21 * Initialize the request with the file name |
22 */ |
22 */ |
23 req.name = name; |
23 req.name = name; |
24 |
24 |
25 /* |
25 /* |
26 * Create client handle used for calling FTPPROG on |
26 * Create client handle used for calling FTPPROG on |
27 * the server designated on the command line. Use |
27 * the server designated on the command line. Use |
28 * the tcp protocol when contacting the server. |
28 * the tcp protocol when contacting the server. |
38 } |
38 } |
39 |
39 |
40 /* |
40 /* |
41 * Open the file for writing on the client machine |
41 * Open the file for writing on the client machine |
42 */ |
42 */ |
43 file = fopen(name, "w"); |
43 file = fopen(name, "wb"); |
44 |
44 |
45 /* |
45 /* |
46 * Call the remote procedure retrieve_file on the server. |
46 * Call the remote procedure retrieve_file on the server. |
47 * During each iteration of the loop remote procedure is |
47 * During each iteration of the loop remote procedure is |
48 * called and only 1024 bytes of data is read and returned |
48 * called and only 1024 bytes of data is read and returned |
49 * by the server. The loop terminates when the data returned |
49 * by the server. The loop terminates when the data returned |
50 * from the server is less than 1024 bytes |
50 * from the server is less than 1024 bytes |
51 */ |
51 */ |
52 while (1) { |
52 while (1) { |
53 /* |
53 /* |
54 * Specifies the byte position where the next read should be |
54 * Specifies the byte position where the next read should be |
55 * started in the server. |
55 * started in the server. |
56 */ |
56 */ |
57 req.start = total_bytes; |
57 req.start = bytes; |
58 |
58 |
59 result = retrieve_file_1(&req, clnt); |
59 result = retrieve_file_1(&req, clnt); |
60 if (result == NULL) { |
60 if (result == NULL) { |
61 /* |
61 /* |
62 * An RPC error occurred while calling the server. |
62 * An RPC error occurred while calling the server. |
80 } |
80 } |
81 |
81 |
82 /* |
82 /* |
83 * Successfully got a chunk of the file. |
83 * Successfully got a chunk of the file. |
84 * Write into our local file and update the |
84 * Write into our local file and update the |
85 * total bytes of data read till now. |
85 * total bytes of data read till now. |
86 */ |
86 */ |
87 write_bytes = fwrite(result->readfile_res_u.chunk.data, 1, result->readfile_res_u.chunk.bytes, file); |
87 write_items = fwrite(result->readfile_res_u.chunk.data, sizeof(int), result->readfile_res_u.chunk.items, file); |
88 total_bytes += result->readfile_res_u.chunk.bytes; |
88 bytes += result->readfile_res_u.chunk.items * (sizeof(int)); |
89 if (result->readfile_res_u.chunk.bytes < MAXLEN) |
89 if (result->readfile_res_u.chunk.items < MAXLEN) |
90 break; |
90 break; |
91 } |
91 } |
92 |
92 |
93 fclose(file); |
93 fclose(file); |
94 |
94 |
95 return 0; |
95 return 0; |
96 } |
96 } |
97 |
97 |
98 /* |
98 /* |
99 * Stores the file on the server by calling the remote |
99 * Stores the file on the server by calling the remote |
100 * procedure in a loop |
100 * procedure in a loop |
101 */ |
101 */ |
102 int put_file(char *host, char *name) |
102 int put_file(char *host, char *name) |
103 { |
103 { |
104 CLIENT *clnt; |
104 CLIENT *clnt; |
105 char data[1024]; |
|
106 int read_bytes; |
|
107 int *result; |
105 int *result; |
108 chunksend chunk; |
106 filechunk data; |
109 FILE *file; |
107 chunksend chunk; |
|
108 FILE *file; |
110 |
109 |
111 /* |
110 /* |
112 * Create client handle used for calling FTPPROG on |
111 * Create client handle used for calling FTPPROG on |
113 * the server designated on the command line. Use |
112 * the server designated on the command line. Use |
114 * the tcp protocol when contacting the server. |
113 * the tcp protocol when contacting the server. |
122 clnt_pcreateerror(host); |
121 clnt_pcreateerror(host); |
123 exit(1); |
122 exit(1); |
124 } |
123 } |
125 |
124 |
126 /* |
125 /* |
127 * Open the file that should be stored on the server |
126 * Open the file that should be stored on the server |
128 */ |
127 */ |
129 file = fopen(name, "r"); |
128 file = fopen(name, "rb"); |
130 |
129 |
131 /* |
130 /* |
132 * Initialize the chunk to be sent with the name |
131 * Initialize the chunk to be sent with the name |
133 * of the file |
132 * of the file |
134 */ |
133 */ |
135 chunk.name = name; |
134 chunk.name = name; |
136 |
135 |
137 /* |
136 /* |
138 * Call the remote procedure readdir on the server |
137 * Call the remote procedure readdir on the server |
139 * in a loop sending only 1024 bytes of data in each |
138 * in a loop sending only 1024 bytes of data in each |
140 * iteration. The loop terminates once the data less |
139 * iteration. The loop terminates once the data less |
141 * than 1024 bytes is sent. |
140 * than 1024 bytes is sent. |
142 */ |
141 */ |
143 while (1) { |
142 while (1) { |
144 read_bytes = fread(data, 1, MAXLEN, file); |
143 chunk.items = fread(chunk.data, sizeof(int), MAXLEN, file); |
145 |
|
146 chunk.data = data; |
|
147 chunk.bytes = read_bytes; |
|
148 result = send_file_1(&chunk, clnt); |
144 result = send_file_1(&chunk, clnt); |
149 |
145 |
150 if (result == NULL) { |
146 if (result == NULL) { |
151 /* |
147 /* |
152 * An RPC error occurred while calling the server. |
148 * An RPC error occurred while calling the server. |
171 |
167 |
172 /* |
168 /* |
173 * Successfully got a chunk of the file. |
169 * Successfully got a chunk of the file. |
174 * Write into our local file. |
170 * Write into our local file. |
175 */ |
171 */ |
176 if (read_bytes < MAXLEN) |
172 if (chunk.items < MAXLEN) |
177 break; |
173 break; |
178 } |
174 } |
179 |
175 |
180 fclose(file); |
176 fclose(file); |
181 |
177 |
182 return 0; |
178 return 0; |
183 } |
179 } |
184 |
180 |
185 /* |
181 /* |
186 * Handles commands read on the command line and calls |
182 * Handles commands read on the command line and calls |
187 * the appropriate function to handle the command |
183 * the appropriate function to handle the command |
188 */ |
184 */ |
189 int read_command(char *host) |
185 int read_command(char *host) |
190 { |
186 { |
191 char command[MAXLEN]; |
187 char command[MAXLEN]; |
192 |
188 |
193 printf("> "); |
189 printf("> "); |
194 fflush(stdin); |
190 fflush(stdin); |
195 gets(command); |
191 gets(command); |
196 |
192 |
197 if (strncmp(command, "get", 3) == 0) { |
193 if (strncmp(command, "get", 3) == 0) { |
198 return get_file(host, command+4); |
194 return get_file(host, command+4); |
199 } else if(strncmp(command, "put", 3) == 0){ |
195 } else if(strncmp(command, "put", 3) == 0){ |
200 return put_file(host, command+4); |
196 return put_file(host, command+4); |
201 } else if(strncmp(command, "exit", 4) == 0){ |
197 } else if(strncmp(command, "exit", 4) == 0){ |
202 exit(0); |
198 exit(0); |
203 } else { |
199 } else { |
204 return -1; |
200 return -1; |
205 } |
201 } |
206 } |
202 } |
207 |
203 |
208 int main(int argc, char *argv[]) |
204 int main(int argc, char *argv[]) |
209 { |
205 { |
210 int result; |
206 int result; |