2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* Sample program using multi_Rx, to execute calls in parallel to multiple hosts */
12 #include <afsconfig.h>
13 #include <afs/param.h>
16 #include <sys/types.h>
20 #include <netinet/in.h>
24 #include <rx/rx_null.h>
31 rx_PrintStats(stdout);
36 /* Bogus procedure to get internet address of host */
38 GetIpAddress(hostname)
41 struct hostent *hostent;
43 hostent = gethostbyname(hostname);
45 printf("host %s not found", hostname);
48 if (hostent->h_length != sizeof(u_long)) {
49 printf("host address is disagreeable length (%d)", hostent->h_length);
52 memcpy((char *)&host, hostent->h_addr, sizeof(host));
56 long FetchFile(), StoreFile();
60 struct rx_connection *conn;
74 char *new = (char *)malloc(strlen(s) + 1);
83 struct async_work *work = (struct async_work *)data;
85 struct clock startTime, endTime;
91 for (i = 0; i < work->count; i++) {
92 call = rx_NewCall(work->conn);
94 clock_GetTime(&startTime);
96 ((work->store ? StoreFile : FetchFile) (call, work->verbose,
97 work->local, work->remote,
99 error = rx_EndCall(call, error);
102 clock_GetTime(&endTime);
103 msec = clock_ElapsedTime(&startTime, &endTime);
104 if (work->verbose && !error)
105 printf("%s: %s %s, %d bytes in %d msec, %d bps\n", work->host,
106 work->store ? "stored" : "fetched",
107 work->store ? work->local : work->remote, length, msec,
108 length * 1000 / msec);
110 printf("%s: %s of %s failed: error %d\n", work->host,
111 work->store ? "store" : "fetch",
112 work->store ? work->local : work->remote, error);
115 osi_Wakeup(&async_nProcs);
118 async_BulkTest(host, conn, store, count, verbose, file)
120 struct rx_connection *conn;
129 struct async_work *work =
130 (struct async_work *)malloc(sizeof(struct async_work));
135 work->verbose = verbose;
136 name = strrchr(file, '/');
141 /* sprintf(tempfile, "/usr/tmp/%s.%s", myHostName, name);*/
142 sprintf(tempfile, "/usr/tmp/%s", name);
143 work->local = allocString(store ? file : tempfile);
144 work->remote = allocString(store ? tempfile : file);
146 LWP_CreateProcess(async_BulkProc, 3000, RX_PROCESS_PRIORITY, (void *)work,
151 FetchFile(call, verbose, localFile, remoteFile, length_ptr)
152 struct rx_call *call;
154 char *localFile, *remoteFile;
157 int fd = -1, error = 0;
160 if (StartBULK_FetchFile(call, verbose, remoteFile))
162 fd = open(localFile, O_CREAT | O_TRUNC | O_WRONLY, 0666);
163 if (fd < 0 || fstat(fd, &status) < 0) {
164 fprintf("Could not create %s\n", localFile);
166 } else if (bulk_ReceiveFile(fd, call, &status))
168 *length_ptr = status.st_size;
171 /* If there were any output parameters, then it would be necessary to call EndBULKFetchFile(call, &out1,...) here to pick them up */
176 StoreFile(call, verbose, localFile, remoteFile, length_ptr)
177 struct rx_call *call;
179 char *localFile, *remoteFile;
182 int fd = -1, error = 0;
185 fd = open(localFile, O_RDONLY, 0);
186 if (fd < 0 || fstat(fd, &status) < 0) {
187 fprintf("Could not open %s\n", localFile);
190 error = StartBULK_StoreFile(call, verbose, remoteFile);
192 error = bulk_SendFile(fd, call, &status);
193 /* If there were any output parameters, then it would be necessary to call EndBULKStoreFile(call, &out1,...) here to pick them up */
195 *length_ptr = status.st_size;
205 char *hostname = NULL;
207 struct rx_securityClass *null_securityObject;
208 struct rx_connection *conn = (struct rx_connection *)0;
209 gethostname(myHostName, sizeof(myHostName));
210 signal(SIGINT, InterruptSignal);
213 null_securityObject = rxnull_NewClientSecurityObject();
217 while (**argv == '-') {
218 if (strcmp(*argv, "-h") == 0) {
221 host = GetIpAddress(hostname);
223 rx_NewConnection(host, BULK_SERVER_PORT, BULK_SERVICE_ID,
224 null_securityObject, BULK_NULL);
225 } else if (strcmp(*argv, "-f") == 0)
227 else if (strcmp(*argv, "-s") == 0)
229 else if (strcmp(*argv, "-v") == 0)
231 else if (strcmp(*argv, "-c") == 0)
232 (count = atoi(*++argv)), argc--;
234 fprintf(stderr, "Unknown option %s\n", *argv);
241 fprintf(stderr, "local + remote file names expected\n");
244 if (conn == (struct rx_connection *)0) {
245 fprintf(stderr, "No host specified\n");
248 async_BulkTest(hostname, conn, store, count, verbose, argv[0]);
253 osi_Sleep(&async_nProcs);
255 fprintf(stderr, "All transfers done\n");
256 rx_PrintStats(stdout);
257 /* Allow Rx to idle down any calls; it's a good idea, but not essential, to call this routine */