Standardize License information
[openafs.git] / src / rx / multi.example / sample_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 <sys/types.h>
13 #include <netdb.h>
14 #include <netinet/in.h>
15 #include <stdio.h>
16 #include "sample.h"
17 #include "rx/rx_clock.h"
18
19 /* Bogus procedure to get internet address of host */
20 static u_long GetIpAddress(hostname)
21     char *hostname;
22 {
23     struct hostent *hostent;
24     u_long host;
25     hostent = gethostbyname(hostname);
26     if (!hostent) {printf("host %s not found", hostname);exit(1);}
27     if (hostent->h_length != sizeof(u_long)) {
28         printf("host address is disagreeable length (%d)", hostent->h_length);
29         exit(1);
30     }
31     bcopy(hostent->h_addr, (char *)&host, sizeof(host));
32     return host;
33 }
34
35 main(argc, argv)
36     char **argv;
37 {
38     struct rx_securityClass *null_securityObject;
39     register int i, nHosts = 0;
40     struct {
41         u_long ipaddr;
42         char *name;
43     } host[50];
44     int arg[50];
45     struct rx_connection *conns[50];
46     int nSuccesses = 0;
47     register int trials=1;
48     register int verbose=0;
49     register int abort=0;
50     int msec;
51     struct clock startTime, endTime;
52     int result;
53     argc--; argv++;
54     while (**argv == '-') {
55         if (strcmp(*argv, "-verbose") == 0) verbose = 1;
56         else if (strcmp(*argv, "-count") == 0) trials = atoi(*++argv), argc--;
57         else if (strcmp(*argv, "-abort") == 0) abort = 1;
58         else {
59             fprintf("Unknown option %s\n", *argv);
60             exit(1);
61         }
62         argc--; argv++;
63     }
64     while (argc--) {
65         host[nHosts].name = *argv;
66         host[nHosts].ipaddr = GetIpAddress(*argv);
67         arg[nHosts] = 10000*(nHosts+1); /* a bogus argument to show how an input argument to the multi call can be indexed by multi_i */
68         nHosts++;
69         argv++;
70     }
71
72     rx_Init(0);
73     null_securityObject = rxnull_NewClientSecurityObject();
74     for (i=0; i<nHosts; i++) {
75         conns[i] = rx_NewConnection(host[i].ipaddr, SAMPLE_SERVER_PORT, SAMPLE_SERVICE_ID, null_securityObject, SAMPLE_NULL);
76     }
77
78     clock_NewTime();
79     clock_GetTime(&startTime);
80     for (i=0;i<trials;i++) {
81         multi_Rx(conns, nHosts) {
82             /* Note that multi_i is available both for arguments (result could also be indexed by multi_i, if you want to keep the results apart, and after the call completes) and in the code which follows the completion of each multi_TEST_Add.  At completion, multi_i will be set to the connection index of the call which completed, and multi_error will be set to the error code returned by that call. */
83             if (verbose) printf("%s:  About to add %d to %d\n", host[multi_i].name, arg[multi_i], i*10, &result);
84             multi_TEST_Add(verbose, arg[multi_i], i*10, &result);
85             if (verbose) printf("%s: error %d, %d + %d is %d\n", host[multi_i].name, multi_error, arg[multi_i], i*10, result);
86             if (abort && multi_error) multi_Abort;
87             if (multi_error == 0) nSuccesses++;
88             else if (!verbose) printf("%s: error %d\n", host[multi_i].name, multi_error);
89         } multi_End;
90     }
91     clock_NewTime();
92     clock_GetTime(&endTime);
93     msec = clock_ElapsedTime(&startTime, &endTime);
94     printf("nSuccesses=%d in %d msec; %d msec per iteration for %d iterations over %d hosts\n", nSuccesses, msec, msec/trials, trials, nHosts);
95
96     /* Allow Rx to idle down any calls; it's a good idea, but not essential, to call this routine */
97     rx_Finalize();
98 }