Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / rx / bulk.example / bulk_client.c
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
4 *
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
9
10 /* Sample program using multi_Rx, to execute calls in parallel to multiple hosts */
11
12 #include <afsconfig.h>
13 #include <afs/param.h>
14
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/file.h>
19 #include <netdb.h>
20 #include <netinet/in.h>
21 #include <stdio.h>
22 #include <rx/xdr.h>
23 #include "bulk.h"
24
25 /* Bogus procedure to get internet address of host */
26 static u_long
27 GetIpAddress(char *hostname)
28 {
29 struct hostent *hostent;
30 u_long host;
31 hostent = gethostbyname(hostname);
32 if (!hostent) {
33 printf("host %s not found", hostname);
34 exit(1);
35 }
36 if (hostent->h_length != sizeof(u_long)) {
37 printf("host address is disagreeable length (%d)", hostent->h_length);
38 exit(1);
39 }
40 memcpy((char *)&host, hostent->h_addr, sizeof(host));
41 return host;
42 }
43
44 long FetchFile(struct rx_call *call, int verbose, char *localFile,
45 char *remoteFile, long *length_ptr);
46 long StoreFile(struct rx_call *call, int verbose, char *localFile,
47 char *remoteFile, long *length_ptr);
48
49
50 int
51 main(int argc, char **argv)
52 {
53 char *localFile, *remoteFile;
54 u_long host;
55 long length;
56 struct rx_connection *conn;
57 struct rx_call *call;
58 struct clock startTime, endTime;
59 int fetch = 0, store = 0, verbose = 0;
60 struct rx_securityClass *null_securityObject;
61 int error = 0;
62 long msec;
63
64 argc--;
65 argv++;
66 while (**argv == '-') {
67 if (strcmp(*argv, "-fetch") == 0)
68 fetch = 1;
69 else if (strcmp(*argv, "-store") == 0)
70 store = 1;
71 else if (strcmp(*argv, "-verbose") == 0)
72 verbose = 1;
73 else {
74 fprintf(stderr, "Unknown option %s\n", *argv);
75 exit(1);
76 }
77 argc--;
78 argv++;
79 }
80 if (argc != 3 || !(fetch ^ store)) {
81 fprintf(stderr,
82 "bulk_client -fetch/-store localFile host remoteFile\n");
83 exit(1);
84 }
85 localFile = argv[0];
86 host = GetIpAddress(argv[1]);
87 remoteFile = argv[2];
88
89 rx_Init(0);
90 null_securityObject = rxnull_NewClientSecurityObject();
91 conn =
92 rx_NewConnection(host, BULK_SERVER_PORT, BULK_SERVICE_ID,
93 null_securityObject, RX_SECIDX_NULL);
94
95 clock_NewTime();
96 clock_GetTime(&startTime);
97
98 call = rx_NewCall(conn);
99 (fetch ? FetchFile : StoreFile) (call, verbose, localFile, remoteFile,
100 &length);
101 error = rx_EndCall(call, error);
102
103 clock_NewTime();
104 clock_GetTime(&endTime);
105 msec = clock_ElapsedTime(&startTime, &endTime);
106 if (!error)
107 printf("Transferred %d bytes in %d msec, %d bps\n", length, msec,
108 length * 1000 / msec);
109 else
110 printf("transfer failed: error %d\n", error);
111
112 /* Allow Rx to idle down any calls; it's a good idea, but not essential, to call this routine */
113 rx_Finalize();
114 }
115
116 long
117 FetchFile(struct rx_call *call, int verbose, char *localFile,
118 char *remoteFile, long *length_ptr)
119 {
120 int fd = -1, error = 0;
121 struct stat status;
122
123 if (StartBULK_FetchFile(call, verbose, remoteFile))
124 return BULK_ERROR;
125 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
126 if (fd < 0 || fstat(fd, &status) < 0) {
127 fprintf(stderr, "Could not create %s\n", localFile);
128 error = BULK_ERROR;
129 }
130 if (bulk_ReceiveFile(fd, call, &status))
131 error = BULK_ERROR;
132 *length_ptr = status.st_size;
133 if (fd >= 0)
134 close(fd);
135 /* If there were any output parameters, then it would be necessary to call EndBULKFetchFile(call, &out1,...) here to pick them up */
136 return error;
137 }
138
139 long
140 StoreFile(struct rx_call *call, int verbose, char *localFile,
141 char *remoteFile, long *length_ptr)
142 {
143 int fd = -1, error = 0;
144 struct stat status;
145
146 fd = open(localFile, O_RDONLY, 0);
147 if (fd < 0 || fstat(fd, &status) < 0) {
148 fprintf(stderr, "Could not open %s\n", localFile);
149 return BULK_ERROR;
150 }
151 error = StartBULK_StoreFile(call, verbose, remoteFile);
152 if (!error)
153 error = bulk_SendFile(fd, call, &status);
154 /* If there were any output parameters, then it would be necessary to call EndBULKStoreFile(call, &out1,...) here to pick them up */
155 close(fd);
156 *length_ptr = status.st_size;
157 return error;
158 }